lotus/chain/gen/gen.go

165 lines
3.0 KiB
Go
Raw Normal View History

2019-07-25 22:15:33 +00:00
package gen
import (
"context"
"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/wallet"
"github.com/filecoin-project/go-lotus/node/repo"
2019-07-26 12:19:27 +00:00
2019-07-25 22:15:33 +00:00
block "github.com/ipfs/go-block-format"
2019-07-26 12:19:27 +00:00
"github.com/ipfs/go-cid"
2019-07-25 22:15:33 +00:00
blockstore "github.com/ipfs/go-ipfs-blockstore"
logging "github.com/ipfs/go-log"
)
var log = logging.Logger("gen")
type ChainGen struct {
accounts []address.Address
msgsPerBlock int
bs blockstore.Blockstore
cs *store.ChainStore
genesis *types.BlockHeader
curBlock *types.FullBlock
miner address.Address
2019-07-29 19:34:09 +00:00
r repo.Repo
lr repo.LockedRepo
2019-07-25 22:15:33 +00:00
}
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) {
mr := repo.NewMemory(nil)
lr, err := mr.Lock()
if err != nil {
return nil, err
}
2019-07-29 19:34:09 +00:00
ds, err := lr.Datastore("/metadata")
if err != nil {
return nil, err
}
bds, err := lr.Datastore("/blocks")
2019-07-25 22:15:33 +00:00
if err != nil {
return nil, err
}
2019-07-29 19:34:09 +00:00
bs := mybs{blockstore.NewBlockstore(bds)}
2019-07-25 22:15:33 +00:00
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
}
cs := store.NewChainStore(bs, ds)
msgsPerBlock := 10
genfb := &types.FullBlock{Header: genb.Genesis}
2019-07-26 12:19:27 +00:00
if err := cs.SetGenesis(genb.Genesis); err != nil {
return nil, err
}
2019-07-25 22:15:33 +00:00
gen := &ChainGen{
bs: bs,
cs: cs,
msgsPerBlock: msgsPerBlock,
genesis: genb.Genesis,
miner: miner,
curBlock: genfb,
2019-07-29 19:34:09 +00:00
r: mr,
lr: lr,
2019-07-25 22:15:33 +00:00
}
return gen, nil
}
func (cg *ChainGen) Genesis() *types.BlockHeader {
return cg.genesis
}
func (cg *ChainGen) nextBlockProof() (address.Address, types.ElectionProof, []types.Ticket, error) {
return cg.miner, []byte("cat in a box"), []types.Ticket{types.Ticket("im a ticket, promise")}, nil
}
func (cg *ChainGen) NextBlock() (*types.FullBlock, error) {
miner, proof, tickets, err := cg.nextBlockProof()
if err != nil {
return nil, err
}
var msgs []*types.SignedMessage
parents, err := types.NewTipSet([]*types.BlockHeader{cg.curBlock.Header})
if err != nil {
return nil, err
}
fblk, err := MinerCreateBlock(context.TODO(), cg.cs, miner, parents, tickets, proof, msgs)
if err != nil {
return nil, err
}
2019-07-26 12:19:27 +00:00
if err := cg.cs.AddBlock(fblk.Header); err != nil {
return nil, err
}
2019-07-25 22:15:33 +00:00
cg.curBlock = fblk
return fblk, nil
}
2019-07-29 19:34:09 +00:00
func (cg *ChainGen) YieldRepo() (repo.Repo, error) {
if err := cg.lr.Close(); err != nil {
return nil, err
}
return cg.r, nil
}