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"
chore: migrate to boxo This migrates everything except the `go-car` librairy: https://github.com/ipfs/boxo/issues/218#issuecomment-1529922103 I didn't migrated everything in the previous release because all the boxo code wasn't compatible with the go-ipld-prime one due to a an in flight (/ aftermath) revert of github.com/ipfs/go-block-format. go-block-format has been unmigrated since slight bellow absolutely everything depends on it that would have required everything to be moved on boxo or everything to optin into using boxo which were all deal breakers for different groups. This worked fine because lotus's codebase could live hapely on the first multirepo setup however boost is now trying to use boxo's code with lotus's (still on multirepo) setup: https://filecoinproject.slack.com/archives/C03AQ3QAUG1/p1685022344779649 The alternative would be for boost to write shim types which just forward calls and return with the different interface definitions. Btw why is that an issue in the first place is because unlike what go's duck typing model suggest interfaces are not transparent https://github.com/golang/go/issues/58112, interfaces are strongly typed but they have implicit narrowing. The issue is if you return an interface from an interface Go does not have a function definition to insert the implicit conversion thus instead the type checker complains you are not returning the right type. Stubbing types were reverted https://github.com/ipfs/boxo/issues/218#issuecomment-1478650351 Last time I only migrated `go-bitswap` to `boxo/bitswap` because of the security issues and because we never had the interface return an interface problem (we had concrete wrappers where the implicit conversion took place).
2023-05-25 14:31:53 +00:00
"github.com/ipfs/boxo/blockservice"
offline "github.com/ipfs/boxo/exchange/offline"
"github.com/ipfs/boxo/ipld/merkledag"
2019-07-25 11:46:48 +00:00
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
"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
}
}