Refactor out more types into types package, and pull genesis block code into gen package

This commit is contained in:
whyrusleeping 2019-07-25 15:15:03 -07:00
parent 663cdbe167
commit e09ad3d65c
28 changed files with 590 additions and 540 deletions

View File

@ -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 ?

View File

@ -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"`
@ -103,11 +103,11 @@ 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)
}
@ -115,7 +115,7 @@ 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 []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)
}
@ -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)
}

View File

@ -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")
}
@ -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
}
@ -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
}

View File

@ -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"
@ -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)
}

View File

@ -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"
@ -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)
}
@ -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)
@ -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)
}

View File

@ -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"
@ -65,9 +66,9 @@ type BlockSyncResponse struct {
}
type BSTipSet struct {
Blocks []*BlockHeader
Blocks []*types.BlockHeader
Messages []*SignedMessage
Messages []*types.SignedMessage
MsgIncludes [][]int
}
@ -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() {
@ -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] {
@ -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) {
@ -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 {
@ -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
}

View File

@ -6,8 +6,8 @@ import (
"fmt"
"sync"
actors "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/types"
"github.com/ipfs/go-cid"
@ -26,194 +26,6 @@ var log = logging.Logger("f2")
var chainHeadKey = dstore.NewKey("head")
type GenesisBootstrap struct {
Genesis *BlockHeader
MinerKey address.Address
}
func SetupInitActor(bs bstore.Blockstore, addrs []address.Address) (*types.Actor, error) {
var ias actors.InitActorState
ias.NextID = 100
cst := hamt.CSTFromBstore(bs)
amap := hamt.NewNode(cst)
for i, a := range addrs {
if err := amap.Set(context.TODO(), string(a.Bytes()), 100+uint64(i)); err != nil {
return nil, err
}
}
ias.NextID += uint64(len(addrs))
if err := amap.Flush(context.TODO()); err != nil {
return nil, err
}
amapcid, err := cst.Put(context.TODO(), amap)
if err != nil {
return nil, err
}
ias.AddressMap = amapcid
statecid, err := cst.Put(context.TODO(), &ias)
if err != nil {
return nil, err
}
act := &types.Actor{
Code: actors.InitActorCodeCid,
Head: statecid,
}
return act, nil
}
func init() {
bs := bstore.NewBlockstore(dstore.NewMapDatastore())
cst := hamt.CSTFromBstore(bs)
emptyobject, err := cst.Put(context.TODO(), map[string]string{})
if err != nil {
panic(err)
}
EmptyObjectCid = emptyobject
}
var EmptyObjectCid cid.Cid
func MakeInitialStateTree(bs bstore.Blockstore, actmap map[address.Address]types.BigInt) (*StateTree, error) {
cst := hamt.CSTFromBstore(bs)
state, err := NewStateTree(cst)
if err != nil {
return nil, err
}
emptyobject, err := cst.Put(context.TODO(), map[string]string{})
if err != nil {
return nil, err
}
var addrs []address.Address
for a := range actmap {
addrs = append(addrs, a)
}
initact, err := SetupInitActor(bs, addrs)
if err != nil {
return nil, err
}
if err := state.SetActor(actors.InitActorAddress, initact); err != nil {
return nil, err
}
smact, err := SetupStorageMarketActor(bs)
if err != nil {
return nil, err
}
if err := state.SetActor(actors.StorageMarketAddress, smact); err != nil {
return nil, err
}
err = state.SetActor(actors.NetworkAddress, &types.Actor{
Code: actors.AccountActorCodeCid,
Balance: types.NewInt(100000000000),
Head: emptyobject,
})
if err != nil {
return nil, err
}
for a, v := range actmap {
err = state.SetActor(a, &types.Actor{
Code: actors.AccountActorCodeCid,
Balance: v,
Head: emptyobject,
})
if err != nil {
return nil, err
}
}
return state, nil
}
func SetupStorageMarketActor(bs bstore.Blockstore) (*types.Actor, error) {
sms := &actors.StorageMarketState{
Miners: make(map[address.Address]struct{}),
TotalStorage: types.NewInt(0),
}
stcid, err := hamt.CSTFromBstore(bs).Put(context.TODO(), sms)
if err != nil {
return nil, err
}
return &types.Actor{
Code: actors.StorageMarketActorCodeCid,
Head: stcid,
Nonce: 0,
Balance: types.NewInt(0),
}, nil
}
func MakeGenesisBlock(bs bstore.Blockstore, w *Wallet) (*GenesisBootstrap, error) {
fmt.Println("at end of make Genesis block")
minerAddr, err := w.GenerateKey(types.KTSecp256k1)
if err != nil {
return nil, err
}
addrs := map[address.Address]types.BigInt{
minerAddr: types.NewInt(50000000),
}
state, err := MakeInitialStateTree(bs, addrs)
if err != nil {
return nil, err
}
stateroot, err := state.Flush()
if err != nil {
return nil, err
}
cst := hamt.CSTFromBstore(bs)
emptyroot, err := sharray.Build(context.TODO(), 4, []interface{}{}, cst)
if err != nil {
return nil, err
}
fmt.Println("Empty Genesis root: ", emptyroot)
b := &BlockHeader{
Miner: actors.InitActorAddress,
Tickets: []Ticket{},
ElectionProof: []byte("the Genesis block"),
Parents: []cid.Cid{},
Height: 0,
ParentWeight: types.NewInt(0),
StateRoot: stateroot,
Messages: emptyroot,
MessageReceipts: emptyroot,
}
sb, err := b.ToStorageBlock()
if err != nil {
return nil, err
}
if err := bs.Put(sb); err != nil {
return nil, err
}
return &GenesisBootstrap{
Genesis: b,
MinerKey: minerAddr,
}, nil
}
type ChainStore struct {
bs bstore.Blockstore
ds dstore.Datastore
@ -310,13 +122,13 @@ func (cs *ChainStore) SubscribeHeadChanges(f func(rev, app []*TipSet) error) {
cs.headChangeNotifs = append(cs.headChangeNotifs, f)
}
func (cs *ChainStore) SetGenesis(b *BlockHeader) error {
gents, err := NewTipSet([]*BlockHeader{b})
func (cs *ChainStore) SetGenesis(b *types.BlockHeader) error {
gents, err := NewTipSet([]*types.BlockHeader{b})
if err != nil {
return err
}
fts := &FullTipSet{
Blocks: []*FullBlock{
Blocks: []*types.FullBlock{
{Header: b},
},
}
@ -377,17 +189,17 @@ func (cs *ChainStore) Contains(ts *TipSet) (bool, error) {
return true, nil
}
func (cs *ChainStore) GetBlock(c cid.Cid) (*BlockHeader, error) {
func (cs *ChainStore) GetBlock(c cid.Cid) (*types.BlockHeader, error) {
sb, err := cs.bs.Get(c)
if err != nil {
return nil, err
}
return DecodeBlock(sb.RawData())
return types.DecodeBlock(sb.RawData())
}
func (cs *ChainStore) LoadTipSet(cids []cid.Cid) (*TipSet, error) {
var blks []*BlockHeader
var blks []*types.BlockHeader
for _, c := range cids {
b, err := cs.GetBlock(c)
if err != nil {
@ -466,7 +278,7 @@ func (cs *ChainStore) GetHeaviestTipSet() *TipSet {
return cs.heaviest
}
func (cs *ChainStore) persistBlockHeader(b *BlockHeader) error {
func (cs *ChainStore) persistBlockHeader(b *types.BlockHeader) error {
sb, err := b.ToStorageBlock()
if err != nil {
return err
@ -475,7 +287,7 @@ func (cs *ChainStore) persistBlockHeader(b *BlockHeader) error {
return cs.bs.Put(sb)
}
func (cs *ChainStore) persistBlock(b *FullBlock) error {
func (cs *ChainStore) persistBlock(b *types.FullBlock) error {
if err := cs.persistBlockHeader(b.Header); err != nil {
return err
}
@ -488,7 +300,7 @@ func (cs *ChainStore) persistBlock(b *FullBlock) error {
return nil
}
func (cs *ChainStore) PutMessage(m *SignedMessage) error {
func (cs *ChainStore) PutMessage(m *types.SignedMessage) error {
sb, err := m.ToStorageBlock()
if err != nil {
return err
@ -497,18 +309,18 @@ func (cs *ChainStore) PutMessage(m *SignedMessage) error {
return cs.bs.Put(sb)
}
func (cs *ChainStore) AddBlock(b *BlockHeader) error {
func (cs *ChainStore) AddBlock(b *types.BlockHeader) error {
if err := cs.persistBlockHeader(b); err != nil {
return err
}
ts, _ := NewTipSet([]*BlockHeader{b})
ts, _ := NewTipSet([]*types.BlockHeader{b})
cs.maybeTakeHeavierTipSet(ts)
return nil
}
func (cs *ChainStore) GetGenesis() (*BlockHeader, error) {
func (cs *ChainStore) GetGenesis() (*types.BlockHeader, error) {
data, err := cs.ds.Get(dstore.NewKey("0"))
if err != nil {
return nil, err
@ -524,7 +336,7 @@ func (cs *ChainStore) GetGenesis() (*BlockHeader, error) {
return nil, err
}
return DecodeBlock(genb.RawData())
return types.DecodeBlock(genb.RawData())
}
func (cs *ChainStore) TipSetState(cids []cid.Cid) (cid.Cid, error) {
@ -542,16 +354,16 @@ func (cs *ChainStore) TipSetState(cids []cid.Cid) (cid.Cid, error) {
}
func (cs *ChainStore) GetMessage(c cid.Cid) (*SignedMessage, error) {
func (cs *ChainStore) GetMessage(c cid.Cid) (*types.SignedMessage, error) {
sb, err := cs.bs.Get(c)
if err != nil {
return nil, err
}
return DecodeSignedMessage(sb.RawData())
return types.DecodeSignedMessage(sb.RawData())
}
func (cs *ChainStore) MessageCidsForBlock(b *BlockHeader) ([]cid.Cid, error) {
func (cs *ChainStore) MessageCidsForBlock(b *types.BlockHeader) ([]cid.Cid, error) {
cst := hamt.CSTFromBstore(cs.bs)
shar, err := sharray.Load(context.TODO(), b.Messages, 4, cst)
if err != nil {
@ -575,7 +387,7 @@ func (cs *ChainStore) MessageCidsForBlock(b *BlockHeader) ([]cid.Cid, error) {
return cids, nil
}
func (cs *ChainStore) MessagesForBlock(b *BlockHeader) ([]*SignedMessage, error) {
func (cs *ChainStore) MessagesForBlock(b *types.BlockHeader) ([]*types.SignedMessage, error) {
cids, err := cs.MessageCidsForBlock(b)
if err != nil {
return nil, err
@ -584,7 +396,7 @@ func (cs *ChainStore) MessagesForBlock(b *BlockHeader) ([]*SignedMessage, error)
return cs.LoadMessagesFromCids(cids)
}
func (cs *ChainStore) GetReceipt(b *BlockHeader, i int) (*types.MessageReceipt, error) {
func (cs *ChainStore) GetReceipt(b *types.BlockHeader, i int) (*types.MessageReceipt, error) {
cst := hamt.CSTFromBstore(cs.bs)
shar, err := sharray.Load(context.TODO(), b.MessageReceipts, 4, cst)
if err != nil {
@ -609,8 +421,8 @@ func (cs *ChainStore) GetReceipt(b *BlockHeader, i int) (*types.MessageReceipt,
return &r, nil
}
func (cs *ChainStore) LoadMessagesFromCids(cids []cid.Cid) ([]*SignedMessage, error) {
msgs := make([]*SignedMessage, 0, len(cids))
func (cs *ChainStore) LoadMessagesFromCids(cids []cid.Cid) ([]*types.SignedMessage, error) {
msgs := make([]*types.SignedMessage, 0, len(cids))
for _, c := range cids {
m, err := cs.GetMessage(c)
if err != nil {
@ -631,7 +443,7 @@ func (cs *ChainStore) GetBalance(addr address.Address) (types.BigInt, error) {
}
cst := hamt.CSTFromBstore(cs.bs)
state, err := LoadStateTree(cst, stcid)
state, err := state.LoadStateTree(cst, stcid)
if err != nil {
return types.BigInt{}, err
}
@ -692,7 +504,7 @@ func (cs *ChainStore) tipsetContainsMsg(ts *TipSet, msg cid.Cid) (cid.Cid, *type
return cid.Undef, nil, nil
}
func (cs *ChainStore) blockContainsMsg(blk *BlockHeader, msg cid.Cid) (*types.MessageReceipt, error) {
func (cs *ChainStore) blockContainsMsg(blk *types.BlockHeader, msg cid.Cid) (*types.MessageReceipt, error) {
msgs, err := cs.MessageCidsForBlock(blk)
if err != nil {
return nil, err

194
chain/gen/utils.go Normal file
View File

@ -0,0 +1,194 @@
package gen
import (
"context"
"fmt"
actors "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/types"
"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"
sharray "github.com/whyrusleeping/sharray"
)
func init() {
bs := bstore.NewBlockstore(dstore.NewMapDatastore())
cst := hamt.CSTFromBstore(bs)
emptyobject, err := cst.Put(context.TODO(), map[string]string{})
if err != nil {
panic(err)
}
EmptyObjectCid = emptyobject
}
var EmptyObjectCid cid.Cid
type GenesisBootstrap struct {
Genesis *types.BlockHeader
}
func SetupInitActor(bs bstore.Blockstore, addrs []address.Address) (*types.Actor, error) {
var ias actors.InitActorState
ias.NextID = 100
cst := hamt.CSTFromBstore(bs)
amap := hamt.NewNode(cst)
for i, a := range addrs {
if err := amap.Set(context.TODO(), string(a.Bytes()), 100+uint64(i)); err != nil {
return nil, err
}
}
ias.NextID += uint64(len(addrs))
if err := amap.Flush(context.TODO()); err != nil {
return nil, err
}
amapcid, err := cst.Put(context.TODO(), amap)
if err != nil {
return nil, err
}
ias.AddressMap = amapcid
statecid, err := cst.Put(context.TODO(), &ias)
if err != nil {
return nil, err
}
act := &types.Actor{
Code: actors.InitActorCodeCid,
Head: statecid,
}
return act, nil
}
func MakeInitialStateTree(bs bstore.Blockstore, actmap map[address.Address]types.BigInt) (*state.StateTree, error) {
cst := hamt.CSTFromBstore(bs)
state, err := state.NewStateTree(cst)
if err != nil {
return nil, err
}
emptyobject, err := cst.Put(context.TODO(), map[string]string{})
if err != nil {
return nil, err
}
var addrs []address.Address
for a := range actmap {
addrs = append(addrs, a)
}
initact, err := SetupInitActor(bs, addrs)
if err != nil {
return nil, err
}
if err := state.SetActor(actors.InitActorAddress, initact); err != nil {
return nil, err
}
smact, err := SetupStorageMarketActor(bs)
if err != nil {
return nil, err
}
if err := state.SetActor(actors.StorageMarketAddress, smact); err != nil {
return nil, err
}
err = state.SetActor(actors.NetworkAddress, &types.Actor{
Code: actors.AccountActorCodeCid,
Balance: types.NewInt(100000000000),
Head: emptyobject,
})
if err != nil {
return nil, err
}
for a, v := range actmap {
err = state.SetActor(a, &types.Actor{
Code: actors.AccountActorCodeCid,
Balance: v,
Head: emptyobject,
})
if err != nil {
return nil, err
}
}
return state, nil
}
func SetupStorageMarketActor(bs bstore.Blockstore) (*types.Actor, error) {
sms := &actors.StorageMarketState{
Miners: make(map[address.Address]struct{}),
TotalStorage: types.NewInt(0),
}
stcid, err := hamt.CSTFromBstore(bs).Put(context.TODO(), sms)
if err != nil {
return nil, err
}
return &types.Actor{
Code: actors.StorageMarketActorCodeCid,
Head: stcid,
Nonce: 0,
Balance: types.NewInt(0),
}, nil
}
func MakeGenesisBlock(bs bstore.Blockstore, balances map[address.Address]types.BigInt) (*GenesisBootstrap, error) {
fmt.Println("at end of make Genesis block")
state, err := MakeInitialStateTree(bs, balances)
if err != nil {
return nil, err
}
stateroot, err := state.Flush()
if err != nil {
return nil, err
}
cst := hamt.CSTFromBstore(bs)
emptyroot, err := sharray.Build(context.TODO(), 4, []interface{}{}, cst)
if err != nil {
return nil, err
}
fmt.Println("Empty Genesis root: ", emptyroot)
b := &types.BlockHeader{
Miner: actors.InitActorAddress,
Tickets: []types.Ticket{},
ElectionProof: []byte("the Genesis block"),
Parents: []cid.Cid{},
Height: 0,
ParentWeight: types.NewInt(0),
StateRoot: stateroot,
Messages: emptyroot,
MessageReceipts: emptyroot,
}
sb, err := b.ToStorageBlock()
if err != nil {
return nil, err
}
if err := bs.Put(sb); err != nil {
return nil, err
}
return &GenesisBootstrap{
Genesis: b,
}, nil
}

View File

@ -4,6 +4,8 @@ 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/types"
hamt "github.com/ipfs/go-hamt-ipld"
)
@ -16,17 +18,17 @@ type MessagePool struct {
}
type msgSet struct {
msgs map[uint64]*SignedMessage
msgs map[uint64]*types.SignedMessage
startNonce uint64
}
func newMsgSet() *msgSet {
return &msgSet{
msgs: make(map[uint64]*SignedMessage),
msgs: make(map[uint64]*types.SignedMessage),
}
}
func (ms *msgSet) add(m *SignedMessage) {
func (ms *msgSet) add(m *types.SignedMessage) {
if len(ms.msgs) == 0 || m.Message.Nonce < ms.startNonce {
ms.startNonce = m.Message.Nonce
}
@ -43,7 +45,7 @@ func NewMessagePool(cs *ChainStore) *MessagePool {
return mp
}
func (mp *MessagePool) Add(m *SignedMessage) error {
func (mp *MessagePool) Add(m *types.SignedMessage) error {
mp.lk.Lock()
defer mp.lk.Unlock()
@ -86,13 +88,13 @@ func (mp *MessagePool) GetNonce(addr address.Address) (uint64, error) {
head := mp.cs.GetHeaviestTipSet()
state, err := mp.cs.TipSetState(head.Cids())
stc, err := mp.cs.TipSetState(head.Cids())
if err != nil {
return 0, err
}
cst := hamt.CSTFromBstore(mp.cs.bs)
st, err := LoadStateTree(cst, state)
st, err := state.LoadStateTree(cst, stc)
if err != nil {
return 0, err
}
@ -105,7 +107,7 @@ func (mp *MessagePool) GetNonce(addr address.Address) (uint64, error) {
return act.Nonce, nil
}
func (mp *MessagePool) Remove(m *SignedMessage) {
func (mp *MessagePool) Remove(m *types.SignedMessage) {
mp.lk.Lock()
defer mp.lk.Unlock()
@ -123,10 +125,10 @@ func (mp *MessagePool) Remove(m *SignedMessage) {
}
}
func (mp *MessagePool) Pending() []*SignedMessage {
func (mp *MessagePool) Pending() []*types.SignedMessage {
mp.lk.Lock()
defer mp.lk.Unlock()
var out []*SignedMessage
var out []*types.SignedMessage
for _, mset := range mp.pending {
for i := mset.startNonce; true; i++ {
m, ok := mset.msgs[i]

View File

@ -19,7 +19,7 @@ func miningRewardForBlock(base *TipSet) types.BigInt {
return types.NewInt(10000)
}
func MinerCreateBlock(cs *ChainStore, miner address.Address, parents *TipSet, tickets []Ticket, proof ElectionProof, msgs []*SignedMessage) (*FullBlock, error) {
func MinerCreateBlock(cs *ChainStore, miner address.Address, parents *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")
@ -37,7 +37,7 @@ func MinerCreateBlock(cs *ChainStore, miner address.Address, parents *TipSet, ti
return nil, err
}
next := &BlockHeader{
next := &types.BlockHeader{
Miner: miner,
Parents: parents.Cids(),
Tickets: tickets,
@ -100,7 +100,7 @@ func MinerCreateBlock(cs *ChainStore, miner address.Address, parents *TipSet, ti
pweight := cs.Weight(parents)
next.ParentWeight = types.NewInt(pweight)
fullBlock := &FullBlock{
fullBlock := &types.FullBlock{
Header: next,
Messages: msgs,
}

View File

@ -1,4 +1,4 @@
package chain
package state
import (
"context"
@ -6,15 +6,18 @@ 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"
"github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/types"
)
var log = logging.Logger("statetree")
type StateTree struct {
root *hamt.Node
store *hamt.CborIpldStore
Store *hamt.CborIpldStore
actorcache map[address.Address]*types.Actor
snapshot cid.Cid
@ -23,7 +26,7 @@ type StateTree struct {
func NewStateTree(cst *hamt.CborIpldStore) (*StateTree, error) {
return &StateTree{
root: hamt.NewNode(cst),
store: cst,
Store: cst,
actorcache: make(map[address.Address]*types.Actor),
}, nil
}
@ -37,7 +40,7 @@ func LoadStateTree(cst *hamt.CborIpldStore, c cid.Cid) (*StateTree, error) {
return &StateTree{
root: nd,
store: cst,
Store: cst,
actorcache: make(map[address.Address]*types.Actor),
}, nil
}
@ -68,11 +71,11 @@ func (st *StateTree) lookupID(addr address.Address) (address.Address, error) {
}
var ias actors.InitActorState
if err := st.store.Get(context.TODO(), act.Head, &ias); err != nil {
if err := st.Store.Get(context.TODO(), act.Head, &ias); err != nil {
return address.Undef, err
}
return ias.Lookup(st.store, addr)
return ias.Lookup(st.Store, addr)
}
func (st *StateTree) GetActor(addr address.Address) (*types.Actor, error) {
@ -127,7 +130,7 @@ func (st *StateTree) Flush() (cid.Cid, error) {
return cid.Undef, err
}
return st.store.Put(context.TODO(), st.root)
return st.Store.Put(context.TODO(), st.root)
}
func (st *StateTree) Snapshot() error {
@ -144,18 +147,17 @@ func (st *StateTree) RegisterNewAddress(addr address.Address, act *types.Actor)
var out address.Address
err := st.MutateActor(actors.InitActorAddress, func(initact *types.Actor) error {
var ias actors.InitActorState
if err := st.store.Get(context.TODO(), initact.Head, &ias); err != nil {
if err := st.Store.Get(context.TODO(), initact.Head, &ias); err != nil {
return err
}
fvm := &VMContext{cst: st.store}
oaddr, err := ias.AddActor(fvm, addr)
oaddr, err := ias.AddActor(st.Store, addr)
if err != nil {
return err
}
out = oaddr
ncid, err := st.store.Put(context.TODO(), &ias)
ncid, err := st.Store.Put(context.TODO(), &ias)
if err != nil {
return err
}
@ -175,7 +177,7 @@ func (st *StateTree) RegisterNewAddress(addr address.Address, act *types.Actor)
}
func (st *StateTree) Revert() error {
nd, err := hamt.LoadNode(context.Background(), st.store, st.snapshot)
nd, err := hamt.LoadNode(context.Background(), st.Store, st.snapshot)
if err != nil {
return err
}

View File

@ -1,4 +1,4 @@
package chain
package state
import (
"testing"

View File

@ -8,6 +8,7 @@ import (
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/filecoin-project/go-lotus/chain"
"github.com/filecoin-project/go-lotus/chain/types"
)
var log = logging.Logger("sub")
@ -33,7 +34,7 @@ func HandleIncomingBlocks(ctx context.Context, bsub *pubsub.Subscription, s *cha
return
}
fmt.Println("inform new block over pubsub")
s.InformNewBlock(msg.GetFrom(), &chain.FullBlock{
s.InformNewBlock(msg.GetFrom(), &types.FullBlock{
Header: blk.Header,
Messages: msgs,
})
@ -49,7 +50,7 @@ func HandleIncomingMessages(ctx context.Context, mpool *chain.MessagePool, msub
continue
}
m, err := chain.DecodeSignedMessage(msg.GetData())
m, err := types.DecodeSignedMessage(msg.GetData())
if err != nil {
log.Errorf("got incorrectly formatted Message: %s", err)
continue

View File

@ -7,6 +7,8 @@ import (
"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/state"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/ipfs/go-cid"
@ -53,7 +55,7 @@ func NewSyncer(cs *ChainStore, bsync *BlockSync, self peer.ID) (*Syncer, error)
return nil, err
}
gent, err := NewTipSet([]*BlockHeader{gen})
gent, err := NewTipSet([]*types.BlockHeader{gen})
if err != nil {
return nil, err
}
@ -172,11 +174,11 @@ func (syncer *Syncer) GetPeers() []peer.ID {
return out
}
func (syncer *Syncer) InformNewBlock(from peer.ID, blk *FullBlock) {
func (syncer *Syncer) InformNewBlock(from peer.ID, blk *types.FullBlock) {
// TODO: search for other blocks that could form a tipset with this block
// and then send that tipset to InformNewHead
fts := &FullTipSet{Blocks: []*FullBlock{blk}}
fts := &FullTipSet{Blocks: []*types.FullBlock{blk}}
syncer.InformNewHead(from, fts)
}
@ -348,7 +350,7 @@ func copyBlockstore(from, to bstore.Blockstore) error {
return nil
}
func zipTipSetAndMessages(cst *hamt.CborIpldStore, ts *TipSet, messages []*SignedMessage, msgincl [][]int) (*FullTipSet, error) {
func zipTipSetAndMessages(cst *hamt.CborIpldStore, ts *TipSet, messages []*types.SignedMessage, msgincl [][]int) (*FullTipSet, error) {
if len(ts.Blocks()) != len(msgincl) {
return nil, fmt.Errorf("msgincl length didnt match tipset size")
}
@ -357,7 +359,7 @@ func zipTipSetAndMessages(cst *hamt.CborIpldStore, ts *TipSet, messages []*Signe
fts := &FullTipSet{}
for bi, b := range ts.Blocks() {
var msgs []*SignedMessage
var msgs []*types.SignedMessage
var msgCids []interface{}
for _, m := range msgincl[bi] {
msgs = append(msgs, messages[m])
@ -375,7 +377,7 @@ func zipTipSetAndMessages(cst *hamt.CborIpldStore, ts *TipSet, messages []*Signe
return nil, fmt.Errorf("messages didnt match message root in header")
}
fb := &FullBlock{
fb := &types.FullBlock{
Header: b,
Messages: msgs,
}
@ -451,7 +453,7 @@ func (syncer *Syncer) tryLoadFullTipSet(cids []cid.Cid) (*FullTipSet, error) {
return nil, err
}
fb := &FullBlock{
fb := &types.FullBlock{
Header: b,
Messages: messages,
}
@ -463,12 +465,12 @@ func (syncer *Syncer) tryLoadFullTipSet(cids []cid.Cid) (*FullTipSet, error) {
// FullTipSet is an expanded version of the TipSet that contains all the blocks and messages
type FullTipSet struct {
Blocks []*FullBlock
Blocks []*types.FullBlock
tipset *TipSet
cids []cid.Cid
}
func NewFullTipSet(blks []*FullBlock) *FullTipSet {
func NewFullTipSet(blks []*types.FullBlock) *FullTipSet {
return &FullTipSet{
Blocks: blks,
}
@ -493,7 +495,7 @@ func (fts *FullTipSet) TipSet() *TipSet {
return fts.tipset
}
var headers []*BlockHeader
var headers []*types.BlockHeader
for _, b := range fts.Blocks {
headers = append(headers, b.Header)
}
@ -553,7 +555,7 @@ func (syncer *Syncer) ValidateTipSet(fts *FullTipSet) error {
return nil
}
func (syncer *Syncer) ValidateBlock(b *FullBlock) error {
func (syncer *Syncer) ValidateBlock(b *types.FullBlock) error {
h := b.Header
stateroot, err := syncer.store.TipSetState(h.Parents)
if err != nil {
@ -619,7 +621,7 @@ func DepositFunds(act *types.Actor, amt types.BigInt) {
act.Balance = types.BigAdd(act.Balance, amt)
}
func TryCreateAccountActor(st *StateTree, addr address.Address) (*types.Actor, error) {
func TryCreateAccountActor(st *state.StateTree, addr address.Address) (*types.Actor, error) {
act, err := makeActor(st, addr)
if err != nil {
return nil, err
@ -633,7 +635,7 @@ func TryCreateAccountActor(st *StateTree, addr address.Address) (*types.Actor, e
return act, nil
}
func makeActor(st *StateTree, addr address.Address) (*types.Actor, error) {
func makeActor(st *state.StateTree, addr address.Address) (*types.Actor, error) {
switch addr.Protocol() {
case address.BLS:
return NewBLSAccountActor(st, addr)
@ -648,11 +650,11 @@ func makeActor(st *StateTree, addr address.Address) (*types.Actor, error) {
}
}
func NewBLSAccountActor(st *StateTree, addr address.Address) (*types.Actor, error) {
func NewBLSAccountActor(st *state.StateTree, addr address.Address) (*types.Actor, error) {
var acstate actors.AccountActorState
acstate.Address = addr
c, err := st.store.Put(context.TODO(), acstate)
c, err := st.Store.Put(context.TODO(), acstate)
if err != nil {
return nil, err
}
@ -666,11 +668,11 @@ func NewBLSAccountActor(st *StateTree, addr address.Address) (*types.Actor, erro
return nact, nil
}
func NewSecp256k1AccountActor(st *StateTree, addr address.Address) (*types.Actor, error) {
func NewSecp256k1AccountActor(st *state.StateTree, addr address.Address) (*types.Actor, error) {
nact := &types.Actor{
Code: actors.AccountActorCodeCid,
Balance: types.NewInt(0),
Head: EmptyObjectCid,
Head: gen.EmptyObjectCid,
}
return nact, nil

View File

@ -8,10 +8,7 @@ import (
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
ipld "github.com/ipfs/go-ipld-format"
"github.com/multiformats/go-multihash"
"github.com/polydawn/refmt/obj/atlas"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/types"
)
@ -19,205 +16,11 @@ func init() {
ipld.Register(0x1f, IpldDecode)
cbor.RegisterCborType(BlockMsg{})
///*
//*/
cbor.RegisterCborType(atlas.BuildEntry(SignedMessage{}).UseTag(45).Transform().
TransformMarshal(atlas.MakeMarshalTransformFunc(
func(sm SignedMessage) ([]interface{}, error) {
return []interface{}{
sm.Message,
sm.Signature,
}, nil
})).
TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(
func(x []interface{}) (SignedMessage, error) {
sigb, ok := x[1].([]byte)
if !ok {
return SignedMessage{}, fmt.Errorf("signature in signed message was not bytes")
}
sig, err := types.SignatureFromBytes(sigb)
if err != nil {
return SignedMessage{}, err
}
return SignedMessage{
Message: x[0].(types.Message),
Signature: sig,
}, nil
})).
Complete())
cbor.RegisterCborType(atlas.BuildEntry(BlockHeader{}).UseTag(43).Transform().
TransformMarshal(atlas.MakeMarshalTransformFunc(
func(blk BlockHeader) ([]interface{}, error) {
if blk.Tickets == nil {
blk.Tickets = []Ticket{}
}
if blk.Parents == nil {
blk.Parents = []cid.Cid{}
}
return []interface{}{
blk.Miner.Bytes(),
blk.Tickets,
blk.ElectionProof,
blk.Parents,
blk.ParentWeight,
blk.Height,
blk.StateRoot,
blk.Messages,
blk.MessageReceipts,
}, nil
})).
TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(
func(arr []interface{}) (BlockHeader, error) {
miner, err := address.NewFromBytes(arr[0].([]byte))
if err != nil {
return BlockHeader{}, err
}
tickets := []Ticket{}
ticketarr, _ := arr[1].([]interface{})
for _, t := range ticketarr {
tickets = append(tickets, Ticket(t.([]byte)))
}
electionProof, _ := arr[2].([]byte)
parents := []cid.Cid{}
parentsArr, _ := arr[3].([]interface{})
for _, p := range parentsArr {
parents = append(parents, p.(cid.Cid))
}
parentWeight := arr[4].(types.BigInt)
height := arr[5].(uint64)
stateRoot := arr[6].(cid.Cid)
msgscid := arr[7].(cid.Cid)
recscid := arr[8].(cid.Cid)
return BlockHeader{
Miner: miner,
Tickets: tickets,
ElectionProof: electionProof,
Parents: parents,
ParentWeight: parentWeight,
Height: height,
StateRoot: stateRoot,
Messages: msgscid,
MessageReceipts: recscid,
}, nil
})).
Complete())
}
type BlockHeader struct {
Miner address.Address
Tickets []Ticket
ElectionProof []byte
Parents []cid.Cid
ParentWeight types.BigInt
Height uint64
StateRoot cid.Cid
Messages cid.Cid
BLSAggregate types.Signature
MessageReceipts cid.Cid
}
func (b *BlockHeader) ToStorageBlock() (block.Block, error) {
data, err := b.Serialize()
if err != nil {
return nil, err
}
pref := cid.NewPrefixV1(0x1f, multihash.BLAKE2B_MIN+31)
c, err := pref.Sum(data)
if err != nil {
return nil, err
}
return block.NewBlockWithCid(data, c)
}
func (b *BlockHeader) Cid() cid.Cid {
sb, err := b.ToStorageBlock()
if err != nil {
panic(err)
}
return sb.Cid()
}
func DecodeBlock(b []byte) (*BlockHeader, error) {
var blk BlockHeader
if err := cbor.DecodeInto(b, &blk); err != nil {
return nil, err
}
return &blk, nil
}
func (blk *BlockHeader) Serialize() ([]byte, error) {
return cbor.DumpObject(blk)
}
func (m *SignedMessage) ToStorageBlock() (block.Block, error) {
data, err := m.Serialize()
if err != nil {
return nil, err
}
pref := cid.NewPrefixV1(0x1f, multihash.BLAKE2B_MIN+31)
c, err := pref.Sum(data)
if err != nil {
return nil, err
}
return block.NewBlockWithCid(data, c)
}
func (m *SignedMessage) Cid() cid.Cid {
sb, err := m.ToStorageBlock()
if err != nil {
panic(err)
}
return sb.Cid()
}
type SignedMessage struct {
Message types.Message
Signature types.Signature
}
func DecodeSignedMessage(data []byte) (*SignedMessage, error) {
var msg SignedMessage
if err := cbor.DecodeInto(data, &msg); err != nil {
return nil, err
}
return &msg, nil
}
func (sm *SignedMessage) Serialize() ([]byte, error) {
data, err := cbor.DumpObject(sm)
if err != nil {
return nil, err
}
return data, nil
}
type TipSet struct {
cids []cid.Cid
blks []*BlockHeader
blks []*types.BlockHeader
height uint64
}
@ -225,7 +28,7 @@ type TipSet struct {
// same names already
type expTipSet struct {
Cids []cid.Cid
Blocks []*BlockHeader
Blocks []*types.BlockHeader
Height uint64
}
@ -249,7 +52,7 @@ func (ts *TipSet) UnmarshalJSON(b []byte) error {
return nil
}
func NewTipSet(blks []*BlockHeader) (*TipSet, error) {
func NewTipSet(blks []*types.BlockHeader) (*TipSet, error) {
var ts TipSet
ts.cids = []cid.Cid{blks[0].Cid()}
ts.blks = blks
@ -280,7 +83,7 @@ func (ts *TipSet) Parents() []cid.Cid {
return ts.blks[0].Parents
}
func (ts *TipSet) Blocks() []*BlockHeader {
func (ts *TipSet) Blocks() []*types.BlockHeader {
return ts.blks
}
@ -298,9 +101,6 @@ func (ts *TipSet) Equals(ots *TipSet) bool {
return true
}
type Ticket []byte
type ElectionProof []byte
func IpldDecode(block block.Block) (ipld.Node, error) {
var i interface{}
if err := cbor.DecodeInto(block.RawData(), &i); err != nil {
@ -317,9 +117,9 @@ type filecoinIpldNode struct {
func (f *filecoinIpldNode) Cid() cid.Cid {
switch t := f.val.(type) {
case BlockHeader:
case types.BlockHeader:
return t.Cid()
case SignedMessage:
case types.SignedMessage:
return t.Cid()
default:
panic("whats going on")
@ -332,7 +132,7 @@ func (f *filecoinIpldNode) Copy() ipld.Node {
func (f *filecoinIpldNode) Links() []*ipld.Link {
switch t := f.val.(type) {
case BlockHeader:
case types.BlockHeader:
fmt.Println("block links!", t.StateRoot)
return []*ipld.Link{
{
@ -392,13 +192,13 @@ func (f *filecoinIpldNode) Loggable() map[string]interface{} {
func (f *filecoinIpldNode) RawData() []byte {
switch t := f.val.(type) {
case BlockHeader:
case types.BlockHeader:
sb, err := t.ToStorageBlock()
if err != nil {
panic(err)
}
return sb.RawData()
case SignedMessage:
case types.SignedMessage:
sb, err := t.ToStorageBlock()
if err != nil {
panic(err)
@ -413,17 +213,8 @@ func (f *filecoinIpldNode) String() string {
return "cats"
}
type FullBlock struct {
Header *BlockHeader
Messages []*SignedMessage
}
func (fb *FullBlock) Cid() cid.Cid {
return fb.Header.Cid()
}
type BlockMsg struct {
Header *BlockHeader
Header *types.BlockHeader
Messages []cid.Cid
}

136
chain/types/blockheader.go Normal file
View File

@ -0,0 +1,136 @@
package types
import (
block "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/multiformats/go-multihash"
"github.com/polydawn/refmt/obj/atlas"
"github.com/filecoin-project/go-lotus/chain/address"
)
func init() {
cbor.RegisterCborType(atlas.BuildEntry(BlockHeader{}).UseTag(43).Transform().
TransformMarshal(atlas.MakeMarshalTransformFunc(
func(blk BlockHeader) ([]interface{}, error) {
if blk.Tickets == nil {
blk.Tickets = []Ticket{}
}
if blk.Parents == nil {
blk.Parents = []cid.Cid{}
}
return []interface{}{
blk.Miner.Bytes(),
blk.Tickets,
blk.ElectionProof,
blk.Parents,
blk.ParentWeight,
blk.Height,
blk.StateRoot,
blk.Messages,
blk.MessageReceipts,
}, nil
})).
TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(
func(arr []interface{}) (BlockHeader, error) {
miner, err := address.NewFromBytes(arr[0].([]byte))
if err != nil {
return BlockHeader{}, err
}
tickets := []Ticket{}
ticketarr, _ := arr[1].([]interface{})
for _, t := range ticketarr {
tickets = append(tickets, Ticket(t.([]byte)))
}
electionProof, _ := arr[2].([]byte)
parents := []cid.Cid{}
parentsArr, _ := arr[3].([]interface{})
for _, p := range parentsArr {
parents = append(parents, p.(cid.Cid))
}
parentWeight := arr[4].(BigInt)
height := arr[5].(uint64)
stateRoot := arr[6].(cid.Cid)
msgscid := arr[7].(cid.Cid)
recscid := arr[8].(cid.Cid)
return BlockHeader{
Miner: miner,
Tickets: tickets,
ElectionProof: electionProof,
Parents: parents,
ParentWeight: parentWeight,
Height: height,
StateRoot: stateRoot,
Messages: msgscid,
MessageReceipts: recscid,
}, nil
})).
Complete())
}
type Ticket []byte
type ElectionProof []byte
type BlockHeader struct {
Miner address.Address
Tickets []Ticket
ElectionProof []byte
Parents []cid.Cid
ParentWeight BigInt
Height uint64
StateRoot cid.Cid
Messages cid.Cid
BLSAggregate Signature
MessageReceipts cid.Cid
}
func (b *BlockHeader) ToStorageBlock() (block.Block, error) {
data, err := b.Serialize()
if err != nil {
return nil, err
}
pref := cid.NewPrefixV1(0x1f, multihash.BLAKE2B_MIN+31)
c, err := pref.Sum(data)
if err != nil {
return nil, err
}
return block.NewBlockWithCid(data, c)
}
func (b *BlockHeader) Cid() cid.Cid {
sb, err := b.ToStorageBlock()
if err != nil {
panic(err)
}
return sb.Cid()
}
func DecodeBlock(b []byte) (*BlockHeader, error) {
var blk BlockHeader
if err := cbor.DecodeInto(b, &blk); err != nil {
return nil, err
}
return &blk, nil
}
func (blk *BlockHeader) Serialize() ([]byte, error) {
return cbor.DumpObject(blk)
}

12
chain/types/fullblock.go Normal file
View File

@ -0,0 +1,12 @@
package types
import "github.com/ipfs/go-cid"
type FullBlock struct {
Header *BlockHeader
Messages []*SignedMessage
}
func (fb *FullBlock) Cid() cid.Cid {
return fb.Header.Cid()
}

View File

@ -0,0 +1,86 @@
package types
import (
"fmt"
block "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/multiformats/go-multihash"
"github.com/polydawn/refmt/obj/atlas"
)
func init() {
cbor.RegisterCborType(atlas.BuildEntry(SignedMessage{}).UseTag(45).Transform().
TransformMarshal(atlas.MakeMarshalTransformFunc(
func(sm SignedMessage) ([]interface{}, error) {
return []interface{}{
sm.Message,
sm.Signature,
}, nil
})).
TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(
func(x []interface{}) (SignedMessage, error) {
sigb, ok := x[1].([]byte)
if !ok {
return SignedMessage{}, fmt.Errorf("signature in signed message was not bytes")
}
sig, err := SignatureFromBytes(sigb)
if err != nil {
return SignedMessage{}, err
}
return SignedMessage{
Message: x[0].(Message),
Signature: sig,
}, nil
})).
Complete())
}
func (m *SignedMessage) ToStorageBlock() (block.Block, error) {
data, err := m.Serialize()
if err != nil {
return nil, err
}
pref := cid.NewPrefixV1(0x1f, multihash.BLAKE2B_MIN+31)
c, err := pref.Sum(data)
if err != nil {
return nil, err
}
return block.NewBlockWithCid(data, c)
}
func (m *SignedMessage) Cid() cid.Cid {
sb, err := m.ToStorageBlock()
if err != nil {
panic(err)
}
return sb.Cid()
}
type SignedMessage struct {
Message Message
Signature Signature
}
func DecodeSignedMessage(data []byte) (*SignedMessage, error) {
var msg SignedMessage
if err := cbor.DecodeInto(data, &msg); err != nil {
return nil, err
}
return &msg, nil
}
func (sm *SignedMessage) Serialize() ([]byte, error) {
data, err := cbor.DumpObject(sm)
if err != nil {
return nil, err
}
return data, nil
}

View File

@ -11,7 +11,7 @@ import (
func TestSignedMessageJsonRoundtrip(t *testing.T) {
to, _ := address.NewIDAddress(5234623)
from, _ := address.NewIDAddress(603911192)
smsg := &SignedMessage{
smsg := &types.SignedMessage{
Message: types.Message{
To: to,
From: from,
@ -29,7 +29,7 @@ func TestSignedMessageJsonRoundtrip(t *testing.T) {
t.Fatal(err)
}
var osmsg SignedMessage
var osmsg types.SignedMessage
if err := json.Unmarshal(out, &osmsg); err != nil {
t.Fatal(err)
}

View File

@ -7,6 +7,7 @@ import (
"github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/actors/aerrors"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/state"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/filecoin-project/go-lotus/lib/bufbstore"
@ -20,7 +21,7 @@ import (
type VMContext struct {
vm *VM
state *StateTree
state *state.StateTree
msg *types.Message
height uint64
cst *hamt.CborIpldStore
@ -152,7 +153,7 @@ func (vm *VM) makeVMContext(sroot cid.Cid, origin address.Address, msg *types.Me
}
type VM struct {
cstate *StateTree
cstate *state.StateTree
base cid.Cid
cs *ChainStore
buf *bufbstore.BufferedBS
@ -164,7 +165,7 @@ type VM struct {
func NewVM(base cid.Cid, height uint64, maddr address.Address, cs *ChainStore) (*VM, error) {
buf := bufbstore.NewBufferedBstore(cs.bs)
cst := hamt.CSTFromBstore(buf)
state, err := LoadStateTree(cst, base)
state, err := state.LoadStateTree(cst, base)
if err != nil {
return nil, err
}

View File

@ -6,7 +6,7 @@ import (
"gopkg.in/urfave/cli.v2"
"github.com/filecoin-project/go-lotus/chain"
types "github.com/filecoin-project/go-lotus/chain/types"
cid "github.com/ipfs/go-cid"
)
@ -87,8 +87,8 @@ var chainGetBlock = &cli.Command{
}
cblock := struct {
chain.BlockHeader
Messages []*chain.SignedMessage
types.BlockHeader
Messages []*types.SignedMessage
}{}
cblock.BlockHeader = *blk

View File

@ -7,7 +7,6 @@ import (
"golang.org/x/xerrors"
"gopkg.in/urfave/cli.v2"
"github.com/filecoin-project/go-lotus/chain"
actors "github.com/filecoin-project/go-lotus/chain/actors"
address "github.com/filecoin-project/go-lotus/chain/address"
types "github.com/filecoin-project/go-lotus/chain/types"
@ -94,7 +93,7 @@ var createMinerCmd = &cli.Command{
return xerrors.Errorf("failed to sign message: %w", err)
}
smsg := &chain.SignedMessage{
smsg := &types.SignedMessage{
Message: msg,
Signature: *sig,
}

3
go.mod
View File

@ -59,6 +59,7 @@ require (
github.com/pkg/errors v0.8.1
github.com/polydawn/refmt v0.0.0-20190408063855-01bf1e26dd14
github.com/stretchr/testify v1.3.0
github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7
github.com/whyrusleeping/pubsub v0.0.0-20131020042734-02de8aa2db3d
github.com/whyrusleeping/sharray v0.0.0-20190718051354-e41931821e33
@ -69,7 +70,7 @@ require (
go4.org v0.0.0-20190313082347-94abd6928b1d // indirect
golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
google.golang.org/appengine v1.4.0 // indirect
google.golang.org/appengine v1.4.0
gopkg.in/urfave/cli.v2 v2.0.0-20180128182452-d3ae77c26ac8
launchpad.net/gocheck v0.0.0-20140225173054-000000000087 // indirect
)

2
go.sum
View File

@ -456,6 +456,7 @@ github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f h1:jQa4QT2UP
github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f/go.mod h1:p9UJB6dDgdPgMJZs7UjUOdulKyRr9fqkS+6JKAInPy8=
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 h1:EKhdznlJHPMoKr0XTrX+IlJs1LH3lyx2nfr1dOlZ79k=
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1/go.mod h1:8UvriyWtv5Q5EOgjHaSseUEdkQfvwFv1I/In/O2M9gc=
github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc h1:9lDbC6Rz4bwmou+oE6Dt4Cb2BGMur5eR/GYptkKUVHo=
github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM=
github.com/whyrusleeping/go-notifier v0.0.0-20170827234753-097c5d47330f h1:M/lL30eFZTKnomXY6huvM6G0+gVquFNf6mxghaWlFUg=
github.com/whyrusleeping/go-notifier v0.0.0-20170827234753-097c5d47330f/go.mod h1:cZNvX9cFybI01GriPRMXDtczuvUhgbcYr9iCGaNlRv8=
@ -561,6 +562,7 @@ golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=

View File

@ -9,6 +9,7 @@ import (
chain "github.com/filecoin-project/go-lotus/chain"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/types"
)
var log = logging.Logger("miner")
@ -18,7 +19,7 @@ type api interface {
// returns a set of messages that havent been included in the chain as of
// the given tipset
MpoolPending(ctx context.Context, base *chain.TipSet) ([]*chain.SignedMessage, error)
MpoolPending(ctx context.Context, base *chain.TipSet) ([]*types.SignedMessage, error)
// Returns the best tipset for the miner to mine on top of.
// TODO: Not sure this feels right (including the messages api). Miners
@ -33,7 +34,7 @@ type api interface {
// it seems realllllly annoying to do all the actions necessary to build a
// block through the API. so, we just add the block creation to the API
// now, all the 'miner' does is check if they win, and call create block
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)
}
func NewMiner(api api, addr address.Address) *Miner {
@ -79,7 +80,7 @@ func (m *Miner) Mine(ctx context.Context) {
type MiningBase struct {
ts *chain.TipSet
tickets []chain.Ticket
tickets []types.Ticket
}
func (m *Miner) GetBestMiningCandidate() (*MiningBase, error) {
@ -129,12 +130,12 @@ func (m *Miner) mineOne(ctx context.Context, base *MiningBase) (*chain.BlockMsg,
return b, nil
}
func (m *Miner) submitNullTicket(base *MiningBase, ticket chain.Ticket) {
func (m *Miner) submitNullTicket(base *MiningBase, ticket types.Ticket) {
base.tickets = append(base.tickets, ticket)
m.lastWork = base
}
func (m *Miner) isWinnerNextRound(base *MiningBase) (bool, chain.ElectionProof, error) {
func (m *Miner) isWinnerNextRound(base *MiningBase) (bool, types.ElectionProof, error) {
r, err := m.api.ChainGetRandomness(context.TODO(), base.ts)
if err != nil {
return false, nil, err
@ -145,7 +146,7 @@ func (m *Miner) isWinnerNextRound(base *MiningBase) (bool, chain.ElectionProof,
return true, []byte("election prooooof"), nil
}
func (m *Miner) scratchTicket(ctx context.Context, base *MiningBase) (chain.Ticket, error) {
func (m *Miner) scratchTicket(ctx context.Context, base *MiningBase) (types.Ticket, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
@ -155,7 +156,7 @@ func (m *Miner) scratchTicket(ctx context.Context, base *MiningBase) (chain.Tick
return []byte("this is a ticket"), nil
}
func (m *Miner) createBlock(base *MiningBase, ticket chain.Ticket, proof chain.ElectionProof) (*chain.BlockMsg, error) {
func (m *Miner) createBlock(base *MiningBase, ticket types.Ticket, proof types.ElectionProof) (*chain.BlockMsg, error) {
pending, err := m.api.MpoolPending(context.TODO(), base.ts)
if err != nil {
@ -168,7 +169,7 @@ func (m *Miner) createBlock(base *MiningBase, ticket chain.Ticket, proof chain.E
return m.api.MinerCreateBlock(context.TODO(), m.address, base.ts, append(base.tickets, ticket), proof, msgs)
}
func (m *Miner) selectMessages(msgs []*chain.SignedMessage) []*chain.SignedMessage {
func (m *Miner) selectMessages(msgs []*types.SignedMessage) []*types.SignedMessage {
// TODO: filter and select 'best' message if too many to fit in one block
return msgs
}

View File

@ -55,11 +55,11 @@ func (a *FullNodeAPI) ChainWaitMsg(ctx context.Context, msg cid.Cid) (*api.MsgWa
panic("TODO")
}
func (a *FullNodeAPI) ChainGetBlock(ctx context.Context, msg cid.Cid) (*chain.BlockHeader, error) {
func (a *FullNodeAPI) ChainGetBlock(ctx context.Context, msg cid.Cid) (*types.BlockHeader, error) {
return a.Chain.GetBlock(msg)
}
func (a *FullNodeAPI) ChainGetBlockMessages(ctx context.Context, msg cid.Cid) ([]*chain.SignedMessage, error) {
func (a *FullNodeAPI) ChainGetBlockMessages(ctx context.Context, msg cid.Cid) ([]*types.SignedMessage, error) {
b, err := a.Chain.GetBlock(msg)
if err != nil {
return nil, err
@ -68,13 +68,13 @@ func (a *FullNodeAPI) ChainGetBlockMessages(ctx context.Context, msg cid.Cid) ([
return a.Chain.MessagesForBlock(b)
}
func (a *FullNodeAPI) MpoolPending(ctx context.Context, ts *chain.TipSet) ([]*chain.SignedMessage, error) {
func (a *FullNodeAPI) MpoolPending(ctx context.Context, ts *chain.TipSet) ([]*types.SignedMessage, error) {
// TODO: need to make sure we don't return messages that were already included in the referenced chain
// also need to accept ts == nil just fine, assume nil == chain.Head()
return a.Mpool.Pending(), nil
}
func (a *FullNodeAPI) MpoolPush(ctx context.Context, smsg *chain.SignedMessage) error {
func (a *FullNodeAPI) MpoolPush(ctx context.Context, smsg *types.SignedMessage) error {
msgb, err := smsg.Serialize()
if err != nil {
return err
@ -96,7 +96,7 @@ func (a *FullNodeAPI) MinerStart(ctx context.Context, addr address.Address) erro
return nil
}
func (a *FullNodeAPI) MinerCreateBlock(ctx context.Context, addr address.Address, parents *chain.TipSet, tickets []chain.Ticket, proof chain.ElectionProof, msgs []*chain.SignedMessage) (*chain.BlockMsg, error) {
func (a *FullNodeAPI) MinerCreateBlock(ctx context.Context, addr address.Address, parents *chain.TipSet, tickets []types.Ticket, proof types.ElectionProof, msgs []*types.SignedMessage) (*chain.BlockMsg, error) {
fblk, err := chain.MinerCreateBlock(a.Chain, addr, parents, tickets, proof, msgs)
if err != nil {
return nil, err

View File

@ -38,7 +38,7 @@ import (
var log = logging.Logger("modules")
type Genesis func() (*chain.BlockHeader, error)
type Genesis func() (*types.BlockHeader, error)
// RecordValidator provides namesys compatible routing record validator
func RecordValidator(ps peerstore.Peerstore) record.Validator {
@ -192,14 +192,14 @@ func ChainStore(lc fx.Lifecycle, bs blockstore.Blockstore, ds datastore.Batching
}
func ErrorGenesis() Genesis {
return func() (header *chain.BlockHeader, e error) {
return func() (header *types.BlockHeader, e error) {
return nil, xerrors.New("No genesis block provided, provide the file with 'lotus daemon --genesis=[genesis file]'")
}
}
func LoadGenesis(genBytes []byte) func(blockstore.Blockstore) Genesis {
return func(bs blockstore.Blockstore) Genesis {
return func() (header *chain.BlockHeader, e error) {
return func() (header *types.BlockHeader, e error) {
c, err := car.LoadCar(bs, bytes.NewReader(genBytes))
if err != nil {
return nil, err
@ -209,10 +209,10 @@ func LoadGenesis(genBytes []byte) func(blockstore.Blockstore) Genesis {
}
root, err := bs.Get(c.Roots[0])
if err != nil {
return &chain.BlockHeader{}, err
return &types.BlockHeader{}, err
}
return chain.DecodeBlock(root.RawData())
return types.DecodeBlock(root.RawData())
}
}
}

View File

@ -14,6 +14,8 @@ import (
"github.com/ipfs/go-merkledag"
"github.com/filecoin-project/go-lotus/chain"
"github.com/filecoin-project/go-lotus/chain/gen"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/filecoin-project/go-lotus/node/modules"
)
@ -21,9 +23,10 @@ var glog = logging.Logger("genesis")
func MakeGenesisMem(out io.Writer) func(bs blockstore.Blockstore, w *chain.Wallet) modules.Genesis {
return func(bs blockstore.Blockstore, w *chain.Wallet) modules.Genesis {
return func() (*chain.BlockHeader, error) {
return func() (*types.BlockHeader, error) {
glog.Warn("Generating new random genesis block, note that this SHOULD NOT happen unless you are setting up new network")
b, err := chain.MakeGenesisBlock(bs, w)
// TODO: make an address allocation
b, err := gen.MakeGenesisBlock(bs, nil)
if err != nil {
return nil, err
}
@ -42,9 +45,10 @@ func MakeGenesisMem(out io.Writer) func(bs blockstore.Blockstore, w *chain.Walle
func MakeGenesis(outFile string) func(bs blockstore.Blockstore, w *chain.Wallet) modules.Genesis {
return func(bs blockstore.Blockstore, w *chain.Wallet) modules.Genesis {
return func() (*chain.BlockHeader, error) {
return func() (*types.BlockHeader, error) {
glog.Warn("Generating new random genesis block, note that this SHOULD NOT happen unless you are setting up new network")
b, err := chain.MakeGenesisBlock(bs, w)
// TODO: make an address allocation
b, err := gen.MakeGenesisBlock(bs, nil)
if err != nil {
return nil, err
}