Update Snapshot & Root approach

Update Snapshot & Root approach to get the root always from the db,
except in the cases that the tree is a snapshot, in which the root will
be in memory.
In this way, when a snapshot is performed and the original tree gets
modifyed, the snapshot will still point to the old root. Also, the root
obtained from the db, uses also the db.ReadTx, so if the root is being
modifyied in the current tx (db.WriteTx), when getting the root it will
be return the lastest version that is in the tx but not yet in the db.
This commit is contained in:
2021-08-11 19:30:31 +02:00
parent 2514b3188f
commit 5f6c35e435
6 changed files with 262 additions and 141 deletions

View File

@@ -914,7 +914,9 @@ func TestLoadVT(t *testing.T) {
c.Assert(err, qt.IsNil)
// check that tree & vt roots are equal
c.Check(tree.Root(), qt.DeepEquals, vt.root.h)
root, err := tree.Root()
c.Assert(err, qt.IsNil)
c.Check(root, qt.DeepEquals, vt.root.h)
}
// TestAddKeysWithEmptyValues calls AddBatch giving an array of empty values
@@ -976,12 +978,14 @@ func TestAddKeysWithEmptyValues(t *testing.T) {
c.Assert(existence, qt.IsTrue)
// check with empty array
verif, err := CheckProof(tree.hashFunction, keys[9], []byte{}, tree.Root(), siblings)
root, err := tree.Root()
c.Assert(err, qt.IsNil)
verif, err := CheckProof(tree.hashFunction, keys[9], []byte{}, root, siblings)
c.Assert(err, qt.IsNil)
c.Check(verif, qt.IsTrue)
// check with array with only 1 zero
verif, err = CheckProof(tree.hashFunction, keys[9], []byte{0}, tree.Root(), siblings)
verif, err = CheckProof(tree.hashFunction, keys[9], []byte{0}, root, siblings)
c.Assert(err, qt.IsNil)
c.Check(verif, qt.IsTrue)
@@ -989,12 +993,12 @@ func TestAddKeysWithEmptyValues(t *testing.T) {
e32 := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
c.Assert(len(e32), qt.Equals, 32)
verif, err = CheckProof(tree.hashFunction, keys[9], e32, tree.Root(), siblings)
verif, err = CheckProof(tree.hashFunction, keys[9], e32, root, siblings)
c.Assert(err, qt.IsNil)
c.Check(verif, qt.IsTrue)
// check with array with value!=0 returns false at verification
verif, err = CheckProof(tree.hashFunction, keys[9], []byte{0, 1}, tree.Root(), siblings)
verif, err = CheckProof(tree.hashFunction, keys[9], []byte{0, 1}, root, siblings)
c.Assert(err, qt.IsNil)
c.Check(verif, qt.IsFalse)
}