lotus/node/modules/testing/genesis.go
2020-02-21 19:00:10 +01:00

106 lines
3.0 KiB
Go

package testing
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"time"
"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-car"
"github.com/ipfs/go-cid"
offline "github.com/ipfs/go-ipfs-exchange-offline"
logging "github.com/ipfs/go-log/v2"
"github.com/ipfs/go-merkledag"
"github.com/mitchellh/go-homedir"
"golang.org/x/xerrors"
"github.com/filecoin-project/specs-actors/actors/runtime"
genesis2 "github.com/filecoin-project/lotus/chain/gen/genesis"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/genesis"
"github.com/filecoin-project/lotus/node/modules"
"github.com/filecoin-project/lotus/node/modules/dtypes"
)
var glog = logging.Logger("genesis")
func MakeGenesisMem(out io.Writer, template genesis.Template) func(bs dtypes.ChainBlockstore, syscalls runtime.Syscalls) modules.Genesis {
return func(bs dtypes.ChainBlockstore, syscalls runtime.Syscalls) 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")
b, err := genesis2.MakeGenesisBlock(context.TODO(), bs, syscalls, template)
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, genesisTemplate string) func(bs dtypes.ChainBlockstore, syscalls runtime.Syscalls) modules.Genesis {
return func(bs dtypes.ChainBlockstore, syscalls runtime.Syscalls) 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")
genesisTemplate, err := homedir.Expand(genesisTemplate)
if err != nil {
return nil, err
}
fdata, err := ioutil.ReadFile(genesisTemplate)
if err != nil {
return nil, xerrors.Errorf("reading preseals json: %w", err)
}
var template genesis.Template
if err := json.Unmarshal(fdata, template); err != nil {
return nil, err
}
if template.Timestamp == 0 {
template.Timestamp = uint64(time.Now().Unix())
}
b, err := genesis2.MakeGenesisBlock(context.TODO(), bs, syscalls, template)
if err != nil {
return nil, err
}
fmt.Printf("GENESIS MINER ADDRESS: t0%d\n", genesis2.MinerStart)
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
}
}
}