Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verifreg balance to datacap #110

Merged
merged 2 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions builtin/v9/migration/datacap.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
)

type datacapMigrator struct {
emptyMapCid cid.Cid
verifregStateV8 verifreg8.State
OutCodeCID cid.Cid
emptyMapCid cid.Cid
verifregStateV8 verifreg8.State
OutCodeCID cid.Cid
pendingVerifiedDealSize uint64
}

func (d *datacapMigrator) migratedCodeCID() cid.Cid {
Expand Down Expand Up @@ -74,6 +75,9 @@ func (d *datacapMigrator) migrateState(ctx context.Context, store cbor.IpldStore
}); err != nil {
return nil, xerrors.Errorf("failed to loop over verified clients: %w", err)
}
verifregBalance := big.Mul(big.NewIntUnsigned(d.pendingVerifiedDealSize), verifreg9.DataCapGranularity)
tokenSupply = big.Add(tokenSupply, verifregBalance)
balancesMap.Put(abi.IdAddrKey(builtin.VerifiedRegistryActorAddr), &verifregBalance)

balancesMapRoot, err := balancesMap.Root()
if err != nil {
Expand Down
15 changes: 11 additions & 4 deletions builtin/v9/migration/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ func MigrateStateTree(ctx context.Context, store cbor.IpldStore, newManifestCID
return cid.Undef, xerrors.Errorf("failed to get market actor state: %w", err)
}

// Find verified pending deals for both datacap and verifreg migrations
pendingVerifiedDeals, pendingVerifiedDealSize, err := getPendingVerifiedDealsAndTotalSize(ctx, adtStore, marketStateV8)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to get pending verified deals")
}

proposals, err := market8.AsDealProposalArray(adtStore, marketStateV8.Proposals)
if err != nil {
return cid.Undef, xerrors.Errorf("failed to get proposals: %w", err)
Expand Down Expand Up @@ -255,9 +261,10 @@ func MigrateStateTree(ctx context.Context, store cbor.IpldStore, newManifestCID
}

migrations[dataCapCode] = &datacapMigrator{
emptyMapCid: emptyMapCid,
verifregStateV8: verifregStateV8,
OutCodeCID: dataCapCode,
emptyMapCid: emptyMapCid,
verifregStateV8: verifregStateV8,
OutCodeCID: dataCapCode,
pendingVerifiedDealSize: pendingVerifiedDealSize,
}

// The Verifreg & Market Actor need special handling,
Expand Down Expand Up @@ -291,7 +298,7 @@ func MigrateStateTree(ctx context.Context, store cbor.IpldStore, newManifestCID
marketHead: cid.Undef,
err: nil,
}
verifregHead, dealAllocationTuples, err := migrateVerifreg(ctx, adtStore, priorEpoch, initStateV8, marketStateV8, verifregStateV8, emptyMapCid)
verifregHead, dealAllocationTuples, err := migrateVerifreg(ctx, adtStore, priorEpoch, initStateV8, marketStateV8, pendingVerifiedDeals, verifregStateV8, emptyMapCid)
if err != nil {
ret.err = xerrors.Errorf("failed to migrate verifreg actor: %w", err)
verifregMarketResultCh <- ret
Expand Down
47 changes: 47 additions & 0 deletions builtin/v9/migration/util.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package migration

import (
"context"
"sync"

"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/builtin"
market8 "github.com/filecoin-project/go-state-types/builtin/v8/market"
adt8 "github.com/filecoin-project/go-state-types/builtin/v8/util/adt"
"github.com/ipfs/go-cid"
"golang.org/x/xerrors"
)
Expand Down Expand Up @@ -61,3 +66,45 @@ func (m *MemMigrationCache) Update(other *MemMigrationCache) {
return true
})
}

func getPendingVerifiedDealsAndTotalSize(ctx context.Context, adtStore adt8.Store, marketStateV8 market8.State) ([]abi.DealID, uint64, error) {
pendingProposals, err := adt8.AsSet(adtStore, marketStateV8.PendingProposals, builtin.DefaultHamtBitwidth)
if err != nil {
return nil, 0, xerrors.Errorf("failed to load pending proposals: %w", err)
}

proposals, err := market8.AsDealProposalArray(adtStore, marketStateV8.Proposals)
if err != nil {
return nil, 0, xerrors.Errorf("failed to get proposals: %w", err)
}
var pendingVerifiedDeals []abi.DealID
pendingSize := uint64(0)
var proposal market8.DealProposal
if err = proposals.ForEach(&proposal, func(dealID int64) error {
// Nothing to do for unverified deals
if !proposal.VerifiedDeal {
return nil
}

pcid, err := proposal.Cid()
if err != nil {
return err
}

isPending, err := pendingProposals.Has(abi.CidKey(pcid))
if err != nil {
return xerrors.Errorf("failed to check pending: %w", err)
}

// Nothing to do for not-pending deals
if !isPending {
return nil
}
pendingVerifiedDeals = append(pendingVerifiedDeals, abi.DealID(dealID))
pendingSize += uint64(proposal.PieceSize)
return nil
}); err != nil {
return nil, 0, xerrors.Errorf("failed to iterate over proposals: %w", err)
}
return pendingVerifiedDeals, pendingSize, nil
}
40 changes: 9 additions & 31 deletions builtin/v9/migration/verifreg.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ type DealAllocationTuple struct {
Allocation verifreg9.AllocationId
}

func migrateVerifreg(ctx context.Context, adtStore adt8.Store, priorEpoch abi.ChainEpoch, initStateV8 init8.State, marketStateV8 market8.State, verifregStateV8 verifreg8.State, emptyMapCid cid.Cid) (cid.Cid, []DealAllocationTuple, error) {
pendingProposals, err := adt8.AsSet(adtStore, marketStateV8.PendingProposals, builtin.DefaultHamtBitwidth)
if err != nil {
return cid.Undef, nil, xerrors.Errorf("failed to load pending proposals: %w", err)
}
func migrateVerifreg(ctx context.Context, adtStore adt8.Store, priorEpoch abi.ChainEpoch, initStateV8 init8.State, marketStateV8 market8.State, pendingDeals []abi.DealID, verifregStateV8 verifreg8.State, emptyMapCid cid.Cid) (cid.Cid, []DealAllocationTuple, error) {

proposals, err := market8.AsDealProposalArray(adtStore, marketStateV8.Proposals)
if err != nil {
Expand All @@ -39,38 +35,23 @@ func migrateVerifreg(ctx context.Context, adtStore adt8.Store, priorEpoch abi.Ch
nextAllocationID := verifreg9.AllocationId(1)
allocationsMapMap := make(map[address.Address]*adt9.Map)
var dealAllocationTuples []DealAllocationTuple
var proposal market8.DealProposal
if err = proposals.ForEach(&proposal, func(dealID int64) error {
// Nothing to do for unverified deals
if !proposal.VerifiedDeal {
return nil
}

pcid, err := proposal.Cid()
if err != nil {
return err
}

isPending, err := pendingProposals.Has(abi.CidKey(pcid))
if err != nil {
return xerrors.Errorf("failed to check pending: %w", err)
}

// Nothing to do for not-pending deals
if !isPending {
return nil
for _, dealID := range pendingDeals {
proposal, err := proposals.GetDealProposal(dealID)
if err != nil || proposal == nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

proposal == nil should not be reachable but I figured an explicit error is minutely nicer than a panic

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

return cid.Undef, nil, xerrors.Errorf("failed to get pending deal proposal %d: %w", dealID, err)
}

clientIDAddress, clientIDu64, _, providerIDu64, err := resolveDealAddresses(adtStore, initStateV8, proposal)
clientIDAddress, clientIDu64, _, providerIDu64, err := resolveDealAddresses(adtStore, initStateV8, *proposal)
if err != nil {
return xerrors.Errorf("failed to resolve proposal addresses %w: ", err)
return cid.Undef, nil, xerrors.Errorf("failed to resolve proposal addresses %w: ", err)
}

clientAllocationMap, ok := allocationsMapMap[clientIDAddress]
if !ok {
clientAllocationMap, err = adt9.AsMap(adtStore, emptyMapCid, builtin.DefaultHamtBitwidth)
if err != nil {
return xerrors.Errorf("failed to load empty map: %w", err)
return cid.Undef, nil, xerrors.Errorf("failed to load empty map: %w", err)
}

allocationsMapMap[clientIDAddress] = clientAllocationMap
Expand All @@ -90,7 +71,7 @@ func migrateVerifreg(ctx context.Context, adtStore adt8.Store, priorEpoch abi.Ch
TermMax: market9.DealMaxDuration,
Expiration: expiration,
}); err != nil {
return xerrors.Errorf("failed to put new allocation obj: %w", err)
return cid.Undef, nil, xerrors.Errorf("failed to put new allocation obj: %w", err)
}

dealAllocationTuples = append(dealAllocationTuples, DealAllocationTuple{
Expand All @@ -100,9 +81,6 @@ func migrateVerifreg(ctx context.Context, adtStore adt8.Store, priorEpoch abi.Ch

nextAllocationID++

return nil
}); err != nil {
return cid.Undef, nil, xerrors.Errorf("failed to iterate over proposals: %w", err)
}

allocationsMap, err := adt9.AsMap(adtStore, emptyMapCid, builtin.DefaultHamtBitwidth)
Expand Down