lotus/node/modules/testing/genesis.go

163 lines
4.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-11-25 04:45:13 +00:00
"io/ioutil"
2019-07-24 22:49:37 +00:00
"os"
"time"
2019-07-24 22:49:37 +00:00
2019-07-25 11:46:48 +00:00
"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"
2019-07-25 11:46:48 +00:00
"github.com/ipfs/go-merkledag"
2019-12-02 12:51:16 +00:00
"github.com/libp2p/go-libp2p-core/peer"
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"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/chain/gen"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet"
2019-11-25 04:45:13 +00:00
"github.com/filecoin-project/lotus/genesis"
"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-01-13 20:47:27 +00:00
func MakeGenesisMem(out io.Writer, gmc *gen.GenMinerCfg) func(bs dtypes.ChainBlockstore, w *wallet.Wallet, syscalls *types.VMSyscalls) modules.Genesis {
return func(bs dtypes.ChainBlockstore, w *wallet.Wallet, syscalls *types.VMSyscalls) 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")
defk, err := w.GenerateKey(types.KTBLS)
2019-08-15 02:30:21 +00:00
if err != nil {
return nil, err
}
alloc := map[address.Address]types.BigInt{
defk: types.FromFil(1000),
}
for _, genm := range gmc.PreSeals {
waddr, err := w.Import(&genm.Key)
if err != nil {
return nil, err
}
alloc[waddr] = types.FromFil(10000)
2019-08-15 02:30:21 +00:00
}
2020-01-13 20:47:27 +00:00
b, err := gen.MakeGenesisBlock(bs, syscalls, alloc, gmc, 100000)
2019-07-24 23:23:06 +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 23:23:06 +00:00
2019-07-25 11:46:48 +00:00
if err := car.WriteCar(context.TODO(), dserv, []cid.Cid{b.Genesis.Cid()}, out); err != nil {
2019-07-24 23:23:06 +00:00
return nil, err
}
return b.Genesis, nil
}
}
}
2020-01-13 20:47:27 +00:00
func MakeGenesis(outFile, presealInfo, timestamp string) func(bs dtypes.ChainBlockstore, w *wallet.Wallet, syscalls *types.VMSyscalls) modules.Genesis {
return func(bs dtypes.ChainBlockstore, w *wallet.Wallet, syscalls *types.VMSyscalls) 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")
2019-12-02 22:10:22 +00:00
presealInfo, err := homedir.Expand(presealInfo)
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
}
2019-12-02 22:10:22 +00:00
fdata, err := ioutil.ReadFile(presealInfo)
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
}
2019-11-30 09:25:31 +00:00
var preseals map[string]genesis.GenesisMiner
if err := json.Unmarshal(fdata, &preseals); err != nil {
2019-11-25 04:45:13 +00:00
return nil, err
}
var fakePeerIDs []peer.ID
2019-11-30 09:25:31 +00:00
minerAddresses := make([]address.Address, 0, len(preseals))
for s := range preseals {
2019-11-28 22:50:58 +00:00
a, err := address.NewFromString(s)
if err != nil {
return nil, err
}
if a.Protocol() != address.ID {
return nil, xerrors.New("expected ID address")
}
minerAddresses = append(minerAddresses, a)
fakePeerIDs = append(fakePeerIDs, peer.ID("peer"+a.String()))
2019-07-26 12:51:32 +00:00
}
2019-08-15 02:30:21 +00:00
gmc := &gen.GenMinerCfg{
PeerIDs: fakePeerIDs,
2019-11-30 09:25:31 +00:00
PreSeals: preseals,
2019-11-28 22:50:58 +00:00
MinerAddrs: minerAddresses,
2019-08-15 02:30:21 +00:00
}
2019-11-28 22:50:58 +00:00
addrs := map[address.Address]types.BigInt{}
2019-11-30 09:25:31 +00:00
for _, miner := range preseals {
2019-11-28 22:50:58 +00:00
if _, err := w.Import(&miner.Key); err != nil {
return nil, xerrors.Errorf("importing miner key: %w", err)
}
_ = w.SetDefault(miner.Worker)
addrs[miner.Worker] = types.FromFil(100000)
2019-07-26 12:51:32 +00:00
}
ts := uint64(time.Now().Unix())
if timestamp != "" {
t, err := time.Parse(time.RFC3339, timestamp)
if err != nil {
return nil, xerrors.Errorf("parsing input genesis timestamp: %w", err)
}
glog.Infof("will use %s as the genesis timestamp", t)
ts = uint64(t.Unix())
}
2020-01-13 20:47:27 +00:00
b, err := gen.MakeGenesisBlock(bs, syscalls, addrs, gmc, ts)
2019-07-24 22:49:37 +00:00
if err != nil {
return nil, err
}
fmt.Println("GENESIS MINER ADDRESS: ", gmc.MinerAddrs[0].String())
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
2019-07-25 11:46:48 +00:00
if err := car.WriteCar(context.TODO(), dserv, []cid.Cid{b.Genesis.Cid()}, f); 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
}
}