Skip to content

Commit

Permalink
Merge pull request #199 from filecoin-project/feat/serialization-3
Browse files Browse the repository at this point in the history
final chain serialization bits
  • Loading branch information
whyrusleeping authored Sep 17, 2019
2 parents fc5c455 + e7b642f commit 0f92df7
Show file tree
Hide file tree
Showing 12 changed files with 335 additions and 111 deletions.
61 changes: 56 additions & 5 deletions chain/actors/cbor_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (t *InitActorState) MarshalCBOR(w io.Writer) error {
}

// t.t.AddressMap (cid.Cid)

if err := cbg.WriteCid(w, t.AddressMap); err != nil {
return xerrors.Errorf("failed to write cid field t.AddressMap: %w", err)
}
Expand Down Expand Up @@ -54,11 +55,14 @@ func (t *InitActorState) UnmarshalCBOR(r io.Reader) error {
// t.t.AddressMap (cid.Cid)

{

c, err := cbg.ReadCid(br)
if err != nil {
return xerrors.Errorf("failed to read cid field t.AddressMap: %w", err)
}

t.AddressMap = c

}
// t.t.NextID (uint64)

Expand All @@ -83,6 +87,7 @@ func (t *ExecParams) MarshalCBOR(w io.Writer) error {
}

// t.t.Code (cid.Cid)

if err := cbg.WriteCid(w, t.Code); err != nil {
return xerrors.Errorf("failed to write cid field t.Code: %w", err)
}
Expand Down Expand Up @@ -115,11 +120,14 @@ func (t *ExecParams) UnmarshalCBOR(r io.Reader) error {
// t.t.Code (cid.Cid)

{

c, err := cbg.ReadCid(br)
if err != nil {
return xerrors.Errorf("failed to read cid field t.Code: %w", err)
}

t.Code = c

}
// t.t.Params ([]uint8)

Expand Down Expand Up @@ -194,6 +202,7 @@ func (t *StorageMinerActorState) MarshalCBOR(w io.Writer) error {
}

// t.t.Info (cid.Cid)

if err := cbg.WriteCid(w, t.Info); err != nil {
return xerrors.Errorf("failed to write cid field t.Info: %w", err)
}
Expand All @@ -209,6 +218,7 @@ func (t *StorageMinerActorState) MarshalCBOR(w io.Writer) error {
}

// t.t.Sectors (cid.Cid)

if err := cbg.WriteCid(w, t.Sectors); err != nil {
return xerrors.Errorf("failed to write cid field t.Sectors: %w", err)
}
Expand All @@ -219,6 +229,7 @@ func (t *StorageMinerActorState) MarshalCBOR(w io.Writer) error {
}

// t.t.ProvingSet (cid.Cid)

if err := cbg.WriteCid(w, t.ProvingSet); err != nil {
return xerrors.Errorf("failed to write cid field t.ProvingSet: %w", err)
}
Expand Down Expand Up @@ -268,11 +279,14 @@ func (t *StorageMinerActorState) UnmarshalCBOR(r io.Reader) error {
// t.t.Info (cid.Cid)

{

c, err := cbg.ReadCid(br)
if err != nil {
return xerrors.Errorf("failed to read cid field t.Info: %w", err)
}

t.Info = c

}
// t.t.DePledgedCollateral (types.BigInt)

Expand All @@ -295,11 +309,14 @@ func (t *StorageMinerActorState) UnmarshalCBOR(r io.Reader) error {
// t.t.Sectors (cid.Cid)

{

c, err := cbg.ReadCid(br)
if err != nil {
return xerrors.Errorf("failed to read cid field t.Sectors: %w", err)
}

t.Sectors = c

}
// t.t.SectorSetSize (uint64)

Expand All @@ -314,11 +331,14 @@ func (t *StorageMinerActorState) UnmarshalCBOR(r io.Reader) error {
// t.t.ProvingSet (cid.Cid)

{

c, err := cbg.ReadCid(br)
if err != nil {
return xerrors.Errorf("failed to read cid field t.ProvingSet: %w", err)
}

t.ProvingSet = c

}
// t.t.ProvingSetSize (uint64)

Expand Down Expand Up @@ -1057,6 +1077,7 @@ func (t *MultiSigActorState) UnmarshalCBOR(r io.Reader) error {
t.Signers = make([]address.Address, extra)
}
for i := 0; i < int(extra); i++ {

var v address.Address
if err := v.UnmarshalCBOR(br); err != nil {
return err
Expand Down Expand Up @@ -1102,6 +1123,7 @@ func (t *MultiSigActorState) UnmarshalCBOR(r io.Reader) error {
t.Transactions = make([]MTransaction, extra)
}
for i := 0; i < int(extra); i++ {

var v MTransaction
if err := v.UnmarshalCBOR(br); err != nil {
return err
Expand Down Expand Up @@ -1171,6 +1193,7 @@ func (t *MultiSigConstructorParams) UnmarshalCBOR(r io.Reader) error {
t.Signers = make([]address.Address, extra)
}
for i := 0; i < int(extra); i++ {

var v address.Address
if err := v.UnmarshalCBOR(br); err != nil {
return err
Expand Down Expand Up @@ -1600,6 +1623,7 @@ func (t *MTransaction) UnmarshalCBOR(r io.Reader) error {
t.Approved = make([]address.Address, extra)
}
for i := 0; i < int(extra); i++ {

var v address.Address
if err := v.UnmarshalCBOR(br); err != nil {
return err
Expand Down Expand Up @@ -2195,8 +2219,15 @@ func (t *PaymentInfo) MarshalCBOR(w io.Writer) error {
}

// t.t.ChannelMessage (cid.Cid)
if err := cbg.WriteCid(w, *t.ChannelMessage); err != nil {
return xerrors.Errorf("failed to write cid field t.ChannelMessage: %w", err)

if t.ChannelMessage == nil {
if _, err := w.Write(cbg.CborNull); err != nil {
return err
}
} else {
if err := cbg.WriteCid(w, *t.ChannelMessage); err != nil {
return xerrors.Errorf("failed to write cid field t.ChannelMessage: %w", err)
}
}

// t.t.Vouchers ([]*types.SignedVoucher)
Expand Down Expand Up @@ -2247,11 +2278,26 @@ func (t *PaymentInfo) UnmarshalCBOR(r io.Reader) error {
// t.t.ChannelMessage (cid.Cid)

{
c, err := cbg.ReadCid(br)

pb, err := br.PeekByte()
if err != nil {
return xerrors.Errorf("failed to read cid field t.ChannelMessage: %w", err)
return err
}
if pb == cbg.CborNull[0] {
var nbuf [1]byte
if _, err := br.Read(nbuf[:]); err != nil {
return err
}
} else {

c, err := cbg.ReadCid(br)
if err != nil {
return xerrors.Errorf("failed to read cid field t.ChannelMessage: %w", err)
}

t.ChannelMessage = &c
}
t.ChannelMessage = &c

}
// t.t.Vouchers ([]*types.SignedVoucher)

Expand All @@ -2270,6 +2316,7 @@ func (t *PaymentInfo) UnmarshalCBOR(r io.Reader) error {
t.Vouchers = make([]*types.SignedVoucher, extra)
}
for i := 0; i < int(extra); i++ {

var v types.SignedVoucher
if err := v.UnmarshalCBOR(br); err != nil {
return err
Expand All @@ -2291,6 +2338,7 @@ func (t *StorageMarketState) MarshalCBOR(w io.Writer) error {
}

// t.t.Miners (cid.Cid)

if err := cbg.WriteCid(w, t.Miners); err != nil {
return xerrors.Errorf("failed to write cid field t.Miners: %w", err)
}
Expand Down Expand Up @@ -2320,11 +2368,14 @@ func (t *StorageMarketState) UnmarshalCBOR(r io.Reader) error {
// t.t.Miners (cid.Cid)

{

c, err := cbg.ReadCid(br)
if err != nil {
return xerrors.Errorf("failed to read cid field t.Miners: %w", err)
}

t.Miners = c

}
// t.t.TotalStorage (types.BigInt)

Expand Down
33 changes: 18 additions & 15 deletions chain/gen/mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package gen
import (
"context"

amt "github.com/filecoin-project/go-amt-ipld"
bls "github.com/filecoin-project/go-bls-sigs"
cid "github.com/ipfs/go-cid"
hamt "github.com/ipfs/go-hamt-ipld"
"github.com/pkg/errors"
"github.com/whyrusleeping/sharray"
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors"

"github.com/filecoin-project/go-lotus/chain/actors"
Expand Down Expand Up @@ -77,35 +78,35 @@ func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wal
}
}

var receipts []interface{}
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)
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)
receipts = append(receipts, &rec.MessageReceipt)
}

cst := hamt.CSTFromBstore(sm.ChainStore().Blockstore())
blsmsgroot, err := sharray.Build(context.TODO(), 4, toIfArr(blsMsgCids), cst)
bs := amt.WrapBlockstore(sm.ChainStore().Blockstore())
blsmsgroot, err := amt.FromArray(bs, toIfArr(blsMsgCids))
if err != nil {
return nil, err
return nil, xerrors.Errorf("building bls amt: %w", err)
}
secpkmsgroot, err := sharray.Build(context.TODO(), 4, toIfArr(secpkMsgCids), cst)
secpkmsgroot, err := amt.FromArray(bs, toIfArr(secpkMsgCids))
if err != nil {
return nil, err
return nil, xerrors.Errorf("building secpk amt: %w", err)
}

mmcid, err := cst.Put(context.TODO(), &types.MsgMeta{
mmcid, err := bs.Put(&types.MsgMeta{
BlsMessages: blsmsgroot,
SecpkMessages: secpkmsgroot,
})
Expand All @@ -114,9 +115,9 @@ func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wal
}
next.Messages = mmcid

rectroot, err := sharray.Build(context.TODO(), 4, receipts, cst)
rectroot, err := amt.FromArray(bs, receipts)
if err != nil {
return nil, err
return nil, xerrors.Errorf("failed to build receipts amt: %w", err)
}
next.MessageReceipts = rectroot

Expand All @@ -142,6 +143,7 @@ func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wal
return nil, xerrors.Errorf("failed to get signing bytes for block: %w", err)
}

cst := hamt.CSTFromBstore(sm.ChainStore().Blockstore())
waddr, err := vm.ResolveToKeyAddr(vmi.StateTree(), cst, worker)
if err != nil {
return nil, xerrors.Errorf("failed to resolve miner address to key address: %w", err)
Expand Down Expand Up @@ -178,10 +180,11 @@ func aggregateSignatures(sigs []types.Signature) (types.Signature, error) {
}, nil
}

func toIfArr(cids []cid.Cid) []interface{} {
out := make([]interface{}, 0, len(cids))
func toIfArr(cids []cid.Cid) []cbg.CBORMarshaler {
out := make([]cbg.CBORMarshaler, 0, len(cids))
for _, c := range cids {
out = append(out, c)
oc := cbg.CborCid(c)
out = append(out, &oc)
}
return out
}
8 changes: 4 additions & 4 deletions chain/gen/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

amt "github.com/filecoin-project/go-amt-ipld"
"github.com/filecoin-project/go-lotus/build"
actors "github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/address"
Expand All @@ -19,7 +20,6 @@ import (
bstore "github.com/ipfs/go-ipfs-blockstore"
peer "github.com/libp2p/go-libp2p-peer"
cbg "github.com/whyrusleeping/cbor-gen"
sharray "github.com/whyrusleeping/sharray"
)

type GenesisBootstrap struct {
Expand Down Expand Up @@ -287,11 +287,11 @@ func MakeGenesisBlock(bs bstore.Blockstore, balances map[address.Address]types.B
return nil, xerrors.Errorf("setup storage miners failed: %w", err)
}

cst := hamt.CSTFromBstore(bs)
blks := amt.WrapBlockstore(bs)

emptyroot, err := sharray.Build(context.TODO(), 4, []interface{}{}, cst)
emptyroot, err := amt.FromArray(blks, nil)
if err != nil {
return nil, xerrors.Errorf("sharray build failed: %w", err)
return nil, xerrors.Errorf("amt build failed: %w", err)
}

mm := &types.MsgMeta{
Expand Down
15 changes: 2 additions & 13 deletions chain/state/statetree.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/ipfs/go-cid"
hamt "github.com/ipfs/go-hamt-ipld"
cbor "github.com/ipfs/go-ipld-cbor"
logging "github.com/ipfs/go-log"
"golang.org/x/xerrors"

Expand Down Expand Up @@ -101,25 +100,15 @@ func (st *StateTree) GetActor(addr address.Address) (*types.Actor, error) {
return cact, nil
}

var thing interface{}
err := st.root.Find(context.TODO(), string(addr.Bytes()), &thing)
var act types.Actor
err := st.root.Find(context.TODO(), string(addr.Bytes()), &act)
if err != nil {
if err == hamt.ErrNotFound {
return nil, types.ErrActorNotFound
}
return nil, err
}

var act types.Actor
badout, err := cbor.DumpObject(thing)
if err != nil {
return nil, err
}

if err := cbor.DecodeInto(badout, &act); err != nil {
return nil, err
}

st.actorcache[addr] = &act

return &act, nil
Expand Down
Loading

0 comments on commit 0f92df7

Please sign in to comment.