genesis: Cleanup miner creation

This commit is contained in:
Łukasz Magiera 2020-02-12 03:13:00 +01:00
parent 23b662fe7d
commit ca3eabafef
3 changed files with 92 additions and 158 deletions

View File

@ -55,6 +55,7 @@ The process:
- Each:
- power.CreateMiner, set msg value to PowerBalance
- market.AddFunds with correct value
- market.PublishDeals for related sectors
- Set precommits
- Commit presealed sectors
@ -205,7 +206,7 @@ func MakeGenesisBlock(ctx context.Context, bs bstore.Blockstore, sys *types.VMSy
// temp chainstore
cs := store.NewChainStore(bs, datastore.NewMapDatastore(), sys)
stateroot, deals, err := SetupStorageMiners(ctx, cs, stateroot, gmcfg)
stateroot, err = SetupStorageMiners(ctx, cs, stateroot, template.Miners)
if err != nil {
return nil, xerrors.Errorf("setup storage miners failed: %w", err)
}

View File

@ -9,195 +9,128 @@ import (
"github.com/filecoin-project/specs-actors/actors/abi"
"github.com/filecoin-project/specs-actors/actors/abi/big"
"github.com/filecoin-project/specs-actors/actors/builtin"
"github.com/filecoin-project/specs-actors/actors/builtin/market"
"github.com/filecoin-project/specs-actors/actors/builtin/miner"
"github.com/filecoin-project/specs-actors/actors/builtin/power"
"github.com/filecoin-project/specs-actors/actors/util/adt"
"github.com/filecoin-project/specs-actors/actors/crypto"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/libp2p/go-libp2p-core/peer"
"golang.org/x/xerrors"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/vm"
"github.com/filecoin-project/lotus/genesis"
)
type GenMinerCfg struct {
PreSeals map[string]genesis.Miner
// The addresses of the created miner, this is set by the genesis setup
MinerAddrs []address.Address
PeerIDs []peer.ID
}
func SetupStorageMiners(ctx context.Context, cs *store.ChainStore, sroot cid.Cid, gmcfg *GenMinerCfg) (cid.Cid, []actors.StorageDealProposal, error) {
vm, err := vm.NewVM(sroot, 0, nil, actors.NetworkAddress, cs.Blockstore(), cs.VMSys())
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 {
return cid.Undef, nil, xerrors.Errorf("failed to create NewVM: %w", err)
return cid.Undef, xerrors.Errorf("failed to create NewVM: %w", err)
}
if len(gmcfg.MinerAddrs) == 0 {
return cid.Undef, nil, xerrors.New("no genesis miners")
if len(miners) == 0 {
return cid.Undef, xerrors.New("no genesis miners")
}
if len(gmcfg.MinerAddrs) != len(gmcfg.PreSeals) {
return cid.Undef, nil, xerrors.Errorf("miner address list, and preseal count doesn't match (%d != %d)", len(gmcfg.MinerAddrs), len(gmcfg.PreSeals))
}
for i, m := range miners {
// Create miner through power actor
var deals []actors.StorageDealProposal
for i, maddr := range gmcfg.MinerAddrs {
ps, psok := gmcfg.PreSeals[maddr.String()]
if !psok {
return cid.Undef, nil, xerrors.Errorf("no preseal for miner %s", maddr)
}
minerParams := &miner.ConstructorParams{
OwnerAddr: ps.Owner,
WorkerAddr: ps.Worker,
SectorSize: ps.SectorSize,
PeerId: gmcfg.PeerIDs[i], // TODO: grab from preseal too
}
params := mustEnc(minerParams)
// TODO: hardcoding 6500 here is a little fragile, it changes any
// time anyone changes the initial account allocations
rval, err := doExecValue(ctx, vm, actors.StoragePowerAddress, ps.Worker, types.FromFil(6500), actors.SPAMethods.CreateMiner, params)
if err != nil {
return cid.Undef, nil, xerrors.Errorf("failed to create genesis miner: %w", err)
}
maddrret, err := address.NewFromBytes(rval)
if err != nil {
return cid.Undef, nil, err
}
_, err = vm.Flush(ctx)
if err != nil {
return cid.Undef, nil, err
}
cst := cbor.NewCborStore(cs.Blockstore())
if err := reassignMinerActorAddress(vm, cst, maddrret, maddr); err != nil {
return cid.Undef, nil, err
}
pledgeRequirements := make([]big.Int, len(ps.Sectors))
for i, sector := range ps.Sectors {
if sector.Deal.StartEpoch != 0 {
return cid.Undef, nil, xerrors.New("all deals must start at epoch 0")
var maddr address.Address
{
constructorParams := &miner.ConstructorParams{
OwnerAddr: m.Owner,
WorkerAddr: m.Worker,
SectorSize: m.SectorSize,
PeerId: m.PeerId,
}
dur := big.NewInt(int64(sector.Deal.Duration()))
siz := big.NewInt(int64(sector.Deal.PieceSize))
weight := big.Mul(dur, siz)
params = mustEnc(&power.OnSectorProveCommitParams{
Weight: power.SectorStorageWeightDesc{
SectorSize: ps.SectorSize,
Duration: sector.Deal.Duration(),
DealWeight: weight,
}})
ret, err := doExec(ctx, vm, actors.StoragePowerAddress, maddr, builtin.MethodsPower.OnSectorProveCommit, params)
params := mustEnc(constructorParams)
rval, err := doExecValue(ctx, vm, actors.StoragePowerAddress, m.Worker, m.PowerBalance, actors.SPAMethods.CreateMiner, params)
if err != nil {
return cid.Undef, nil, xerrors.Errorf("failed to update total storage: %w", err)
return cid.Undef, xerrors.Errorf("failed to create genesis miner: %w", err)
}
if err := pledgeRequirements[i].UnmarshalCBOR(bytes.NewReader(ret)); err != nil {
return cid.Undef, nil, xerrors.Errorf("unmarshal pledge requirement: %w", err)
maddrret, err := address.NewFromBytes(rval)
if err != nil {
return cid.Undef, err
}
expma, err := address.NewIDAddress(uint64(MinerStart + i))
if err != nil {
return cid.Undef, err
}
if maddrret != expma {
return cid.Undef, xerrors.Errorf("miner assigned wrong address: %s != %s", maddrret, expma)
}
maddr = maddrret
}
// Add market funds
{
params := mustEnc(&m.Worker)
_, err := doExecValue(ctx, vm, actors.StorageMarketAddress, m.Worker, m.MarketBalance, builtin.MethodsMarket.AddBalance, params)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to create genesis miner: %w", err)
}
}
// we have to flush the vm here because it buffers stuff internally for perf reasons
if _, err := vm.Flush(ctx); err != nil {
return cid.Undef, nil, xerrors.Errorf("vm.Flush failed: %w", err)
}
// Publish preseal deals
st := vm.StateTree()
mact, err := st.GetActor(maddr)
if err != nil {
return cid.Undef, nil, xerrors.Errorf("get miner actor failed: %w", err)
}
var mstate miner.State
if err := cst.Get(ctx, mact.Head, &mstate); err != nil {
return cid.Undef, nil, xerrors.Errorf("getting miner actor state failed: %w", err)
}
for i, s := range ps.Sectors {
dur := big.NewInt(int64(s.Deal.Duration()))
siz := big.NewInt(int64(s.Deal.PieceSize))
weight := big.Mul(dur, siz)
oci := miner.SectorOnChainInfo{
Info: miner.SectorPreCommitInfo{
SectorNumber: s.SectorID,
SealedCID: commcid.ReplicaCommitmentV1ToCID(s.CommR[:]),
SealEpoch: 0,
DealIDs: []abi.DealID{abi.DealID(len(deals))},
Expiration: 0,
},
ActivationEpoch: s.Deal.StartEpoch,
DealWeight: weight,
PledgeRequirement: pledgeRequirements[i],
DeclaredFaultEpoch: -1,
DeclaredFaultDuration: -1,
var dealIDs []abi.DealID
{
params := &market.PublishStorageDealsParams{}
for _, preseal := range m.Sectors {
params.Deals = append(params.Deals, market.ClientDealProposal{
Proposal: preseal.Deal,
ClientSignature: crypto.Signature{},
})
}
nssroot := adt.AsArray(cs.Store(ctx), mstate.Sectors)
if err := nssroot.Set(uint64(s.SectorID), &oci); err != nil {
return cid.Cid{}, nil, xerrors.Errorf("add sector to set: %w", err)
ret, err := doExecValue(ctx, vm, builtin.StorageMarketActorAddr, m.Worker, big.Zero(), builtin.MethodsMarket.PublishStorageDeals, mustEnc(params))
if err != nil {
return cid.Undef, xerrors.Errorf("failed to create genesis miner: %w", err)
}
var ids market.PublishStorageDealsReturn
if err := ids.UnmarshalCBOR(bytes.NewReader(ret)); err != nil {
return cid.Undef, err
}
mstate.Sectors = nssroot.Root()
mstate.ProvingSet = nssroot.Root()
deals = append(deals, s.Deal)
dealIDs = ids.IDs
}
nstate, err := cst.Put(ctx, &mstate)
if err != nil {
return cid.Undef, nil, err
}
// Publish preseals
mact.Head = nstate
if err := st.SetActor(maddr, mact); err != nil {
return cid.Undef, nil, err
{
for pi, preseal := range m.Sectors {
// Precommit
{
params := &miner.PreCommitSectorParams{Info:miner.SectorPreCommitInfo{
SectorNumber: preseal.SectorID,
SealedCID: commcid.ReplicaCommitmentV1ToCID(preseal.CommR[:]),
SealEpoch: 0,
DealIDs: []abi.DealID{dealIDs[pi]},
Expiration: preseal.Deal.EndEpoch,
}}
_, err := doExecValue(ctx, vm, maddr, m.Worker, big.Zero(), builtin.MethodsMiner.PreCommitSector, mustEnc(params))
if err != nil {
return cid.Undef, xerrors.Errorf("failed to create genesis miner: %w", err)
}
}
// Commit
{
params := &miner.ProveCommitSectorParams{
SectorNumber: preseal.SectorID,
Proof: abi.SealProof{},
}
_, err := doExecValue(ctx, vm, maddr, m.Worker, big.Zero(), builtin.MethodsMiner.ProveCommitSector, mustEnc(params))
if err != nil {
return cid.Undef, xerrors.Errorf("failed to create genesis miner: %w", err)
}
}
}
}
}
c, err := vm.Flush(ctx)
return c, deals, err
}
func reassignMinerActorAddress(vm *vm.VM, cst cbor.IpldStore, from, to address.Address) error {
if from == to {
return nil
}
act, err := vm.StateTree().GetActor(from)
if err != nil {
return xerrors.Errorf("reassign: failed to get 'from' actor: %w", err)
}
_, err = vm.StateTree().GetActor(to)
if err == nil {
return xerrors.Errorf("cannot reassign actor, target address taken")
}
if err := vm.StateTree().SetActor(to, act); err != nil {
return xerrors.Errorf("failed to reassign actor: %w", err)
}
// TODO: remove from
if err := adjustStoragePowerTracking(vm, cst, from, to); err != nil {
return xerrors.Errorf("adjusting storage market tracking: %w", err)
}
// Now, adjust the tracking in the init actor
return initActorReassign(vm, cst, from, to)
return c, err
}

View File

@ -32,7 +32,7 @@ func doExecValue(ctx context.Context, vm *vm.VM, to, from address.Address, value
return nil, xerrors.Errorf("doExec failed to get from actor: %w", err)
}
ret, err := vm.ApplyMessage(context.TODO(), &types.Message{
ret, err := vm.ApplyMessage(ctx, &types.Message{
To: to,
From: from,
Method: method,