lotus/chain/gen/genesis/miners.go

411 lines
13 KiB
Go
Raw Normal View History

2020-02-11 20:48:03 +00:00
package genesis
import (
"bytes"
"context"
"fmt"
"math/rand"
2020-09-23 06:18:52 +00:00
market0 "github.com/filecoin-project/specs-actors/actors/builtin/market"
"github.com/filecoin-project/lotus/chain/actors/builtin/power"
"github.com/filecoin-project/lotus/chain/actors/builtin/reward"
"github.com/filecoin-project/lotus/chain/actors/builtin/market"
2020-09-17 02:34:13 +00:00
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
2020-04-10 21:07:18 +00:00
"github.com/ipfs/go-cid"
2020-02-18 21:37:59 +00:00
cbor "github.com/ipfs/go-ipld-cbor"
2020-04-20 13:54:06 +00:00
cbg "github.com/whyrusleeping/cbor-gen"
2020-04-10 21:07:18 +00:00
"golang.org/x/xerrors"
2020-02-11 20:48:03 +00:00
"github.com/filecoin-project/go-address"
2020-09-07 03:49:10 +00:00
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/crypto"
2020-10-08 01:09:33 +00:00
builtin0 "github.com/filecoin-project/specs-actors/actors/builtin"
miner0 "github.com/filecoin-project/specs-actors/actors/builtin/miner"
power0 "github.com/filecoin-project/specs-actors/actors/builtin/power"
reward0 "github.com/filecoin-project/specs-actors/actors/builtin/reward"
2020-10-08 01:09:33 +00:00
runtime2 "github.com/filecoin-project/specs-actors/v2/actors/runtime"
2020-02-11 20:48:03 +00:00
2020-09-14 13:17:00 +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
}
type fakedSigSyscalls struct {
2020-10-08 01:09:33 +00:00
runtime2.Syscalls
}
func (fss *fakedSigSyscalls) VerifySignature(signature crypto.Signature, signer address.Address, plaintext []byte) error {
return nil
}
2020-07-18 13:46:47 +00:00
func mkFakedSigSyscalls(base vm.SyscallBuilder) vm.SyscallBuilder {
return func(ctx context.Context, rt *vm.Runtime) runtime2.Syscalls {
2020-07-18 13:46:47 +00:00
return &fakedSigSyscalls{
base(ctx, rt),
2020-07-18 13:46:47 +00:00
}
}
}
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) {
csc := func(context.Context, abi.ChainEpoch, *state.StateTree) (abi.TokenAmount, error) {
2020-07-25 22:29:33 +00:00
return big.Zero(), nil
}
vmopt := &vm.VMOpts{
StateBase: sroot,
Epoch: 0,
Rand: &fakeRand{},
Bstore: cs.Blockstore(),
Syscalls: mkFakedSigSyscalls(cs.VMSys()),
CircSupplyCalc: csc,
2020-09-14 12:17:45 +00:00
NtwkVersion: genesisNetworkVersion,
BaseFee: types.NewInt(0),
}
2020-09-14 12:17:45 +00:00
vm, err := vm.NewVM(ctx, vmopt)
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
}
minerInfos := make([]struct {
maddr address.Address
presealExp abi.ChainEpoch
dealIDs []abi.DealID
}, len(miners))
2020-02-12 02:13:00 +00:00
for i, m := range miners {
// Create miner through power actor
i := i
m := m
2020-02-11 20:48:03 +00:00
2020-11-06 17:46:36 +00:00
spt, err := miner.SealProofTypeFromSectorSize(m.SectorSize, GenesisNetworkVersion)
if err != nil {
return cid.Undef, err
}
2020-02-12 02:13:00 +00:00
{
constructorParams := &power0.CreateMinerParams{
2020-04-29 18:06:05 +00:00
Owner: m.Worker,
Worker: m.Worker,
2020-06-05 20:06:11 +00:00
Peer: []byte(m.PeerId),
2020-04-29 18:06:05 +00:00
SealProofType: spt,
2020-02-11 20:48:03 +00:00
}
2020-02-12 02:13:00 +00:00
params := mustEnc(constructorParams)
2020-10-08 01:09:33 +00:00
rval, err := doExecValue(ctx, vm, power.Address, m.Owner, m.PowerBalance, builtin0.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 power0.CreateMinerReturn
if err := ma.UnmarshalCBOR(bytes.NewReader(rval)); err != nil {
2020-04-21 14:32:17 +00:00
return cid.Undef, xerrors.Errorf("unmarshaling CreateMinerReturn: %w", err)
2020-02-12 02:13:00 +00:00
}
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
}
minerInfos[i].maddr = ma.IDAddress
2020-09-17 02:34:13 +00:00
// TODO: ActorUpgrade
err = vm.MutateState(ctx, minerInfos[i].maddr, func(cst cbor.IpldStore, st *miner0.State) error {
maxPeriods := miner0.MaxSectorExpirationExtension / miner0.WPoStProvingPeriod
minerInfos[i].presealExp = (maxPeriods-1)*miner0.WPoStProvingPeriod + st.ProvingPeriodStart - 1
return nil
})
if err != nil {
return cid.Undef, xerrors.Errorf("mutating state: %w", err)
}
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-07-28 16:16:46 +00:00
if m.MarketBalance.GreaterThan(big.Zero()) {
params := mustEnc(&minerInfos[i].maddr)
2020-10-08 01:09:33 +00:00
_, err := doExecValue(ctx, vm, market.Address, m.Worker, m.MarketBalance, builtin0.MethodsMarket.AddBalance, params)
2020-02-12 02:13:00 +00:00
if err != nil {
return cid.Undef, xerrors.Errorf("failed to create genesis miner (add balance): %w", err)
2020-02-12 02:13:00 +00:00
}
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
{
2020-04-20 13:54:06 +00:00
publish := func(params *market.PublishStorageDealsParams) error {
fmt.Printf("publishing %d storage deals on miner %s with worker %s\n", len(params.Deals), params.Deals[0].Proposal.Provider, m.Worker)
2020-10-08 01:09:33 +00:00
ret, err := doExecValue(ctx, vm, market.Address, m.Worker, big.Zero(), builtin0.MethodsMarket.PublishStorageDeals, mustEnc(params))
2020-04-20 13:54:06 +00:00
if err != nil {
return xerrors.Errorf("failed to create genesis miner (publish deals): %w", err)
2020-04-20 13:54:06 +00:00
}
var ids market.PublishStorageDealsReturn
if err := ids.UnmarshalCBOR(bytes.NewReader(ret)); err != nil {
2020-04-21 14:32:17 +00:00
return xerrors.Errorf("unmarsahling publishStorageDeals result: %w", err)
2020-04-20 13:54:06 +00:00
}
minerInfos[i].dealIDs = append(minerInfos[i].dealIDs, ids.IDs...)
2020-04-20 13:54:06 +00:00
return nil
}
2020-02-12 02:13:00 +00:00
params := &market.PublishStorageDealsParams{}
for _, preseal := range m.Sectors {
2020-05-14 02:32:04 +00:00
preseal.Deal.VerifiedDeal = true
preseal.Deal.EndEpoch = minerInfos[i].presealExp
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
})
2020-02-11 20:48:03 +00:00
2020-04-20 13:54:06 +00:00
if len(params.Deals) == cbg.MaxLength {
if err := publish(params); err != nil {
return cid.Undef, err
}
params = &market.PublishStorageDealsParams{}
}
2020-02-11 20:48:03 +00:00
}
2020-04-20 13:54:06 +00:00
if len(params.Deals) > 0 {
if err := publish(params); err != nil {
return cid.Undef, err
}
}
2020-02-11 20:48:03 +00:00
}
}
2020-02-11 20:48:03 +00:00
// adjust total network power for equal pledge per sector
rawPow, qaPow := big.NewInt(0), big.NewInt(0)
{
for i, m := range miners {
for pi := range m.Sectors {
rawPow = types.BigAdd(rawPow, types.NewInt(uint64(m.SectorSize)))
2020-02-18 21:37:59 +00:00
dweight, err := dealWeight(ctx, vm, minerInfos[i].maddr, []abi.DealID{minerInfos[i].dealIDs[pi]}, 0, minerInfos[i].presealExp)
2020-02-18 21:37:59 +00:00
if err != nil {
return cid.Undef, xerrors.Errorf("getting deal weight: %w", err)
2020-02-18 21:37:59 +00:00
}
sectorWeight := miner0.QAPowerForWeight(m.SectorSize, minerInfos[i].presealExp, dweight.DealWeight, dweight.VerifiedDealWeight)
qaPow = types.BigAdd(qaPow, sectorWeight)
2020-02-18 21:37:59 +00:00
}
}
2020-09-23 06:18:52 +00:00
err = vm.MutateState(ctx, power.Address, func(cst cbor.IpldStore, st *power0.State) error {
st.TotalQualityAdjPower = qaPow
st.TotalRawBytePower = rawPow
2020-07-17 21:34:18 +00:00
st.ThisEpochQualityAdjPower = qaPow
st.ThisEpochRawBytePower = rawPow
return nil
})
if err != nil {
return cid.Undef, xerrors.Errorf("mutating state: %w", err)
}
2020-09-23 06:18:52 +00:00
err = vm.MutateState(ctx, reward.Address, func(sct cbor.IpldStore, st *reward0.State) error {
*st = *reward0.ConstructState(qaPow)
return nil
})
if err != nil {
return cid.Undef, xerrors.Errorf("mutating state: %w", err)
}
}
2020-02-18 21:37:59 +00:00
for i, m := range miners {
// Commit sectors
{
for pi, preseal := range m.Sectors {
params := &miner.SectorPreCommitInfo{
SealProof: preseal.ProofType,
SectorNumber: preseal.SectorID,
SealedCID: preseal.CommR,
SealRandEpoch: -1,
DealIDs: []abi.DealID{minerInfos[i].dealIDs[pi]},
Expiration: minerInfos[i].presealExp, // TODO: Allow setting externally!
}
2020-04-10 20:04:04 +00:00
dweight, err := dealWeight(ctx, vm, minerInfos[i].maddr, params.DealIDs, 0, minerInfos[i].presealExp)
if err != nil {
return cid.Undef, xerrors.Errorf("getting deal weight: %w", err)
}
sectorWeight := miner0.QAPowerForWeight(m.SectorSize, minerInfos[i].presealExp, dweight.DealWeight, dweight.VerifiedDealWeight)
// we've added fake power for this sector above, remove it now
2020-09-23 06:18:52 +00:00
err = vm.MutateState(ctx, power.Address, func(cst cbor.IpldStore, st *power0.State) error {
st.TotalQualityAdjPower = types.BigSub(st.TotalQualityAdjPower, sectorWeight) //nolint:scopelint
st.TotalRawBytePower = types.BigSub(st.TotalRawBytePower, types.NewInt(uint64(m.SectorSize)))
2020-02-18 21:37:59 +00:00
return nil
})
if err != nil {
return cid.Undef, xerrors.Errorf("removing fake power: %w", err)
2020-02-18 21:37:59 +00:00
}
epochReward, err := currentEpochBlockReward(ctx, vm, minerInfos[i].maddr)
if err != nil {
return cid.Undef, xerrors.Errorf("getting current epoch reward: %w", err)
2020-02-18 21:37:59 +00:00
}
tpow, err := currentTotalPower(ctx, vm, minerInfos[i].maddr)
if err != nil {
return cid.Undef, xerrors.Errorf("getting current total power: %w", err)
}
2020-02-18 21:37:59 +00:00
pcd := miner0.PreCommitDepositForPower(epochReward.ThisEpochRewardSmoothed, tpow.QualityAdjPowerSmoothed, sectorWeight)
2020-08-18 17:35:46 +00:00
pledge := miner0.InitialPledgeForPower(
2020-07-28 18:55:20 +00:00
sectorWeight,
epochReward.ThisEpochBaselinePower,
tpow.PledgeCollateral,
epochReward.ThisEpochRewardSmoothed,
tpow.QualityAdjPowerSmoothed,
circSupply(ctx, vm, minerInfos[i].maddr),
)
2020-07-28 14:36:32 +00:00
2020-08-18 17:35:46 +00:00
pledge = big.Add(pcd, pledge)
2020-07-17 21:34:18 +00:00
fmt.Println(types.FIL(pledge))
2020-10-08 01:09:33 +00:00
_, err = doExecValue(ctx, vm, minerInfos[i].maddr, m.Worker, pledge, builtin0.MethodsMiner.PreCommitSector, mustEnc(params))
if err != nil {
return cid.Undef, xerrors.Errorf("failed to confirm presealed sectors: %w", err)
}
2020-02-18 21:37:59 +00:00
// Commit one-by-one, otherwise pledge math tends to explode
2020-10-08 01:09:33 +00:00
confirmParams := &builtin0.ConfirmSectorProofsParams{
Sectors: []abi.SectorNumber{preseal.SectorID},
}
2020-10-08 01:09:33 +00:00
_, err = doExecValue(ctx, vm, minerInfos[i].maddr, power.Address, big.Zero(), builtin0.MethodsMiner.ConfirmSectorProofsValid, mustEnc(confirmParams))
2020-02-19 19:26:11 +00:00
if err != nil {
return cid.Undef, xerrors.Errorf("failed to confirm presealed sectors: %w", err)
2020-02-19 19:26:11 +00:00
}
2020-02-18 21:37:59 +00:00
}
2020-02-11 20:48:03 +00:00
}
}
// Sanity-check total network power
2020-09-23 06:18:52 +00:00
err = vm.MutateState(ctx, power.Address, func(cst cbor.IpldStore, st *power0.State) error {
if !st.TotalRawBytePower.Equals(rawPow) {
return xerrors.Errorf("st.TotalRawBytePower doesn't match previously calculated rawPow")
}
if !st.TotalQualityAdjPower.Equals(qaPow) {
return xerrors.Errorf("st.TotalQualityAdjPower doesn't match previously calculated qaPow")
}
2020-03-11 07:29:17 +00:00
return nil
})
if err != nil {
return cid.Undef, xerrors.Errorf("mutating state: %w", err)
}
2020-03-11 07:29:17 +00:00
2020-07-10 05:07:22 +00:00
// TODO: Should we re-ConstructState for the reward actor using rawPow as currRealizedPower here?
2020-02-11 20:48:03 +00:00
c, err := vm.Flush(ctx)
2020-04-21 14:32:17 +00:00
if err != nil {
2020-04-21 21:38:26 +00:00
return cid.Undef, xerrors.Errorf("flushing vm: %w", err)
2020-04-21 14:32:17 +00:00
}
return c, nil
2020-02-11 20:48:03 +00:00
}
// TODO: copied from actors test harness, deduplicate or remove from here
type fakeRand struct{}
2020-08-12 02:47:45 +00:00
func (fr *fakeRand) GetChainRandomness(ctx context.Context, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) ([]byte, error) {
out := make([]byte, 32)
2020-08-24 10:51:55 +00:00
_, _ = rand.New(rand.NewSource(int64(randEpoch * 1000))).Read(out) //nolint
2020-08-12 02:47:45 +00:00
return out, nil
}
func (fr *fakeRand) GetBeaconRandomness(ctx context.Context, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) ([]byte, error) {
out := make([]byte, 32)
2020-08-24 10:51:55 +00:00
_, _ = rand.New(rand.NewSource(int64(randEpoch))).Read(out) //nolint
return out, nil
}
func currentTotalPower(ctx context.Context, vm *vm.VM, maddr address.Address) (*power0.CurrentTotalPowerReturn, error) {
2020-10-08 01:09:33 +00:00
pwret, err := doExecValue(ctx, vm, power.Address, maddr, big.Zero(), builtin0.MethodsPower.CurrentTotalPower, nil)
if err != nil {
return nil, err
}
var pwr power0.CurrentTotalPowerReturn
if err := pwr.UnmarshalCBOR(bytes.NewReader(pwret)); err != nil {
return nil, err
}
return &pwr, nil
}
2020-09-23 06:18:52 +00:00
func dealWeight(ctx context.Context, vm *vm.VM, maddr address.Address, dealIDs []abi.DealID, sectorStart, sectorExpiry abi.ChainEpoch) (market0.VerifyDealsForActivationReturn, error) {
params := &market.VerifyDealsForActivationParams{
DealIDs: dealIDs,
SectorStart: sectorStart,
SectorExpiry: sectorExpiry,
}
2020-09-23 06:18:52 +00:00
var dealWeights market0.VerifyDealsForActivationReturn
ret, err := doExecValue(ctx, vm,
2020-09-23 06:18:52 +00:00
market.Address,
maddr,
abi.NewTokenAmount(0),
2020-10-08 01:09:33 +00:00
builtin0.MethodsMarket.VerifyDealsForActivation,
mustEnc(params),
)
if err != nil {
2020-09-23 06:18:52 +00:00
return market0.VerifyDealsForActivationReturn{}, err
}
if err := dealWeights.UnmarshalCBOR(bytes.NewReader(ret)); err != nil {
2020-09-23 06:18:52 +00:00
return market0.VerifyDealsForActivationReturn{}, err
}
return dealWeights, nil
}
func currentEpochBlockReward(ctx context.Context, vm *vm.VM, maddr address.Address) (*reward0.ThisEpochRewardReturn, error) {
2020-10-08 01:09:33 +00:00
rwret, err := doExecValue(ctx, vm, reward.Address, maddr, big.Zero(), builtin0.MethodsReward.ThisEpochReward, nil)
if err != nil {
2020-07-17 20:58:04 +00:00
return nil, err
}
var epochReward reward0.ThisEpochRewardReturn
if err := epochReward.UnmarshalCBOR(bytes.NewReader(rwret)); err != nil {
2020-07-17 20:58:04 +00:00
return nil, err
}
2020-07-17 20:58:04 +00:00
return &epochReward, nil
}
func circSupply(ctx context.Context, vmi *vm.VM, maddr address.Address) abi.TokenAmount {
unsafeVM := &vm.UnsafeVM{VM: vmi}
rt := unsafeVM.MakeRuntime(ctx, &types.Message{
GasLimit: 1_000_000_000,
From: maddr,
2020-10-19 17:26:07 +00:00
})
return rt.TotalFilCircSupply()
}