Skip to content

Commit

Permalink
add StateEpoch to Get
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Oct 11, 2024
1 parent 0bcb7e6 commit 029c858
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 33 deletions.
2 changes: 1 addition & 1 deletion empty.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (Empty) Delete([]byte, StateEpoch, NodeResolverFn) (bool, error) {
return false, errors.New("cant delete an empty node")
}

func (Empty) Get([]byte, NodeResolverFn) ([]byte, error) {
func (Empty) Get([]byte, StateEpoch, NodeResolverFn) ([]byte, error) {
return nil, nil
}

Expand Down
2 changes: 1 addition & 1 deletion empty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestEmptyFuncs(t *testing.T) {
if err == nil {
t.Fatal("got nil error when deleting from empty")
}
v, err := e.Get(zeroKeyTest, nil)
v, err := e.Get(zeroKeyTest, 0, nil)
if err != nil {
t.Fatal("got non-nil error when getting from empty")
}
Expand Down
2 changes: 1 addition & 1 deletion expired_leaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (ExpiredLeafNode) Delete([]byte, StateEpoch, NodeResolverFn) (bool, error)
return false, errExpiredLeafNode
}

func (ExpiredLeafNode) Get([]byte, NodeResolverFn) ([]byte, error) {
func (ExpiredLeafNode) Get([]byte, StateEpoch, NodeResolverFn) ([]byte, error) {
return nil, errExpiredLeafNode
}

Expand Down
2 changes: 1 addition & 1 deletion hashednode.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (HashedNode) Delete([]byte, StateEpoch, NodeResolverFn) (bool, error) {
return false, errors.New("cant delete a hashed node in-place")
}

func (HashedNode) Get([]byte, NodeResolverFn) ([]byte, error) {
func (HashedNode) Get([]byte, StateEpoch, NodeResolverFn) ([]byte, error) {
return nil, errors.New("can not read from a hash node")
}

Expand Down
2 changes: 1 addition & 1 deletion hashednode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestHashedNodeFuncs(t *testing.T) {
if err == nil {
t.Fatal("got nil error when deleting from a hashed node")
}
v, err := e.Get(zeroKeyTest, nil)
v, err := e.Get(zeroKeyTest, 0, nil)
if err == nil {
t.Fatal("got nil error when getting from a hashed node")
}
Expand Down
4 changes: 2 additions & 2 deletions proof_ipa.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func getProofElementsFromTree(preroot, postroot VerkleNode, keys [][]byte, resol
// keys were sorted already in the above GetcommitmentsForMultiproof.
// Set the post values, if they are untouched, leave them `nil`
for i := range keys {
val, err := postroot.Get(keys[i], resolver)
val, err := postroot.Get(keys[i], 0, resolver)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("error getting post-state value for key %x: %w", keys[i], err)
}
Expand Down Expand Up @@ -580,7 +580,7 @@ func Verify(vp *VerkleProof, preStateRoot []byte, postStateRoot []byte, statedif
copy(key[:31], stemdiff.Stem[:])
key[31] = suffixdiff.Suffix

val, err := pretree.Get(key[:], nil)
val, err := pretree.Get(key[:], 0, nil)
if err != nil {
return fmt.Errorf("could not find key %x in tree rebuilt from proof: %w", key, err)
}
Expand Down
4 changes: 2 additions & 2 deletions proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ func TestProofOfAbsenceOtherMultipleLeaves(t *testing.T) {
t.Fatalf("error deserializing %v", err)
}

got, err := deserialized.Get(ret1, nil)
got, err := deserialized.Get(ret1, 0, nil)
if err != nil {
t.Fatalf("error while trying to read missing value: %v", err)
}
Expand Down Expand Up @@ -1132,7 +1132,7 @@ func TestGenerateProofWithOnlyAbsentKeys(t *testing.T) {
var key [32]byte
copy(key[:], presentKey)
key[StemSize] = byte(i)
if _, err := droot.Get(key[:], nil); err != errIsPOAStub {
if _, err := droot.Get(key[:], 0, nil); err != errIsPOAStub {
t.Fatalf("expected ErrPOALeafValue, got %v", err)
}
}
Expand Down
18 changes: 11 additions & 7 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type VerkleNode interface {
Delete([]byte, StateEpoch, NodeResolverFn) (bool, error)

// Get value at a given key
Get([]byte, NodeResolverFn) ([]byte, error)
Get([]byte, StateEpoch, NodeResolverFn) ([]byte, error)

// Commit computes the commitment of the node. The
// result (the curve point) is cached.
Expand Down Expand Up @@ -558,7 +558,7 @@ func (n *InternalNode) CreatePath(path []byte, stemInfo stemInfo, comms []*Point
// GetValuesAtStem returns the all NodeWidth values of the stem.
// The returned slice is internal to the tree, so it *must* be considered readonly
// for callers.
func (n *InternalNode) GetValuesAtStem(stem Stem, resolver NodeResolverFn) ([][]byte, error) {
func (n *InternalNode) GetValuesAtStem(stem Stem, curEpoch StateEpoch, resolver NodeResolverFn) ([][]byte, error) {
nchild := offset2key(stem, n.depth) // index of the child pointed by the next byte in the key
switch child := n.children[nchild].(type) {
case UnknownNode:
Expand All @@ -580,10 +580,14 @@ func (n *InternalNode) GetValuesAtStem(stem Stem, resolver NodeResolverFn) ([][]
n.children[nchild] = resolved
// recurse to handle the case of a LeafNode child that
// splits.
return n.GetValuesAtStem(stem, resolver)
return n.GetValuesAtStem(stem, curEpoch, resolver)
case *ExpiredLeafNode:
return nil, errEpochExpired
case *LeafNode:
if EpochExpired(child.lastEpoch, curEpoch) {
return nil, errEpochExpired
}

if equalPaths(child.stem, stem) {
// We can't return the values since it's a POA leaf node, so we know nothing
// about its values.
Expand All @@ -594,7 +598,7 @@ func (n *InternalNode) GetValuesAtStem(stem Stem, resolver NodeResolverFn) ([][]
}
return nil, nil
case *InternalNode:
return child.GetValuesAtStem(stem, resolver)
return child.GetValuesAtStem(stem, curEpoch, resolver)
default:
return nil, errUnknownNodeType
}
Expand Down Expand Up @@ -784,11 +788,11 @@ func (n *InternalNode) FlushAtDepth(depth uint8, flush NodeFlushFn) {
}
}

func (n *InternalNode) Get(key []byte, resolver NodeResolverFn) ([]byte, error) {
func (n *InternalNode) Get(key []byte, curEpoch StateEpoch, resolver NodeResolverFn) ([]byte, error) {
if len(key) != StemSize+1 {
return nil, fmt.Errorf("invalid key length, expected %d, got %d", StemSize+1, len(key))
}
stemValues, err := n.GetValuesAtStem(KeyToStem(key), resolver)
stemValues, err := n.GetValuesAtStem(KeyToStem(key), curEpoch, resolver)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1455,7 +1459,7 @@ func (n *LeafNode) Delete(k []byte, curEpoch StateEpoch, _ NodeResolverFn) (bool
return false, n.updateLeaf(k[StemSize], nil, curEpoch)
}

func (n *LeafNode) Get(k []byte, _ NodeResolverFn) ([]byte, error) {
func (n *LeafNode) Get(k []byte, curEpoch StateEpoch, _ NodeResolverFn) ([]byte, error) {
if n.isPOAStub {
return nil, errIsPOAStub
}
Expand Down
30 changes: 15 additions & 15 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestGetTwoLeaves(t *testing.T) {
t.Fatalf("error inserting: %v", err)
}

val, err := root.Get(zeroKeyTest, nil)
val, err := root.Get(zeroKeyTest, 0, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -149,7 +149,7 @@ func TestGetTwoLeaves(t *testing.T) {
t.Fatalf("got a different value from the tree than expected %x != %x", val, testValue)
}

val, err = root.Get(oneKeyTest, nil)
val, err = root.Get(oneKeyTest, 0, nil)
if err != nil {
t.Fatalf("wrong error type, expected %v, got %v", nil, err)
}
Expand Down Expand Up @@ -327,7 +327,7 @@ func TestDelLeaf(t *testing.T) { // skipcq: GO-R1005
t.Errorf("deleting leaf resulted in unexpected tree %x %x", init.Bytes(), postHash.Bytes())
}

res, err := tree.Get(key3, nil)
res, err := tree.Get(key3, 0, nil)
if err != nil {
t.Error(err)
}
Expand All @@ -338,7 +338,7 @@ func TestDelLeaf(t *testing.T) { // skipcq: GO-R1005
if _, err := tree.Delete(key1pp, 0, nil); err != nil {
t.Fatal(err)
}
res, err = tree.Get(key1pp, nil)
res, err = tree.Get(key1pp, 0, nil)
if err != nil {
t.Error(err)
}
Expand All @@ -349,7 +349,7 @@ func TestDelLeaf(t *testing.T) { // skipcq: GO-R1005
if _, err := tree.Delete(key1p, 0, nil); err != nil {
t.Fatal(err)
}
res, err = tree.Get(key1p, nil)
res, err = tree.Get(key1p, 0, nil)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -385,14 +385,14 @@ func TestDeleteAtStem(t *testing.T) {
t.Error(err)
}

res, err := tree.Get(key1, nil)
res, err := tree.Get(key1, 0, nil)
if err != nil {
t.Error(err)
}
if len(res) > 0 {
t.Error("leaf hasnt been deleted")
}
res, err = tree.Get(key1pp, nil)
res, err = tree.Get(key1pp, 0, nil)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -467,7 +467,7 @@ func TestDeletePrune(t *testing.T) { // skipcq: GO-R1005
if !hashPostKey4.Equal(postHash) {
t.Error("deleting leaf #5 resulted in unexpected tree")
}
res, err := tree.Get(key5, nil)
res, err := tree.Get(key5, 0, nil)
if err != nil {
t.Error(err)
}
Expand All @@ -487,7 +487,7 @@ func TestDeletePrune(t *testing.T) { // skipcq: GO-R1005
if !hashPostKey2.Equal(postHash) {
t.Error("deleting leaf #3 resulted in unexpected tree")
}
res, err = tree.Get(key3, nil)
res, err = tree.Get(key3, 0, nil)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -884,17 +884,17 @@ func TestGetResolveFromHash(t *testing.T) {
t.Fatal(err)
}

data, err := root.Get(zeroKeyTest, nil)
data, err := root.Get(zeroKeyTest, 0, nil)
if !errors.Is(err, errReadFromInvalid) || len(data) != 0 {
t.Fatal(err)
}

data, err = root.Get(zeroKeyTest, failingGetter)
data, err = root.Get(zeroKeyTest, 0, failingGetter)
if !errors.Is(err, dummyError) || len(data) != 0 {
t.Fatal(err)
}

data, err = root.Get(zeroKeyTest, getter)
data, err = root.Get(zeroKeyTest, 0, getter)
if err != nil {
t.Fatalf("error resolving hash: %v", err)
}
Expand Down Expand Up @@ -1648,7 +1648,7 @@ func TestLeafNodeInsert(t *testing.T) {
}

// Check we get the value correctly via Get(...).
getValue, err := ln.Get(append(KeyToStem(keyTest), byte(valIdx)), nil)
getValue, err := ln.Get(append(KeyToStem(keyTest), byte(valIdx)), 0, nil)
if err != nil {
t.Fatalf("failed to get leaf node key/value: %v", err)
}
Expand Down Expand Up @@ -1809,7 +1809,7 @@ func runRandTest(rt randTest) error {
}
delete(values, string(step.key))
case opGet:
v, err := root.Get(step.key, nil)
v, err := root.Get(step.key, 0, nil)
want := values[string(step.key)]
if string(v) != want {
rt[i].err = fmt.Errorf("mismatch for key %#x, got %#x want %#x, err %v", step.key, v, want, err)
Expand Down Expand Up @@ -1872,7 +1872,7 @@ func TestRandomExtracted(t *testing.T) {
t.Fatalf("error deleting key: %v", err)
}

val, err := root.Get(k1490, nil)
val, err := root.Get(k1490, 0, nil)
if err != nil {
t.Fatalf("error getting key: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion unknown.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (UnknownNode) Delete([]byte, StateEpoch, NodeResolverFn) (bool, error) {
return false, errors.New("cant delete in a subtree missing form a stateless view")
}

func (UnknownNode) Get([]byte, NodeResolverFn) ([]byte, error) {
func (UnknownNode) Get([]byte, StateEpoch, NodeResolverFn) ([]byte, error) {
return nil, nil
}

Expand Down
2 changes: 1 addition & 1 deletion unknown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestUnknownFuncs(t *testing.T) {
if _, err := un.Delete(nil, 0, nil); err == nil {
t.Errorf("got nil error when deleting from a hashed node")
}
if _, err := un.Get(nil, nil); err != nil {
if _, err := un.Get(nil, 0, nil); err != nil {
t.Errorf("got %v, want nil", err)
}
var identity Point
Expand Down

0 comments on commit 029c858

Please sign in to comment.