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-11-25 04:45:13 +00:00
|
|
|
"io/ioutil"
|
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"
|
2020-01-08 19:10:57 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2019-07-25 11:46:48 +00:00
|
|
|
"github.com/ipfs/go-merkledag"
|
2020-05-05 01:31:56 +00:00
|
|
|
"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"
|
2019-07-08 15:14:36 +00:00
|
|
|
|
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"
|
2019-10-18 04:47:41 +00:00
|
|
|
"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"
|
2020-10-09 19:52:04 +00:00
|
|
|
"github.com/filecoin-project/lotus/journal"
|
2019-10-18 04:47:41 +00:00
|
|
|
"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")
|
|
|
|
|
2020-10-09 19:52:04 +00:00
|
|
|
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 {
|
2019-07-25 22:15:03 +00:00
|
|
|
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")
|
2020-10-09 19:52:04 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:52:04 +00:00
|
|
|
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 {
|
2019-07-25 22:15:03 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-21 18:00:10 +00:00
|
|
|
fdata, err := ioutil.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())
|
2019-12-11 14:36:39 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 19:52:04 +00:00
|
|
|
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
|
|
|
|
2019-07-24 22:49:37 +00:00
|
|
|
f, err := os.OpenFile(outFile, os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|