lotus/node/modules/testing/genesis.go
2019-07-25 13:46:48 +02:00

75 lines
2.0 KiB
Go

package testing
import (
"context"
"io"
"os"
"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-car"
"github.com/ipfs/go-cid"
blockstore "github.com/ipfs/go-ipfs-blockstore"
offline "github.com/ipfs/go-ipfs-exchange-offline"
logging "github.com/ipfs/go-log"
"github.com/ipfs/go-merkledag"
"github.com/filecoin-project/go-lotus/chain"
"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 {
return func() (*chain.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)
if err != nil {
return nil, err
}
offl := offline.Exchange(bs)
blkserv := blockservice.New(bs, offl)
dserv := merkledag.NewDAGService(blkserv)
if err := car.WriteCar(context.TODO(), dserv, []cid.Cid{b.Genesis.Cid()}, out); err != nil {
return nil, err
}
return b.Genesis, nil
}
}
}
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) {
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)
if err != nil {
return nil, err
}
f, err := os.OpenFile(outFile, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, err
}
offl := offline.Exchange(bs)
blkserv := blockservice.New(bs, offl)
dserv := merkledag.NewDAGService(blkserv)
if err := car.WriteCar(context.TODO(), dserv, []cid.Cid{b.Genesis.Cid()}, f); err != nil {
return nil, err
}
glog.Warnf("WRITING GENESIS FILE AT %s", f.Name())
if err := f.Close(); err != nil {
return nil, err
}
return b.Genesis, nil
}
}
}