2019-07-08 13:36:43 +00:00
|
|
|
package testing
|
|
|
|
|
|
|
|
import (
|
2019-07-25 11:46:48 +00:00
|
|
|
"context"
|
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-10-09 17:08:06 +00:00
|
|
|
"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"
|
2019-07-24 22:49:37 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2019-07-25 11:46:48 +00:00
|
|
|
"github.com/ipfs/go-merkledag"
|
2019-09-06 06:26:02 +00:00
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
2019-07-08 15:14:36 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/address"
|
|
|
|
"github.com/filecoin-project/lotus/chain/gen"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/wallet"
|
|
|
|
"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")
|
|
|
|
|
2019-08-01 14:26:12 +00:00
|
|
|
func MakeGenesisMem(out io.Writer) func(bs dtypes.ChainBlockstore, w *wallet.Wallet) modules.Genesis {
|
|
|
|
return func(bs dtypes.ChainBlockstore, w *wallet.Wallet) 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")
|
2019-07-25 22:15:03 +00:00
|
|
|
// TODO: make an address allocation
|
2019-08-15 02:30:21 +00:00
|
|
|
w, err := w.GenerateKey(types.KTBLS)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
gmc := &gen.GenMinerCfg{
|
2019-09-06 06:26:02 +00:00
|
|
|
Owners: []address.Address{w},
|
|
|
|
Workers: []address.Address{w},
|
|
|
|
PeerIDs: []peer.ID{"peerID 1"},
|
2019-08-15 02:30:21 +00:00
|
|
|
}
|
|
|
|
alloc := map[address.Address]types.BigInt{
|
2019-09-26 19:23:03 +00:00
|
|
|
w: types.FromFil(10000),
|
2019-08-15 02:30:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 02:59:54 +00:00
|
|
|
b, err := gen.MakeGenesisBlock(bs, 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 14:14:16 +00:00
|
|
|
func MakeGenesis(outFile string) func(bs dtypes.ChainBlockstore, w *wallet.Wallet) modules.Genesis {
|
|
|
|
return func(bs dtypes.ChainBlockstore, w *wallet.Wallet) 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")
|
2019-08-15 02:30:21 +00:00
|
|
|
minerAddr, err := w.GenerateKey(types.KTBLS)
|
2019-07-26 12:51:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-15 02:30:21 +00:00
|
|
|
gmc := &gen.GenMinerCfg{
|
2019-09-06 06:26:02 +00:00
|
|
|
Owners: []address.Address{minerAddr},
|
|
|
|
Workers: []address.Address{minerAddr},
|
|
|
|
PeerIDs: []peer.ID{"peer ID 1"},
|
2019-08-15 02:30:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 12:51:32 +00:00
|
|
|
addrs := map[address.Address]types.BigInt{
|
2019-09-26 23:07:40 +00:00
|
|
|
minerAddr: types.FromFil(100000),
|
2019-07-26 12:51:32 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 17:08:06 +00:00
|
|
|
b, err := gen.MakeGenesisBlock(bs, addrs, gmc, uint64(time.Now().Unix()))
|
2019-07-24 22:49:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|