lotus/chain/gen/genesis/miners.go

144 lines
4.1 KiB
Go
Raw Normal View History

2020-02-11 20:48:03 +00:00
package genesis
import (
"bytes"
"context"
2020-02-14 21:38:18 +00:00
"github.com/filecoin-project/specs-actors/actors/builtin/power"
2020-02-11 20:48:03 +00:00
"github.com/filecoin-project/go-address"
commcid "github.com/filecoin-project/go-fil-commcid"
"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"
2020-02-12 02:13:00 +00:00
"github.com/filecoin-project/specs-actors/actors/builtin/market"
2020-02-11 20:48:03 +00:00
"github.com/filecoin-project/specs-actors/actors/builtin/miner"
2020-02-12 02:13:00 +00:00
"github.com/filecoin-project/specs-actors/actors/crypto"
2020-02-11 20:48:03 +00:00
"github.com/ipfs/go-cid"
"golang.org/x/xerrors"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/vm"
"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
}
2020-02-12 02:13:00 +00:00
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())
2020-02-11 20:48:03 +00:00
if err != nil {
2020-02-12 02:13:00 +00:00
return cid.Undef, xerrors.Errorf("failed to create NewVM: %w", err)
2020-02-11 20:48:03 +00:00
}
2020-02-12 02:13:00 +00:00
if len(miners) == 0 {
return cid.Undef, xerrors.New("no genesis miners")
2020-02-11 20:48:03 +00:00
}
2020-02-12 02:13:00 +00:00
for i, m := range miners {
// Create miner through power actor
2020-02-11 20:48:03 +00:00
2020-02-12 02:13:00 +00:00
var maddr address.Address
{
2020-02-14 21:38:18 +00:00
constructorParams := &power.CreateMinerParams{
2020-02-14 21:38:30 +00:00
Worker: m.Worker,
2020-02-12 02:13:00 +00:00
SectorSize: m.SectorSize,
2020-02-14 21:38:30 +00:00
Peer: m.PeerId,
2020-02-11 20:48:03 +00:00
}
2020-02-12 02:13:00 +00:00
params := mustEnc(constructorParams)
2020-02-14 21:38:18 +00:00
rval, err := doExecValue(ctx, vm, actors.StoragePowerAddress, m.Owner, m.PowerBalance, actors.SPAMethods.CreateMiner, params)
2020-02-12 02:13:00 +00:00
if err != nil {
return cid.Undef, xerrors.Errorf("failed to create genesis miner: %w", err)
}
2020-02-11 20:48:03 +00:00
2020-02-12 02:13:00 +00:00
maddrret, err := address.NewFromBytes(rval)
if err != nil {
return cid.Undef, err
}
expma := MinerAddress(uint64(i))
2020-02-12 02:13:00 +00:00
if maddrret != expma {
return cid.Undef, xerrors.Errorf("miner assigned wrong address: %s != %s", maddrret, expma)
2020-02-11 20:48:03 +00:00
}
2020-02-12 02:13:00 +00:00
maddr = maddrret
2020-02-11 20:48:03 +00:00
}
2020-02-12 02:13:00 +00:00
// Add market funds
2020-02-11 20:48:03 +00:00
2020-02-12 02:13:00 +00:00
{
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)
}
2020-02-11 20:48:03 +00:00
}
2020-02-12 02:13:00 +00:00
// Publish preseal deals
2020-02-11 20:48:03 +00:00
2020-02-12 02:13:00 +00:00
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{},
})
2020-02-11 20:48:03 +00:00
}
2020-02-12 02:13:00 +00:00
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
2020-02-11 20:48:03 +00:00
}
2020-02-12 02:13:00 +00:00
dealIDs = ids.IDs
2020-02-11 20:48:03 +00:00
}
2020-02-12 02:13:00 +00:00
// Publish preseals
{
for pi, preseal := range m.Sectors {
// Precommit
{
2020-02-14 00:24:24 +00:00
params := &miner.SectorPreCommitInfo{
2020-02-12 02:13:00 +00:00
SectorNumber: preseal.SectorID,
SealedCID: commcid.ReplicaCommitmentV1ToCID(preseal.CommR[:]),
SealEpoch: 0,
DealIDs: []abi.DealID{dealIDs[pi]},
Expiration: preseal.Deal.EndEpoch,
2020-02-14 00:24:24 +00:00
}
2020-02-12 02:13:00 +00:00
_, 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)
}
}
}
2020-02-11 20:48:03 +00:00
}
}
c, err := vm.Flush(ctx)
2020-02-12 02:13:00 +00:00
return c, err
2020-02-11 20:48:03 +00:00
}