lotus/chain/gen/genesis/miners.go

280 lines
9.0 KiB
Go
Raw Normal View History

2020-02-11 20:48:03 +00:00
package genesis
import (
"bytes"
"context"
"fmt"
"math/rand"
2020-02-18 21:37:59 +00:00
cborutil "github.com/filecoin-project/go-cbor-util"
2020-02-14 21:38:18 +00:00
"github.com/filecoin-project/specs-actors/actors/builtin/power"
2020-02-18 21:37:59 +00:00
cbor "github.com/ipfs/go-ipld-cbor"
2020-02-11 20:48:03 +00:00
"github.com/filecoin-project/go-address"
"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"
2020-04-10 20:04:04 +00:00
"github.com/filecoin-project/lotus/build"
2020-02-18 21:37:59 +00:00
"github.com/filecoin-project/lotus/chain/state"
2020-02-11 20:48:03 +00:00
"github.com/filecoin-project/lotus/chain/store"
2020-04-10 20:04:04 +00:00
"github.com/filecoin-project/lotus/chain/types"
2020-02-11 20:48:03 +00:00
"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) {
2020-02-18 21:37:59 +00:00
networkPower := big.Zero()
for _, m := range miners {
networkPower = big.Add(networkPower, big.NewInt(int64(m.SectorSize)*int64(len(m.Sectors))))
}
2020-02-25 20:54:58 +00:00
vm, err := vm.NewVM(sroot, 0, &fakeRand{}, builtin.SystemActorAddr, 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-27 21:45:31 +00:00
Owner: m.Worker,
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-25 20:54:58 +00:00
rval, err := doExecValue(ctx, vm, builtin.StoragePowerActorAddr, m.Owner, m.PowerBalance, builtin.MethodsPower.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
var ma power.CreateMinerReturn
if err := ma.UnmarshalCBOR(bytes.NewReader(rval)); err != nil {
2020-02-12 02:13:00 +00:00
return cid.Undef, err
}
expma := MinerAddress(uint64(i))
if ma.IDAddress != expma {
return cid.Undef, xerrors.Errorf("miner assigned wrong address: %s != %s", ma.IDAddress, expma)
2020-02-11 20:48:03 +00:00
}
maddr = ma.IDAddress
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(&maddr)
2020-02-25 20:54:58 +00:00
_, err := doExecValue(ctx, vm, builtin.StorageMarketActorAddr, m.Worker, m.MarketBalance, builtin.MethodsMarket.AddBalance, 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-21 16:57:40 +00:00
{
params := mustEnc(&m.Worker)
2020-02-25 20:54:58 +00:00
_, err := doExecValue(ctx, vm, builtin.StorageMarketActorAddr, m.Worker, big.Zero(), builtin.MethodsMarket.AddBalance, params)
2020-02-21 16:57:40 +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
// 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 {
2020-02-12 02:13:00 +00:00
params.Deals = append(params.Deals, market.ClientDealProposal{
Proposal: preseal.Deal,
2020-02-21 19:28:20 +00:00
ClientSignature: crypto.Signature{Type: crypto.SigTypeBLS}, // TODO: do we want to sign these? Or do we want to fake signatures for genesis setup?
2020-02-12 02:13:00 +00:00
})
fmt.Printf("calling publish storage deals on miner %s with worker %s\n", preseal.Deal.Provider, m.Worker)
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-18 21:37:59 +00:00
// setup windowed post
{
err = vm.MutateState(ctx, maddr, func(cst cbor.IpldStore, st *miner.State) error {
// TODO: Randomize so all genesis miners don't fall on the same epoch
st.PoStState.ProvingPeriodStart = miner.ProvingPeriod
return nil
})
payload, err := cborutil.Dump(&miner.CronEventPayload{
EventType: miner.CronEventWindowedPoStExpiration,
})
if err != nil {
return cid.Undef, err
}
2020-02-18 21:37:59 +00:00
params := &power.EnrollCronEventParams{
EventEpoch: miner.ProvingPeriod + power.WindowedPostChallengeDuration,
2020-02-18 21:37:59 +00:00
Payload: payload,
}
_, err = doExecValue(ctx, vm, builtin.StoragePowerActorAddr, maddr, big.Zero(), builtin.MethodsPower.EnrollCronEvent, mustEnc(params))
if err != nil {
2020-02-18 21:37:59 +00:00
return cid.Undef, xerrors.Errorf("failed to verify preseal deals miner: %w", err)
}
}
2020-02-18 21:37:59 +00:00
// Commit sectors
for pi, preseal := range m.Sectors {
// TODO: Maybe check seal (Can just be snark inputs, doesn't go into the genesis file)
2020-02-19 19:26:11 +00:00
// check deals, get dealWeight
2020-02-18 21:37:59 +00:00
dealWeight := big.Zero()
{
params := &market.VerifyDealsOnSectorProveCommitParams{
DealIDs: []abi.DealID{dealIDs[pi]},
SectorExpiry: preseal.Deal.EndEpoch,
}
ret, err := doExecValue(ctx, vm, builtin.StorageMarketActorAddr, maddr, big.Zero(), builtin.MethodsMarket.VerifyDealsOnSectorProveCommit, mustEnc(params))
if err != nil {
return cid.Undef, xerrors.Errorf("failed to verify preseal deals miner: %w", err)
}
if err := dealWeight.UnmarshalCBOR(bytes.NewReader(ret)); err != nil {
return cid.Undef, err
}
}
2020-02-19 19:26:11 +00:00
// update power claims
2020-02-18 21:37:59 +00:00
pledge := big.Zero()
{
err = vm.MutateState(ctx, builtin.StoragePowerActorAddr, func(cst cbor.IpldStore, st *power.State) error {
weight := &power.SectorStorageWeightDesc{
SectorSize: m.SectorSize,
Duration: preseal.Deal.Duration(),
DealWeight: dealWeight,
}
2020-04-10 20:04:04 +00:00
// TODO: This is almost definitely not correct
circSupply := types.BigMul(types.NewInt(build.TotalFilecoin - build.MiningRewardTotal), types.NewInt(build.FilecoinPrecision))
totalPledge := types.NewInt(3)
perEpochReward := types.NewInt(9)
qapower := power.QAPowerForWeight(weight)
pledge = power.InitialPledgeForWeight(qapower, st.TotalQualityAdjPower, circSupply, totalPledge, perEpochReward)
err := st.AddToClaim(&state.AdtStore{cst}, maddr, types.NewInt(uint64(weight.SectorSize)), qapower, pledge)
2020-02-18 21:37:59 +00:00
if err != nil {
return xerrors.Errorf("add to claim: %w", err)
}
2020-04-10 20:04:04 +00:00
fmt.Println("Added weight to claim: ", st.TotalRawBytePower, st.TotalQualityAdjPower)
2020-02-18 21:37:59 +00:00
return nil
})
if err != nil {
return cid.Undef, xerrors.Errorf("register power claim in power actor: %w", err)
}
}
2020-02-18 21:37:59 +00:00
2020-02-19 19:26:11 +00:00
// Put sectors to miner sector sets
2020-02-18 21:37:59 +00:00
{
newSectorInfo := &miner.SectorOnChainInfo{
Info: miner.SectorPreCommitInfo{
RegisteredProof: preseal.ProofType,
SectorNumber: preseal.SectorID,
SealedCID: preseal.CommR,
SealRandEpoch: 0,
DealIDs: []abi.DealID{dealIDs[pi]},
Expiration: preseal.Deal.EndEpoch,
2020-02-18 21:37:59 +00:00
},
ActivationEpoch: 0,
DealWeight: dealWeight,
PledgeRequirement: pledge,
DeclaredFaultEpoch: -1,
DeclaredFaultDuration: -1,
2020-02-18 21:37:59 +00:00
}
err = vm.MutateState(ctx, maddr, func(cst cbor.IpldStore, st *miner.State) error {
store := &state.AdtStore{cst}
if err = st.PutSector(store, newSectorInfo); err != nil {
return xerrors.Errorf("failed to prove commit: %v", err)
}
st.ProvingSet = st.Sectors
return nil
})
2020-02-19 19:26:11 +00:00
if err != nil {
return cid.Cid{}, xerrors.Errorf("put to sset: %w", err)
}
2020-02-18 21:37:59 +00:00
}
{
sectorBf := abi.NewBitField()
sectorBf.Set(uint64(preseal.SectorID))
payload, err := cborutil.Dump(&miner.CronEventPayload{
EventType: miner.CronEventSectorExpiry,
Sectors: &sectorBf,
})
if err != nil {
return cid.Undef, err
}
params := &power.EnrollCronEventParams{
EventEpoch: preseal.Deal.EndEpoch,
Payload: payload,
}
_, err = doExecValue(ctx, vm, builtin.StoragePowerActorAddr, maddr, big.Zero(), builtin.MethodsPower.EnrollCronEvent, mustEnc(params))
if err != nil {
return cid.Undef, xerrors.Errorf("failed to verify preseal deals miner: %w", err)
}
2020-02-12 02:13:00 +00:00
}
2020-02-11 20:48:03 +00:00
}
2020-02-18 21:37:59 +00:00
2020-02-11 20:48:03 +00:00
}
2020-03-11 07:29:17 +00:00
// TODO: to avoid division by zero, we set the initial power actor power to 1, this adjusts that back down so the accounting is accurate.
err = vm.MutateState(ctx, builtin.StoragePowerActorAddr, func(cst cbor.IpldStore, st *power.State) error {
2020-04-10 20:04:04 +00:00
st.TotalQualityAdjPower = big.Sub(st.TotalQualityAdjPower, big.NewInt(1))
2020-03-11 07:29:17 +00:00
return nil
})
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
}
// TODO: copied from actors test harness, deduplicate or remove from here
type fakeRand struct{}
2020-04-08 15:11:42 +00:00
func (fr *fakeRand) GetRandomness(ctx context.Context, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) ([]byte, error) {
out := make([]byte, 32)
_, _ = rand.New(rand.NewSource(int64(randEpoch))).Read(out)
return out, nil
}