2019-07-25 22:15:03 +00:00
|
|
|
package state
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
import (
|
2020-09-21 19:50:12 +00:00
|
|
|
"bytes"
|
2019-07-05 14:29:17 +00:00
|
|
|
"context"
|
2019-07-30 00:46:56 +00:00
|
|
|
"fmt"
|
2020-03-03 23:01:35 +00:00
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2020-02-04 22:19:05 +00:00
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
2020-01-08 19:10:57 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2022-06-14 15:00:51 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
2020-01-22 19:53:06 +00:00
|
|
|
"go.opencensus.io/trace"
|
2019-09-10 02:05:24 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-07-12 10:23:05 +00:00
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-09-14 11:14:06 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-11-09 08:44:51 +00:00
|
|
|
builtin_types "github.com/filecoin-project/go-state-types/builtin"
|
2020-10-06 22:22:54 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
2021-01-19 00:28:18 +00:00
|
|
|
states0 "github.com/filecoin-project/specs-actors/actors/states"
|
|
|
|
states2 "github.com/filecoin-project/specs-actors/v2/actors/states"
|
|
|
|
states3 "github.com/filecoin-project/specs-actors/v3/actors/states"
|
2021-04-23 12:29:46 +00:00
|
|
|
states4 "github.com/filecoin-project/specs-actors/v4/actors/states"
|
2021-05-17 20:02:23 +00:00
|
|
|
states5 "github.com/filecoin-project/specs-actors/v5/actors/states"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/adt"
|
|
|
|
init_ "github.com/filecoin-project/lotus/chain/actors/builtin/init"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-07-05 14:29:17 +00:00
|
|
|
)
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
var log = logging.Logger("statetree")
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
// StateTree stores actors state by their ID.
|
2019-07-05 14:29:17 +00:00
|
|
|
type StateTree struct {
|
2020-10-19 23:25:41 +00:00
|
|
|
root adt.Map
|
|
|
|
version types.StateTreeVersion
|
|
|
|
info cid.Cid
|
|
|
|
Store cbor.IpldStore
|
|
|
|
lookupIDFun func(address.Address) (address.Address, error)
|
2019-07-05 14:29:17 +00:00
|
|
|
|
2020-04-03 00:09:41 +00:00
|
|
|
snaps *stateSnaps
|
|
|
|
}
|
|
|
|
|
|
|
|
type stateSnaps struct {
|
2020-10-19 20:32:05 +00:00
|
|
|
layers []*stateSnapLayer
|
|
|
|
lastMaybeNonEmptyResolveCache int
|
2020-04-03 23:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type stateSnapLayer struct {
|
|
|
|
actors map[address.Address]streeOp
|
|
|
|
resolveCache map[address.Address]address.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStateSnapLayer() *stateSnapLayer {
|
|
|
|
return &stateSnapLayer{
|
|
|
|
actors: make(map[address.Address]streeOp),
|
|
|
|
resolveCache: make(map[address.Address]address.Address),
|
|
|
|
}
|
2020-04-03 00:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type streeOp struct {
|
|
|
|
Act types.Actor
|
|
|
|
Delete bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStateSnaps() *stateSnaps {
|
2020-04-03 23:26:52 +00:00
|
|
|
ss := &stateSnaps{}
|
|
|
|
ss.addLayer()
|
|
|
|
return ss
|
2020-04-03 00:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *stateSnaps) addLayer() {
|
2020-04-03 23:26:52 +00:00
|
|
|
ss.layers = append(ss.layers, newStateSnapLayer())
|
2020-04-03 00:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *stateSnaps) dropLayer() {
|
2020-04-03 01:20:49 +00:00
|
|
|
ss.layers[len(ss.layers)-1] = nil // allow it to be GCed
|
2020-10-19 20:32:05 +00:00
|
|
|
|
2020-04-03 00:09:41 +00:00
|
|
|
ss.layers = ss.layers[:len(ss.layers)-1]
|
2020-10-19 20:32:05 +00:00
|
|
|
|
|
|
|
if ss.lastMaybeNonEmptyResolveCache == len(ss.layers) {
|
|
|
|
ss.lastMaybeNonEmptyResolveCache = len(ss.layers) - 1
|
|
|
|
}
|
2020-04-03 00:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *stateSnaps) mergeLastLayer() {
|
|
|
|
last := ss.layers[len(ss.layers)-1]
|
|
|
|
nextLast := ss.layers[len(ss.layers)-2]
|
|
|
|
|
2020-04-03 23:26:52 +00:00
|
|
|
for k, v := range last.actors {
|
|
|
|
nextLast.actors[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range last.resolveCache {
|
|
|
|
nextLast.resolveCache[k] = v
|
2020-04-03 00:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ss.dropLayer()
|
|
|
|
}
|
|
|
|
|
2020-04-03 23:26:52 +00:00
|
|
|
func (ss *stateSnaps) resolveAddress(addr address.Address) (address.Address, bool) {
|
2020-10-19 20:32:05 +00:00
|
|
|
for i := ss.lastMaybeNonEmptyResolveCache; i >= 0; i-- {
|
|
|
|
if len(ss.layers[i].resolveCache) == 0 {
|
|
|
|
if ss.lastMaybeNonEmptyResolveCache == i {
|
|
|
|
ss.lastMaybeNonEmptyResolveCache = i - 1
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2020-04-03 23:26:52 +00:00
|
|
|
resa, ok := ss.layers[i].resolveCache[addr]
|
|
|
|
if ok {
|
|
|
|
return resa, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return address.Undef, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *stateSnaps) cacheResolveAddress(addr, resa address.Address) {
|
|
|
|
ss.layers[len(ss.layers)-1].resolveCache[addr] = resa
|
2020-10-19 20:32:05 +00:00
|
|
|
ss.lastMaybeNonEmptyResolveCache = len(ss.layers) - 1
|
2020-04-03 23:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 00:09:41 +00:00
|
|
|
func (ss *stateSnaps) getActor(addr address.Address) (*types.Actor, error) {
|
|
|
|
for i := len(ss.layers) - 1; i >= 0; i-- {
|
2020-04-03 23:26:52 +00:00
|
|
|
act, ok := ss.layers[i].actors[addr]
|
2020-04-03 00:09:41 +00:00
|
|
|
if ok {
|
|
|
|
if act.Delete {
|
|
|
|
return nil, types.ErrActorNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return &act.Act, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *stateSnaps) setActor(addr address.Address, act *types.Actor) {
|
2020-04-03 23:26:52 +00:00
|
|
|
ss.layers[len(ss.layers)-1].actors[addr] = streeOp{Act: *act}
|
2020-04-03 00:09:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ss *stateSnaps) deleteActor(addr address.Address) {
|
2020-04-03 23:26:52 +00:00
|
|
|
ss.layers[len(ss.layers)-1].actors[addr] = streeOp{Delete: true}
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 22:22:54 +00:00
|
|
|
// VersionForNetwork returns the state tree version for the given network
|
|
|
|
// version.
|
2021-05-15 01:11:23 +00:00
|
|
|
func VersionForNetwork(ver network.Version) (types.StateTreeVersion, error) {
|
|
|
|
switch ver {
|
|
|
|
case network.Version0, network.Version1, network.Version2, network.Version3:
|
|
|
|
return types.StateTreeVersion0, nil
|
|
|
|
case network.Version4, network.Version5, network.Version6, network.Version7, network.Version8, network.Version9:
|
|
|
|
return types.StateTreeVersion1, nil
|
|
|
|
case network.Version10, network.Version11:
|
|
|
|
return types.StateTreeVersion2, nil
|
|
|
|
case network.Version12:
|
|
|
|
return types.StateTreeVersion3, nil
|
2021-10-18 15:46:50 +00:00
|
|
|
|
2022-11-09 06:30:42 +00:00
|
|
|
case network.Version13, network.Version14, network.Version15, network.Version16, network.Version17:
|
|
|
|
return types.StateTreeVersion4, nil
|
2021-10-18 15:46:50 +00:00
|
|
|
|
chore: Merge nv22 into master (#11699)
* [WIP] feat: Add nv22 skeleton
Addition of Network Version 22 skeleton
* update FFI
* feat: drand: refactor round verification
* feat: sealing: Support nv22 DDO features in the sealing pipeline (#11226)
* Initial work supporting DDO pieces in lotus-miner
* sealing: Update pipeline input to operate on UniversalPiece
* sealing: Update pipeline checks/sealing states to operate on UniversalPiece
* sealing: Make pipeline build with UniversalPiece
* move PieceDealInfo out of api
* make gen
* make sealing pipeline unit tests pass
* fix itest ensemble build
* don't panic in SectorsStatus with deals
* stop linter from complaining about checkPieces
* fix sector import tests
* mod tidy
* sealing: Add logic for (pre)committing DDO sectors
* sealing: state-types with method defs
* DDO non-snap pipeline works(?), DDO Itests
* DDO support in snapdeals pipeline
* make gen
* update actor bundles
* update the gst market fix
* fix: chain: use PreCommitSectorsBatch2 when setting up genesis
* some bug fixes
* integration working changes
* update actor bundles
* Make TestOnboardRawPieceSnap pass
* Appease the linter
* Make deadlines test pass with v12 actors
* Update go-state-types, abstract market DealState
* make gen
* mod tidy, lint fixes
* Fix some more tests
* Bump version in master
Bump version in master
* Make gen
Make gen
* fix sender
* fix: lotus-provider: Fix winning PoSt
* fix: sql Scan cannot write to an object
* Actually show miner-addrs in info-log
Actually show miner-addrs in lotus-provider info-log
* [WIP] feat: Add nv22 skeleton
Addition of Network Version 22 skeleton
* update FFI
* ddo is now nv22
* make gen
* temp actor bundle with ddo
* use working go-state-types
* gst with v13 market migration
* update bundle, builtin.MethodsMiner.ProveCommitSectors2 -> 3
* actually working v13 migration, v13 migration itest
* Address review
* sealing: Correct DDO snap pledge math
* itests: Mixed ddo itest
* pipeline: Fix sectorWeight
* sealing: convert market deals into PAMs in mixed sectors
* sealing: make market to ddo conversion work
* fix lint
* update gst
* Update actors and GST to lastest integ branch
* commit batcher: Update ProveCommitSectors3Params builder logic
* make gen
* use builtin-actors master
* ddo: address review
* itests: Add commd assertions to ddo tests
* make gen
* gst with fixed types
* config knobs for RequireActivationSuccess
* storage: Drop obsolete flaky tasts
---------
Co-authored-by: Jennifer Wang <jiayingw703@gmail.com>
Co-authored-by: Aayush <arajasek94@gmail.com>
Co-authored-by: Shrenuj Bansal <shrenuj.bansal@protocol.ai>
Co-authored-by: Phi <orjan.roren@gmail.com>
Co-authored-by: Andrew Jackson (Ajax) <snadrus@gmail.com>
Co-authored-by: TippyFlits <james.bluett@protocol.ai>
* feat: implement FIP-0063
* chore: deps: update to go-multiaddr v0.12.2 (#11602)
* feat: fvm: update the FVM/FFI to v4.1 (#11608) (#11612)
This:
1. Adds nv22 support.
2. Updates the message tracing format.
Co-authored-by: Steven Allen <steven@stebalien.com>
* AggregateProofType nil when doing batch updates
Use latest nv22 go-state-types version with matching update
* Update to v13.0.0-rc.2 bundle
* chore: Upgrade heights and codename
Update upgrade heights
Co-Authored-By: Steven Allen <steven@stebalien.com>
* Update epoch after nv22 DRAND switch
Update epoch after nv22 DRAND switch
* Update Mango codename to Phoneix
Make the codename for the Drand-change inline with Dragon style.
* Add UpgradePhoenixHeight to API params
* set UpgradePhoenixHeight to be one hour after Dragon
* Make gen
Make gen and UpgradePhoenixHeight in butterfly and local devnet to be in line with Calibration and Mainnet
* Update epoch heights (#11637)
Update epoch heights
* new: add forest bootstrap nodes (#11636)
Signed-off-by: samuelarogbonlo <sbayo971@gmail.com>
* Merge pull request #11491 from filecoin-project/fix/remove-decommissioned-pl-bootstrap-nodes
Remove PL operated bootstrap nodes from mainnet.pi
* feat: api: new verified registry methods to get all allocations and claims (#11631)
* new verireg methods
* update changelog and add itest
* update itest and cli
* update new method's support till v9
* remove gateway APIs
* fix cli internal var names
* chore:: backport #11609 to the feat/nv22 branch (#11644)
* feat: api: improve the correctness of Eth's trace_block (#11609)
* Improve the correctness of Eth's trace_block
- Improve encoding/decoding of parameters and return values:
- Encode "native" parameters and return values with Solidity ABI.
- Correctly decode parameters to "create" calls.
- Use the correct (ish) output for "create" calls.
- Handle all forms of "create".
- Make robust with respect to reverts:
- Use the actor ID/address from the trace instead of looking it up in
the state-tree (may not exist in the state-tree due to a revert).
- Gracefully handle failed actor/contract creation.
- Improve performance:
- We avoid looking anything up in the state-tree when translating the
trace, which should significantly improve performance.
- Improve code readability:
- Remove all "backtracking" logic.
- Use an "environment" struct to store temporary state instead of
attaching it to the trace.
- Fix random bugs:
- Fix an allocation bug in the "address" logic (need to set the
capacity before modifying the slice).
- Improved error checking/handling.
- Use correct types for `trace_block` action/results (create, call, etc.).
- And use the correct types for Result/Action structs instead of reusing the same "Call" action every time.
- Improve error messages.
* Make gen
Make gen
---------
Co-authored-by: Steven Allen <steven@stebalien.com>
* fix: add UpgradePhoenixHeight to StateGetNetworkParams (#11648)
* chore: deps: update to go-state-types v13.0.0-rc.1
* do NOT update the cache when running the real migration
* Merge pull request #11632 from hanabi1224/hm/drand-test
feat: drand quicknet: allow scheduling drand quicknet upgrade before nv22 on 2k devnet
* chore: deps: update to go-state-types v13.0.0-rc.2
chore: deps: update to go-state-types v13.0.0-rc.2
* feat: set migration config UpgradeEpoch for v13 actors upgrade
* Built-in actor events first draft
* itest for DDO non-market verified data w/ builtin actor events
* Tests for builtin actor events API
* Clean up DDO+Events tests, add lots of explainer comments
* Minor tweaks to events types
* Avoid duplicate messages when looking for receipts
* Rename internal events modules for clarity
* Adjust actor event API after review
* s/ActorEvents/Events/g in global config
* Manage event sending rate for SubscribeActorEvents
* Terminate SubscribeActorEvents chan when at max height
* Document future API changes
* More clarity in actor event API docs
* More post-review changes, lots of tests for SubscribeActorEvents
Use BlockDelay as the window for receiving events on the SubscribeActorEvents
channel. We expect the user to have received the initial batch of historical
events (if any) in one block's time. For real-time events we expect them to
not fall behind by roughly one block's time.
* Remove duplicate code from actor event type marshalling tests
Reduce verbosity and remove duplicate test logic from actor event types
JSON marshalling tests.
* Rename actor events test to follow go convention
Add missing `s` to `actor_events` test file to follow golang convention
used across the repo.
* Run actor events table tests in deterministic order
Refactor `map` usage for actor event table tests to ensure deterministic
test execution order, making debugging potential issues easier. If
non-determinism is a target, leverage Go's built-in parallel testing
capabilities.
* Reduce scope for filter removal failure when getting actor events
Use a fresh context to remove the temporary filter installed solely to
get the actor events. This should reduce chances of failure in a case
where the original context may be expired/cancelled.
Refactor removal into a `defer` statement for a more readable, concise
return statement.
* Use fixed RNG seed for actor event tests
Improve determinism in actor event tests by using a fixed RNG seed. This
makes up a more reproducible test suit.
* Use provided libraries to assert eventual conditions
Use the functionalities already provided by `testify` to assert eventual
conditions, and remove the use of `time.Sleep`.
Remove duplicate code in utility functions that are already defined.
Refactor assertion helper functions to use consistent terminology:
"require" implies fatal error, whereas "assert" implies error where the
test may proceed executing.
* Update changelog for actor events APIs
* Fix concerns and docs identified by review
* Update actor bundle to v13.0.0-rc3
Update actor bundle to v13.0.0-rc3
* Prep Lotus v1.26.0-rc1
- For sanity reverting the mainnet upgrade epoch to 99999999, and then only set it when cutting the final release
-Update Calibnet CIDs to v13.0.0-rc3
- Add GetActorEvents, SubscribeActorEvents, GetAllClaims and GetAllAllocations methods to the changelog
Co-Authored-By: Jiaying Wang <42981373+jennijuju@users.noreply.github.com>
* Update CHANGELOG.md
Co-authored-by: Masih H. Derkani <m@derkani.org>
* Make gen
Make gen
* fix: beacon: validate drand change at nv16 correctly
* bump to v1.26.0-rc2
* test: cleanup ddo verified itest, extract steps to functions
also add allocation-removed event case
* test: extract verified DDO test to separate file, add more checks
* test: add additional actor events checks
* Add verification for "deal-activated" actor event
* docs(drand): document the meaning of "IsChained" (#11692)
* Resolve conflicts
I encountered multiple issues when trying to run make gen. And these changes fixed a couple of them:
- go mod tidy
- Remove RaftState/RaftLeader
- Revert `if ts.Height() > claim.TermMax+claim.TermStart || !cctx.IsSet("expired")` to the what is in the release/v1.26.0: `if tsHeight > val.TermMax || !expired`
* fixup imports, make jen
* Update version
Update version in master to v1.27.0-dev
* Update node/impl/full/dummy.go
Co-authored-by: Łukasz Magiera <magik6k@users.noreply.github.com>
* Adjust ListClaimsCmd
Adjust ListClaimsCmd according to review
---------
Signed-off-by: samuelarogbonlo <sbayo971@gmail.com>
Co-authored-by: TippyFlits <james.bluett@protocol.ai>
Co-authored-by: Aayush <arajasek94@gmail.com>
Co-authored-by: Łukasz Magiera <magik6k@users.noreply.github.com>
Co-authored-by: Jennifer Wang <jiayingw703@gmail.com>
Co-authored-by: Shrenuj Bansal <shrenuj.bansal@protocol.ai>
Co-authored-by: Andrew Jackson (Ajax) <snadrus@gmail.com>
Co-authored-by: Steven Allen <steven@stebalien.com>
Co-authored-by: Rod Vagg <rod@vagg.org>
Co-authored-by: Samuel Arogbonlo <47984109+samuelarogbonlo@users.noreply.github.com>
Co-authored-by: LexLuthr <88259624+LexLuthr@users.noreply.github.com>
Co-authored-by: tom123222 <160735201+tom123222@users.noreply.github.com>
Co-authored-by: Aarsh Shah <aarshkshah1992@gmail.com>
Co-authored-by: Masih H. Derkani <m@derkani.org>
Co-authored-by: Jiaying Wang <42981373+jennijuju@users.noreply.github.com>
2024-03-12 09:33:58 +00:00
|
|
|
case network.Version18, network.Version19, network.Version20, network.Version21, network.Version22:
|
2022-11-09 06:30:42 +00:00
|
|
|
return types.StateTreeVersion5, nil
|
2021-10-18 15:46:50 +00:00
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unsupported network version %d", ver))
|
2020-10-06 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStateTree(cst cbor.IpldStore, ver types.StateTreeVersion) (*StateTree, error) {
|
2020-09-14 22:43:12 +00:00
|
|
|
var info cid.Cid
|
2020-10-06 22:22:54 +00:00
|
|
|
switch ver {
|
|
|
|
case types.StateTreeVersion0:
|
2020-09-14 22:43:12 +00:00
|
|
|
// info is undefined
|
2022-11-09 08:44:51 +00:00
|
|
|
case types.StateTreeVersion1, types.StateTreeVersion2, types.StateTreeVersion3, types.StateTreeVersion4, types.StateTreeVersion5:
|
2020-09-25 19:55:10 +00:00
|
|
|
var err error
|
2020-10-06 22:22:54 +00:00
|
|
|
info, err = cst.Put(context.TODO(), new(types.StateInfo0))
|
2020-09-25 19:55:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-14 22:43:12 +00:00
|
|
|
default:
|
2020-10-06 22:22:54 +00:00
|
|
|
return nil, xerrors.Errorf("unsupported state tree version: %d", ver)
|
2020-09-14 22:43:12 +00:00
|
|
|
}
|
2021-01-19 00:06:16 +00:00
|
|
|
|
|
|
|
store := adt.WrapStore(context.TODO(), cst)
|
2021-01-19 00:28:18 +00:00
|
|
|
var hamt adt.Map
|
2021-01-19 00:06:16 +00:00
|
|
|
switch ver {
|
|
|
|
case types.StateTreeVersion0:
|
2021-01-19 00:28:18 +00:00
|
|
|
tree, err := states0.NewTree(store)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to create state tree: %w", err)
|
|
|
|
}
|
|
|
|
hamt = tree.Map
|
2021-01-19 00:06:16 +00:00
|
|
|
case types.StateTreeVersion1:
|
2021-01-19 00:28:18 +00:00
|
|
|
tree, err := states2.NewTree(store)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to create state tree: %w", err)
|
|
|
|
}
|
|
|
|
hamt = tree.Map
|
2021-01-19 00:06:16 +00:00
|
|
|
case types.StateTreeVersion2:
|
2021-01-19 00:28:18 +00:00
|
|
|
tree, err := states3.NewTree(store)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to create state tree: %w", err)
|
|
|
|
}
|
|
|
|
hamt = tree.Map
|
2021-04-23 12:29:46 +00:00
|
|
|
case types.StateTreeVersion3:
|
|
|
|
tree, err := states4.NewTree(store)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to create state tree: %w", err)
|
|
|
|
}
|
|
|
|
hamt = tree.Map
|
2021-05-17 20:02:23 +00:00
|
|
|
case types.StateTreeVersion4:
|
|
|
|
tree, err := states5.NewTree(store)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to create state tree: %w", err)
|
|
|
|
}
|
|
|
|
hamt = tree.Map
|
2022-11-09 08:44:51 +00:00
|
|
|
case types.StateTreeVersion5:
|
|
|
|
tree, err := builtin_types.NewTree(store)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to create state tree: %w", err)
|
|
|
|
}
|
|
|
|
hamt = tree.Map
|
|
|
|
|
2021-01-19 00:06:16 +00:00
|
|
|
default:
|
|
|
|
return nil, xerrors.Errorf("unsupported state tree version: %d", ver)
|
2020-09-14 11:14:06 +00:00
|
|
|
}
|
2020-07-23 00:14:54 +00:00
|
|
|
|
2020-10-19 23:25:41 +00:00
|
|
|
s := &StateTree{
|
2021-01-19 00:06:16 +00:00
|
|
|
root: hamt,
|
2020-09-14 22:43:12 +00:00
|
|
|
info: info,
|
2020-10-06 22:22:54 +00:00
|
|
|
version: ver,
|
2020-09-14 22:43:12 +00:00
|
|
|
Store: cst,
|
|
|
|
snaps: newStateSnaps(),
|
2020-10-19 23:25:41 +00:00
|
|
|
}
|
2024-04-23 11:34:54 +00:00
|
|
|
s.lookupIDFun = s.lookupInternalIDAddress
|
2020-10-19 23:25:41 +00:00
|
|
|
return s, nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 22:43:12 +00:00
|
|
|
func LoadStateTree(cst cbor.IpldStore, c cid.Cid) (*StateTree, error) {
|
|
|
|
var root types.StateRoot
|
|
|
|
// Try loading as a new-style state-tree (version/actors tuple).
|
|
|
|
if err := cst.Get(context.TODO(), c, &root); err != nil {
|
|
|
|
// We failed to decode as the new version, must be an old version.
|
|
|
|
root.Actors = c
|
2020-10-06 22:22:54 +00:00
|
|
|
root.Version = types.StateTreeVersion0
|
2020-09-14 22:43:12 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 00:06:16 +00:00
|
|
|
store := adt.WrapStore(context.TODO(), cst)
|
2020-09-28 23:46:29 +00:00
|
|
|
|
2021-01-19 01:10:14 +00:00
|
|
|
var (
|
|
|
|
hamt adt.Map
|
|
|
|
err error
|
|
|
|
)
|
2021-01-19 00:06:16 +00:00
|
|
|
switch root.Version {
|
|
|
|
case types.StateTreeVersion0:
|
2021-01-19 01:10:14 +00:00
|
|
|
var tree *states0.Tree
|
|
|
|
tree, err = states0.LoadTree(store, root.Actors)
|
|
|
|
if tree != nil {
|
|
|
|
hamt = tree.Map
|
2021-01-19 00:28:18 +00:00
|
|
|
}
|
2021-01-19 00:06:16 +00:00
|
|
|
case types.StateTreeVersion1:
|
2021-01-19 01:10:14 +00:00
|
|
|
var tree *states2.Tree
|
|
|
|
tree, err = states2.LoadTree(store, root.Actors)
|
|
|
|
if tree != nil {
|
|
|
|
hamt = tree.Map
|
2021-01-19 00:28:18 +00:00
|
|
|
}
|
2021-01-19 00:06:16 +00:00
|
|
|
case types.StateTreeVersion2:
|
2021-01-19 01:10:14 +00:00
|
|
|
var tree *states3.Tree
|
|
|
|
tree, err = states3.LoadTree(store, root.Actors)
|
|
|
|
if tree != nil {
|
|
|
|
hamt = tree.Map
|
2021-01-19 00:28:18 +00:00
|
|
|
}
|
2021-04-23 12:29:46 +00:00
|
|
|
case types.StateTreeVersion3:
|
|
|
|
var tree *states4.Tree
|
|
|
|
tree, err = states4.LoadTree(store, root.Actors)
|
|
|
|
if tree != nil {
|
|
|
|
hamt = tree.Map
|
|
|
|
}
|
2021-05-17 20:02:23 +00:00
|
|
|
case types.StateTreeVersion4:
|
|
|
|
var tree *states5.Tree
|
|
|
|
tree, err = states5.LoadTree(store, root.Actors)
|
|
|
|
if tree != nil {
|
|
|
|
hamt = tree.Map
|
|
|
|
}
|
2022-11-09 08:44:51 +00:00
|
|
|
case types.StateTreeVersion5:
|
|
|
|
var tree *builtin_types.ActorTree
|
|
|
|
tree, err = builtin_types.LoadTree(store, root.Actors)
|
|
|
|
if tree != nil {
|
|
|
|
hamt = tree.Map
|
|
|
|
}
|
|
|
|
|
2020-09-14 22:43:12 +00:00
|
|
|
default:
|
|
|
|
return nil, xerrors.Errorf("unsupported state tree version: %d", root.Version)
|
|
|
|
}
|
2021-01-19 01:10:14 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to load state tree: %s", err)
|
2021-07-29 00:50:22 +00:00
|
|
|
return nil, xerrors.Errorf("failed to load state tree %s: %w", c, err)
|
2021-01-19 01:10:14 +00:00
|
|
|
}
|
2021-01-19 00:06:16 +00:00
|
|
|
|
|
|
|
s := &StateTree{
|
|
|
|
root: hamt,
|
|
|
|
info: root.Info,
|
|
|
|
version: root.Version,
|
|
|
|
Store: cst,
|
|
|
|
snaps: newStateSnaps(),
|
|
|
|
}
|
2024-04-23 11:34:54 +00:00
|
|
|
s.lookupIDFun = s.lookupInternalIDAddress
|
2021-01-19 00:06:16 +00:00
|
|
|
|
|
|
|
return s, nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 10:23:05 +00:00
|
|
|
func (st *StateTree) SetActor(addr address.Address, act *types.Actor) error {
|
2024-04-23 11:34:54 +00:00
|
|
|
iaddr, err := st.LookupIDAddress(addr)
|
2019-11-15 16:39:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("ID lookup failed: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
2019-11-15 16:39:43 +00:00
|
|
|
addr = iaddr
|
2019-07-05 14:29:17 +00:00
|
|
|
|
2020-04-03 00:09:41 +00:00
|
|
|
st.snaps.setActor(addr, act)
|
|
|
|
return nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 11:34:54 +00:00
|
|
|
func (st *StateTree) lookupInternalIDAddress(addr address.Address) (address.Address, error) {
|
2020-09-14 22:43:12 +00:00
|
|
|
act, err := st.GetActor(init_.Address)
|
2019-07-05 14:29:17 +00:00
|
|
|
if err != nil {
|
2019-09-10 02:05:24 +00:00
|
|
|
return address.Undef, xerrors.Errorf("getting init actor: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 11:14:06 +00:00
|
|
|
ias, err := init_.Load(&AdtStore{st.Store}, act)
|
2020-09-12 03:07:52 +00:00
|
|
|
if err != nil {
|
2019-09-10 02:05:24 +00:00
|
|
|
return address.Undef, xerrors.Errorf("loading init actor state: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 03:07:52 +00:00
|
|
|
a, found, err := ias.ResolveAddress(addr)
|
2020-08-12 02:47:24 +00:00
|
|
|
if err == nil && !found {
|
|
|
|
err = types.ErrActorNotFound
|
|
|
|
}
|
2020-02-22 13:10:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("resolve address %s: %w", addr, err)
|
|
|
|
}
|
2020-10-19 23:25:41 +00:00
|
|
|
return a, err
|
|
|
|
}
|
|
|
|
|
2024-04-23 11:34:54 +00:00
|
|
|
// LookupIDAddress gets the ID address of this actor's `addr` stored in the `InitActor`.
|
|
|
|
func (st *StateTree) LookupIDAddress(addr address.Address) (address.Address, error) {
|
2020-10-19 23:25:41 +00:00
|
|
|
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
|
|
|
|
}
|
2020-04-03 23:26:52 +00:00
|
|
|
|
|
|
|
st.snaps.cacheResolveAddress(addr, a)
|
|
|
|
|
2020-02-22 13:10:46 +00:00
|
|
|
return a, nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-18 18:10:42 +00:00
|
|
|
// GetActor returns the actor from any type of `addr` provided.
|
2019-07-12 10:23:05 +00:00
|
|
|
func (st *StateTree) GetActor(addr address.Address) (*types.Actor, error) {
|
2019-07-30 00:46:56 +00:00
|
|
|
if addr == address.Undef {
|
|
|
|
return nil, fmt.Errorf("GetActor called on undefined address")
|
|
|
|
}
|
|
|
|
|
2020-02-18 18:10:42 +00:00
|
|
|
// Transform `addr` to its ID format.
|
2024-04-23 11:34:54 +00:00
|
|
|
iaddr, err := st.LookupIDAddress(addr)
|
2019-11-15 16:39:43 +00:00
|
|
|
if err != nil {
|
2020-08-12 02:47:24 +00:00
|
|
|
if xerrors.Is(err, types.ErrActorNotFound) {
|
2020-02-22 13:10:46 +00:00
|
|
|
return nil, xerrors.Errorf("resolution lookup failed (%s): %w", addr, err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
2019-11-15 16:39:43 +00:00
|
|
|
return nil, xerrors.Errorf("address resolution: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
2019-11-15 16:39:43 +00:00
|
|
|
addr = iaddr
|
2019-07-05 14:29:17 +00:00
|
|
|
|
2020-04-03 00:09:41 +00:00
|
|
|
snapAct, err := st.snaps.getActor(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if snapAct != nil {
|
|
|
|
return snapAct, nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 01:56:37 +00:00
|
|
|
var act types.Actor
|
2022-11-09 08:44:51 +00:00
|
|
|
var found bool
|
|
|
|
if st.version <= types.StateTreeVersion4 {
|
|
|
|
var act4 types.ActorV4
|
|
|
|
found, err = st.root.Get(abi.AddrKey(addr), &act4)
|
|
|
|
if found {
|
|
|
|
act = *types.AsActorV5(&act4)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
found, err = st.root.Get(abi.AddrKey(addr), &act)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2019-09-30 23:55:35 +00:00
|
|
|
return nil, xerrors.Errorf("hamt find failed: %w", err)
|
2020-07-23 00:14:54 +00:00
|
|
|
} else if !found {
|
|
|
|
return nil, types.ErrActorNotFound
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 00:09:41 +00:00
|
|
|
st.snaps.setActor(addr, &act)
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
return &act, nil
|
|
|
|
}
|
|
|
|
|
2020-03-10 02:24:02 +00:00
|
|
|
func (st *StateTree) DeleteActor(addr address.Address) error {
|
|
|
|
if addr == address.Undef {
|
|
|
|
return xerrors.Errorf("DeleteActor called on undefined address")
|
|
|
|
}
|
|
|
|
|
2024-04-23 11:34:54 +00:00
|
|
|
iaddr, err := st.LookupIDAddress(addr)
|
2020-03-10 02:24:02 +00:00
|
|
|
if err != nil {
|
2020-08-12 02:47:24 +00:00
|
|
|
if xerrors.Is(err, types.ErrActorNotFound) {
|
2020-03-10 02:24:02 +00:00
|
|
|
return xerrors.Errorf("resolution lookup failed (%s): %w", addr, err)
|
|
|
|
}
|
|
|
|
return xerrors.Errorf("address resolution: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = iaddr
|
|
|
|
|
2020-04-03 00:09:41 +00:00
|
|
|
_, err = st.GetActor(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-03-10 02:24:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 00:09:41 +00:00
|
|
|
st.snaps.deleteActor(addr)
|
|
|
|
|
2020-03-10 02:24:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-22 19:53:06 +00:00
|
|
|
func (st *StateTree) Flush(ctx context.Context) (cid.Cid, error) {
|
2020-08-20 04:49:10 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "stateTree.Flush") //nolint:staticcheck
|
2020-01-22 19:53:06 +00:00
|
|
|
defer span.End()
|
2020-04-03 00:09:41 +00:00
|
|
|
if len(st.snaps.layers) != 1 {
|
|
|
|
return cid.Undef, xerrors.Errorf("tried to flush state tree with snapshots on the stack")
|
|
|
|
}
|
2020-01-22 19:53:06 +00:00
|
|
|
|
2023-09-22 23:06:26 +00:00
|
|
|
for addr, stoTmp := range st.snaps.layers[0].actors {
|
|
|
|
sto := stoTmp
|
2020-04-03 00:09:41 +00:00
|
|
|
if sto.Delete {
|
2020-09-14 11:14:06 +00:00
|
|
|
if err := st.root.Delete(abi.AddrKey(addr)); err != nil {
|
2020-04-03 00:09:41 +00:00
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
} else {
|
2022-11-09 08:44:51 +00:00
|
|
|
if st.version <= types.StateTreeVersion4 {
|
|
|
|
act4 := types.AsActorV4(&sto.Act)
|
|
|
|
if err := st.root.Put(abi.AddrKey(addr), act4); err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := st.root.Put(abi.AddrKey(addr), &sto.Act); err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
2020-04-03 00:09:41 +00:00
|
|
|
}
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 22:43:12 +00:00
|
|
|
root, err := st.root.Root()
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("failed to flush state-tree hamt: %w", err)
|
|
|
|
}
|
|
|
|
// If we're version 0, return a raw tree.
|
2020-10-06 22:22:54 +00:00
|
|
|
if st.version == types.StateTreeVersion0 {
|
2020-09-14 22:43:12 +00:00
|
|
|
return root, nil
|
|
|
|
}
|
|
|
|
// Otherwise, return a versioned tree.
|
2020-10-06 22:22:54 +00:00
|
|
|
return st.Store.Put(ctx, &types.StateRoot{Version: st.version, Actors: root, Info: st.info})
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 19:53:06 +00:00
|
|
|
func (st *StateTree) Snapshot(ctx context.Context) error {
|
2020-08-20 04:49:10 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "stateTree.SnapShot") //nolint:staticcheck
|
2020-01-22 19:53:06 +00:00
|
|
|
defer span.End()
|
|
|
|
|
2020-04-03 00:09:41 +00:00
|
|
|
st.snaps.addLayer()
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-05 01:19:15 +00:00
|
|
|
func (st *StateTree) ClearSnapshot() {
|
2020-04-03 00:09:41 +00:00
|
|
|
st.snaps.mergeLastLayer()
|
2020-03-05 01:19:15 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 02:25:20 +00:00
|
|
|
func (st *StateTree) RegisterNewAddress(addr address.Address) (address.Address, error) {
|
2019-07-05 14:29:17 +00:00
|
|
|
var out address.Address
|
2020-09-14 22:43:12 +00:00
|
|
|
err := st.MutateActor(init_.Address, func(initact *types.Actor) error {
|
2020-09-14 11:14:06 +00:00
|
|
|
ias, err := init_.Load(&AdtStore{st.Store}, initact)
|
2020-09-12 03:07:52 +00:00
|
|
|
if err != nil {
|
2019-07-05 14:29:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-12 03:07:52 +00:00
|
|
|
oaddr, err := ias.MapAddressToNewID(addr)
|
2019-07-05 14:29:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
out = oaddr
|
|
|
|
|
2020-09-12 03:07:52 +00:00
|
|
|
ncid, err := st.Store.Put(context.TODO(), ias)
|
2019-07-05 14:29:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
initact.Head = ncid
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2020-02-18 21:37:59 +00:00
|
|
|
type AdtStore struct{ cbor.IpldStore }
|
2020-02-12 07:22:55 +00:00
|
|
|
|
2020-02-18 21:37:59 +00:00
|
|
|
func (a *AdtStore) Context() context.Context {
|
2020-02-12 07:22:55 +00:00
|
|
|
return context.TODO()
|
|
|
|
}
|
|
|
|
|
2020-02-18 21:37:59 +00:00
|
|
|
var _ adt.Store = (*AdtStore)(nil)
|
2020-02-12 07:22:55 +00:00
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
func (st *StateTree) Revert() error {
|
2020-04-03 00:09:41 +00:00
|
|
|
st.snaps.dropLayer()
|
|
|
|
st.snaps.addLayer()
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-12 10:23:05 +00:00
|
|
|
func (st *StateTree) MutateActor(addr address.Address, f func(*types.Actor) error) error {
|
2019-07-05 14:29:17 +00:00
|
|
|
act, err := st.GetActor(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f(act); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return st.SetActor(addr, act)
|
|
|
|
}
|
2020-08-12 22:20:32 +00:00
|
|
|
|
|
|
|
func (st *StateTree) ForEach(f func(address.Address, *types.Actor) error) error {
|
2021-06-11 00:41:23 +00:00
|
|
|
// Walk through layers, if any.
|
|
|
|
seen := make(map[address.Address]struct{})
|
|
|
|
for i := len(st.snaps.layers) - 1; i >= 0; i-- {
|
|
|
|
for addr, op := range st.snaps.layers[i].actors {
|
|
|
|
if _, ok := seen[addr]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
seen[addr] = struct{}{}
|
|
|
|
if op.Delete {
|
|
|
|
continue
|
|
|
|
}
|
2021-06-18 21:20:48 +00:00
|
|
|
act := op.Act // copy
|
|
|
|
if err := f(addr, &act); err != nil {
|
2021-06-11 00:41:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now walk through the saved actors.
|
2022-11-09 08:44:51 +00:00
|
|
|
if st.version <= types.StateTreeVersion4 {
|
|
|
|
var act types.ActorV4
|
|
|
|
return st.root.ForEach(&act, func(k string) error {
|
|
|
|
act := act // copy
|
|
|
|
addr, err := address.NewFromBytes([]byte(k))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("invalid address (%x) found in state tree key: %w", []byte(k), err)
|
|
|
|
}
|
2020-08-12 22:20:32 +00:00
|
|
|
|
2022-11-09 08:44:51 +00:00
|
|
|
// no need to record anything here, there are no duplicates in the actors HAMT
|
2023-10-27 18:32:42 +00:00
|
|
|
// itself.
|
2022-11-09 08:44:51 +00:00
|
|
|
if _, ok := seen[addr]; ok {
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-11 00:41:23 +00:00
|
|
|
|
2022-11-09 08:44:51 +00:00
|
|
|
return f(addr, types.AsActorV5(&act))
|
|
|
|
})
|
2022-11-09 09:38:16 +00:00
|
|
|
}
|
2022-11-09 08:44:51 +00:00
|
|
|
|
2022-11-09 09:38:16 +00:00
|
|
|
var act types.Actor
|
|
|
|
return st.root.ForEach(&act, func(k string) error {
|
|
|
|
act := act // copy
|
|
|
|
addr, err := address.NewFromBytes([]byte(k))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("invalid address (%x) found in state tree key: %w", []byte(k), err)
|
|
|
|
}
|
2022-11-09 08:44:51 +00:00
|
|
|
|
2022-11-09 09:38:16 +00:00
|
|
|
// no need to record anything here, there are no duplicates in the actors HAMT
|
2023-10-27 18:32:42 +00:00
|
|
|
// itself.
|
2022-11-09 09:38:16 +00:00
|
|
|
if _, ok := seen[addr]; ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return f(addr, &act)
|
|
|
|
})
|
2020-08-12 22:20:32 +00:00
|
|
|
}
|
2020-09-21 19:50:12 +00:00
|
|
|
|
2020-09-24 16:58:49 +00:00
|
|
|
// Version returns the version of the StateTree data structure in use.
|
2020-10-06 22:22:54 +00:00
|
|
|
func (st *StateTree) Version() types.StateTreeVersion {
|
2020-09-24 16:58:49 +00:00
|
|
|
return st.version
|
|
|
|
}
|
|
|
|
|
2021-06-30 23:40:43 +00:00
|
|
|
func Diff(ctx context.Context, oldTree, newTree *StateTree) (map[string]types.Actor, error) {
|
2020-09-21 19:50:12 +00:00
|
|
|
out := map[string]types.Actor{}
|
|
|
|
|
|
|
|
var (
|
|
|
|
ncval, ocval cbg.Deferred
|
|
|
|
buf = bytes.NewReader(nil)
|
|
|
|
)
|
|
|
|
if err := newTree.root.ForEach(&ncval, func(k string) error {
|
2021-06-30 23:40:43 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
addr, err := address.NewFromBytes([]byte(k))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("address in state tree was not valid: %w", err)
|
|
|
|
}
|
2020-09-21 19:50:12 +00:00
|
|
|
|
2021-06-30 23:40:43 +00:00
|
|
|
found, err := oldTree.root.Get(abi.AddrKey(addr), &ocval)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-21 19:50:12 +00:00
|
|
|
|
2021-06-30 23:40:43 +00:00
|
|
|
if found && bytes.Equal(ocval.Raw, ncval.Raw) {
|
|
|
|
return nil // not changed
|
|
|
|
}
|
2020-09-21 19:50:12 +00:00
|
|
|
|
2022-11-09 08:44:51 +00:00
|
|
|
if newTree.version <= types.StateTreeVersion4 {
|
|
|
|
var act types.ActorV4
|
2020-09-21 19:50:12 +00:00
|
|
|
|
2022-11-09 08:44:51 +00:00
|
|
|
buf.Reset(ncval.Raw)
|
|
|
|
err = act.UnmarshalCBOR(buf)
|
|
|
|
buf.Reset(nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-21 19:50:12 +00:00
|
|
|
|
2022-11-09 08:44:51 +00:00
|
|
|
out[addr.String()] = *types.AsActorV5(&act)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
var act types.Actor
|
|
|
|
|
|
|
|
buf.Reset(ncval.Raw)
|
|
|
|
err = act.UnmarshalCBOR(buf)
|
|
|
|
buf.Reset(nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
out[addr.String()] = act
|
|
|
|
}
|
2020-09-21 19:50:12 +00:00
|
|
|
|
2021-06-30 23:40:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-09-21 19:50:12 +00:00
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|