Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

polish(statetree): accept a context in statetree diff for timeouts #6639

Merged
merged 1 commit into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions chain/state/statetree.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,41 +547,46 @@ func (st *StateTree) Version() types.StateTreeVersion {
return st.version
}

func Diff(oldTree, newTree *StateTree) (map[string]types.Actor, error) {
func Diff(ctx context.Context, oldTree, newTree *StateTree) (map[string]types.Actor, error) {
out := map[string]types.Actor{}

var (
ncval, ocval cbg.Deferred
buf = bytes.NewReader(nil)
)
if err := newTree.root.ForEach(&ncval, func(k string) error {
var act types.Actor

addr, err := address.NewFromBytes([]byte(k))
if err != nil {
return xerrors.Errorf("address in state tree was not valid: %w", err)
}
select {
case <-ctx.Done():
return ctx.Err()
default:
var act types.Actor

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would normally close the select here with the default case being essentially a no-op. The context check then becomes like an early return.

addr, err := address.NewFromBytes([]byte(k))
if err != nil {
return xerrors.Errorf("address in state tree was not valid: %w", err)
}

found, err := oldTree.root.Get(abi.AddrKey(addr), &ocval)
if err != nil {
return err
}
found, err := oldTree.root.Get(abi.AddrKey(addr), &ocval)
if err != nil {
return err
}

if found && bytes.Equal(ocval.Raw, ncval.Raw) {
return nil // not changed
}
if found && bytes.Equal(ocval.Raw, ncval.Raw) {
return nil // not changed
}

buf.Reset(ncval.Raw)
err = act.UnmarshalCBOR(buf)
buf.Reset(nil)
buf.Reset(ncval.Raw)
err = act.UnmarshalCBOR(buf)
buf.Reset(nil)

if err != nil {
return err
}
if err != nil {
return err
}

out[addr.String()] = act
out[addr.String()] = act

return nil
return nil
}
}); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion node/impl/full/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ func (a *StateAPI) StateChangedActors(ctx context.Context, old cid.Cid, new cid.
return nil, xerrors.Errorf("failed to load new state tree: %w", err)
}

return state.Diff(oldTree, newTree)
return state.Diff(ctx, oldTree, newTree)
}

func (a *StateAPI) StateMinerSectorCount(ctx context.Context, addr address.Address, tsk types.TipSetKey) (api.MinerSectors, error) {
Expand Down