switch to parent state roots

This commit is contained in:
whyrusleeping 2019-09-27 16:55:15 -07:00
parent 6a729263a4
commit 1419031f1e
16 changed files with 206 additions and 192 deletions

View File

@ -145,7 +145,7 @@ func cheatStorageMarketTotal(t *testing.T, vm *vm.VM, bs bstore.Blockstore) {
func fakeBlock(t *testing.T, minerAddr address.Address, ts uint64) *types.BlockHeader {
c := fakeCid(t, 1)
return &types.BlockHeader{Height: 5, Miner: minerAddr, Timestamp: ts, StateRoot: c, Messages: c, MessageReceipts: c, BLSAggregate: types.Signature{Type: types.KTBLS}}
return &types.BlockHeader{Height: 5, Miner: minerAddr, Timestamp: ts, ParentStateRoot: c, Messages: c, ParentMessageReceipts: c, BLSAggregate: types.Signature{Type: types.KTBLS}}
}
func fakeCid(t *testing.T, s int) cid.Cid {

View File

@ -143,7 +143,7 @@ func (h *Handler) saveAsk(a *types.SignedStorageAsk) error {
}
func (c *Client) checkAskSignature(ask *types.SignedStorageAsk) error {
tss, err := c.sm.TipSetState(c.sm.ChainStore().GetHeaviestTipSet().Cids())
tss, _, err := c.sm.TipSetState(c.sm.ChainStore().GetHeaviestTipSet().Cids())
if err != nil {
return xerrors.Errorf("failed to get tipsetstate to query for miner worker: %w", err)
}

View File

@ -3,11 +3,12 @@ package events
import (
"context"
"fmt"
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/chain/store"
"testing"
"time"
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/chain/store"
"github.com/ipfs/go-cid"
"github.com/multiformats/go-multihash"
"github.com/stretchr/testify/require"
@ -48,9 +49,9 @@ func makeTs(t *testing.T, h uint64, msgcid cid.Cid) *types.TipSet {
{
Height: h,
StateRoot: dummyCid,
ParentStateRoot: dummyCid,
Messages: msgcid,
MessageReceipts: dummyCid,
ParentMessageReceipts: dummyCid,
},
})
@ -608,5 +609,4 @@ func TestCalledOrder(t *testing.T) {
})
fcs.advance(9, 1, nil)
}

View File

@ -201,7 +201,7 @@ func (cg *ChainGen) nextBlockProof(ctx context.Context, m address.Address, ticks
lastTicket = ticks[len(ticks)-1]
}
st, err := cg.sm.TipSetState(pts.Cids())
st, _, err := cg.sm.TipSetState(pts.Cids())
if err != nil {
return nil, nil, err
}
@ -376,7 +376,7 @@ func (mca mca) StateMinerPower(ctx context.Context, maddr address.Address, ts *t
}
func (mca mca) StateMinerWorker(ctx context.Context, maddr address.Address, ts *types.TipSet) (address.Address, error) {
st, err := mca.sm.TipSetState(ts.Cids())
st, _, err := mca.sm.TipSetState(ts.Cids())
if err != nil {
return address.Undef, err
}

View File

@ -20,7 +20,7 @@ import (
)
func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wallet, miner address.Address, parents *types.TipSet, tickets []*types.Ticket, proof types.ElectionProof, msgs []*types.SignedMessage, timestamp uint64) (*types.FullBlock, error) {
st, err := sm.TipSetState(parents.Cids())
st, recpts, err := sm.TipSetState(parents.Cids())
if err != nil {
return nil, errors.Wrap(err, "failed to load tipset state")
}
@ -59,6 +59,8 @@ func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wal
Height: height,
Timestamp: timestamp,
ElectionProof: proof,
ParentStateRoot: st,
ParentMessageReceipts: recpts,
}
var blsMessages []*types.Message
@ -83,24 +85,6 @@ func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wal
}
}
var receipts []cbg.CBORMarshaler
for _, msg := range blsMessages {
rec, err := vmi.ApplyMessage(ctx, msg)
if err != nil {
return nil, errors.Wrap(err, "apply message failure")
}
receipts = append(receipts, &rec.MessageReceipt)
}
for _, msg := range secpkMessages {
rec, err := vmi.ApplyMessage(ctx, &msg.Message)
if err != nil {
return nil, errors.Wrap(err, "apply message failure")
}
receipts = append(receipts, &rec.MessageReceipt)
}
bs := amt.WrapBlockstore(sm.ChainStore().Blockstore())
blsmsgroot, err := amt.FromArray(bs, toIfArr(blsMsgCids))
if err != nil {
@ -120,24 +104,12 @@ func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wal
}
next.Messages = mmcid
rectroot, err := amt.FromArray(bs, receipts)
if err != nil {
return nil, xerrors.Errorf("failed to build receipts amt: %w", err)
}
next.MessageReceipts = rectroot
stateRoot, err := vmi.Flush(context.TODO())
if err != nil {
return nil, errors.Wrap(err, "flushing state tree failed")
}
aggSig, err := aggregateSignatures(blsSigs)
if err != nil {
return nil, err
}
next.BLSAggregate = aggSig
next.StateRoot = stateRoot
pweight := sm.ChainStore().Weight(parents)
next.ParentWeight = types.NewInt(pweight)

View File

@ -341,9 +341,9 @@ func MakeGenesisBlock(bs bstore.Blockstore, balances map[address.Address]types.B
Parents: []cid.Cid{},
Height: 0,
ParentWeight: types.NewInt(0),
StateRoot: stateroot,
ParentStateRoot: stateroot,
Messages: mmb.Cid(),
MessageReceipts: emptyroot,
ParentMessageReceipts: emptyroot,
BLSAggregate: types.Signature{Type: types.KTBLS, Data: []byte("signatureeee")},
BlockSig: types.Signature{Type: types.KTBLS, Data: []byte("block signatureeee")},
Timestamp: ts,

View File

@ -53,7 +53,7 @@ func (sm *StateManager) Call(ctx context.Context, msg *types.Message, ts *types.
ts = sm.cs.GetHeaviestTipSet()
}
state, err := sm.TipSetState(ts.Cids())
state, _, err := sm.TipSetState(ts.Cids())
if err != nil {
return nil, xerrors.Errorf("getting tipset state: %w", err)
}
@ -69,7 +69,7 @@ func (sm *StateManager) Replay(ctx context.Context, ts *types.TipSet, mcid cid.C
var outm *types.Message
var outr *vm.ApplyRet
_, err := sm.computeTipSetState(ctx, ts.Blocks(), func(c cid.Cid, m *types.Message, ret *vm.ApplyRet) error {
_, _, err := sm.computeTipSetState(ctx, ts.Blocks(), func(c cid.Cid, m *types.Message, ret *vm.ApplyRet) error {
if c == mcid {
outm = m
outr = ret

View File

@ -4,11 +4,13 @@ import (
"context"
"sync"
amt "github.com/filecoin-project/go-amt-ipld"
"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"
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors"
"github.com/ipfs/go-cid"
@ -21,14 +23,14 @@ var log = logging.Logger("statemgr")
type StateManager struct {
cs *store.ChainStore
stCache map[string]cid.Cid
stCache map[string][]cid.Cid
stlk sync.Mutex
}
func NewStateManager(cs *store.ChainStore) *StateManager {
return &StateManager{
cs: cs,
stCache: make(map[string]cid.Cid),
stCache: make(map[string][]cid.Cid),
}
}
@ -40,7 +42,7 @@ func cidsToKey(cids []cid.Cid) string {
return out
}
func (sm *StateManager) TipSetState(cids []cid.Cid) (cid.Cid, error) {
func (sm *StateManager) TipSetState(cids []cid.Cid) (cid.Cid, cid.Cid, error) {
ctx := context.TODO()
ck := cidsToKey(cids)
@ -48,34 +50,32 @@ func (sm *StateManager) TipSetState(cids []cid.Cid) (cid.Cid, error) {
cached, ok := sm.stCache[ck]
sm.stlk.Unlock()
if ok {
return cached, nil
return cached[0], cached[1], nil
}
ts, err := sm.cs.LoadTipSet(cids)
if err != nil {
return cid.Undef, err
return cid.Undef, cid.Undef, err
}
out, err := sm.computeTipSetState(ctx, ts.Blocks(), nil)
st, rec, err := sm.computeTipSetState(ctx, ts.Blocks(), nil)
if err != nil {
return cid.Undef, err
return cid.Undef, cid.Undef, err
}
sm.stlk.Lock()
sm.stCache[ck] = out
sm.stCache[ck] = []cid.Cid{st, rec}
sm.stlk.Unlock()
return out, nil
return st, rec, nil
}
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
}
type ChainMsg interface {
Cid() cid.Cid
VMMessage() *types.Message
}
pstate, err := sm.TipSetState(blks[0].Parents)
if err != nil {
return cid.Undef, xerrors.Errorf("recursive TipSetState failed: %w", err)
}
func (sm *StateManager) computeTipSetState(ctx context.Context, blks []*types.BlockHeader, cb func(cid.Cid, *types.Message, *vm.ApplyRet) error) (cid.Cid, cid.Cid, error) {
pstate := blks[0].ParentStateRoot
cids := make([]cid.Cid, len(blks))
for i, v := range blks {
@ -86,56 +86,94 @@ func (sm *StateManager) computeTipSetState(ctx context.Context, blks []*types.Bl
vmi, err := vm.NewVM(pstate, blks[0].Height, r, address.Undef, sm.cs)
if err != nil {
return cid.Undef, xerrors.Errorf("instantiating VM failed: %w", err)
return cid.Undef, cid.Undef, xerrors.Errorf("instantiating VM failed: %w", err)
}
applied := make(map[cid.Cid]bool)
/* TODO: apply mining reward
netbalance, err := vmi.ActorBalance(actors.NetworkAddress)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to get network actor balance: %w", err)
}
vm.MiningRewardForBlock(netbalance)
*/
applied := make(map[address.Address]uint64)
balances := make(map[address.Address]types.BigInt)
preloadAddr := func(a address.Address) error {
if _, ok := applied[a]; !ok {
act, err := vmi.StateTree().GetActor(a)
if err != nil {
return err
}
applied[a] = act.Nonce
balances[a] = act.Balance
}
return nil
}
var receipts []cbg.CBORMarshaler
for _, b := range blks {
vmi.SetBlockMiner(b.Miner)
bms, sms, err := sm.cs.MessagesForBlock(b)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to get messages for block: %w", err)
return cid.Undef, cid.Undef, xerrors.Errorf("failed to get messages for block: %w", err)
}
cmsgs := make([]ChainMsg, 0, len(bms)+len(sms))
for _, m := range bms {
if applied[m.Cid()] {
cmsgs = append(cmsgs, m)
}
for _, sm := range sms {
cmsgs = append(cmsgs, sm)
}
for _, cm := range cmsgs {
m := cm.VMMessage()
if err := preloadAddr(m.From); err != nil {
return cid.Undef, cid.Undef, err
}
if applied[m.From] != m.Nonce {
continue
}
applied[m.Cid()] = true
applied[m.From]++
if balances[m.From].LessThan(m.RequiredFunds()) {
continue
}
balances[m.From] = types.BigSub(balances[m.From], m.RequiredFunds())
r, err := vmi.ApplyMessage(ctx, m)
if err != nil {
return cid.Undef, err
return cid.Undef, cid.Undef, err
}
receipts = append(receipts, &r.MessageReceipt)
if cb != nil {
if err := cb(m.Cid(), m, r); err != nil {
return cid.Undef, err
if err := cb(cm.Cid(), m, r); err != nil {
return cid.Undef, cid.Undef, err
}
}
}
}
for _, sm := range sms {
if applied[sm.Cid()] {
continue
}
applied[sm.Cid()] = true
r, err := vmi.ApplyMessage(ctx, &sm.Message)
bs := amt.WrapBlockstore(sm.cs.Blockstore())
rectroot, err := amt.FromArray(bs, receipts)
if err != nil {
return cid.Undef, err
return cid.Undef, cid.Undef, xerrors.Errorf("failed to build receipts amt: %w", err)
}
if cb != nil {
if err := cb(sm.Cid(), &sm.Message, r); err != nil {
return cid.Undef, err
}
}
}
st, err := vmi.Flush(ctx)
if err != nil {
return cid.Undef, cid.Undef, xerrors.Errorf("vm flush failed: %w", err)
}
return vmi.Flush(ctx)
return st, rectroot, nil
}
func (sm *StateManager) GetActor(addr address.Address, ts *types.TipSet) (*types.Actor, error) {
@ -143,7 +181,7 @@ func (sm *StateManager) GetActor(addr address.Address, ts *types.TipSet) (*types
ts = sm.cs.GetHeaviestTipSet()
}
stcid, err := sm.TipSetState(ts.Cids())
stcid, _, err := sm.TipSetState(ts.Cids())
if err != nil {
return nil, xerrors.Errorf("tipset state: %w", err)
}

View File

@ -551,7 +551,7 @@ func (cs *ChainStore) MessagesForBlock(b *types.BlockHeader) ([]*types.Message,
func (cs *ChainStore) GetReceipt(b *types.BlockHeader, i int) (*types.MessageReceipt, error) {
bs := amt.WrapBlockstore(cs.bs)
a, err := amt.LoadAMT(bs, b.MessageReceipts)
a, err := amt.LoadAMT(bs, b.ParentMessageReceipts)
if err != nil {
return nil, errors.Wrap(err, "amt load")
}

View File

@ -10,15 +10,16 @@ import (
"github.com/filecoin-project/go-lotus/build"
"github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/state"
"github.com/filecoin-project/go-lotus/chain/stmgr"
"github.com/filecoin-project/go-lotus/chain/store"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/filecoin-project/go-lotus/chain/vm"
"github.com/filecoin-project/go-lotus/lib/vdf"
amt "github.com/filecoin-project/go-amt-ipld"
"github.com/ipfs/go-cid"
dstore "github.com/ipfs/go-datastore"
hamt "github.com/ipfs/go-hamt-ipld"
bstore "github.com/ipfs/go-ipfs-blockstore"
logging "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p-core/peer"
@ -380,11 +381,19 @@ func (syncer *Syncer) validateTickets(ctx context.Context, mworker address.Addre
// Should match up with 'Semantical Validation' in validation.md in the spec
func (syncer *Syncer) ValidateBlock(ctx context.Context, b *types.FullBlock) error {
h := b.Header
stateroot, err := syncer.sm.TipSetState(h.Parents)
stateroot, precp, err := syncer.sm.TipSetState(h.Parents)
if err != nil {
return xerrors.Errorf("get tipsetstate(%d, %s) failed: %w", h.Height, h.Parents, err)
}
if stateroot != h.ParentStateRoot {
return xerrors.Errorf("parent state root did not match computed state (%s != %s)", stateroot, h.ParentStateRoot)
}
if precp != h.ParentMessageReceipts {
return xerrors.Errorf("parent receipts root did not match computed value (%s != %s)", precp, h.ParentMessageReceipts)
}
baseTs, err := syncer.store.LoadTipSet(h.Parents)
if err != nil {
return xerrors.Errorf("load tipset failed: %w", err)
@ -434,61 +443,48 @@ func (syncer *Syncer) ValidateBlock(ctx context.Context, b *types.FullBlock) err
return xerrors.Errorf("miner created a block but was not a winner")
}
r := vm.NewChainRand(syncer.store, baseTs.Cids(), baseTs.Height(), h.Tickets)
vmi, err := vm.NewVM(stateroot, h.Height, r, h.Miner, syncer.store)
nonces := make(map[address.Address]uint64)
balances := make(map[address.Address]types.BigInt)
cst := hamt.CSTFromBstore(syncer.store.Blockstore())
st, err := state.LoadStateTree(cst, stateroot)
if err != nil {
return xerrors.Errorf("failed to instantiate VM: %w", err)
return xerrors.Errorf("failed to load base state tree: %w", err)
}
owner, err := stmgr.GetMinerOwner(ctx, syncer.sm, stateroot, b.Header.Miner)
checkMsg := func(m *types.Message) error {
if _, ok := nonces[m.From]; !ok {
act, err := st.GetActor(m.From)
if err != nil {
return xerrors.Errorf("getting miner owner for block miner failed: %w", err)
return xerrors.Errorf("failed to get actor: %w", err)
}
networkBalance, err := vmi.ActorBalance(actors.NetworkAddress)
if err != nil {
return xerrors.Errorf("getting network balance")
nonces[m.From] = act.Nonce
balances[m.From] = act.Balance
}
if err := vmi.TransferFunds(actors.NetworkAddress, owner,
vm.MiningReward(networkBalance)); err != nil {
return xerrors.Errorf("fund transfer failed: %w", err)
if nonces[m.From] != m.Nonce {
return xerrors.Errorf("wrong nonce")
}
nonces[m.From]++
if balances[m.From].LessThan(m.RequiredFunds()) {
return xerrors.Errorf("not enough funds for message execution")
}
balances[m.From] = types.BigSub(balances[m.From], m.RequiredFunds())
return nil
}
var receipts []cbg.CBORMarshaler
for i, m := range b.BlsMessages {
receipt, err := vmi.ApplyMessage(ctx, m)
if err != nil {
return xerrors.Errorf("failed executing bls message %d in block %s: %w", i, b.Header.Cid(), err)
if err := checkMsg(m); err != nil {
xerrors.Errorf("block had invalid bls message at index %d: %w", i, err)
}
receipts = append(receipts, &receipt.MessageReceipt)
}
for i, m := range b.SecpkMessages {
receipt, err := vmi.ApplyMessage(ctx, &m.Message)
if err != nil {
return xerrors.Errorf("failed executing secpk message %d in block %s: %w", i, b.Header.Cid(), err)
if err := checkMsg(&m.Message); err != nil {
xerrors.Errorf("block had invalid secpk message at index %d: %w", i, err)
}
receipts = append(receipts, &receipt.MessageReceipt)
}
bs := amt.WrapBlockstore(syncer.store.Blockstore())
recptRoot, err := amt.FromArray(bs, receipts)
if err != nil {
return xerrors.Errorf("building receipts amt failed: %w", err)
}
if recptRoot != b.Header.MessageReceipts {
return fmt.Errorf("receipts mismatched")
}
final, err := vmi.Flush(context.TODO())
if err != nil {
return xerrors.Errorf("failed to flush VM state: %w", err)
}
if b.Header.StateRoot != final {
return fmt.Errorf("final state root does not match block")
}
return nil

View File

@ -34,14 +34,14 @@ type BlockHeader struct {
Height uint64
StateRoot cid.Cid
ParentStateRoot cid.Cid
ParentMessageReceipts cid.Cid
Messages cid.Cid
BLSAggregate Signature
MessageReceipts cid.Cid
Timestamp uint64
BlockSig Signature

View File

@ -33,12 +33,12 @@ func testBlockHeader(t testing.TB) *BlockHeader {
},
},
Parents: []cid.Cid{c, c},
MessageReceipts: c,
ParentMessageReceipts: c,
BLSAggregate: Signature{Type: KTBLS, Data: []byte("boo! im a signature")},
ParentWeight: NewInt(123125126212),
Messages: c,
Height: 85919298723,
StateRoot: c,
ParentStateRoot: c,
BlockSig: Signature{Type: KTBLS, Data: []byte("boo! im a signature")},
}
}

View File

@ -5,7 +5,7 @@ import (
"io"
"math"
"github.com/ipfs/go-cid"
cid "github.com/ipfs/go-cid"
cbg "github.com/whyrusleeping/cbor-gen"
xerrors "golang.org/x/xerrors"
)
@ -66,10 +66,16 @@ func (t *BlockHeader) MarshalCBOR(w io.Writer) error {
return err
}
// t.t.StateRoot (cid.Cid)
// t.t.ParentStateRoot (cid.Cid)
if err := cbg.WriteCid(w, t.StateRoot); err != nil {
return xerrors.Errorf("failed to write cid field t.StateRoot: %w", err)
if err := cbg.WriteCid(w, t.ParentStateRoot); err != nil {
return xerrors.Errorf("failed to write cid field t.ParentStateRoot: %w", err)
}
// t.t.ParentMessageReceipts (cid.Cid)
if err := cbg.WriteCid(w, t.ParentMessageReceipts); err != nil {
return xerrors.Errorf("failed to write cid field t.ParentMessageReceipts: %w", err)
}
// t.t.Messages (cid.Cid)
@ -83,12 +89,6 @@ func (t *BlockHeader) MarshalCBOR(w io.Writer) error {
return err
}
// t.t.MessageReceipts (cid.Cid)
if err := cbg.WriteCid(w, t.MessageReceipts); err != nil {
return xerrors.Errorf("failed to write cid field t.MessageReceipts: %w", err)
}
// t.t.Timestamp (uint64)
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, t.Timestamp)); err != nil {
return err
@ -212,16 +212,28 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error {
return fmt.Errorf("wrong type for uint64 field")
}
t.Height = extra
// t.t.StateRoot (cid.Cid)
// t.t.ParentStateRoot (cid.Cid)
{
c, err := cbg.ReadCid(br)
if err != nil {
return xerrors.Errorf("failed to read cid field t.StateRoot: %w", err)
return xerrors.Errorf("failed to read cid field t.ParentStateRoot: %w", err)
}
t.StateRoot = c
t.ParentStateRoot = c
}
// t.t.ParentMessageReceipts (cid.Cid)
{
c, err := cbg.ReadCid(br)
if err != nil {
return xerrors.Errorf("failed to read cid field t.ParentMessageReceipts: %w", err)
}
t.ParentMessageReceipts = c
}
// t.t.Messages (cid.Cid)
@ -244,18 +256,6 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error {
return err
}
}
// t.t.MessageReceipts (cid.Cid)
{
c, err := cbg.ReadCid(br)
if err != nil {
return xerrors.Errorf("failed to read cid field t.MessageReceipts: %w", err)
}
t.MessageReceipts = c
}
// t.t.Timestamp (uint64)

View File

@ -73,3 +73,7 @@ func (m *Message) RequiredFunds() BigInt {
BigMul(m.GasPrice, m.GasLimit),
)
}
func (m *Message) VMMessage() *Message {
return m
}

View File

@ -57,3 +57,7 @@ func (sm *SignedMessage) Serialize() ([]byte, error) {
}
return buf.Bytes(), nil
}
func (sm *SignedMessage) VMMessage() *Message {
return &sm.Message
}

View File

@ -134,7 +134,7 @@ func (a *StateAPI) stateForTs(ts *types.TipSet) (*state.StateTree, error) {
ts = a.Chain.GetHeaviestTipSet()
}
st, err := a.StateManager.TipSetState(ts.Cids())
st, _, err := a.StateManager.TipSetState(ts.Cids())
if err != nil {
return nil, err
}