lotus/node/modules/testing/genesis.go

106 lines
3.4 KiB
Go
Raw Normal View History

2019-07-08 13:36:43 +00:00
package testing
import (
2019-07-25 11:46:48 +00:00
"context"
2019-11-25 04:45:13 +00:00
"encoding/json"
2019-08-16 19:39:09 +00:00
"fmt"
2019-07-24 23:23:06 +00:00
"io"
2019-07-24 22:49:37 +00:00
"os"
2019-07-25 11:46:48 +00:00
"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-cid"
offline "github.com/ipfs/go-ipfs-exchange-offline"
logging "github.com/ipfs/go-log/v2"
2019-07-25 11:46:48 +00:00
"github.com/ipfs/go-merkledag"
"github.com/ipld/go-car"
2019-11-30 09:25:31 +00:00
"github.com/mitchellh/go-homedir"
2019-12-02 12:51:16 +00:00
"golang.org/x/xerrors"
2020-07-10 14:43:14 +00:00
"github.com/filecoin-project/lotus/build"
2020-02-22 12:03:41 +00:00
"github.com/filecoin-project/lotus/chain/gen"
2020-02-11 20:48:03 +00:00
genesis2 "github.com/filecoin-project/lotus/chain/gen/genesis"
"github.com/filecoin-project/lotus/chain/types"
2020-07-18 13:46:47 +00:00
"github.com/filecoin-project/lotus/chain/vm"
2019-11-25 04:45:13 +00:00
"github.com/filecoin-project/lotus/genesis"
"github.com/filecoin-project/lotus/journal"
"github.com/filecoin-project/lotus/node/modules"
"github.com/filecoin-project/lotus/node/modules/dtypes"
2019-07-08 13:36:43 +00:00
)
2019-07-24 22:49:37 +00:00
var glog = logging.Logger("genesis")
func MakeGenesisMem(out io.Writer, template genesis.Template) func(bs dtypes.ChainBlockstore, syscalls vm.SyscallBuilder, j journal.Journal) modules.Genesis {
return func(bs dtypes.ChainBlockstore, syscalls vm.SyscallBuilder, j journal.Journal) modules.Genesis {
return func() (*types.BlockHeader, error) {
2019-07-24 23:23:06 +00:00
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(), j, bs, syscalls, template)
2019-07-24 23:23:06 +00:00
if err != nil {
2020-02-27 23:14:15 +00:00
return nil, xerrors.Errorf("make genesis block failed: %w", err)
2019-07-24 23:23:06 +00:00
}
2019-07-25 11:46:48 +00:00
offl := offline.Exchange(bs)
blkserv := blockservice.New(bs, offl)
dserv := merkledag.NewDAGService(blkserv)
2019-07-24 23:23:06 +00:00
2020-02-22 12:03:41 +00:00
if err := car.WriteCarWithWalker(context.TODO(), dserv, []cid.Cid{b.Genesis.Cid()}, out, gen.CarWalkFunc); err != nil {
2020-02-27 23:14:15 +00:00
return nil, xerrors.Errorf("failed to write car file: %w", err)
2019-07-24 23:23:06 +00:00
}
return b.Genesis, nil
}
}
}
func MakeGenesis(outFile, genesisTemplate string) func(bs dtypes.ChainBlockstore, syscalls vm.SyscallBuilder, j journal.Journal) modules.Genesis {
return func(bs dtypes.ChainBlockstore, syscalls vm.SyscallBuilder, j journal.Journal) modules.Genesis {
return func() (*types.BlockHeader, error) {
2019-07-24 22:49:37 +00:00
glog.Warn("Generating new random genesis block, note that this SHOULD NOT happen unless you are setting up new network")
2020-02-21 18:00:10 +00:00
genesisTemplate, err := homedir.Expand(genesisTemplate)
2019-11-30 09:25:31 +00:00
if err != nil {
2019-12-02 22:10:22 +00:00
return nil, err
2019-11-30 09:25:31 +00:00
}
fdata, err := os.ReadFile(genesisTemplate)
2019-11-25 04:45:13 +00:00
if err != nil {
2019-11-30 09:25:31 +00:00
return nil, xerrors.Errorf("reading preseals json: %w", err)
2019-11-25 04:45:13 +00:00
}
2020-02-21 18:00:10 +00:00
var template genesis.Template
2020-02-21 20:56:30 +00:00
if err := json.Unmarshal(fdata, &template); err != nil {
2019-11-25 04:45:13 +00:00
return nil, err
}
2020-02-21 18:00:10 +00:00
if template.Timestamp == 0 {
2020-07-10 14:43:14 +00:00
template.Timestamp = uint64(build.Clock.Now().Unix())
}
b, err := genesis2.MakeGenesisBlock(context.TODO(), j, bs, syscalls, template)
2019-07-24 22:49:37 +00:00
if err != nil {
2020-02-22 12:03:41 +00:00
return nil, xerrors.Errorf("make genesis block: %w", err)
2019-07-24 22:49:37 +00:00
}
2020-02-21 18:00:10 +00:00
fmt.Printf("GENESIS MINER ADDRESS: t0%d\n", genesis2.MinerStart)
2019-08-16 19:39:09 +00:00
f, err := os.OpenFile(outFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
2019-07-24 22:49:37 +00:00
if err != nil {
return nil, err
}
2019-07-25 11:46:48 +00:00
offl := offline.Exchange(bs)
blkserv := blockservice.New(bs, offl)
dserv := merkledag.NewDAGService(blkserv)
2019-07-24 22:49:37 +00:00
2020-02-22 12:03:41 +00:00
if err := car.WriteCarWithWalker(context.TODO(), dserv, []cid.Cid{b.Genesis.Cid()}, f, gen.CarWalkFunc); err != nil {
2019-07-24 22:49:37 +00:00
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
}
}