2020-02-11 20:48:03 +00:00
|
|
|
package genesis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-02-18 07:15:30 +00:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
|
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"
|
2021-08-06 10:26:33 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
|
|
|
|
2020-10-08 01:09:33 +00:00
|
|
|
builtin0 "github.com/filecoin-project/specs-actors/actors/builtin"
|
2021-08-06 10:26:33 +00:00
|
|
|
market0 "github.com/filecoin-project/specs-actors/actors/builtin/market"
|
2020-09-18 21:59:27 +00:00
|
|
|
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"
|
2021-08-06 10:26:33 +00:00
|
|
|
market2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/market"
|
|
|
|
reward2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/reward"
|
|
|
|
market4 "github.com/filecoin-project/specs-actors/v4/actors/builtin/market"
|
|
|
|
power4 "github.com/filecoin-project/specs-actors/v4/actors/builtin/power"
|
|
|
|
reward4 "github.com/filecoin-project/specs-actors/v4/actors/builtin/reward"
|
2021-03-10 15:16:44 +00:00
|
|
|
runtime5 "github.com/filecoin-project/specs-actors/v5/actors/runtime"
|
2020-02-11 20:48:03 +00:00
|
|
|
|
2021-08-06 10:26:33 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/adt"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/market"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
|
|
|
|
"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/policy"
|
2021-09-02 16:07:23 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/consensus/filcns"
|
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"
|
|
|
|
)
|
|
|
|
|
2020-02-12 21:41:59 +00:00
|
|
|
func MinerAddress(genesisIndex uint64) address.Address {
|
|
|
|
maddr, err := address.NewIDAddress(MinerStart + genesisIndex)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return maddr
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:36:39 +00:00
|
|
|
type fakedSigSyscalls struct {
|
2021-03-10 15:16:44 +00:00
|
|
|
runtime5.Syscalls
|
2020-05-05 21:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-03-10 15:16:44 +00:00
|
|
|
return func(ctx context.Context, rt *vm.Runtime) runtime5.Syscalls {
|
2020-07-18 13:46:47 +00:00
|
|
|
return &fakedSigSyscalls{
|
2020-10-24 09:57:17 +00:00
|
|
|
base(ctx, rt),
|
2020-07-18 13:46:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
// Note: Much of this is brittle, if the methodNum / param / return changes, it will break things
|
2021-07-27 13:30:23 +00:00
|
|
|
func SetupStorageMiners(ctx context.Context, cs *store.ChainStore, sys vm.SyscallBuilder, sroot cid.Cid, miners []genesis.Miner, nv network.Version) (cid.Cid, error) {
|
2021-05-15 01:11:23 +00:00
|
|
|
|
|
|
|
cst := cbor.NewCborStore(cs.StateBlockstore())
|
2021-08-10 17:07:30 +00:00
|
|
|
av, err := actors.VersionForNetwork(nv)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("failed to get network version: %w", err)
|
|
|
|
}
|
2021-05-15 01:11:23 +00:00
|
|
|
|
2020-08-09 22:49:38 +00:00
|
|
|
csc := func(context.Context, abi.ChainEpoch, *state.StateTree) (abi.TokenAmount, error) {
|
2020-07-25 22:29:33 +00:00
|
|
|
return big.Zero(), nil
|
|
|
|
}
|
|
|
|
|
2020-08-06 17:09:03 +00:00
|
|
|
vmopt := &vm.VMOpts{
|
2020-08-09 22:49:38 +00:00
|
|
|
StateBase: sroot,
|
|
|
|
Epoch: 0,
|
|
|
|
Rand: &fakeRand{},
|
2021-02-28 22:48:36 +00:00
|
|
|
Bstore: cs.StateBlockstore(),
|
2021-09-02 16:07:23 +00:00
|
|
|
Actors: filcns.NewActorRegistry(),
|
2021-07-27 13:30:23 +00:00
|
|
|
Syscalls: mkFakedSigSyscalls(sys),
|
2020-08-09 22:49:38 +00:00
|
|
|
CircSupplyCalc: csc,
|
2021-05-15 01:11:23 +00:00
|
|
|
NtwkVersion: func(_ context.Context, _ abi.ChainEpoch) network.Version {
|
|
|
|
return nv
|
|
|
|
},
|
|
|
|
BaseFee: types.NewInt(0),
|
2020-08-06 17:09:03 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-06-26 13:08:03 +00:00
|
|
|
minerInfos := make([]struct {
|
|
|
|
maddr address.Address
|
|
|
|
|
|
|
|
presealExp abi.ChainEpoch
|
|
|
|
|
|
|
|
dealIDs []abi.DealID
|
|
|
|
}, len(miners))
|
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
maxPeriods := policy.GetMaxSectorExpirationExtension() / miner.WPoStProvingPeriod
|
2020-02-12 02:13:00 +00:00
|
|
|
for i, m := range miners {
|
|
|
|
// Create miner through power actor
|
2020-05-27 20:53:20 +00:00
|
|
|
i := i
|
|
|
|
m := m
|
2020-02-11 20:48:03 +00:00
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
spt, err := miner.SealProofTypeFromSectorSize(m.SectorSize, nv)
|
2020-11-05 16:46:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
2020-11-04 20:29:08 +00:00
|
|
|
|
2020-02-12 02:13:00 +00:00
|
|
|
{
|
2020-09-18 21:59:27 +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)
|
2021-05-15 01:11:23 +00:00
|
|
|
rval, err := doExecValue(ctx, vm, power.Address, m.Owner, m.PowerBalance, power.Methods.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-09-18 21:59:27 +00:00
|
|
|
var ma power0.CreateMinerReturn
|
2020-02-17 18:20:38 +00:00
|
|
|
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
|
|
|
}
|
2020-02-12 21:41:59 +00:00
|
|
|
|
|
|
|
expma := MinerAddress(uint64(i))
|
2020-02-17 18:20:38 +00:00
|
|
|
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
|
|
|
}
|
2020-06-26 13:08:03 +00:00
|
|
|
minerInfos[i].maddr = ma.IDAddress
|
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
_, err = vm.Flush(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("flushing vm: %w", err)
|
|
|
|
}
|
2020-06-26 13:08:03 +00:00
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
mact, err := vm.StateTree().GetActor(minerInfos[i].maddr)
|
2020-08-20 04:49:10 +00:00
|
|
|
if err != nil {
|
2021-05-15 01:11:23 +00:00
|
|
|
return cid.Undef, xerrors.Errorf("getting newly created miner actor: %w", err)
|
2020-08-20 04:49:10 +00:00
|
|
|
}
|
2021-05-15 01:11:23 +00:00
|
|
|
|
|
|
|
mst, err := miner.Load(adt.WrapStore(ctx, cst), mact)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("getting newly created miner state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pps, err := mst.GetProvingPeriodStart()
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("getting newly created miner proving period start: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
minerInfos[i].presealExp = (maxPeriods-1)*miner0.WPoStProvingPeriod + pps - 1
|
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()) {
|
2020-06-26 13:08:03 +00:00
|
|
|
params := mustEnc(&minerInfos[i].maddr)
|
2021-05-15 01:11:23 +00:00
|
|
|
_, err := doExecValue(ctx, vm, market.Address, m.Worker, m.MarketBalance, market.Methods.AddBalance, params)
|
2020-02-12 02:13:00 +00:00
|
|
|
if err != nil {
|
2020-08-19 19:54:33 +00:00
|
|
|
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 {
|
2020-08-19 19:54:33 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-06-26 13:08:03 +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
|
2020-07-19 22:38:52 +00:00
|
|
|
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-06-26 13:08:03 +00:00
|
|
|
}
|
2020-02-11 20:48:03 +00:00
|
|
|
|
2020-06-26 13:08: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
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
dweight, vdweight, err := dealWeight(ctx, vm, minerInfos[i].maddr, []abi.DealID{minerInfos[i].dealIDs[pi]}, 0, minerInfos[i].presealExp, av)
|
2020-02-18 21:37:59 +00:00
|
|
|
if err != nil {
|
2020-06-26 13:08:03 +00:00
|
|
|
return cid.Undef, xerrors.Errorf("getting deal weight: %w", err)
|
2020-02-18 21:37:59 +00:00
|
|
|
}
|
2020-06-26 13:08:03 +00:00
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
sectorWeight := builtin.QAPowerForWeight(m.SectorSize, minerInfos[i].presealExp, dweight, vdweight)
|
2020-06-26 13:08:03 +00:00
|
|
|
|
|
|
|
qaPow = types.BigAdd(qaPow, sectorWeight)
|
2020-02-18 21:37:59 +00:00
|
|
|
}
|
2020-06-26 13:08:03 +00:00
|
|
|
}
|
2020-02-18 07:15:30 +00:00
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
_, err = vm.Flush(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("flushing vm: %w", err)
|
|
|
|
}
|
2020-07-17 21:34:18 +00:00
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
pact, err := vm.StateTree().GetActor(power.Address)
|
2020-06-26 13:08:03 +00:00
|
|
|
if err != nil {
|
2021-05-15 01:11:23 +00:00
|
|
|
return cid.Undef, xerrors.Errorf("getting power actor: %w", err)
|
2020-06-26 13:08:03 +00:00
|
|
|
}
|
2020-07-20 15:59:05 +00:00
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
pst, err := power.Load(adt.WrapStore(ctx, cst), pact)
|
2020-08-20 04:49:10 +00:00
|
|
|
if err != nil {
|
2021-05-15 01:11:23 +00:00
|
|
|
return cid.Undef, xerrors.Errorf("getting power state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = pst.SetTotalQualityAdjPower(qaPow); err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("setting TotalQualityAdjPower in power state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = pst.SetTotalRawBytePower(rawPow); err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("setting TotalRawBytePower in power state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = pst.SetThisEpochQualityAdjPower(qaPow); err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("setting ThisEpochQualityAdjPower in power state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = pst.SetThisEpochRawBytePower(rawPow); err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("setting ThisEpochRawBytePower in power state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pcid, err := cst.Put(ctx, pst.GetState())
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("putting power state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pact.Head = pcid
|
|
|
|
|
|
|
|
if err = vm.StateTree().SetActor(power.Address, pact); err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("setting power state: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-08-10 17:07:30 +00:00
|
|
|
rewact, err := SetupRewardActor(ctx, cs.StateBlockstore(), big.Zero(), av)
|
2021-05-15 01:11:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("setup reward actor: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = vm.StateTree().SetActor(reward.Address, rewact); err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("set reward actor: %w", err)
|
2020-08-20 04:49:10 +00:00
|
|
|
}
|
2020-06-26 13:08:03 +00:00
|
|
|
}
|
2020-02-18 21:37:59 +00:00
|
|
|
|
2020-06-26 13:08:03 +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
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
dweight, vdweight, err := dealWeight(ctx, vm, minerInfos[i].maddr, params.DealIDs, 0, minerInfos[i].presealExp, av)
|
2020-06-26 13:08:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("getting deal weight: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
sectorWeight := builtin.QAPowerForWeight(m.SectorSize, minerInfos[i].presealExp, dweight, vdweight)
|
2020-06-26 13:08:03 +00:00
|
|
|
|
|
|
|
// we've added fake power for this sector above, remove it now
|
2021-05-15 01:11:23 +00:00
|
|
|
|
|
|
|
_, err = vm.Flush(ctx)
|
2020-02-18 21:37:59 +00:00
|
|
|
if err != nil {
|
2021-05-15 01:11:23 +00:00
|
|
|
return cid.Undef, xerrors.Errorf("flushing vm: %w", err)
|
2020-02-18 21:37:59 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
pact, err := vm.StateTree().GetActor(power.Address)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("getting power actor: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pst, err := power.Load(adt.WrapStore(ctx, cst), pact)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("getting power state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pc, err := pst.TotalPower()
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("getting total power: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = pst.SetTotalRawBytePower(types.BigSub(pc.RawBytePower, types.NewInt(uint64(m.SectorSize)))); err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("setting TotalRawBytePower in power state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = pst.SetTotalQualityAdjPower(types.BigSub(pc.QualityAdjPower, sectorWeight)); err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("setting TotalQualityAdjPower in power state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pcid, err := cst.Put(ctx, pst.GetState())
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("putting power state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pact.Head = pcid
|
|
|
|
|
|
|
|
if err = vm.StateTree().SetActor(power.Address, pact); err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("setting power state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
baselinePower, rewardSmoothed, err := currentEpochBlockReward(ctx, vm, minerInfos[i].maddr, av)
|
2020-06-26 13:08:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("getting current epoch reward: %w", err)
|
2020-02-18 21:37:59 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 13:08:03 +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
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
pcd := miner0.PreCommitDepositForPower(&rewardSmoothed, tpow.QualityAdjPowerSmoothed, sectorWeight)
|
2020-08-18 17:35:46 +00:00
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
pledge := miner0.InitialPledgeForPower(
|
2020-07-28 18:55:20 +00:00
|
|
|
sectorWeight,
|
2021-05-15 01:11:23 +00:00
|
|
|
baselinePower,
|
2020-07-28 18:55:20 +00:00
|
|
|
tpow.PledgeCollateral,
|
2021-05-15 01:11:23 +00:00
|
|
|
&rewardSmoothed,
|
2020-07-28 18:55:20 +00:00
|
|
|
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))
|
2021-05-15 01:11:23 +00:00
|
|
|
_, err = doExecValue(ctx, vm, minerInfos[i].maddr, m.Worker, pledge, miner.Methods.PreCommitSector, mustEnc(params))
|
2020-06-26 13:08:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("failed to confirm presealed sectors: %w", err)
|
|
|
|
}
|
2020-02-18 21:37:59 +00:00
|
|
|
|
2020-06-26 13:08:03 +00:00
|
|
|
// Commit one-by-one, otherwise pledge math tends to explode
|
2020-10-08 01:09:33 +00:00
|
|
|
confirmParams := &builtin0.ConfirmSectorProofsParams{
|
2020-06-26 13:08:03 +00:00
|
|
|
Sectors: []abi.SectorNumber{preseal.SectorID},
|
|
|
|
}
|
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
_, err = doExecValue(ctx, vm, minerInfos[i].maddr, power.Address, big.Zero(), miner.Methods.ConfirmSectorProofsValid, mustEnc(confirmParams))
|
2020-02-19 19:26:11 +00:00
|
|
|
if err != nil {
|
2020-06-26 13:08:03 +00:00
|
|
|
return cid.Undef, xerrors.Errorf("failed to confirm presealed sectors: %w", err)
|
2020-02-19 19:26:11 +00:00
|
|
|
}
|
2021-05-15 01:11:23 +00:00
|
|
|
|
2021-08-06 10:26:33 +00:00
|
|
|
if av >= actors.Version2 {
|
|
|
|
// post v0, we need to explicitly Claim this power since ConfirmSectorProofsValid doesn't do it anymore
|
2021-05-15 01:11:23 +00:00
|
|
|
claimParams := &power4.UpdateClaimedPowerParams{
|
|
|
|
RawByteDelta: types.NewInt(uint64(m.SectorSize)),
|
|
|
|
QualityAdjustedDelta: sectorWeight,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = doExecValue(ctx, vm, power.Address, minerInfos[i].maddr, big.Zero(), power.Methods.UpdateClaimedPower, mustEnc(claimParams))
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("failed to confirm presealed sectors: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = vm.Flush(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("flushing vm: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mact, err := vm.StateTree().GetActor(minerInfos[i].maddr)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("getting miner actor: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mst, err := miner.Load(adt.WrapStore(ctx, cst), mact)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("getting miner state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = mst.EraseAllUnproven(); err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("failed to erase unproven sectors: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mcid, err := cst.Put(ctx, mst.GetState())
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("putting miner state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mact.Head = mcid
|
|
|
|
|
|
|
|
if err = vm.StateTree().SetActor(minerInfos[i].maddr, mact); err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("setting miner state: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2020-02-18 21:37:59 +00:00
|
|
|
}
|
2020-02-11 20:48:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 13:08:03 +00:00
|
|
|
// Sanity-check total network power
|
2021-05-15 01:11:23 +00:00
|
|
|
_, err = vm.Flush(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("flushing vm: %w", err)
|
|
|
|
}
|
2020-06-26 13:08:03 +00:00
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
pact, err := vm.StateTree().GetActor(power.Address)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("getting power actor: %w", err)
|
|
|
|
}
|
2020-06-26 13:08:03 +00:00
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
pst, err := power.Load(adt.WrapStore(ctx, cst), pact)
|
2020-06-13 01:34:00 +00:00
|
|
|
if err != nil {
|
2021-05-15 01:11:23 +00:00
|
|
|
return cid.Undef, xerrors.Errorf("getting power state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pc, err := pst.TotalPower()
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("getting total power: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !pc.RawBytePower.Equals(rawPow) {
|
|
|
|
return cid.Undef, xerrors.Errorf("TotalRawBytePower (%s) doesn't match previously calculated rawPow (%s)", pc.RawBytePower, rawPow)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !pc.QualityAdjPower.Equals(qaPow) {
|
|
|
|
return cid.Undef, xerrors.Errorf("QualityAdjPower (%s) doesn't match previously calculated qaPow (%s)", pc.QualityAdjPower, qaPow)
|
2020-06-13 01:34:00 +00:00
|
|
|
}
|
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
|
|
|
}
|
2020-02-18 07:15:30 +00:00
|
|
|
|
|
|
|
// TODO: copied from actors test harness, deduplicate or remove from here
|
|
|
|
type fakeRand struct{}
|
|
|
|
|
2021-05-26 23:03:46 +00:00
|
|
|
func (fr *fakeRand) GetChainRandomnessLookingForward(ctx context.Context, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) ([]byte, error) {
|
2020-08-12 02:47:45 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-05-26 23:03:46 +00:00
|
|
|
func (fr *fakeRand) GetChainRandomnessLookingBack(ctx context.Context, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) ([]byte, error) {
|
|
|
|
out := make([]byte, 32)
|
|
|
|
_, _ = rand.New(rand.NewSource(int64(randEpoch * 1000))).Read(out) //nolint
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fr *fakeRand) GetBeaconRandomnessLookingForward(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) //nolint
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fr *fakeRand) GetBeaconRandomnessLookingBack(ctx context.Context, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) ([]byte, error) {
|
2020-02-18 07:15:30 +00:00
|
|
|
out := make([]byte, 32)
|
2020-08-24 10:51:55 +00:00
|
|
|
_, _ = rand.New(rand.NewSource(int64(randEpoch))).Read(out) //nolint
|
2020-02-18 07:15:30 +00:00
|
|
|
return out, nil
|
|
|
|
}
|
2020-06-26 13:08:03 +00:00
|
|
|
|
2020-09-18 21:59:27 +00:00
|
|
|
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)
|
2020-06-26 13:08:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-18 21:59:27 +00:00
|
|
|
var pwr power0.CurrentTotalPowerReturn
|
2020-06-26 13:08:03 +00:00
|
|
|
if err := pwr.UnmarshalCBOR(bytes.NewReader(pwret)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pwr, nil
|
|
|
|
}
|
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
func dealWeight(ctx context.Context, vm *vm.VM, maddr address.Address, dealIDs []abi.DealID, sectorStart, sectorExpiry abi.ChainEpoch, av actors.Version) (abi.DealWeight, abi.DealWeight, error) {
|
|
|
|
// TODO: This hack should move to market actor wrapper
|
|
|
|
if av <= actors.Version2 {
|
|
|
|
params := &market0.VerifyDealsForActivationParams{
|
|
|
|
DealIDs: dealIDs,
|
|
|
|
SectorStart: sectorStart,
|
|
|
|
SectorExpiry: sectorExpiry,
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, err := doExecValue(ctx, vm,
|
|
|
|
market.Address,
|
|
|
|
maddr,
|
|
|
|
abi.NewTokenAmount(0),
|
|
|
|
builtin0.MethodsMarket.VerifyDealsForActivation,
|
|
|
|
mustEnc(params),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return big.Zero(), big.Zero(), err
|
|
|
|
}
|
2021-08-06 10:26:33 +00:00
|
|
|
var weight, verifiedWeight abi.DealWeight
|
|
|
|
if av < actors.Version2 {
|
|
|
|
var dealWeights market0.VerifyDealsForActivationReturn
|
|
|
|
err = dealWeights.UnmarshalCBOR(bytes.NewReader(ret))
|
|
|
|
weight = dealWeights.DealWeight
|
|
|
|
verifiedWeight = dealWeights.VerifiedDealWeight
|
|
|
|
} else {
|
|
|
|
var dealWeights market2.VerifyDealsForActivationReturn
|
|
|
|
err = dealWeights.UnmarshalCBOR(bytes.NewReader(ret))
|
|
|
|
weight = dealWeights.DealWeight
|
|
|
|
verifiedWeight = dealWeights.VerifiedDealWeight
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-05-15 01:11:23 +00:00
|
|
|
return big.Zero(), big.Zero(), err
|
|
|
|
}
|
|
|
|
|
2021-08-06 10:26:33 +00:00
|
|
|
return weight, verifiedWeight, nil
|
2020-06-26 13:08:03 +00:00
|
|
|
}
|
2021-05-15 01:11:23 +00:00
|
|
|
params := &market4.VerifyDealsForActivationParams{Sectors: []market4.SectorDeals{{
|
|
|
|
SectorExpiry: sectorExpiry,
|
|
|
|
DealIDs: dealIDs,
|
|
|
|
}}}
|
2020-06-26 13:08:03 +00:00
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
var dealWeights market4.VerifyDealsForActivationReturn
|
2020-06-26 13:08:03 +00:00
|
|
|
ret, err := doExecValue(ctx, vm,
|
2020-09-23 06:18:52 +00:00
|
|
|
market.Address,
|
2020-06-26 13:08:03 +00:00
|
|
|
maddr,
|
|
|
|
abi.NewTokenAmount(0),
|
2021-05-15 01:11:23 +00:00
|
|
|
market.Methods.VerifyDealsForActivation,
|
2020-06-26 13:08:03 +00:00
|
|
|
mustEnc(params),
|
|
|
|
)
|
|
|
|
if err != nil {
|
2021-05-15 01:11:23 +00:00
|
|
|
return big.Zero(), big.Zero(), err
|
2020-06-26 13:08:03 +00:00
|
|
|
}
|
|
|
|
if err := dealWeights.UnmarshalCBOR(bytes.NewReader(ret)); err != nil {
|
2021-05-15 01:11:23 +00:00
|
|
|
return big.Zero(), big.Zero(), err
|
2020-06-26 13:08:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
return dealWeights.Sectors[0].DealWeight, dealWeights.Sectors[0].VerifiedDealWeight, nil
|
2020-06-26 13:08:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
func currentEpochBlockReward(ctx context.Context, vm *vm.VM, maddr address.Address, av actors.Version) (abi.StoragePower, builtin.FilterEstimate, error) {
|
|
|
|
rwret, err := doExecValue(ctx, vm, reward.Address, maddr, big.Zero(), reward.Methods.ThisEpochReward, nil)
|
2020-06-26 13:08:03 +00:00
|
|
|
if err != nil {
|
2021-05-15 01:11:23 +00:00
|
|
|
return big.Zero(), builtin.FilterEstimate{}, err
|
2020-06-26 13:08:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
// TODO: This hack should move to reward actor wrapper
|
2021-08-06 10:26:33 +00:00
|
|
|
switch av {
|
|
|
|
case actors.Version0:
|
2021-05-15 01:11:23 +00:00
|
|
|
var epochReward reward0.ThisEpochRewardReturn
|
|
|
|
|
|
|
|
if err := epochReward.UnmarshalCBOR(bytes.NewReader(rwret)); err != nil {
|
|
|
|
return big.Zero(), builtin.FilterEstimate{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return epochReward.ThisEpochBaselinePower, *epochReward.ThisEpochRewardSmoothed, nil
|
2021-08-06 10:26:33 +00:00
|
|
|
case actors.Version2:
|
|
|
|
var epochReward reward2.ThisEpochRewardReturn
|
|
|
|
|
|
|
|
if err := epochReward.UnmarshalCBOR(bytes.NewReader(rwret)); err != nil {
|
|
|
|
return big.Zero(), builtin.FilterEstimate{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return epochReward.ThisEpochBaselinePower, builtin.FilterEstimate(epochReward.ThisEpochRewardSmoothed), nil
|
2021-05-15 01:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var epochReward reward4.ThisEpochRewardReturn
|
|
|
|
|
2020-06-26 13:08:03 +00:00
|
|
|
if err := epochReward.UnmarshalCBOR(bytes.NewReader(rwret)); err != nil {
|
2021-05-15 01:11:23 +00:00
|
|
|
return big.Zero(), builtin.FilterEstimate{}, err
|
2020-06-26 13:08:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 01:11:23 +00:00
|
|
|
return epochReward.ThisEpochBaselinePower, builtin.FilterEstimate(epochReward.ThisEpochRewardSmoothed), nil
|
2020-06-26 13:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
})
|
2020-06-26 13:08:03 +00:00
|
|
|
|
|
|
|
return rt.TotalFilCircSupply()
|
|
|
|
}
|