lotus/node/modules/testing/genesis.go

72 lines
1.7 KiB
Go
Raw Normal View History

2019-07-08 13:36:43 +00:00
package testing
import (
2019-07-24 23:23:06 +00:00
"io"
2019-07-24 22:49:37 +00:00
"os"
blockstore "github.com/ipfs/go-ipfs-blockstore"
2019-07-24 22:49:37 +00:00
logging "github.com/ipfs/go-log"
2019-07-08 13:36:43 +00:00
"github.com/filecoin-project/go-lotus/chain"
"github.com/filecoin-project/go-lotus/node/modules"
)
2019-07-24 22:49:37 +00:00
var glog = logging.Logger("genesis")
2019-07-24 23:23:06 +00:00
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
}
genBytes, err := b.Genesis.Serialize()
if err != nil {
return nil, err
}
if _, err := out.Write(genBytes); err != nil {
return nil, err
}
return b.Genesis, nil
}
}
}
2019-07-24 22:49:37 +00:00
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
}
genBytes, err := b.Genesis.Serialize()
if err != nil {
return nil, err
}
if _, err := f.Write(genBytes); 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
}
2019-07-08 13:36:43 +00:00
}
}