Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
  • Loading branch information
Jakub Sztandera committed Oct 19, 2020
1 parent be42dd8 commit bdb881f
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 17 deletions.
44 changes: 27 additions & 17 deletions chain/state/statetree.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ var log = logging.Logger("statetree")

// StateTree stores actors state by their ID.
type StateTree struct {
root adt.Map
version types.StateTreeVersion
info cid.Cid
Store cbor.IpldStore
root adt.Map
version types.StateTreeVersion
info cid.Cid
Store cbor.IpldStore
lookupIDFun func(address.Address) (address.Address, error)

snaps *stateSnaps
}
Expand Down Expand Up @@ -173,13 +174,15 @@ func NewStateTree(cst cbor.IpldStore, ver types.StateTreeVersion) (*StateTree, e
return nil, err
}

return &StateTree{
s := &StateTree{
root: root,
info: info,
version: ver,
Store: cst,
snaps: newStateSnaps(),
}, nil
}
s.lookupIDFun = s.lookupIDinternal
return s, nil
}

func LoadStateTree(cst cbor.IpldStore, c cid.Cid) (*StateTree, error) {
Expand Down Expand Up @@ -226,17 +229,7 @@ func (st *StateTree) SetActor(addr address.Address, act *types.Actor) error {
return nil
}

// LookupID gets the ID address of this actor's `addr` stored in the `InitActor`.
func (st *StateTree) LookupID(addr address.Address) (address.Address, error) {
if addr.Protocol() == address.ID {
return addr, nil
}

resa, ok := st.snaps.resolveAddress(addr)
if ok {
return resa, nil
}

func (st *StateTree) lookupIDinternal(addr address.Address) (address.Address, error) {
act, err := st.GetActor(init_.Address)
if err != nil {
return address.Undef, xerrors.Errorf("getting init actor: %w", err)
Expand All @@ -254,6 +247,23 @@ func (st *StateTree) LookupID(addr address.Address) (address.Address, error) {
if err != nil {
return address.Undef, xerrors.Errorf("resolve address %s: %w", addr, err)
}
return a, err
}

// LookupID gets the ID address of this actor's `addr` stored in the `InitActor`.
func (st *StateTree) LookupID(addr address.Address) (address.Address, error) {
if addr.Protocol() == address.ID {
return addr, nil
}

resa, ok := st.snaps.resolveAddress(addr)
if ok {
return resa, nil
}
a, err := st.lookupIDFun(addr)
if err != nil {
return a, err
}

st.snaps.cacheResolveAddress(addr, a)

Expand Down
98 changes: 98 additions & 0 deletions chain/state/statetree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,104 @@ func BenchmarkStateTreeSetFlush(b *testing.B) {
}
}

func TestResolveCache(t *testing.T) {
cst := cbor.NewMemCborStore()
st, err := NewStateTree(cst, VersionForNetwork(build.NewestNetworkVersion))
if err != nil {
t.Fatal(err)
}
nonId := address.NewForTestGetter()()
id, _ := address.NewIDAddress(1000)

st.lookupIDFun = func(a address.Address) (address.Address, error) {
if a == nonId {
return id, nil
} else {
return address.Undef, types.ErrActorNotFound
}
}

err = st.SetActor(nonId, &types.Actor{Nonce: 1})
if err != nil {
t.Fatal(err)
}

{
err = st.Snapshot(context.TODO())
if err != nil {
t.Fatal(err)
}
act, err := st.GetActor(nonId)
if err != nil {
t.Fatal(err)
}
if act.Nonce != 1 {
t.Fatalf("expected nonce 1, got %d", act.Nonce)
}
err = st.SetActor(nonId, &types.Actor{Nonce: 2})
if err != nil {
t.Fatal(err)
}

act, err = st.GetActor(nonId)
if err != nil {
t.Fatal(err)
}
if act.Nonce != 2 {
t.Fatalf("expected nonce 2, got %d", act.Nonce)
}

if err := st.Revert(); err != nil {
t.Fatal(err)
}
st.ClearSnapshot()
}

act, err := st.GetActor(nonId)
if err != nil {
t.Fatal(err)
}
if act.Nonce != 1 {
t.Fatalf("expected nonce 1, got %d", act.Nonce)
}

{
err = st.Snapshot(context.TODO())
if err != nil {
t.Fatal(err)
}
act, err := st.GetActor(nonId)
if err != nil {
t.Fatal(err)
}
if act.Nonce != 1 {
t.Fatalf("expected nonce 1, got %d", act.Nonce)
}
err = st.SetActor(nonId, &types.Actor{Nonce: 2})
if err != nil {
t.Fatal(err)
}

act, err = st.GetActor(nonId)
if err != nil {
t.Fatal(err)
}
if act.Nonce != 2 {
t.Fatalf("expected nonce 2, got %d", act.Nonce)
}
st.ClearSnapshot()
}

act, err = st.GetActor(nonId)
if err != nil {
t.Fatal(err)
}
if act.Nonce != 2 {
t.Fatalf("expected nonce 2, got %d", act.Nonce)
}

}

func BenchmarkStateTree10kGetActor(b *testing.B) {
cst := cbor.NewMemCborStore()
st, err := NewStateTree(cst, VersionForNetwork(build.NewestNetworkVersion))
Expand Down

0 comments on commit bdb881f

Please sign in to comment.