[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Include headers in sqlite cache



commit 5bdb4e245581070034dd88217fdb35e0e86f9291
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Fri Jan 12 09:36:52 2018 -0800

    Include headers in sqlite cache
---
 cache_fallback_directories.py  | 6 +++++-
 stem/descriptor/remote.py      | 8 ++++++--
 test/unit/descriptor/remote.py | 5 ++++-
 3 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/cache_fallback_directories.py b/cache_fallback_directories.py
index c5926c8c..50139f56 100755
--- a/cache_fallback_directories.py
+++ b/cache_fallback_directories.py
@@ -46,6 +46,10 @@ if __name__ == '__main__':
     print('Fallback directories are already up to date, nothing to do.')
     sys.exit(0)
 
+  # all fallbacks have the same header metadata, so just picking one
+
+  headers = latest_fallback_directories.values()[0].header if latest_fallback_directories else {}
+
   print('Differences detected...\n')
   print(stem.descriptor.remote._fallback_directory_differences(cached_fallback_directories, latest_fallback_directories))
-  stem.descriptor.remote.FallbackDirectory._write(latest_fallback_directories, fallback_dir_commit, stem_commit)
+  stem.descriptor.remote.FallbackDirectory._write(latest_fallback_directories, fallback_dir_commit, stem_commit, headers)
diff --git a/stem/descriptor/remote.py b/stem/descriptor/remote.py
index 453061e0..f4845922 100644
--- a/stem/descriptor/remote.py
+++ b/stem/descriptor/remote.py
@@ -992,7 +992,7 @@ class FallbackDirectory(Directory):
     results = {}
 
     for fingerprint in set([key.split('.')[0] for key in conf.keys()]):
-      if fingerprint in ('tor_commit', 'stem_commit'):
+      if fingerprint in ('tor_commit', 'stem_commit', 'header'):
         continue
 
       attr = {}
@@ -1185,7 +1185,7 @@ class FallbackDirectory(Directory):
     return section_lines
 
   @staticmethod
-  def _write(fallbacks, tor_commit, stem_commit, path = CACHE_PATH):
+  def _write(fallbacks, tor_commit, stem_commit, headers, path = CACHE_PATH):
     """
     Persists fallback directories to a location in a way that can be read by
     from_cache().
@@ -1193,6 +1193,7 @@ class FallbackDirectory(Directory):
     :param dict fallbacks: mapping of fingerprints to their fallback directory
     :param str tor_commit: tor commit the fallbacks came from
     :param str stem_commit: stem commit the fallbacks came from
+    :param dict headers: metadata about the file these came from
     :param str path: location fallbacks will be persisted to
     """
 
@@ -1200,6 +1201,9 @@ class FallbackDirectory(Directory):
     conf.set('tor_commit', tor_commit)
     conf.set('stem_commit', stem_commit)
 
+    for k, v in headers.items():
+      conf.set('header.%s' % k, v)
+
     for directory in sorted(fallbacks.values(), key = lambda x: x.fingerprint):
       fingerprint = directory.fingerprint
       conf.set('%s.address' % fingerprint, directory.address)
diff --git a/test/unit/descriptor/remote.py b/test/unit/descriptor/remote.py
index 8bfdffc1..c17a1342 100644
--- a/test/unit/descriptor/remote.py
+++ b/test/unit/descriptor/remote.py
@@ -267,6 +267,9 @@ class TestDescriptorDownloader(unittest.TestCase):
     excepted_config = {
       'tor_commit': ['abc'],
       'stem_commit': ['def'],
+      'header.type': ['fallback'],
+      'header.version': ['2.0.0'],
+      'header.timestamp': ['20170526090242'],
       '01A9258A46E97FF8B2CAC7910577862C14F2C524.address': ['193.171.202.146'],
       '01A9258A46E97FF8B2CAC7910577862C14F2C524.or_port': ['9001'],
       '01A9258A46E97FF8B2CAC7910577862C14F2C524.dir_port': ['9030'],
@@ -278,7 +281,7 @@ class TestDescriptorDownloader(unittest.TestCase):
     }
 
     with tempfile.NamedTemporaryFile(prefix = 'fallbacks.') as tmp:
-      stem.descriptor.remote.FallbackDirectory._write(expected, 'abc', 'def', tmp.name)
+      stem.descriptor.remote.FallbackDirectory._write(expected, 'abc', 'def', header, tmp.name)
 
       conf = stem.util.conf.Config()
       conf.load(tmp.name)



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits