lotus/chain/gen/genesis/miners.go

655 lines
21 KiB
Go
Raw Normal View History

2020-02-11 20:48:03 +00:00
package genesis
import (
"bytes"
"context"
"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"
2022-06-14 15:00:51 +00:00
cborutil "github.com/filecoin-project/go-cbor-util"
2020-09-07 03:49:10 +00:00
"github.com/filecoin-project/go-state-types/abi"
2022-09-06 15:49:29 +00:00
actorstypes "github.com/filecoin-project/go-state-types/actors"
2020-09-07 03:49:10 +00:00
"github.com/filecoin-project/go-state-types/big"
2022-06-14 15:00:51 +00:00
builtintypes "github.com/filecoin-project/go-state-types/builtin"
2023-04-05 19:25:16 +00:00
power11 "github.com/filecoin-project/go-state-types/builtin/v11/power"
2022-06-14 15:00:51 +00:00
minertypes "github.com/filecoin-project/go-state-types/builtin/v8/miner"
build: release: v1.18.0 (#9652) * build: Bump version to v1.17.3-dev * build: set version to v1.18.0-dev * chore: actors: Allow builtin-actors to return a map of methods (#9342) * Allow builtin-actors to return a map of methods * go mod * Fix tests * Fix tests, check carefully please * Delete lotus-pond (#9352) * feat: add StateNetworkVersion to mpool API * chore: refactor: rename NewestNetworkVersion * feat: actors: Integrate datacap actor into lotus (#9348) * Integrate datacap actor * Implement datacap actor in chain/builtin * feat: support typed errors over RPC * chore: deps: update to go-jsonrpc 0.1.8 * remove duplicate import * fix: itest: check for closed connection * chore: refactor: move retry test to API * address magik supernit * Add ability to only have single partition per msg for partitions with recovery sectors * doc gen * Address comments * Return beneficiary info from miner state Info() * Update builtin-actors to dev/20220922-v9 which includes FIP-0045 changes in progress * Integrate verifreg changes to lotus * Setup datacap actor * Update builtin-actors to dev/20220922-v9-1 * Update datacap actor to query datacap instead of verifreg * update gst * update markets * update actors with hamt fix * update gst * Update datacap to parse tokens * Update bundles * datacap and verifreg actors use ID addresses without protocol byte * update builtin-actors to rc1 * update go-fil-markets * Update bundles to rc2 * Integrate the v9 migration * Add api for getting allocation * Add upgrade epoch for butterfly * Tweak PreSeal struct to be infra-friendly * docsgen * More tweaking of PreSeal for genesis * review fixes * Use fake cid for test * add butterfly artifacts for oct 5 upgrade * check datacaps for v8 verifreg match v9 datacap actor * Remove print statements * Update to go-state-types master * Update to go-state-types v0.9.0-rc1 * review fixes * use go-fil-markets v1.24.0-v17 * Add accessors for allocations and claims maps * fix: missing permissions tag * butterfly * update butterfly artifacts * sealing pipeline: Prepare deal assigning logic for FIP-45 * sealing pipeline: Get allocationId with StateApi * use NoAllocationID instead of nil AllocationId * address review * Add datacap actor to registry.go * Add cli for listing allocations and removing expired allocations * Update to go-state-types master * deps: upgrade go-merkledag to 0.8.0 * shark params * Update cli/filplus.go Co-authored-by: Aayush Rajasekaran <arajasek94@gmail.com> * revert change to verifreg util * docsgen-cli * miss the stuff * Update FFI * Update go-state-types to v0.9.0 * Update builtin-actors to v9.0.0 * add calib upgrade epcoh * update the upgrade envvar * kill shark * Remove fvm splash banner from nv17 upgrade * check invariance for pending deals and allocations * check pending verified deal proposal migrated to allocation * Add check for unsealed CID in precommit sectors * Fix counting of allocations in nv17 migration test * make gen * pass state trees as pointers * Add assertion that migrations with & without cache are the same * compare allocation to verified deal proposal * Fix miner state precommit info * fix migration test tool * add changelog * Update to go-state-types v0.9.1 * Integrate builtin-actors v9.0.1 * chore: ver: bump version for rc3 (#9512) * Bump version to 1.18.0-rc3 * Update CHANGELOG.md * Update CHANGELOG.md Co-authored-by: Aayush Rajasekaran <arajasek94@gmail.com> * Update CHANGELOG.md Co-authored-by: Aayush Rajasekaran <arajasek94@gmail.com> Co-authored-by: Jiaying Wang <42981373+jennijuju@users.noreply.github.com> Co-authored-by: Aayush Rajasekaran <arajasek94@gmail.com> * Migration: Use autobatch bs * Fix autobatch Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai> * Invoker: Use MethodMeta from go-state-types * Add a second premigration for nv17 * Add more shed tools for migration checking * address review * Lotus release v1.18.0-rc4 * fix: ci: fix app-image build on ci (#9527) * Remove old go version first * Add GO_VERSION file * Use GO_VERSION to set / verify go version * mv GO_VERSION GO_VERSION_MIN * Use GO_VERSION_MIN in Makefile check Co-authored-by: Ian Davis <jungziege@gmail.com> * Update to latest go-state-types for migration fixes * go mod tidy * fix: use api.ErrActorNotFound instead of types.ErrActorNotFound * fix: add fields to ForkUpgradeParams * docs: update actors_version_checklist.md * chore: fix lint * update to go state type v0.9.6 with market migration fix (#9545) * update go-state-types to v-0.9.7 * Add invariant checks to migration * fix invariant check: number of entries in datacap actor should include verifreg * Invariant checks: Only include not-activated deals * test: nv17 migration * Address review * add lotus-shed invariance method * Migration cli takes a stateroot cid and a height * make gen * Update to builtin-actors v9.0.2 * Failing test that shows that notaries can remove datacap from the verifreg actor * Test that should pass when the problem is solved * make gen * Review fixes * statemanager call function will return call information even if call errors * update go-state-types * update builtin-actors * bubble up errors properly from ApplyImplicitMessage * bump to rc5 * set new upgrade heights for calibnet * set new upgrade height for butterfly * tweak calibnet upgrade schedule * clarify changelog note about calibnet * butterfly * update calibnet artifacts * Allow setting local bundles for Debug FVM for av 9+ * fix: autobatch: remove potential deadlock when a block is missing Check the _underlying_ blockstore instead of recursing. Also, drop the lock before we do that. * fix imports * build: set shark mainnet epoch (#9640) * chore: build: Lotus release v1.18.0 (#9641) * Lotus release v1.18.0 * add changelog * address review * changelog improvement Co-authored-by: Jennifer Wang <jiayingw703@gmail.com> Co-authored-by: Jiaying Wang <42981373+jennijuju@users.noreply.github.com> Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai> Co-authored-by: Łukasz Magiera <magik6k@gmail.com> Co-authored-by: Łukasz Magiera <magik6k@users.noreply.github.com> Co-authored-by: Aayush <arajasek94@gmail.com> Co-authored-by: Geoff Stuart <geoff.vball@gmail.com> Co-authored-by: Shrenuj Bansal <shrenuj.bansal@protocol.ai> Co-authored-by: simlecode <69969590+simlecode@users.noreply.github.com> Co-authored-by: Rod Vagg <rod@vagg.org> Co-authored-by: Jakub Sztandera <kubuxu@protocol.ai> Co-authored-by: Ian Davis <jungziege@gmail.com> Co-authored-by: zenground0 <ZenGround0@users.noreply.github.com> Co-authored-by: Steven Allen <steven@stebalien.com>
2022-11-16 01:57:23 +00:00
markettypes "github.com/filecoin-project/go-state-types/builtin/v9/market"
2022-09-09 20:42:55 +00:00
miner9 "github.com/filecoin-project/go-state-types/builtin/v9/miner"
smoothing9 "github.com/filecoin-project/go-state-types/builtin/v9/util/smoothing"
2020-09-07 03:49:10 +00:00
"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"
2022-06-14 15:00:51 +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"
2023-04-05 19:25:16 +00:00
power2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/power"
2021-08-06 10:26:33 +00:00
reward2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/reward"
power4 "github.com/filecoin-project/specs-actors/v4/actors/builtin/power"
reward4 "github.com/filecoin-project/specs-actors/v4/actors/builtin/reward"
2022-06-14 15:00:51 +00:00
builtin6 "github.com/filecoin-project/specs-actors/v6/actors/builtin"
runtime7 "github.com/filecoin-project/specs-actors/v7/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/adt"
2022-06-14 15:00:51 +00:00
"github.com/filecoin-project/lotus/chain/actors/builtin"
2021-08-06 10:26:33 +00:00
"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/chain/wallet/key"
2020-02-11 20:48:03 +00:00
"github.com/filecoin-project/lotus/genesis"
2022-06-14 15:00:51 +00:00
"github.com/filecoin-project/lotus/lib/sigs"
2020-02-11 20:48:03 +00:00
)
func MinerAddress(genesisIndex uint64) address.Address {
maddr, err := address.NewIDAddress(MinerStart + genesisIndex)
if err != nil {
panic(err)
}
return maddr
}
type fakedSigSyscalls struct {
2021-11-04 15:59:29 +00:00
runtime7.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 {
2021-11-04 15:59:29 +00:00
return func(ctx context.Context, rt *vm.Runtime) runtime7.Syscalls {
2020-07-18 13:46:47 +00:00
return &fakedSigSyscalls{
base(ctx, rt),
2020-07-18 13:46:47 +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) {
cst := cbor.NewCborStore(cs.StateBlockstore())
2022-09-06 15:49:29 +00:00
av, err := actorstypes.VersionForNetwork(nv)
2021-08-10 17:07:30 +00:00
if err != nil {
return cid.Undef, xerrors.Errorf("failed to get network version: %w", err)
}
csc := func(context.Context, abi.ChainEpoch, *state.StateTree) (abi.TokenAmount, error) {
2020-07-25 22:29:33 +00:00
return big.Zero(), nil
}
2022-05-15 19:26:52 +00:00
newVM := func(base cid.Cid) (vm.Interface, error) {
vmopt := &vm.VMOpts{
StateBase: base,
Epoch: 0,
Rand: &fakeRand{},
Bstore: cs.StateBlockstore(),
Actors: filcns.NewActorRegistry(),
Syscalls: mkFakedSigSyscalls(sys),
CircSupplyCalc: csc,
NetworkVersion: nv,
BaseFee: big.Zero(),
}
return vm.NewVM(ctx, vmopt)
}
2022-05-15 19:26:52 +00:00
genesisVm, err := newVM(sroot)
2020-02-11 20:48:03 +00:00
if err != nil {
2022-05-15 19:26:52 +00:00
return cid.Undef, fmt.Errorf("creating vm: %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
2022-09-08 20:31:12 +00:00
dealIDs []abi.DealID
sectorWeight []abi.StoragePower
}, len(miners))
2022-04-20 21:34:28 +00:00
maxPeriods := policy.GetMaxSectorExpirationExtension() / minertypes.WPoStProvingPeriod
2022-09-08 20:31:12 +00:00
rawPow, qaPow := big.NewInt(0), big.NewInt(0)
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
spt, err := miner.SealProofTypeFromSectorSize(m.SectorSize, nv)
if err != nil {
return cid.Undef, err
}
2020-02-12 02:13:00 +00:00
{
2023-04-05 19:25:16 +00:00
var params []byte
if nv <= network.Version10 {
constructorParams := &power2.CreateMinerParams{
Owner: m.Worker,
Worker: m.Worker,
Peer: []byte(m.PeerId),
SealProofType: spt,
}
params = mustEnc(constructorParams)
} else {
ppt, err := spt.RegisteredWindowPoStProofByNetworkVersion(nv)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to convert spt to wpt: %w", err)
}
constructorParams := &power11.CreateMinerParams{
Owner: m.Worker,
Worker: m.Worker,
Peer: []byte(m.PeerId),
WindowPoStProofType: ppt,
}
params = mustEnc(constructorParams)
2020-02-11 20:48:03 +00:00
}
2022-05-15 19:26:52 +00:00
rval, err := doExecValue(ctx, genesisVm, 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
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
2022-05-15 19:26:52 +00:00
nh, err := genesisVm.Flush(ctx)
if err != nil {
return cid.Undef, xerrors.Errorf("flushing vm: %w", err)
}
2022-05-15 19:26:52 +00:00
nst, err := state.LoadStateTree(cst, nh)
if err != nil {
return cid.Undef, xerrors.Errorf("loading new state tree: %w", err)
}
mact, err := nst.GetActor(minerInfos[i].maddr)
if err != nil {
return cid.Undef, xerrors.Errorf("getting newly created miner actor: %w", err)
}
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()) {
params := mustEnc(&minerInfos[i].maddr)
2022-05-15 19:26:52 +00:00
_, err := doExecValue(ctx, genesisVm, market.Address, m.Worker, m.MarketBalance, market.Methods.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
}
2022-09-08 20:31:12 +00:00
// Publish preseal deals, and calculate the QAPower
2020-02-11 20:48:03 +00:00
2020-02-12 02:13:00 +00:00
{
2022-04-20 21:34:28 +00:00
publish := func(params *markettypes.PublishStorageDealsParams) error {
2020-04-20 13:54:06 +00:00
fmt.Printf("publishing %d storage deals on miner %s with worker %s\n", len(params.Deals), params.Deals[0].Proposal.Provider, m.Worker)
2022-05-15 19:26:52 +00:00
ret, err := doExecValue(ctx, genesisVm, 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
}
retval, err := market.DecodePublishStorageDealsReturn(ret, nv)
if err != nil {
return xerrors.Errorf("failed to create genesis miner (decoding published deals): %w", err)
}
ids, err := retval.DealIDs()
if err != nil {
return xerrors.Errorf("failed to create genesis miner (getting published dealIDs): %w", err)
}
if len(ids) != len(params.Deals) {
return xerrors.Errorf("failed to create genesis miner (at least one deal was invalid on publication")
2020-04-20 13:54:06 +00:00
}
minerInfos[i].dealIDs = append(minerInfos[i].dealIDs, ids...)
2020-04-20 13:54:06 +00:00
return nil
}
2022-04-20 21:34:28 +00:00
params := &markettypes.PublishStorageDealsParams{}
2020-02-12 02:13:00 +00:00
for _, preseal := range m.Sectors {
2020-05-14 02:32:04 +00:00
preseal.Deal.VerifiedDeal = true
preseal.Deal.EndEpoch = minerInfos[i].presealExp
p := markettypes.ClientDealProposal{
Proposal: preseal.Deal,
ClientSignature: crypto.Signature{Type: crypto.SigTypeBLS},
2022-05-15 19:26:52 +00:00
}
2022-09-06 15:49:29 +00:00
if av >= actorstypes.Version8 {
buf, err := cborutil.Dump(&preseal.Deal)
if err != nil {
return cid.Undef, fmt.Errorf("failed to marshal proposal: %w", err)
}
sig, err := sigs.Sign(key.ActSigType(preseal.DealClientKey.Type), preseal.DealClientKey.PrivateKey, buf)
if err != nil {
return cid.Undef, fmt.Errorf("failed to sign proposal: %w", err)
}
p.ClientSignature = *sig
2022-05-15 19:26:52 +00:00
}
params.Deals = append(params.Deals, p)
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
}
2022-04-20 21:34:28 +00:00
params = &markettypes.PublishStorageDealsParams{}
2020-04-20 13:54:06 +00:00
}
2022-09-08 20:31:12 +00:00
rawPow = big.Add(rawPow, big.NewInt(int64(m.SectorSize)))
2022-09-10 14:34:47 +00:00
sectorWeight := builtin.QAPowerForWeight(m.SectorSize, minerInfos[i].presealExp, big.Zero(), markettypes.DealWeight(&preseal.Deal))
2022-09-08 20:31:12 +00:00
minerInfos[i].sectorWeight = append(minerInfos[i].sectorWeight, sectorWeight)
qaPow = big.Add(qaPow, sectorWeight)
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
2022-09-09 20:42:55 +00:00
{
2022-05-15 19:26:52 +00:00
nh, err := genesisVm.Flush(ctx)
if err != nil {
return cid.Undef, xerrors.Errorf("flushing vm: %w", err)
}
2020-07-17 21:34:18 +00:00
2022-05-15 19:26:52 +00:00
if err != nil {
return cid.Undef, xerrors.Errorf("flushing vm: %w", err)
}
nst, err := state.LoadStateTree(cst, nh)
if err != nil {
return cid.Undef, xerrors.Errorf("loading new state tree: %w", err)
}
pact, err := nst.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)
}
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
2022-05-15 19:26:52 +00:00
if err = nst.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)
if err != nil {
return cid.Undef, xerrors.Errorf("setup reward actor: %w", err)
}
2022-05-15 19:26:52 +00:00
if err = nst.SetActor(reward.Address, rewact); err != nil {
return cid.Undef, xerrors.Errorf("set reward actor: %w", err)
}
2022-05-15 19:26:52 +00:00
nh, err = nst.Flush(ctx)
if err != nil {
return cid.Undef, xerrors.Errorf("flushing state tree: %w", err)
}
genesisVm, err = newVM(nh)
if err != nil {
return cid.Undef, fmt.Errorf("creating new vm: %w", err)
}
}
2020-02-18 21:37:59 +00:00
for i, m := range miners {
// Commit sectors
{
for pi, preseal := range m.Sectors {
2022-04-20 21:34:28 +00:00
params := &minertypes.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
2022-09-08 20:31:12 +00:00
sectorWeight := minerInfos[i].sectorWeight[pi]
// we've added fake power for this sector above, remove it now
2022-05-15 19:26:52 +00:00
nh, err := genesisVm.Flush(ctx)
2020-02-18 21:37:59 +00:00
if err != nil {
return cid.Undef, xerrors.Errorf("flushing vm: %w", err)
2020-02-18 21:37:59 +00:00
}
2022-05-15 19:26:52 +00:00
nst, err := state.LoadStateTree(cst, nh)
if err != nil {
return cid.Undef, xerrors.Errorf("loading new state tree: %w", err)
}
pact, err := nst.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
2022-05-15 19:26:52 +00:00
if err = nst.SetActor(power.Address, pact); err != nil {
return cid.Undef, xerrors.Errorf("setting power state: %w", err)
}
2022-05-15 19:26:52 +00:00
nh, err = nst.Flush(ctx)
if err != nil {
return cid.Undef, xerrors.Errorf("flushing state tree: %w", err)
}
genesisVm, err = newVM(nh)
if err != nil {
return cid.Undef, fmt.Errorf("creating new vm: %w", err)
}
baselinePower, rewardSmoothed, err := currentEpochBlockReward(ctx, genesisVm, minerInfos[i].maddr, av)
if err != nil {
return cid.Undef, xerrors.Errorf("getting current epoch reward: %w", err)
2020-02-18 21:37:59 +00:00
}
2022-05-15 19:26:52 +00:00
tpow, err := currentTotalPower(ctx, genesisVm, 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
2022-09-08 20:31:12 +00:00
pcd := miner9.PreCommitDepositForPower(smoothing9.FilterEstimate(rewardSmoothed), smoothing9.FilterEstimate(*tpow.QualityAdjPowerSmoothed), miner9.QAPowerMax(m.SectorSize))
2020-08-18 17:35:46 +00:00
2022-09-08 20:31:12 +00:00
pledge := miner9.InitialPledgeForPower(
2020-07-28 18:55:20 +00:00
sectorWeight,
baselinePower,
2022-09-08 20:31:12 +00:00
smoothing9.FilterEstimate(rewardSmoothed),
smoothing9.FilterEstimate(*tpow.QualityAdjPowerSmoothed),
2022-05-15 19:26:52 +00:00
big.Zero(),
2020-07-28 18:55:20 +00:00
)
2020-07-28 14:36:32 +00:00
2020-08-18 17:35:46 +00:00
pledge = big.Add(pcd, pledge)
2022-05-15 19:26:52 +00:00
_, err = doExecValue(ctx, genesisVm, minerInfos[i].maddr, m.Worker, pledge, builtintypes.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
2021-09-30 00:44:27 +00:00
var paramBytes []byte
2022-09-06 15:49:29 +00:00
if av >= actorstypes.Version6 {
2021-09-30 00:44:27 +00:00
// TODO: fixup
confirmParams := &builtin6.ConfirmSectorProofsParams{
Sectors: []abi.SectorNumber{preseal.SectorID},
}
paramBytes = mustEnc(confirmParams)
} else {
confirmParams := &builtin0.ConfirmSectorProofsParams{
Sectors: []abi.SectorNumber{preseal.SectorID},
}
paramBytes = mustEnc(confirmParams)
}
2022-05-15 19:26:52 +00:00
_, err = doExecValue(ctx, genesisVm, minerInfos[i].maddr, power.Address, big.Zero(), builtintypes.MethodsMiner.ConfirmSectorProofsValid, paramBytes)
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
}
2022-09-06 15:49:29 +00:00
if av >= actorstypes.Version2 {
2021-08-06 10:26:33 +00:00
// post v0, we need to explicitly Claim this power since ConfirmSectorProofsValid doesn't do it anymore
claimParams := &power4.UpdateClaimedPowerParams{
RawByteDelta: types.NewInt(uint64(m.SectorSize)),
QualityAdjustedDelta: sectorWeight,
}
2022-05-15 19:26:52 +00:00
_, err = doExecValue(ctx, genesisVm, 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)
}
2022-05-15 19:26:52 +00:00
nh, err := genesisVm.Flush(ctx)
if err != nil {
return cid.Undef, xerrors.Errorf("flushing vm: %w", err)
}
2022-05-15 19:26:52 +00:00
nst, err := state.LoadStateTree(cst, nh)
if err != nil {
return cid.Undef, xerrors.Errorf("loading new state tree: %w", err)
}
mact, err := nst.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
2022-05-15 19:26:52 +00:00
if err = nst.SetActor(minerInfos[i].maddr, mact); err != nil {
return cid.Undef, xerrors.Errorf("setting miner state: %w", err)
}
2022-05-15 19:26:52 +00:00
nh, err = nst.Flush(ctx)
if err != nil {
return cid.Undef, xerrors.Errorf("flushing state tree: %w", err)
}
genesisVm, err = newVM(nh)
if err != nil {
return cid.Undef, fmt.Errorf("creating new vm: %w", err)
}
}
2020-02-18 21:37:59 +00:00
}
2020-02-11 20:48:03 +00:00
}
}
// Sanity-check total network power
2022-05-15 19:26:52 +00:00
nh, err := genesisVm.Flush(ctx)
if err != nil {
2022-05-15 19:26:52 +00:00
return cid.Undef, fmt.Errorf("flushing vm: %w", err)
}
2022-05-15 19:26:52 +00:00
nst, err := state.LoadStateTree(cst, nh)
if err != nil {
return cid.Undef, xerrors.Errorf("loading new state tree: %w", err)
}
pact, err := nst.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 !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-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?
2022-05-15 19:26:52 +00:00
c, err := genesisVm.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
}
2022-05-15 19:26:52 +00:00
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{}
func (fr *fakeRand) GetChainRandomness(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
}
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
}
2022-05-15 19:26:52 +00:00
func currentTotalPower(ctx context.Context, vm vm.Interface, 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
}
2022-09-06 15:49:29 +00:00
func currentEpochBlockReward(ctx context.Context, vm vm.Interface, maddr address.Address, av actorstypes.Version) (abi.StoragePower, builtin.FilterEstimate, error) {
rwret, err := doExecValue(ctx, vm, reward.Address, maddr, big.Zero(), reward.Methods.ThisEpochReward, nil)
if err != nil {
return big.Zero(), builtin.FilterEstimate{}, err
}
// TODO: This hack should move to reward actor wrapper
2021-08-06 10:26:33 +00:00
switch av {
2022-09-06 15:49:29 +00:00
case actorstypes.Version0:
var epochReward reward0.ThisEpochRewardReturn
if err := epochReward.UnmarshalCBOR(bytes.NewReader(rwret)); err != nil {
return big.Zero(), builtin.FilterEstimate{}, err
}
2022-04-20 21:34:28 +00:00
return epochReward.ThisEpochBaselinePower, builtin.FilterEstimate(*epochReward.ThisEpochRewardSmoothed), nil
2022-09-06 15:49:29 +00:00
case actorstypes.Version2:
2021-08-06 10:26:33 +00:00
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
}
var epochReward reward4.ThisEpochRewardReturn
if err := epochReward.UnmarshalCBOR(bytes.NewReader(rwret)); err != nil {
return big.Zero(), builtin.FilterEstimate{}, err
}
return epochReward.ThisEpochBaselinePower, builtin.FilterEstimate(epochReward.ThisEpochRewardSmoothed), nil
}