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: - Each:
- power.CreateMiner, set msg value to PowerBalance - power.CreateMiner, set msg value to PowerBalance
- market.AddFunds with correct value - market.AddFunds with correct value
- market.PublishDeals for related sectors
- Set precommits - Set precommits
- Commit presealed sectors - Commit presealed sectors
@ -205,7 +206,7 @@ func MakeGenesisBlock(ctx context.Context, bs bstore.Blockstore, sys *types.VMSy
// temp chainstore // temp chainstore
cs := store.NewChainStore(bs, datastore.NewMapDatastore(), sys) 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 { if err != nil {
return nil, xerrors.Errorf("setup storage miners failed: %w", err) 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"
"github.com/filecoin-project/specs-actors/actors/abi/big" "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"
"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/miner"
"github.com/filecoin-project/specs-actors/actors/builtin/power" "github.com/filecoin-project/specs-actors/actors/crypto"
"github.com/filecoin-project/specs-actors/actors/util/adt"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/libp2p/go-libp2p-core/peer"
"golang.org/x/xerrors" "golang.org/x/xerrors"
"github.com/filecoin-project/lotus/chain/actors" "github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/store" "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/chain/vm"
"github.com/filecoin-project/lotus/genesis" "github.com/filecoin-project/lotus/genesis"
) )
type GenMinerCfg struct { func SetupStorageMiners(ctx context.Context, cs *store.ChainStore, sroot cid.Cid, miners []genesis.Miner) (cid.Cid, error) {
PreSeals map[string]genesis.Miner vm, err := vm.NewVM(sroot, 0, nil, actors.SystemAddress, cs.Blockstore(), cs.VMSys())
// 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())
if err != nil { 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 { if len(miners) == 0 {
return cid.Undef, nil, xerrors.New("no genesis miners") return cid.Undef, xerrors.New("no genesis miners")
} }
if len(gmcfg.MinerAddrs) != len(gmcfg.PreSeals) { for i, m := range miners {
return cid.Undef, nil, xerrors.Errorf("miner address list, and preseal count doesn't match (%d != %d)", len(gmcfg.MinerAddrs), len(gmcfg.PreSeals)) // Create miner through power actor
var maddr address.Address
{
constructorParams := &miner.ConstructorParams{
OwnerAddr: m.Owner,
WorkerAddr: m.Worker,
SectorSize: m.SectorSize,
PeerId: m.PeerId,
} }
var deals []actors.StorageDealProposal params := mustEnc(constructorParams)
rval, err := doExecValue(ctx, vm, actors.StoragePowerAddress, m.Worker, m.PowerBalance, actors.SPAMethods.CreateMiner, params)
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 { if err != nil {
return cid.Undef, nil, xerrors.Errorf("failed to create genesis miner: %w", err) return cid.Undef, xerrors.Errorf("failed to create genesis miner: %w", err)
} }
maddrret, err := address.NewFromBytes(rval) maddrret, err := address.NewFromBytes(rval)
if err != nil { if err != nil {
return cid.Undef, nil, err return cid.Undef, err
} }
expma, err := address.NewIDAddress(uint64(MinerStart + i))
_, err = vm.Flush(ctx)
if err != nil { if err != nil {
return cid.Undef, nil, err return cid.Undef, err
}
if maddrret != expma {
return cid.Undef, xerrors.Errorf("miner assigned wrong address: %s != %s", maddrret, expma)
}
maddr = maddrret
} }
cst := cbor.NewCborStore(cs.Blockstore()) // Add market funds
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 { params := mustEnc(&m.Worker)
if sector.Deal.StartEpoch != 0 { _, err := doExecValue(ctx, vm, actors.StorageMarketAddress, m.Worker, m.MarketBalance, builtin.MethodsMarket.AddBalance, params)
return cid.Undef, nil, xerrors.New("all deals must start at epoch 0")
}
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)
if err != nil { 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)
} }
} }
// we have to flush the vm here because it buffers stuff internally for perf reasons // Publish preseal deals
if _, err := vm.Flush(ctx); err != nil {
return cid.Undef, nil, xerrors.Errorf("vm.Flush failed: %w", err) 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{},
})
} }
st := vm.StateTree() ret, err := doExecValue(ctx, vm, builtin.StorageMarketActorAddr, m.Worker, big.Zero(), builtin.MethodsMarket.PublishStorageDeals, mustEnc(params))
mact, err := st.GetActor(maddr)
if err != nil { if err != nil {
return cid.Undef, nil, xerrors.Errorf("get miner actor failed: %w", err) 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
} }
var mstate miner.State dealIDs = ids.IDs
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 { // Publish preseals
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{ for pi, preseal := range m.Sectors {
SectorNumber: s.SectorID, // Precommit
SealedCID: commcid.ReplicaCommitmentV1ToCID(s.CommR[:]), {
params := &miner.PreCommitSectorParams{Info:miner.SectorPreCommitInfo{
SectorNumber: preseal.SectorID,
SealedCID: commcid.ReplicaCommitmentV1ToCID(preseal.CommR[:]),
SealEpoch: 0, SealEpoch: 0,
DealIDs: []abi.DealID{abi.DealID(len(deals))}, DealIDs: []abi.DealID{dealIDs[pi]},
Expiration: 0, Expiration: preseal.Deal.EndEpoch,
}, }}
ActivationEpoch: s.Deal.StartEpoch, _, err := doExecValue(ctx, vm, maddr, m.Worker, big.Zero(), builtin.MethodsMiner.PreCommitSector, mustEnc(params))
DealWeight: weight,
PledgeRequirement: pledgeRequirements[i],
DeclaredFaultEpoch: -1,
DeclaredFaultDuration: -1,
}
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)
}
mstate.Sectors = nssroot.Root()
mstate.ProvingSet = nssroot.Root()
deals = append(deals, s.Deal)
}
nstate, err := cst.Put(ctx, &mstate)
if err != nil { if err != nil {
return cid.Undef, nil, err return cid.Undef, xerrors.Errorf("failed to create genesis miner: %w", err)
}
} }
mact.Head = nstate // Commit
if err := st.SetActor(maddr, mact); err != nil { {
return cid.Undef, nil, err 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) c, err := vm.Flush(ctx)
return c, deals, err return c, 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)
} }

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) 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, To: to,
From: from, From: from,
Method: method, Method: method,