Skip to content

Commit

Permalink
Merge pull request #87 from filecoin-project/refactor/types-again
Browse files Browse the repository at this point in the history
pull more things apart
  • Loading branch information
magik6k authored Jul 26, 2019
2 parents 6ca033f + 78da356 commit 0c6a8f8
Show file tree
Hide file tree
Showing 22 changed files with 410 additions and 377 deletions.
8 changes: 4 additions & 4 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,24 @@ type FullNode interface {
Common

// chain
ChainHead(context.Context) (*chain.TipSet, error) // TODO: check serialization
ChainHead(context.Context) (*types.TipSet, error) // TODO: check serialization
ChainSubmitBlock(ctx context.Context, blk *chain.BlockMsg) error // TODO: check serialization
ChainGetRandomness(context.Context, *chain.TipSet) ([]byte, error)
ChainGetRandomness(context.Context, *types.TipSet) ([]byte, error)
ChainWaitMsg(context.Context, cid.Cid) (*MsgWait, error)
ChainGetBlock(context.Context, cid.Cid) (*types.BlockHeader, error)
ChainGetBlockMessages(context.Context, cid.Cid) ([]*types.SignedMessage, error)

// messages

MpoolPending(context.Context, *chain.TipSet) ([]*types.SignedMessage, error)
MpoolPending(context.Context, *types.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, []types.Ticket, types.ElectionProof, []*types.SignedMessage) (*chain.BlockMsg, error)
MinerCreateBlock(context.Context, address.Address, *types.TipSet, []types.Ticket, types.ElectionProof, []*types.SignedMessage) (*chain.BlockMsg, error)

// // UX ?

Expand Down
16 changes: 8 additions & 8 deletions api/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ type FullNodeStruct struct {

Internal struct {
ChainSubmitBlock func(ctx context.Context, blk *chain.BlockMsg) error `perm:"write"`
ChainHead func(context.Context) (*chain.TipSet, error) `perm:"read"`
ChainGetRandomness func(context.Context, *chain.TipSet) ([]byte, error) `perm:"read"`
ChainHead func(context.Context) (*types.TipSet, error) `perm:"read"`
ChainGetRandomness func(context.Context, *types.TipSet) ([]byte, error) `perm:"read"`
ChainWaitMsg func(context.Context, cid.Cid) (*MsgWait, 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) ([]*types.SignedMessage, error) `perm:"read"`
MpoolPending func(context.Context, *types.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, []types.Ticket, types.ElectionProof, []*types.SignedMessage) (*chain.BlockMsg, error) `perm:"write"`
MinerCreateBlock func(context.Context, address.Address, *types.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 @@ -115,7 +115,7 @@ 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) ([]*types.SignedMessage, error) {
func (c *FullNodeStruct) MpoolPending(ctx context.Context, ts *types.TipSet) ([]*types.SignedMessage, error) {
return c.Internal.MpoolPending(ctx, ts)
}

Expand All @@ -127,19 +127,19 @@ func (c *FullNodeStruct) MinerStart(ctx context.Context, addr address.Address) e
return c.Internal.MinerStart(ctx, addr)
}

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

func (c *FullNodeStruct) ChainSubmitBlock(ctx context.Context, blk *chain.BlockMsg) error {
return c.Internal.ChainSubmitBlock(ctx, blk)
}

func (c *FullNodeStruct) ChainHead(ctx context.Context) (*chain.TipSet, error) {
func (c *FullNodeStruct) ChainHead(ctx context.Context) (*types.TipSet, error) {
return c.Internal.ChainHead(ctx)
}

func (c *FullNodeStruct) ChainGetRandomness(ctx context.Context, pts *chain.TipSet) ([]byte, error) {
func (c *FullNodeStruct) ChainGetRandomness(ctx context.Context, pts *types.TipSet) ([]byte, error) {
return c.Internal.ChainGetRandomness(ctx, pts)
}

Expand Down
9 changes: 5 additions & 4 deletions chain/actors/actors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"encoding/binary"
"testing"

"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/store"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/filecoin-project/go-lotus/chain/vm"
dstore "github.com/ipfs/go-datastore"
bstore "github.com/ipfs/go-ipfs-blockstore"
cbor "github.com/ipfs/go-ipld-cbor"
Expand All @@ -26,7 +27,7 @@ func blsaddr(n uint64) address.Address {
return addr
}

func setupVMTestEnv(t *testing.T) (*chain.VM, []address.Address) {
func setupVMTestEnv(t *testing.T) (*vm.VM, []address.Address) {
bs := bstore.NewBlockstore(dstore.NewMapDatastore())

from := blsaddr(0)
Expand All @@ -46,9 +47,9 @@ func setupVMTestEnv(t *testing.T) (*chain.VM, []address.Address) {
t.Fatal(err)
}

cs := chain.NewChainStore(bs, nil)
cs := store.NewChainStore(bs, nil)

vm, err := chain.NewVM(stateroot, 1, maddr, cs)
vm, err := vm.NewVM(stateroot, 1, maddr, cs)
if err != nil {
t.Fatal(err)
}
Expand Down
11 changes: 6 additions & 5 deletions chain/actors/harness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (

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

"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"
Expand All @@ -27,9 +28,9 @@ type Harness struct {

t *testing.T
actors []address.Address
vm *chain.VM
vm *vm.VM
bs bstore.Blockstore
cs *chain.ChainStore
cs *store.ChainStore
}

type Step struct {
Expand Down Expand Up @@ -61,9 +62,9 @@ func NewHarness(t *testing.T) *Harness {
t.Fatal(err)
}

h.cs = chain.NewChainStore(h.bs, nil)
h.cs = store.NewChainStore(h.bs, nil)

h.vm, err = chain.NewVM(stateroot, 1, maddr, h.cs)
h.vm, err = vm.NewVM(stateroot, 1, maddr, h.cs)
if err != nil {
t.Fatal(err)
}
Expand Down
25 changes: 13 additions & 12 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/store"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/filecoin-project/go-lotus/lib/cborrpc"

Expand All @@ -31,7 +32,7 @@ func init() {
}

type BlockSyncService struct {
cs *ChainStore
cs *store.ChainStore
}

type BlockSyncRequest struct {
Expand Down Expand Up @@ -72,7 +73,7 @@ type BSTipSet struct {
MsgIncludes [][]int
}

func NewBlockSyncService(cs *ChainStore) *BlockSyncService {
func NewBlockSyncService(cs *store.ChainStore) *BlockSyncService {
return &BlockSyncService{
cs: cs,
}
Expand Down Expand Up @@ -154,7 +155,7 @@ func (bss *BlockSyncService) collectChainSegment(start []cid.Cid, length uint64,
}
}

func (bss *BlockSyncService) gatherMessages(ts *TipSet) ([]*types.SignedMessage, [][]int, error) {
func (bss *BlockSyncService) gatherMessages(ts *types.TipSet) ([]*types.SignedMessage, [][]int, error) {
msgmap := make(map[cid.Cid]int)
var allmsgs []*types.SignedMessage
var msgincl [][]int
Expand Down Expand Up @@ -209,7 +210,7 @@ func (bs *BlockSync) getPeers() []peer.ID {
return out
}

func (bs *BlockSync) GetBlocks(ctx context.Context, tipset []cid.Cid, count int) ([]*TipSet, error) {
func (bs *BlockSync) GetBlocks(ctx context.Context, tipset []cid.Cid, count int) ([]*types.TipSet, error) {
peers := bs.getPeers()
perm := rand.Perm(len(peers))
// TODO: round robin through these peers on error
Expand Down Expand Up @@ -241,7 +242,7 @@ func (bs *BlockSync) GetBlocks(ctx context.Context, tipset []cid.Cid, count int)
}
}

func (bs *BlockSync) GetFullTipSet(ctx context.Context, p peer.ID, h []cid.Cid) (*FullTipSet, error) {
func (bs *BlockSync) GetFullTipSet(ctx context.Context, p peer.ID, h []cid.Cid) (*store.FullTipSet, error) {
// TODO: round robin through these peers on error

req := &BlockSyncRequest{
Expand Down Expand Up @@ -276,7 +277,7 @@ func (bs *BlockSync) GetFullTipSet(ctx context.Context, p peer.ID, h []cid.Cid)
}
}

func (bs *BlockSync) GetChainMessages(ctx context.Context, h *TipSet, count uint64) ([]*BSTipSet, error) {
func (bs *BlockSync) GetChainMessages(ctx context.Context, h *types.TipSet, count uint64) ([]*BSTipSet, error) {
peers := bs.getPeers()
perm := rand.Perm(len(peers))
// TODO: round robin through these peers on error
Expand Down Expand Up @@ -308,8 +309,8 @@ func (bs *BlockSync) GetChainMessages(ctx context.Context, h *TipSet, count uint
}
}

func bstsToFullTipSet(bts *BSTipSet) (*FullTipSet, error) {
fts := &FullTipSet{}
func bstsToFullTipSet(bts *BSTipSet) (*store.FullTipSet, error) {
fts := &store.FullTipSet{}
for i, b := range bts.Blocks {
fb := &types.FullBlock{
Header: b,
Expand Down Expand Up @@ -341,16 +342,16 @@ func (bs *BlockSync) sendRequestToPeer(ctx context.Context, p peer.ID, req *Bloc
return &res, nil
}

func (bs *BlockSync) processBlocksResponse(req *BlockSyncRequest, res *BlockSyncResponse) ([]*TipSet, error) {
cur, err := NewTipSet(res.Chain[0].Blocks)
func (bs *BlockSync) processBlocksResponse(req *BlockSyncRequest, res *BlockSyncResponse) ([]*types.TipSet, error) {
cur, err := types.NewTipSet(res.Chain[0].Blocks)
if err != nil {
return nil, err
}

out := []*TipSet{cur}
out := []*types.TipSet{cur}
for bi := 1; bi < len(res.Chain); bi++ {
next := res.Chain[bi].Blocks
nts, err := NewTipSet(next)
nts, err := types.NewTipSet(next)
if err != nil {
return nil, err
}
Expand Down
31 changes: 6 additions & 25 deletions chain/messagepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import (
"sync"

"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"
hamt "github.com/ipfs/go-hamt-ipld"
)

type MessagePool struct {
lk sync.Mutex

pending map[address.Address]*msgSet

cs *ChainStore
cs *store.ChainStore
}

type msgSet struct {
Expand All @@ -35,7 +34,7 @@ func (ms *msgSet) add(m *types.SignedMessage) {
ms.msgs[m.Message.Nonce] = m
}

func NewMessagePool(cs *ChainStore) *MessagePool {
func NewMessagePool(cs *store.ChainStore) *MessagePool {
mp := &MessagePool{
pending: make(map[address.Address]*msgSet),
cs: cs,
Expand All @@ -58,12 +57,7 @@ func (mp *MessagePool) Add(m *types.SignedMessage) error {
return err
}

msb, err := m.ToStorageBlock()
if err != nil {
return err
}

if err := mp.cs.bs.Put(msb); err != nil {
if _, err := mp.cs.PutMessage(m); err != nil {
return err
}

Expand All @@ -86,20 +80,7 @@ func (mp *MessagePool) GetNonce(addr address.Address) (uint64, error) {
return mset.startNonce + uint64(len(mset.msgs)), nil
}

head := mp.cs.GetHeaviestTipSet()

stc, err := mp.cs.TipSetState(head.Cids())
if err != nil {
return 0, err
}

cst := hamt.CSTFromBstore(mp.cs.bs)
st, err := state.LoadStateTree(cst, stc)
if err != nil {
return 0, err
}

act, err := st.GetActor(addr)
act, err := mp.cs.GetActor(addr)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -142,7 +123,7 @@ func (mp *MessagePool) Pending() []*types.SignedMessage {
return out
}

func (mp *MessagePool) HeadChange(revert []*TipSet, apply []*TipSet) error {
func (mp *MessagePool) HeadChange(revert []*types.TipSet, apply []*types.TipSet) error {
for _, ts := range revert {
for _, b := range ts.Blocks() {
msgs, err := mp.cs.MessagesForBlock(b)
Expand Down
17 changes: 8 additions & 9 deletions chain/mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@ import (

"github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/store"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/filecoin-project/go-lotus/chain/vm"
)

func miningRewardForBlock(base *TipSet) types.BigInt {
func miningRewardForBlock(base *types.TipSet) types.BigInt {
return types.NewInt(10000)
}

func MinerCreateBlock(cs *ChainStore, miner address.Address, parents *TipSet, tickets []types.Ticket, proof types.ElectionProof, msgs []*types.SignedMessage) (*types.FullBlock, error) {
func MinerCreateBlock(cs *store.ChainStore, miner address.Address, parents *types.TipSet, tickets []types.Ticket, proof types.ElectionProof, msgs []*types.SignedMessage) (*types.FullBlock, error) {
st, err := cs.TipSetState(parents.Cids())
if err != nil {
return nil, errors.Wrap(err, "failed to load tipset state")
}

height := parents.Height() + uint64(len(tickets))

vm, err := NewVM(st, height, miner, cs)
vm, err := vm.NewVM(st, height, miner, cs)
if err != nil {
return nil, err
}
Expand All @@ -52,15 +54,12 @@ func MinerCreateBlock(cs *ChainStore, miner address.Address, parents *TipSet, ti
if msg.Signature.TypeCode() == 2 {
blsSigs = append(blsSigs, msg.Signature)

blk, err := msg.Message.ToStorageBlock()
c, err := cs.PutMessage(&msg.Message)
if err != nil {
return nil, err
}
if err := cs.bs.Put(blk); err != nil {
return nil, err
}

msgCids = append(msgCids, blk.Cid())
msgCids = append(msgCids, c)
} else {
msgCids = append(msgCids, msg.Cid())
}
Expand All @@ -72,7 +71,7 @@ func MinerCreateBlock(cs *ChainStore, miner address.Address, parents *TipSet, ti
receipts = append(receipts, rec)
}

cst := hamt.CSTFromBstore(cs.bs)
cst := hamt.CSTFromBstore(cs.Blockstore())
msgroot, err := sharray.Build(context.TODO(), 4, toIfArr(msgCids), cst)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 0c6a8f8

Please sign in to comment.