Skip to content

Commit

Permalink
Merge pull request #85 from filecoin-project/refactor/types
Browse files Browse the repository at this point in the history
Refactor out more types into types package
  • Loading branch information
whyrusleeping committed Jul 26, 2019
2 parents 663cdbe + e09ad3d commit b281e31
Show file tree
Hide file tree
Showing 28 changed files with 590 additions and 540 deletions.
10 changes: 5 additions & 5 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,20 @@ type FullNode interface {
ChainSubmitBlock(ctx context.Context, blk *chain.BlockMsg) error // TODO: check serialization
ChainGetRandomness(context.Context, *chain.TipSet) ([]byte, error)
ChainWaitMsg(context.Context, cid.Cid) (*MsgWait, error)
ChainGetBlock(context.Context, cid.Cid) (*chain.BlockHeader, error)
ChainGetBlockMessages(context.Context, cid.Cid) ([]*chain.SignedMessage, error)
ChainGetBlock(context.Context, cid.Cid) (*types.BlockHeader, error)
ChainGetBlockMessages(context.Context, cid.Cid) ([]*types.SignedMessage, error)

// messages

MpoolPending(context.Context, *chain.TipSet) ([]*chain.SignedMessage, error)
MpoolPush(context.Context, *chain.SignedMessage) error
MpoolPending(context.Context, *chain.TipSet) ([]*types.SignedMessage, error)
MpoolPush(context.Context, *types.SignedMessage) error

// FullNodeStruct

// miner

MinerStart(context.Context, address.Address) error
MinerCreateBlock(context.Context, address.Address, *chain.TipSet, []chain.Ticket, chain.ElectionProof, []*chain.SignedMessage) (*chain.BlockMsg, error)
MinerCreateBlock(context.Context, address.Address, *chain.TipSet, []types.Ticket, types.ElectionProof, []*types.SignedMessage) (*chain.BlockMsg, error)

// // UX ?

Expand Down
20 changes: 10 additions & 10 deletions api/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ type FullNodeStruct struct {
ChainHead func(context.Context) (*chain.TipSet, error) `perm:"read"`
ChainGetRandomness func(context.Context, *chain.TipSet) ([]byte, error) `perm:"read"`
ChainWaitMsg func(context.Context, cid.Cid) (*MsgWait, error) `perm:"read"`
ChainGetBlock func(context.Context, cid.Cid) (*chain.BlockHeader, error) `perm:"read"`
ChainGetBlockMessages func(context.Context, cid.Cid) ([]*chain.SignedMessage, error) `perm:"read"`
ChainGetBlock func(context.Context, cid.Cid) (*types.BlockHeader, error) `perm:"read"`
ChainGetBlockMessages func(context.Context, cid.Cid) ([]*types.SignedMessage, error) `perm:"read"`

MpoolPending func(context.Context, *chain.TipSet) ([]*chain.SignedMessage, error) `perm:"read"`
MpoolPush func(context.Context, *chain.SignedMessage) error `perm:"write"`
MpoolPending func(context.Context, *chain.TipSet) ([]*types.SignedMessage, error) `perm:"read"`
MpoolPush func(context.Context, *types.SignedMessage) error `perm:"write"`

MinerStart func(context.Context, address.Address) error `perm:"admin"`
MinerCreateBlock func(context.Context, address.Address, *chain.TipSet, []chain.Ticket, chain.ElectionProof, []*chain.SignedMessage) (*chain.BlockMsg, error) `perm:"write"`
MinerCreateBlock func(context.Context, address.Address, *chain.TipSet, []types.Ticket, types.ElectionProof, []*types.SignedMessage) (*chain.BlockMsg, error) `perm:"write"`

WalletNew func(context.Context, string) (address.Address, error) `perm:"write"`
WalletList func(context.Context) ([]address.Address, error) `perm:"write"`
Expand Down Expand Up @@ -103,19 +103,19 @@ func (c *FullNodeStruct) ClientImport(ctx context.Context, path string) (cid.Cid
return c.Internal.ClientImport(ctx, path)
}

func (c *FullNodeStruct) MpoolPending(ctx context.Context, ts *chain.TipSet) ([]*chain.SignedMessage, error) {
func (c *FullNodeStruct) MpoolPending(ctx context.Context, ts *chain.TipSet) ([]*types.SignedMessage, error) {
return c.Internal.MpoolPending(ctx, ts)
}

func (c *FullNodeStruct) MpoolPush(ctx context.Context, smsg *chain.SignedMessage) error {
func (c *FullNodeStruct) MpoolPush(ctx context.Context, smsg *types.SignedMessage) error {
return c.Internal.MpoolPush(ctx, smsg)
}

func (c *FullNodeStruct) MinerStart(ctx context.Context, addr address.Address) error {
return c.Internal.MinerStart(ctx, addr)
}

func (c *FullNodeStruct) MinerCreateBlock(ctx context.Context, addr address.Address, base *chain.TipSet, tickets []chain.Ticket, eproof chain.ElectionProof, msgs []*chain.SignedMessage) (*chain.BlockMsg, error) {
func (c *FullNodeStruct) MinerCreateBlock(ctx context.Context, addr address.Address, base *chain.TipSet, tickets []types.Ticket, eproof types.ElectionProof, msgs []*types.SignedMessage) (*chain.BlockMsg, error) {
return c.Internal.MinerCreateBlock(ctx, addr, base, tickets, eproof, msgs)
}

Expand Down Expand Up @@ -159,11 +159,11 @@ func (c *FullNodeStruct) MpoolGetNonce(ctx context.Context, addr address.Address
return c.Internal.MpoolGetNonce(ctx, addr)
}

func (c *FullNodeStruct) ChainGetBlock(ctx context.Context, b cid.Cid) (*chain.BlockHeader, error) {
func (c *FullNodeStruct) ChainGetBlock(ctx context.Context, b cid.Cid) (*types.BlockHeader, error) {
return c.Internal.ChainGetBlock(ctx, b)
}

func (c *FullNodeStruct) ChainGetBlockMessages(ctx context.Context, b cid.Cid) ([]*chain.SignedMessage, error) {
func (c *FullNodeStruct) ChainGetBlockMessages(ctx context.Context, b cid.Cid) ([]*types.SignedMessage, error) {
return c.Internal.ChainGetBlockMessages(ctx, b)
}

Expand Down
8 changes: 4 additions & 4 deletions chain/actors/actor_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (ia InitActor) Exec(act *types.Actor, vmctx types.VMContext, p *ExecParams)
//actor.Constructor(p.Params)

// Store the mapping of address to actor ID.
idAddr, nerr := self.AddActor(vmctx, addr)
idAddr, nerr := self.AddActor(vmctx.Ipld(), addr)
if nerr != nil {
return nil, aerrors.Escalate(err, "adding new actor mapping")
}
Expand Down Expand Up @@ -164,11 +164,11 @@ func IsSingletonActor(code cid.Cid) bool {
return code == StorageMarketActorCodeCid || code == InitActorCodeCid
}

func (ias *InitActorState) AddActor(vmctx types.VMContext, addr address.Address) (address.Address, error) {
func (ias *InitActorState) AddActor(cst *hamt.CborIpldStore, addr address.Address) (address.Address, error) {
nid := ias.NextID
ias.NextID++

amap, err := hamt.LoadNode(context.TODO(), vmctx.Ipld(), ias.AddressMap)
amap, err := hamt.LoadNode(context.TODO(), cst, ias.AddressMap)
if err != nil {
return address.Undef, err
}
Expand All @@ -181,7 +181,7 @@ func (ias *InitActorState) AddActor(vmctx types.VMContext, addr address.Address)
return address.Undef, err
}

ncid, err := vmctx.Ipld().Put(context.TODO(), amap)
ncid, err := cst.Put(context.TODO(), amap)
if err != nil {
return address.Undef, err
}
Expand Down
3 changes: 2 additions & 1 deletion chain/actors/actors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/filecoin-project/go-lotus/chain"
. "github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/gen"
"github.com/filecoin-project/go-lotus/chain/types"
dstore "github.com/ipfs/go-datastore"
bstore "github.com/ipfs/go-ipfs-blockstore"
Expand Down Expand Up @@ -35,7 +36,7 @@ func setupVMTestEnv(t *testing.T) (*chain.VM, []address.Address) {
from: types.NewInt(1000000),
maddr: types.NewInt(0),
}
st, err := chain.MakeInitialStateTree(bs, actors)
st, err := gen.MakeInitialStateTree(bs, actors)
if err != nil {
t.Fatal(err)
}
Expand Down
8 changes: 5 additions & 3 deletions chain/actors/harness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"testing"

. "github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/gen"

"github.com/filecoin-project/go-lotus/chain"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/state"
"github.com/filecoin-project/go-lotus/chain/types"

dstore "github.com/ipfs/go-datastore"
Expand Down Expand Up @@ -49,7 +51,7 @@ func NewHarness(t *testing.T) *Harness {
maddr: types.NewInt(0),
third: types.NewInt(1000),
}
st, err := chain.MakeInitialStateTree(h.bs, actors)
st, err := gen.MakeInitialStateTree(h.bs, actors)
if err != nil {
t.Fatal(err)
}
Expand All @@ -71,7 +73,7 @@ func NewHarness(t *testing.T) *Harness {
return h
}

func (h *Harness) Execute() *chain.StateTree {
func (h *Harness) Execute() *state.StateTree {
for i, step := range h.Steps {
h.currStep = i
ret, err := h.vm.ApplyMessage(&step.M)
Expand All @@ -87,7 +89,7 @@ func (h *Harness) Execute() *chain.StateTree {
h.t.Fatalf("%+v", err)
}
cst := hamt.CSTFromBstore(h.bs)
state, err := chain.LoadStateTree(cst, stateroot)
state, err := state.LoadStateTree(cst, stateroot)
if err != nil {
h.t.Fatal(err)
}
Expand Down
21 changes: 11 additions & 10 deletions chain/blocksync.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/protocol"

"github.com/filecoin-project/go-lotus/chain/types"
"github.com/filecoin-project/go-lotus/lib/cborrpc"

"github.com/ipfs/go-cid"
Expand Down Expand Up @@ -65,9 +66,9 @@ type BlockSyncResponse struct {
}

type BSTipSet struct {
Blocks []*BlockHeader
Blocks []*types.BlockHeader

Messages []*SignedMessage
Messages []*types.SignedMessage
MsgIncludes [][]int
}

Expand Down Expand Up @@ -153,9 +154,9 @@ func (bss *BlockSyncService) collectChainSegment(start []cid.Cid, length uint64,
}
}

func (bss *BlockSyncService) gatherMessages(ts *TipSet) ([]*SignedMessage, [][]int, error) {
func (bss *BlockSyncService) gatherMessages(ts *TipSet) ([]*types.SignedMessage, [][]int, error) {
msgmap := make(map[cid.Cid]int)
var allmsgs []*SignedMessage
var allmsgs []*types.SignedMessage
var msgincl [][]int

for _, b := range ts.Blocks() {
Expand Down Expand Up @@ -310,7 +311,7 @@ func (bs *BlockSync) GetChainMessages(ctx context.Context, h *TipSet, count uint
func bstsToFullTipSet(bts *BSTipSet) (*FullTipSet, error) {
fts := &FullTipSet{}
for i, b := range bts.Blocks {
fb := &FullBlock{
fb := &types.FullBlock{
Header: b,
}
for _, mi := range bts.MsgIncludes[i] {
Expand Down Expand Up @@ -376,13 +377,13 @@ func cidArrsEqual(a, b []cid.Cid) bool {
return true
}

func (bs *BlockSync) GetBlock(ctx context.Context, c cid.Cid) (*BlockHeader, error) {
func (bs *BlockSync) GetBlock(ctx context.Context, c cid.Cid) (*types.BlockHeader, error) {
sb, err := bs.bswap.GetBlock(ctx, c)
if err != nil {
return nil, err
}

return DecodeBlock(sb.RawData())
return types.DecodeBlock(sb.RawData())
}

func (bs *BlockSync) AddPeer(p peer.ID) {
Expand All @@ -391,8 +392,8 @@ func (bs *BlockSync) AddPeer(p peer.ID) {
bs.syncPeers[p] = struct{}{}
}

func (bs *BlockSync) FetchMessagesByCids(cids []cid.Cid) ([]*SignedMessage, error) {
out := make([]*SignedMessage, len(cids))
func (bs *BlockSync) FetchMessagesByCids(cids []cid.Cid) ([]*types.SignedMessage, error) {
out := make([]*types.SignedMessage, len(cids))

resp, err := bs.bswap.GetBlocks(context.TODO(), cids)
if err != nil {
Expand All @@ -415,7 +416,7 @@ func (bs *BlockSync) FetchMessagesByCids(cids []cid.Cid) ([]*SignedMessage, erro
return nil, fmt.Errorf("failed to fetch all messages")
}

sm, err := DecodeSignedMessage(v.RawData())
sm, err := types.DecodeSignedMessage(v.RawData())
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit b281e31

Please sign in to comment.