From eb3b9b16afb1e0993c6179c1a4792c31e949457c Mon Sep 17 00:00:00 2001
From: Brian Warner <warner@lothar.com>
Date: Thu, 14 Dec 2006 04:31:17 -0700
Subject: [PATCH] encode: start to fix a few problems, still a lot of work left
 to go

---
 src/allmydata/chunk.py            |  2 +-
 src/allmydata/encode_new.py       | 11 ++++++++---
 src/allmydata/test/test_encode.py |  4 ++++
 3 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/allmydata/chunk.py b/src/allmydata/chunk.py
index 59495b43..3ee903e5 100644
--- a/src/allmydata/chunk.py
+++ b/src/allmydata/chunk.py
@@ -135,7 +135,7 @@ class CompleteBinaryTreeMixin:
 
   def needed_for(self, i):
     """
-    Return a list of nodes that are necessary for the hash chain.
+    Return a list of node indices that are necessary for the hash chain.
     """
     if i < 0 or i >= len(self):
       raise IndexError('index out of range: ' + repr(i))
diff --git a/src/allmydata/encode_new.py b/src/allmydata/encode_new.py
index c7396c7e..4e13b21c 100644
--- a/src/allmydata/encode_new.py
+++ b/src/allmydata/encode_new.py
@@ -2,7 +2,7 @@
 
 import math
 from twisted.internet import defer
-from allmydata.chunk import HashTree
+from allmydata.chunk import HashTree, roundup_pow2
 from Crypto.Cipher import AES
 import sha
 
@@ -160,8 +160,13 @@ class Encoder(object):
         self.root_hash = t[0]
         # now send just the necessary pieces out to each shareholder
         for i in range(self.num_shares):
-            needed_hash_indices = t.needed_for(i)
-            dl.append(self.send_one_share_hash_tree(i, needed_hash_indices))
+            # the HashTree is given a list of leaves: 0,1,2,3..n .
+            # These become nodes A+0,A+1,A+2.. of the tree, where A=n-1
+            tree_width = roundup_pow2(self.num_shares)
+            base_index = i + tree_width - 1
+            needed_hash_indices = t.needed_for(base_index)
+            hashes = [(hi, t[hi]) for hi in needed_hash_indices]
+            dl.append(self.send_one_share_hash_tree(i, hashes))
         return defer.DeferredList(dl)
 
     def send_one_share_hash_tree(self, share_num, needed_hashes):
diff --git a/src/allmydata/test/test_encode.py b/src/allmydata/test/test_encode.py
index f37cc68b..5af27b7f 100644
--- a/src/allmydata/test/test_encode.py
+++ b/src/allmydata/test/test_encode.py
@@ -7,6 +7,10 @@ from cStringIO import StringIO
 
 class MyEncoder(encode_new.Encoder):
     def send(self, share_num, methname, *args, **kwargs):
+        if False and share_num < 10:
+            print "send[%d].%s()" % (share_num, methname)
+            if methname == "put_share_hashes":
+                print " ", [i for i,h in args[0]]
         return defer.succeed(None)
 
 class Encode(unittest.TestCase):
-- 
2.45.2