genesis: Cleanup miner creation
This commit is contained in:
parent
23b662fe7d
commit
ca3eabafef
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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 deals []actors.StorageDealProposal
|
var maddr address.Address
|
||||||
|
{
|
||||||
for i, maddr := range gmcfg.MinerAddrs {
|
constructorParams := &miner.ConstructorParams{
|
||||||
ps, psok := gmcfg.PreSeals[maddr.String()]
|
OwnerAddr: m.Owner,
|
||||||
if !psok {
|
WorkerAddr: m.Worker,
|
||||||
return cid.Undef, nil, xerrors.Errorf("no preseal for miner %s", maddr)
|
SectorSize: m.SectorSize,
|
||||||
}
|
PeerId: m.PeerId,
|
||||||
|
|
||||||
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")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dur := big.NewInt(int64(sector.Deal.Duration()))
|
params := mustEnc(constructorParams)
|
||||||
siz := big.NewInt(int64(sector.Deal.PieceSize))
|
rval, err := doExecValue(ctx, vm, actors.StoragePowerAddress, m.Worker, m.PowerBalance, actors.SPAMethods.CreateMiner, params)
|
||||||
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)
|
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
|
// Publish preseal deals
|
||||||
if _, err := vm.Flush(ctx); err != nil {
|
|
||||||
return cid.Undef, nil, xerrors.Errorf("vm.Flush failed: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
st := vm.StateTree()
|
var dealIDs []abi.DealID
|
||||||
mact, err := st.GetActor(maddr)
|
{
|
||||||
if err != nil {
|
params := &market.PublishStorageDealsParams{}
|
||||||
return cid.Undef, nil, xerrors.Errorf("get miner actor failed: %w", err)
|
for _, preseal := range m.Sectors {
|
||||||
}
|
params.Deals = append(params.Deals, market.ClientDealProposal{
|
||||||
|
Proposal: preseal.Deal,
|
||||||
var mstate miner.State
|
ClientSignature: crypto.Signature{},
|
||||||
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,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nssroot := adt.AsArray(cs.Store(ctx), mstate.Sectors)
|
ret, err := doExecValue(ctx, vm, builtin.StorageMarketActorAddr, m.Worker, big.Zero(), builtin.MethodsMarket.PublishStorageDeals, mustEnc(params))
|
||||||
|
if err != nil {
|
||||||
if err := nssroot.Set(uint64(s.SectorID), &oci); err != nil {
|
return cid.Undef, xerrors.Errorf("failed to create genesis miner: %w", err)
|
||||||
return cid.Cid{}, nil, xerrors.Errorf("add sector to set: %w", err)
|
}
|
||||||
|
var ids market.PublishStorageDealsReturn
|
||||||
|
if err := ids.UnmarshalCBOR(bytes.NewReader(ret)); err != nil {
|
||||||
|
return cid.Undef, err
|
||||||
}
|
}
|
||||||
|
|
||||||
mstate.Sectors = nssroot.Root()
|
dealIDs = ids.IDs
|
||||||
mstate.ProvingSet = nssroot.Root()
|
|
||||||
|
|
||||||
deals = append(deals, s.Deal)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nstate, err := cst.Put(ctx, &mstate)
|
// Publish preseals
|
||||||
if err != nil {
|
|
||||||
return cid.Undef, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
mact.Head = nstate
|
{
|
||||||
if err := st.SetActor(maddr, mact); err != nil {
|
for pi, preseal := range m.Sectors {
|
||||||
return cid.Undef, nil, err
|
// 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)
|
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)
|
|
||||||
}
|
}
|
||||||
|
@ -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,
|
||||||
|
Loading…
Reference in New Issue
Block a user