Skip to content

Commit

Permalink
try to get a chain/gen test to pass, unsuccessfully
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Jul 26, 2019
1 parent 28a4b9d commit a30dd41
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 27 deletions.
61 changes: 56 additions & 5 deletions chain/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import (
"github.com/filecoin-project/go-lotus/chain/store"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/filecoin-project/go-lotus/chain/wallet"
"github.com/ipfs/go-datastore"
"github.com/filecoin-project/go-lotus/node/repo"
block "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
blockstore "github.com/ipfs/go-ipfs-blockstore"
logging "github.com/ipfs/go-log"
)

var log = logging.Logger("gen")

type ChainGen struct {
accounts []address.Address

Expand All @@ -25,15 +30,57 @@ type ChainGen struct {
miner address.Address
}

type mybs struct {
blockstore.Blockstore
}

func (m mybs) Get(c cid.Cid) (block.Block, error) {
b, err := m.Blockstore.Get(c)
if err != nil {
log.Errorf("Get failed: %s %s", c, err)
return nil, err
}

return b, nil
}

func NewGenerator() (*ChainGen, error) {
ds := datastore.NewMapDatastore()
bs := blockstore.NewBlockstore(ds)
genb, err := MakeGenesisBlock(bs, nil)

mr := repo.NewMemory(nil)
lr, err := mr.Lock()
if err != nil {
return nil, err
}

ds, err := lr.Datastore("/blocks")
if err != nil {
return nil, err
}
bs := mybs{blockstore.NewBlockstore(ds)}

w, err := wallet.NewWallet()
ks, err := lr.KeyStore()
if err != nil {
return nil, err
}

w, err := wallet.NewWallet(ks)
if err != nil {
return nil, err
}

miner, err := w.GenerateKey(types.KTBLS)
if err != nil {
return nil, err
}

banker, err := w.GenerateKey(types.KTBLS)
if err != nil {
return nil, err
}

genb, err := MakeGenesisBlock(bs, map[address.Address]types.BigInt{
banker: types.NewInt(90000000),
})
if err != nil {
return nil, err
}
Expand All @@ -42,11 +89,15 @@ func NewGenerator() (*ChainGen, error) {

msgsPerBlock := 10

genfb := &types.FullBlock{Header: genb.Genesis}

gen := &ChainGen{
bs: bs,
cs: cs,
msgsPerBlock: msgsPerBlock,
genesis: genb.Genesis,
miner: miner,
curBlock: genfb,
}

return gen, nil
Expand Down
13 changes: 8 additions & 5 deletions chain/gen/gen_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gen

import (
"fmt"
"testing"
)

Expand All @@ -11,10 +10,14 @@ func TestChainGeneration(t *testing.T) {
t.Fatal(err)
}

b, err := g.NextBlock()
if err != nil {
t.Fatal(err)
for i := 0; i < 10; i++ {
b, err := g.NextBlock()
if err != nil {
t.Fatal(err)
}
if b.Header.Height != uint64(i+1) {
t.Fatal("wrong height")
}
}

fmt.Println(b)
}
8 changes: 4 additions & 4 deletions chain/gen/mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ func MinerCreateBlock(cs *store.ChainStore, miner address.Address, parents *type

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

vm, err := vm.NewVM(st, height, miner, cs)
vmi, err := vm.NewVM(st, height, miner, cs)
if err != nil {
return nil, err
}

// apply miner reward
if err := vm.TransferFunds(actors.NetworkAddress, miner, vm.MiningRewardForBlock(parents)); err != nil {
if err := vmi.TransferFunds(actors.NetworkAddress, miner, vm.MiningRewardForBlock(parents)); err != nil {
return nil, err
}

Expand Down Expand Up @@ -59,7 +59,7 @@ func MinerCreateBlock(cs *store.ChainStore, miner address.Address, parents *type
} else {
msgCids = append(msgCids, msg.Cid())
}
rec, err := vm.ApplyMessage(&msg.Message)
rec, err := vmi.ApplyMessage(&msg.Message)
if err != nil {
return nil, errors.Wrap(err, "apply message failure")
}
Expand All @@ -80,7 +80,7 @@ func MinerCreateBlock(cs *store.ChainStore, miner address.Address, parents *type
}
next.MessageReceipts = rectroot

stateRoot, err := vm.Flush(context.TODO())
stateRoot, err := vmi.Flush(context.TODO())
if err != nil {
return nil, errors.Wrap(err, "flushing state tree failed")
}
Expand Down
8 changes: 4 additions & 4 deletions chain/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,18 +526,18 @@ func (syncer *Syncer) ValidateBlock(b *types.FullBlock) error {
return err
}

vm, err := vm.NewVM(stateroot, b.Header.Height, b.Header.Miner, syncer.store)
vmi, err := vm.NewVM(stateroot, b.Header.Height, b.Header.Miner, syncer.store)
if err != nil {
return err
}

if err := vm.TransferFunds(actors.NetworkAddress, b.Header.Miner, vm.MiningRewardForBlock(baseTs)); err != nil {
if err := vmi.TransferFunds(actors.NetworkAddress, b.Header.Miner, vm.MiningRewardForBlock(baseTs)); err != nil {
return err
}

var receipts []interface{}
for _, m := range b.Messages {
receipt, err := vm.ApplyMessage(&m.Message)
receipt, err := vmi.ApplyMessage(&m.Message)
if err != nil {
return err
}
Expand All @@ -554,7 +554,7 @@ func (syncer *Syncer) ValidateBlock(b *types.FullBlock) error {
return fmt.Errorf("receipts mismatched")
}

final, err := vm.Flush(context.TODO())
final, err := vmi.Flush(context.TODO())
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions chain/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ func Copy(ctx context.Context, from, to ipld.DAGService, root cid.Cid) error {
}
links := node.Links()
for _, link := range links {
if link.Cid.Prefix().MhType == 0 {
continue
}
_, err := to.Get(ctx, link.Cid)
switch err {
default:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ require (
golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect
golang.org/x/sys v0.0.0-20190726002231-94b544f455ef // 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
)
Expand Down
3 changes: 2 additions & 1 deletion node/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/filecoin-project/go-lotus/chain"
"github.com/filecoin-project/go-lotus/chain/store"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/filecoin-project/go-lotus/chain/wallet"
"github.com/filecoin-project/go-lotus/node/config"
"github.com/filecoin-project/go-lotus/node/hello"
"github.com/filecoin-project/go-lotus/node/impl"
Expand Down Expand Up @@ -194,7 +195,7 @@ func Online() Option {
// Filecoin services
Override(new(*chain.Syncer), chain.NewSyncer),
Override(new(*chain.BlockSync), chain.NewBlockSyncClient),
Override(new(*chain.Wallet), chain.NewWallet),
Override(new(*wallet.Wallet), wallet.NewWallet),
Override(new(*chain.MessagePool), chain.NewMessagePool),

Override(new(modules.Genesis), modules.ErrorGenesis),
Expand Down
6 changes: 4 additions & 2 deletions node/impl/full.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/chain"
"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/wallet"
"github.com/filecoin-project/go-lotus/miner"
"github.com/filecoin-project/go-lotus/node/client"

Expand All @@ -28,7 +30,7 @@ type FullNodeAPI struct {
Chain *store.ChainStore
PubSub *pubsub.PubSub
Mpool *chain.MessagePool
Wallet *chain.Wallet
Wallet *wallet.Wallet
}

func (a *FullNodeAPI) ChainSubmitBlock(ctx context.Context, blk *chain.BlockMsg) error {
Expand Down Expand Up @@ -100,7 +102,7 @@ func (a *FullNodeAPI) MinerStart(ctx context.Context, addr address.Address) erro
}

func (a *FullNodeAPI) MinerCreateBlock(ctx context.Context, addr address.Address, parents *types.TipSet, tickets []types.Ticket, proof types.ElectionProof, msgs []*types.SignedMessage) (*chain.BlockMsg, error) {
fblk, err := chain.MinerCreateBlock(a.Chain, addr, parents, tickets, proof, msgs)
fblk, err := gen.MinerCreateBlock(a.Chain, addr, parents, tickets, proof, msgs)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions node/modules/testing/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import (
logging "github.com/ipfs/go-log"
"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/chain/wallet"
"github.com/filecoin-project/go-lotus/node/modules"
)

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 {
func MakeGenesisMem(out io.Writer) func(bs blockstore.Blockstore, w *wallet.Wallet) modules.Genesis {
return func(bs blockstore.Blockstore, w *wallet.Wallet) modules.Genesis {
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")
// TODO: make an address allocation
Expand All @@ -43,8 +43,8 @@ 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 {
func MakeGenesis(outFile string) func(bs blockstore.Blockstore, w *wallet.Wallet) modules.Genesis {
return func(bs blockstore.Blockstore, w *wallet.Wallet) modules.Genesis {
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")
// TODO: make an address allocation
Expand Down

0 comments on commit a30dd41

Please sign in to comment.