mirror of
https://github.com/arnaucube/arbo.git
synced 2026-01-08 15:01:29 +01:00
Add tree.Get
This commit is contained in:
18
tree.go
18
tree.go
@@ -434,8 +434,22 @@ func bytesToBitmap(b []byte) []bool {
|
|||||||
|
|
||||||
// Get returns the value for a given key
|
// Get returns the value for a given key
|
||||||
func (t *Tree) Get(k []byte) ([]byte, []byte, error) {
|
func (t *Tree) Get(k []byte) ([]byte, []byte, error) {
|
||||||
// unimplemented
|
keyPath := make([]byte, t.hashFunction.Len())
|
||||||
return nil, nil, fmt.Errorf("unimplemented")
|
copy(keyPath[:], k)
|
||||||
|
|
||||||
|
path := getPath(t.maxLevels, keyPath)
|
||||||
|
// go down to the leaf
|
||||||
|
var siblings [][]byte
|
||||||
|
_, value, _, err := t.down(k, t.root, siblings, path, 0, true)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
leafK, leafV := readLeafValue(value)
|
||||||
|
if !bytes.Equal(k, leafK) {
|
||||||
|
panic(fmt.Errorf("%s != %s", BytesToBigInt(k), BytesToBigInt(leafK)))
|
||||||
|
}
|
||||||
|
|
||||||
|
return leafK, leafV, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckProof verifies the given proof. The proof verification depends on the
|
// CheckProof verifies the given proof. The proof verification depends on the
|
||||||
|
|||||||
20
tree_test.go
20
tree_test.go
@@ -145,6 +145,26 @@ func TestAux(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGet(t *testing.T) {
|
||||||
|
tree, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon)
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
defer tree.db.Close()
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
k := BigIntToBytes(big.NewInt(int64(i)))
|
||||||
|
v := BigIntToBytes(big.NewInt(int64(i * 2)))
|
||||||
|
if err := tree.Add(k, v); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
k := BigIntToBytes(big.NewInt(int64(7)))
|
||||||
|
gettedKey, gettedValue, err := tree.Get(k)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, k, gettedKey)
|
||||||
|
assert.Equal(t, BigIntToBytes(big.NewInt(int64(7*2))), gettedValue)
|
||||||
|
}
|
||||||
|
|
||||||
func TestGenProofAndVerify(t *testing.T) {
|
func TestGenProofAndVerify(t *testing.T) {
|
||||||
tree, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon)
|
tree, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user