2019-09-06 06:26:02 +00:00
|
|
|
package stmgr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-06 20:03:28 +00:00
|
|
|
"sync"
|
2019-09-06 06:26:02 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/address"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/state"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/vm"
|
2019-09-06 20:03:28 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-09-06 06:26:02 +00:00
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
hamt "github.com/ipfs/go-hamt-ipld"
|
|
|
|
logging "github.com/ipfs/go-log"
|
|
|
|
)
|
|
|
|
|
2019-09-09 16:18:27 +00:00
|
|
|
var log = logging.Logger("statemgr")
|
2019-09-06 06:26:02 +00:00
|
|
|
|
|
|
|
type StateManager struct {
|
|
|
|
cs *store.ChainStore
|
2019-09-06 20:03:28 +00:00
|
|
|
|
|
|
|
stCache map[string]cid.Cid
|
|
|
|
stlk sync.Mutex
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewStateManager(cs *store.ChainStore) *StateManager {
|
2019-09-06 20:03:28 +00:00
|
|
|
return &StateManager{
|
|
|
|
cs: cs,
|
|
|
|
stCache: make(map[string]cid.Cid),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cidsToKey(cids []cid.Cid) string {
|
|
|
|
var out string
|
|
|
|
for _, c := range cids {
|
|
|
|
out += c.KeyString()
|
|
|
|
}
|
|
|
|
return out
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *StateManager) TipSetState(cids []cid.Cid) (cid.Cid, error) {
|
2019-09-19 20:25:18 +00:00
|
|
|
ctx := context.TODO()
|
|
|
|
|
2019-09-06 20:03:28 +00:00
|
|
|
ck := cidsToKey(cids)
|
|
|
|
sm.stlk.Lock()
|
|
|
|
cached, ok := sm.stCache[ck]
|
|
|
|
sm.stlk.Unlock()
|
|
|
|
if ok {
|
|
|
|
return cached, nil
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:25:18 +00:00
|
|
|
ts, err := sm.cs.LoadTipSet(cids)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := sm.computeTipSetState(ctx, ts.Blocks(), nil)
|
2019-09-06 20:03:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sm.stlk.Lock()
|
|
|
|
sm.stCache[ck] = out
|
|
|
|
sm.stlk.Unlock()
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:25:18 +00:00
|
|
|
func (sm *StateManager) computeTipSetState(ctx context.Context, blks []*types.BlockHeader, cb func(cid.Cid, *types.Message, *vm.ApplyRet) error) (cid.Cid, error) {
|
|
|
|
if len(blks) == 1 && cb == nil {
|
|
|
|
return blks[0].StateRoot, nil
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 20:25:18 +00:00
|
|
|
pstate, err := sm.TipSetState(blks[0].Parents)
|
2019-09-06 06:26:02 +00:00
|
|
|
if err != nil {
|
2019-09-06 20:03:28 +00:00
|
|
|
return cid.Undef, xerrors.Errorf("recursive TipSetState failed: %w", err)
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
2019-09-20 12:22:46 +00:00
|
|
|
cids := make([]cid.Cid, len(blks))
|
|
|
|
for i, v := range blks {
|
|
|
|
cids[i] = v.Cid()
|
|
|
|
}
|
|
|
|
|
|
|
|
r := vm.NewChainRand(sm.cs, cids, blks[0].Height, nil)
|
2019-09-20 02:54:52 +00:00
|
|
|
|
2019-09-20 12:22:22 +00:00
|
|
|
vmi, err := vm.NewVM(pstate, blks[0].Height, r, address.Undef, sm.cs)
|
2019-09-06 06:26:02 +00:00
|
|
|
if err != nil {
|
2019-09-06 20:03:28 +00:00
|
|
|
return cid.Undef, xerrors.Errorf("instantiating VM failed: %w", err)
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
applied := make(map[cid.Cid]bool)
|
2019-09-19 20:25:18 +00:00
|
|
|
for _, b := range blks {
|
2019-09-06 06:26:02 +00:00
|
|
|
vmi.SetBlockMiner(b.Miner)
|
|
|
|
|
|
|
|
bms, sms, err := sm.cs.MessagesForBlock(b)
|
|
|
|
if err != nil {
|
2019-09-06 20:03:28 +00:00
|
|
|
return cid.Undef, xerrors.Errorf("failed to get messages for block: %w", err)
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range bms {
|
|
|
|
if applied[m.Cid()] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
applied[m.Cid()] = true
|
|
|
|
|
2019-09-19 20:25:18 +00:00
|
|
|
r, err := vmi.ApplyMessage(ctx, m)
|
2019-09-06 06:26:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
2019-09-19 20:25:18 +00:00
|
|
|
|
|
|
|
if cb != nil {
|
|
|
|
if err := cb(m.Cid(), m, r); err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
}
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, sm := range sms {
|
|
|
|
if applied[sm.Cid()] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
applied[sm.Cid()] = true
|
|
|
|
|
2019-09-19 20:25:18 +00:00
|
|
|
r, err := vmi.ApplyMessage(ctx, &sm.Message)
|
2019-09-06 06:26:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
2019-09-19 20:25:18 +00:00
|
|
|
|
|
|
|
if cb != nil {
|
|
|
|
if err := cb(sm.Cid(), &sm.Message, r); err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
}
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vmi.Flush(ctx)
|
|
|
|
}
|
|
|
|
|
2019-09-17 22:43:47 +00:00
|
|
|
func (sm *StateManager) GetActor(addr address.Address, ts *types.TipSet) (*types.Actor, error) {
|
|
|
|
if ts == nil {
|
|
|
|
ts = sm.cs.GetHeaviestTipSet()
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
stcid, err := sm.TipSetState(ts.Cids())
|
|
|
|
if err != nil {
|
2019-09-10 19:58:45 +00:00
|
|
|
return nil, xerrors.Errorf("tipset state: %w", err)
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cst := hamt.CSTFromBstore(sm.cs.Blockstore())
|
|
|
|
state, err := state.LoadStateTree(cst, stcid)
|
|
|
|
if err != nil {
|
2019-09-10 19:58:45 +00:00
|
|
|
return nil, xerrors.Errorf("load state tree: %w", err)
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return state.GetActor(addr)
|
|
|
|
}
|
|
|
|
|
2019-09-17 22:43:47 +00:00
|
|
|
func (sm *StateManager) GetBalance(addr address.Address, ts *types.TipSet) (types.BigInt, error) {
|
|
|
|
act, err := sm.GetActor(addr, ts)
|
2019-09-06 06:26:02 +00:00
|
|
|
if err != nil {
|
2019-09-10 19:58:45 +00:00
|
|
|
return types.BigInt{}, xerrors.Errorf("get actor: %w", err)
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return act.Balance, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *StateManager) ChainStore() *store.ChainStore {
|
|
|
|
return sm.cs
|
|
|
|
}
|
|
|
|
|
2019-09-17 22:43:47 +00:00
|
|
|
func (sm *StateManager) LoadActorState(ctx context.Context, a address.Address, out interface{}, ts *types.TipSet) (*types.Actor, error) {
|
|
|
|
act, err := sm.GetActor(a, ts)
|
2019-09-06 06:26:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cst := hamt.CSTFromBstore(sm.cs.Blockstore())
|
|
|
|
if err := cst.Get(ctx, act.Head, out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return act, nil
|
|
|
|
|
|
|
|
}
|