From 05b815828576c1bfe3efe03542c64b887b37e937 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Tue, 16 Jul 2019 23:05:11 -0700 Subject: [PATCH] implement a few of the TODOs --- api/api.go | 9 +++++++-- api/struct.go | 11 +++++------ chain/messagepool.go | 31 +++++++++++++++++++++++++++++++ cli/createminer.go | 8 ++++---- lib/jsonrpc/util.go | 4 +++- node/api.go | 3 ++- 6 files changed, 52 insertions(+), 14 deletions(-) diff --git a/api/api.go b/api/api.go index 1faae97d3..d2c2ad424 100644 --- a/api/api.go +++ b/api/api.go @@ -26,6 +26,11 @@ type Import struct { Size uint64 } +type MsgWait struct { + InBlock cid.Cid + Receipt types.MessageReceipt +} + // API is a low-level interface to the Filecoin network type API interface { // chain @@ -33,7 +38,7 @@ type API interface { ChainHead(context.Context) (*chain.TipSet, error) // TODO: check serialization ChainSubmitBlock(ctx context.Context, blk *chain.BlockMsg) error // TODO: check serialization ChainGetRandomness(context.Context, *chain.TipSet) ([]byte, error) - ChainWaitMsg(context.Context, cid.Cid) (cid.Cid, *types.MessageReceipt, error) + ChainWaitMsg(context.Context, cid.Cid) (*MsgWait, error) // messages @@ -80,7 +85,7 @@ type API interface { WalletDefaultAddress(context.Context) (address.Address, error) // Really not sure where this belongs. It could go on the wallet, or the message pool, or the chain... - WalletGetNonce(context.Context, address.Address) (uint64, error) + MpoolGetNonce(context.Context, address.Address) (uint64, error) // // import // // export diff --git a/api/struct.go b/api/struct.go index 1e2e676c3..200b15e43 100644 --- a/api/struct.go +++ b/api/struct.go @@ -5,7 +5,6 @@ import ( "github.com/filecoin-project/go-lotus/chain" "github.com/filecoin-project/go-lotus/chain/address" - "github.com/filecoin-project/go-lotus/chain/types" "github.com/ipfs/go-cid" "github.com/libp2p/go-libp2p-core/peer" @@ -20,7 +19,7 @@ type Struct struct { ChainSubmitBlock func(ctx context.Context, blk *chain.BlockMsg) error ChainHead func(context.Context) (*chain.TipSet, error) ChainGetRandomness func(context.Context, *chain.TipSet) ([]byte, error) - ChainWaitMsg func(context.Context, cid.Cid) (cid.Cid, *types.MessageReceipt, error) + ChainWaitMsg func(context.Context, cid.Cid) (*MsgWait, error) MpoolPending func(context.Context, *chain.TipSet) ([]*chain.SignedMessage, error) MpoolPush func(context.Context, *chain.SignedMessage) error @@ -33,7 +32,7 @@ type Struct struct { WalletBalance func(context.Context, address.Address) (types.BigInt, error) WalletSign func(context.Context, address.Address, []byte) (*chain.Signature, error) WalletDefaultAddress func(context.Context) (address.Address, error) - WalletGetNonce func(context.Context, address.Address) (uint64, error) + MpoolGetNonce func(context.Context, address.Address) (uint64, error) ClientImport func(ctx context.Context, path string) (cid.Cid, error) ClientListImports func(ctx context.Context) ([]Import, error) @@ -92,7 +91,7 @@ func (c *Struct) ChainGetRandomness(ctx context.Context, pts *chain.TipSet) ([]b return c.Internal.ChainGetRandomness(ctx, pts) } -func (c *Struct) ChainWaitMsg(ctx context.Context, msgc cid.Cid) (cid.Cid, *types.MessageReceipt, error) { +func (c *Struct) ChainWaitMsg(ctx context.Context, msgc cid.Cid) (*MsgWait, error) { return c.Internal.ChainWaitMsg(ctx, msgc) } @@ -126,8 +125,8 @@ func (c *Struct) WalletDefaultAddress(ctx context.Context) (address.Address, err return c.Internal.WalletDefaultAddress(ctx) } -func (c *Struct) WalletGetNonce(ctx context.Context, addr address.Address) (uint64, error) { - return c.Internal.WalletGetNonce(ctx, addr) +func (c *Struct) MpoolGetNonce(ctx context.Context, addr address.Address) (uint64, error) { + return c.Internal.MpoolGetNonce(ctx, addr) } var _ API = &Struct{} diff --git a/chain/messagepool.go b/chain/messagepool.go index a166e3fb0..84af33e41 100644 --- a/chain/messagepool.go +++ b/chain/messagepool.go @@ -4,6 +4,7 @@ import ( "sync" "github.com/filecoin-project/go-lotus/chain/address" + hamt "github.com/ipfs/go-hamt-ipld" ) type MessagePool struct { @@ -74,6 +75,36 @@ func (mp *MessagePool) Add(m *SignedMessage) error { return nil } +func (mp *MessagePool) GetNonce(addr address.Address) (uint64, error) { + mp.lk.Lock() + defer mp.lk.Unlock() + + mset, ok := mp.pending[addr] + if ok { + return mset.startNonce + uint64(len(mset.msgs)), nil + } + + head := mp.cs.GetHeaviestTipSet() + + state, err := mp.cs.TipSetState(head.Cids()) + if err != nil { + return 0, err + } + + cst := hamt.CSTFromBstore(mp.cs.bs) + st, err := LoadStateTree(cst, state) + if err != nil { + return 0, err + } + + act, err := st.GetActor(addr) + if err != nil { + return 0, err + } + + return act.Nonce, nil +} + func (mp *MessagePool) Remove(m *SignedMessage) { mp.lk.Lock() defer mp.lk.Unlock() diff --git a/cli/createminer.go b/cli/createminer.go index 18edb8277..41365bb47 100644 --- a/cli/createminer.go +++ b/cli/createminer.go @@ -67,7 +67,7 @@ var createMinerCmd = &cli.Command{ return err } - nonce, err := api.WalletGetNonce(ctx, addr) + nonce, err := api.MpoolGetNonce(ctx, addr) if err != nil { return err } @@ -101,17 +101,17 @@ var createMinerCmd = &cli.Command{ return err } - inblk, rect, err := api.ChainWaitMsg(ctx, smsg.Cid()) + mwait, err := api.ChainWaitMsg(ctx, smsg.Cid()) if err != nil { return err } - maddr, err := address.NewFromBytes(rect.Return) + maddr, err := address.NewFromBytes(mwait.Receipt.Return) if err != nil { return err } - fmt.Printf("miner created in block %s\n", inblk) + fmt.Printf("miner created in block %s\n", mwait.InBlock) fmt.Println("new miner address: %s\n", maddr) return nil diff --git a/lib/jsonrpc/util.go b/lib/jsonrpc/util.go index ece0af7de..03b0bf7bd 100644 --- a/lib/jsonrpc/util.go +++ b/lib/jsonrpc/util.go @@ -2,6 +2,7 @@ package jsonrpc import ( "encoding/json" + "fmt" "reflect" ) @@ -42,7 +43,8 @@ func processFuncOut(funcType reflect.Type) (valOut int, errOut int, n int) { panic("expected error as second return value") } default: - panic("too many error values") + errstr := fmt.Sprintf("too many return values: %s", funcType) + panic(errstr) } return diff --git a/node/api.go b/node/api.go index f3f9df57f..dca8a1d94 100644 --- a/node/api.go +++ b/node/api.go @@ -51,7 +51,8 @@ func (a *API) ChainGetRandomness(ctx context.Context, pts *chain.TipSet) ([]byte return []byte("foo bar random"), nil } -func (a *API) ChainWaitMsg(ctx context.Context, msg cid.Cid) (*api.WaitMsg, error) { +func (a *API) ChainWaitMsg(ctx context.Context, msg cid.Cid) (*api.MsgWait, error) { + panic("TODO") } func (a *API) ID(context.Context) (peer.ID, error) {