seed: Update lotus-seed for new genesis types

This commit is contained in:
Łukasz Magiera 2020-02-12 22:41:59 +01:00
parent 0aaf7b25a9
commit 220bd376b4
6 changed files with 124 additions and 67 deletions

View File

@ -18,7 +18,6 @@ import (
offline "github.com/ipfs/go-ipfs-exchange-offline"
logging "github.com/ipfs/go-log/v2"
"github.com/ipfs/go-merkledag"
peer "github.com/libp2p/go-libp2p-core/peer"
"github.com/filecoin-project/go-address"
sectorbuilder "github.com/filecoin-project/go-sectorbuilder"
@ -128,61 +127,68 @@ func NewGenerator() (*ChainGen, error) {
}
}
maddr1, err := address.NewFromString("t0300")
if err != nil {
return nil, err
}
maddr1 := genesis2.MinerAddress(0)
m1temp, err := ioutil.TempDir("", "preseal")
if err != nil {
return nil, err
}
genm1, err := seed.PreSeal(maddr1, 1024, 0, 1, m1temp, []byte("some randomness"))
genm1, k1, err := seed.PreSeal(maddr1, 1024, 0, 1, m1temp, []byte("some randomness"), nil)
if err != nil {
return nil, err
}
maddr2, err := address.NewFromString("t0301")
if err != nil {
return nil, err
}
maddr2 := genesis2.MinerAddress(1)
m2temp, err := ioutil.TempDir("", "preseal")
if err != nil {
return nil, err
}
genm2, err := seed.PreSeal(maddr2, 1024, 0, 1, m2temp, []byte("some randomness"))
genm2, k2, err := seed.PreSeal(maddr2, 1024, 0, 1, m2temp, []byte("some randomness"), nil)
if err != nil {
return nil, err
}
mk1, err := w.Import(&genm1.Key)
mk1, err := w.Import(k1)
if err != nil {
return nil, err
}
mk2, err := w.Import(&genm2.Key)
mk2, err := w.Import(k2)
if err != nil {
return nil, err
}
minercfg := &genesis2.GenMinerCfg{
PeerIDs: []peer.ID{"peerID1", "peerID2"},
PreSeals: map[string]genesis.GenesisMiner{
maddr1.String(): *genm1,
maddr2.String(): *genm2,
},
MinerAddrs: []address.Address{maddr1, maddr2},
}
sys := vm.Syscalls(sectorbuilder.ProofVerifier)
genb, err := genesis2.MakeGenesisBlock(bs, sys, map[address.Address]types.BigInt{
mk1: types.FromFil(40000),
mk2: types.FromFil(40000),
banker: types.FromFil(50000),
}, minercfg, 100000)
tpl := genesis.Template{
Accounts: []genesis.Actor{
{
Type: genesis.TAccount,
Balance: types.FromFil(40000),
Meta: (&genesis.AccountMeta{Owner: mk1}).ActorMeta(),
},
{
Type: genesis.TAccount,
Balance: types.FromFil(40000),
Meta: (&genesis.AccountMeta{Owner: mk2}).ActorMeta(),
},
{
Type: genesis.TAccount,
Balance: types.FromFil(50000),
Meta: (&genesis.AccountMeta{Owner: banker}).ActorMeta(),
},
},
Miners: []genesis.Miner{
*genm1,
*genm2,
},
NetworkName: "",
Timestamp: 100000,
}
genb, err := genesis2.MakeGenesisBlock(context.TODO(), bs, sys, tpl)
if err != nil {
return nil, xerrors.Errorf("make genesis block failed: %w", err)
}
@ -196,17 +202,15 @@ func NewGenerator() (*ChainGen, error) {
return nil, xerrors.Errorf("set genesis failed: %w", err)
}
if len(minercfg.MinerAddrs) == 0 {
return nil, xerrors.Errorf("MakeGenesisBlock failed to set miner address")
}
mgen := make(map[address.Address]ElectionPoStProver)
for _, m := range minercfg.MinerAddrs {
mgen[m] = &eppProvider{}
for i := range tpl.Miners {
mgen[genesis2.MinerAddress(uint64(i))] = &eppProvider{}
}
sm := stmgr.NewStateManager(cs)
miners := []address.Address{maddr1, maddr2}
gen := &ChainGen{
bs: bs,
cs: cs,
@ -216,7 +220,7 @@ func NewGenerator() (*ChainGen, error) {
w: w,
GetMessages: getRandomMessages,
Miners: minercfg.MinerAddrs,
Miners: miners,
eppProvs: mgen,
banker: banker,
receivers: receievers,

View File

@ -21,6 +21,15 @@ import (
"github.com/filecoin-project/lotus/genesis"
)
func MinerAddress(genesisIndex uint64) address.Address {
maddr, err := address.NewIDAddress(MinerStart + genesisIndex)
if err != nil {
panic(err)
}
return maddr
}
func SetupStorageMiners(ctx context.Context, cs *store.ChainStore, sroot cid.Cid, miners []genesis.Miner) (cid.Cid, error) {
vm, err := vm.NewVM(sroot, 0, nil, actors.SystemAddress, cs.Blockstore(), cs.VMSys())
if err != nil {
@ -53,10 +62,8 @@ func SetupStorageMiners(ctx context.Context, cs *store.ChainStore, sroot cid.Cid
if err != nil {
return cid.Undef, err
}
expma, err := address.NewIDAddress(uint64(MinerStart + i))
if err != nil {
return cid.Undef, err
}
expma := MinerAddress(uint64(i))
if maddrret != expma {
return cid.Undef, xerrors.Errorf("miner assigned wrong address: %s != %s", maddrret, expma)
}

View File

@ -159,7 +159,6 @@ func (tu *syncTestUtil) pushTsExpectErr(to int, fts *store.FullTipSet, experr bo
require.NoError(tu.t, err)
}
}
}
func (tu *syncTestUtil) mineOnBlock(blk *store.FullTipSet, src int, miners []int, wait, fail bool) *store.FullTipSet {

View File

@ -1,8 +1,10 @@
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
@ -21,6 +23,7 @@ import (
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/cmd/lotus-seed/seed"
"github.com/filecoin-project/lotus/genesis"
)
@ -86,6 +89,11 @@ var preSealCmd = &cli.Command{
Value: 0,
Usage: "how many sector ids to skip when starting to seal",
},
&cli.StringFlag{
Name: "key",
Value: "",
Usage: "(optional) Key to use for signing / owner/worker addresses",
},
},
Action: func(c *cli.Context) error {
sdir := c.String("sectorbuilder-dir")
@ -99,12 +107,25 @@ var preSealCmd = &cli.Command{
return err
}
gm, err := seed.PreSeal(maddr, abi.SectorSize(c.Uint64("sector-size")), abi.SectorNumber(c.Uint64("sector-offset")), c.Int("num-sectors"), sbroot, []byte(c.String("ticket-preimage")))
var k *types.KeyInfo
if c.String("key") != "" {
k = new(types.KeyInfo)
kh, err := ioutil.ReadFile(c.String("key"))
if err != nil {
return err
}
kb, err := hex.DecodeString(string(kh))
if err := json.Unmarshal(kb, k); err != nil {
return err
}
}
gm, key, err := seed.PreSeal(maddr, abi.SectorSize(c.Uint64("sector-size")), abi.SectorNumber(c.Uint64("sector-offset")), c.Int("num-sectors"), sbroot, []byte(c.String("ticket-preimage")), k)
if err != nil {
return err
}
return seed.WriteGenesisMiner(maddr, sbroot, gm)
return seed.WriteGenesisMiner(maddr, sbroot, gm, key)
},
}
@ -112,14 +133,14 @@ var aggregateManifestsCmd = &cli.Command{
Name: "aggregate-manifests",
Usage: "aggregate a set of preseal manifests into a single file",
Action: func(cctx *cli.Context) error {
var inputs []map[string]genesis.GenesisMiner
var inputs []map[string]genesis.Miner
for _, infi := range cctx.Args().Slice() {
fi, err := os.Open(infi)
if err != nil {
return err
}
defer fi.Close()
var val map[string]genesis.GenesisMiner
var val map[string]genesis.Miner
if err := json.NewDecoder(fi).Decode(&val); err != nil {
return err
}
@ -127,7 +148,7 @@ var aggregateManifestsCmd = &cli.Command{
inputs = append(inputs, val)
}
output := make(map[string]genesis.GenesisMiner)
output := make(map[string]genesis.Miner)
for _, in := range inputs {
for maddr, val := range in {
if gm, ok := output[maddr]; ok {
@ -206,7 +227,7 @@ var aggregateSectorDirsCmd = &cli.Command{
return err
}
var aggrGenMiner genesis.GenesisMiner
var aggrGenMiner genesis.Miner
var highestSectorID abi.SectorNumber
for _, dir := range cctx.Args().Slice() {
dir, err := homedir.Expand(dir)
@ -227,7 +248,7 @@ var aggregateSectorDirsCmd = &cli.Command{
return err
}
var genmm map[string]genesis.GenesisMiner
var genmm map[string]genesis.Miner
if err := json.NewDecoder(fi).Decode(&genmm); err != nil {
return err
}
@ -276,7 +297,7 @@ var aggregateSectorDirsCmd = &cli.Command{
return err
}
if err := seed.WriteGenesisMiner(maddr, destdir, &aggrGenMiner); err != nil {
if err := seed.WriteGenesisMiner(maddr, destdir, &aggrGenMiner, nil); err != nil {
return err
}
@ -284,16 +305,15 @@ var aggregateSectorDirsCmd = &cli.Command{
},
}
func mergeGenMiners(a, b genesis.GenesisMiner) genesis.GenesisMiner {
func mergeGenMiners(a, b genesis.Miner) genesis.Miner {
if a.SectorSize != b.SectorSize {
panic("sector sizes mismatch")
}
return genesis.GenesisMiner{
return genesis.Miner{
Owner: a.Owner,
Worker: a.Worker,
SectorSize: a.SectorSize,
Key: a.Key,
Sectors: append(a.Sectors, b.Sectors...),
}
}

View File

@ -4,6 +4,7 @@ import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
@ -32,7 +33,7 @@ import (
var log = logging.Logger("preseal")
func PreSeal(maddr address.Address, ssize abi.SectorSize, offset abi.SectorNumber, sectors int, sbroot string, preimage []byte) (*genesis.Miner, error) {
func PreSeal(maddr address.Address, ssize abi.SectorSize, offset abi.SectorNumber, sectors int, sbroot string, preimage []byte, key *types.KeyInfo) (*genesis.Miner, *types.KeyInfo, error) {
cfg := &sectorbuilder.Config{
Miner: maddr,
SectorSize: ssize,
@ -42,29 +43,29 @@ func PreSeal(maddr address.Address, ssize abi.SectorSize, offset abi.SectorNumbe
}
if err := os.MkdirAll(sbroot, 0775); err != nil {
return nil, err
return nil, nil, err
}
mds, err := badger.NewDatastore(filepath.Join(sbroot, "badger"), nil)
if err != nil {
return nil, err
return nil, nil, err
}
sb, err := sectorbuilder.New(cfg, namespace.Wrap(mds, datastore.NewKey("/sectorbuilder")))
if err != nil {
return nil, err
return nil, nil, err
}
var sealedSectors []*genesis.PreSeal
for i := 0; i < sectors; i++ {
sid, err := sb.AcquireSectorNumber()
if err != nil {
return nil, err
return nil, nil, err
}
pi, err := sb.AddPiece(context.TODO(), abi.PaddedPieceSize(ssize).Unpadded(), sid, rand.Reader, nil)
if err != nil {
return nil, err
return nil, nil, err
}
trand := sha256.Sum256(preimage)
@ -76,11 +77,11 @@ func PreSeal(maddr address.Address, ssize abi.SectorSize, offset abi.SectorNumbe
pco, err := sb.SealPreCommit(context.TODO(), sid, ticket, []sectorbuilder.PublicPieceInfo{pi})
if err != nil {
return nil, xerrors.Errorf("commit: %w", err)
return nil, nil, xerrors.Errorf("commit: %w", err)
}
if err := sb.TrimCache(context.TODO(), sid); err != nil {
return nil, xerrors.Errorf("trim cache: %w", err)
return nil, nil, xerrors.Errorf("trim cache: %w", err)
}
log.Warn("PreCommitOutput: ", sid, pco)
@ -91,9 +92,17 @@ func PreSeal(maddr address.Address, ssize abi.SectorSize, offset abi.SectorNumbe
})
}
minerAddr, err := wallet.GenerateKey(types.KTBLS)
if err != nil {
return nil, err
var minerAddr *wallet.Key
if key != nil {
minerAddr, err = wallet.NewKey(*key)
if err != nil {
return nil, nil, err
}
} else {
minerAddr, err = wallet.GenerateKey(types.KTBLS)
if err != nil {
return nil, nil, err
}
}
miner := &genesis.Miner{
@ -103,22 +112,20 @@ func PreSeal(maddr address.Address, ssize abi.SectorSize, offset abi.SectorNumbe
SectorSize: ssize,
Sectors: sealedSectors,
// Key: minerAddr.KeyInfo, // TODO: Export separately
}
if err := createDeals(miner, minerAddr, maddr, ssize); err != nil {
return nil, xerrors.Errorf("creating deals: %w", err)
return nil, nil, xerrors.Errorf("creating deals: %w", err)
}
if err := mds.Close(); err != nil {
return nil, xerrors.Errorf("closing datastore: %w", err)
return nil, nil, xerrors.Errorf("closing datastore: %w", err)
}
return miner, nil
return miner, &minerAddr.KeyInfo, nil
}
func WriteGenesisMiner(maddr address.Address, sbroot string, gm *genesis.Miner) error {
func WriteGenesisMiner(maddr address.Address, sbroot string, gm *genesis.Miner, key *types.KeyInfo) error {
output := map[string]genesis.Miner{
maddr.String(): *gm,
}
@ -132,6 +139,18 @@ func WriteGenesisMiner(maddr address.Address, sbroot string, gm *genesis.Miner)
return err
}
if key != nil {
b, err := json.Marshal(key)
if err != nil {
return err
}
// TODO: allow providing key
if err := ioutil.WriteFile(filepath.Join(sbroot, "pre-seal-"+maddr.String()+".key"), []byte(hex.EncodeToString(b)), 0664); err != nil {
return err
}
}
return nil
}

View File

@ -39,6 +39,14 @@ type AccountMeta struct {
Owner address.Address // bls / secpk
}
func (am *AccountMeta) ActorMeta() json.RawMessage {
out, err := json.Marshal(am)
if err != nil {
panic(err)
}
return out
}
type MultisigMeta struct {
// TODO
}