Skip to content

Commit

Permalink
Upgrade to HAMT and AMT v3.0.0 (filecoin-project#1356)
Browse files Browse the repository at this point in the history
  • Loading branch information
anorth authored and bibibong committed Feb 20, 2021
1 parent e3e73b5 commit 2566eb8
Show file tree
Hide file tree
Showing 37 changed files with 694 additions and 507 deletions.
2 changes: 1 addition & 1 deletion actors/builtin/expert/expert_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func ConstructExpertInfo(owner addr.Address, eType ExpertType, aHash string) (*E
}

func ConstructState(store adt.Store, info cid.Cid, state ExpertState, emptyChange cid.Cid) (*State, error) {
emptyMapCid, err := adt.MakeEmptyMap(store, builtin.DefaultHamtBitwidth).Root()
emptyMapCid, err := adt.StoreEmptyMap(store, builtin.DefaultHamtBitwidth)
if err != nil {
return nil, xerrors.Errorf("failed to create empty map: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion actors/builtin/expertfund/expertfund_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type PoolInfo struct {

// ConstructState expert fund construct
func ConstructState(store adt.Store, pool cid.Cid) (*State, error) {
emptyMapCid, err := adt.MakeEmptyMap(store, builtin.DefaultHamtBitwidth).Root()
emptyMapCid, err := adt.StoreEmptyMap(store, builtin.DefaultHamtBitwidth)
if err != nil {
return nil, xerrors.Errorf("failed to create empty map: %w", err)
}
Expand Down
7 changes: 5 additions & 2 deletions actors/builtin/govern/govern_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func ConstructState(store adt.Store, supervisor address.Address) (*State, error)
return nil, xerrors.New("supervisor address must be an ID address")
}

emptyMapCid, err := adt.MakeEmptyMap(store, builtin.DefaultHamtBitwidth).Root()
emptyMapCid, err := adt.StoreEmptyMap(store, builtin.DefaultHamtBitwidth)
if err != nil {
return nil, xerrors.Errorf("failed to create empty map: %w", err)
}
Expand Down Expand Up @@ -85,7 +85,10 @@ func (st *State) grantOrRevoke(store adt.Store, governors *adt.Map, governor add
if !grant { // do nothing for revoke
return nil
}
mp = adt.MakeEmptyMap(store, builtin.DefaultHamtBitwidth)
mp, err = adt.MakeEmptyMap(store, builtin.DefaultHamtBitwidth)
if err != nil {
return xerrors.Errorf("failed to create empty map: %w", err)
}
} else {
mp, err = adt.AsMap(store, out.CodeMethods, builtin.DefaultHamtBitwidth)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions actors/builtin/govern/govern_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package govern_test

import (
"testing"

"github.com/filecoin-project/specs-actors/v2/actors/builtin/govern"
"github.com/filecoin-project/specs-actors/v2/support/mock"
)

func TestExports(t *testing.T) {
mock.CheckActorExports(t, govern.Actor{})
}
2 changes: 1 addition & 1 deletion actors/builtin/knowledge/knowledge_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func ConstructState(store adt.Store, initialPayee address.Address) (*State, erro
return nil, xerrors.New("intial payee address must be an ID address")
}

emptyMapCid, err := adt.MakeEmptyMap(store, builtin.DefaultHamtBitwidth).Root()
emptyMapCid, err := adt.StoreEmptyMap(store, builtin.DefaultHamtBitwidth)
if err != nil {
return nil, xerrors.Errorf("failed to create empty map: %w", err)
}
Expand Down
35 changes: 17 additions & 18 deletions actors/builtin/market/index_multimap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package market

import (
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-hamt-ipld/v3"
"github.com/filecoin-project/go-state-types/abi"
cid "github.com/ipfs/go-cid"
"github.com/pkg/errors"
Expand All @@ -29,14 +28,21 @@ func AsIndexMultimap(s adt.Store, r cid.Cid, outerBitwidth, innerBitwidth int) (
}

// Creates a new map backed by an empty HAMT and flushes it to the store.
func MakeEmptyIndexMultimap(s adt.Store, bitwidth int) *IndexMultimap {
m := adt.MakeEmptyMap(s, bitwidth)
return &IndexMultimap{mp: m, store: s, innerBitwidth: bitwidth}
func MakeEmptyIndexMultimap(s adt.Store, bitwidth int) (*IndexMultimap, error) {
m, err := adt.MakeEmptyMap(s, bitwidth)
if err != nil {
return nil, err
}
return &IndexMultimap{mp: m, store: s, innerBitwidth: bitwidth}, nil
}

// Writes a new empty map to the store and returns its CID.
func StoreEmptyIndexMultimap(s adt.Store, bitwidth int) (cid.Cid, error) {
return MakeEmptyIndexMultimap(s, bitwidth).Root()
mm, err := MakeEmptyIndexMultimap(s, bitwidth)
if err != nil {
return cid.Undef, err
}
return mm.Root()
}

// Returns the root cid of the underlying HAMT.
Expand All @@ -56,7 +62,10 @@ func (mm *IndexMultimap) Put(epoch abi.ChainEpoch, providerIndexes map[address.A
return err
}
if !found {
pimap = adt.MakeEmptyMap(mm.store, mm.innerBitwidth)
pimap, err = adt.MakeEmptyMap(mm.store, mm.innerBitwidth)
if err != nil {
return err
}
}

for provider, indexes := range providerIndexes {
Expand Down Expand Up @@ -115,18 +124,8 @@ func (mm *IndexMultimap) Put(epoch abi.ChainEpoch, providerIndexes map[address.A

// Removes all values for a piece.
func (mm *IndexMultimap) RemoveAll(key abi.ChainEpoch) error {
k := abi.UIntKey(uint64(key))
found, err := mm.mp.Has(k)
if err != nil {
return err
}
if !found {
return nil
}

err = mm.mp.Delete(k)
if err != nil && !xerrors.Is(err, hamt.ErrNotFound) {
return xerrors.Errorf("failed to delete map key %v: %w", key, err)
if _, err := mm.mp.TryDelete(abi.UIntKey(uint64(key))); err != nil {
return xerrors.Errorf("failed to delete set key %v: %w", key, err)
}
return nil
}
Expand Down
5 changes: 2 additions & 3 deletions actors/builtin/market/market_actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,6 @@ func (a Actor) CronTick(rt Runtime, _ *abi.EmptyValue) *abi.EmptyValue {

pdErr := msm.pendingDeals.Delete(abi.CidKey(dcid))
builtin.RequireNoErr(rt, pdErr, exitcode.ErrIllegalState, "failed to delete pending proposal %v", dcid)

return nil
}

Expand Down Expand Up @@ -736,8 +735,8 @@ func genTerminateEpoch(currEpoch abi.ChainEpoch, dealID abi.DealID) (abi.ChainEp
func deleteDealProposalAndState(dealId abi.DealID, states *DealMetaArray, proposals *DealArray, removeProposal bool,
removeState bool) error {
if removeProposal {
if err := proposals.Delete(uint64(dealId)); err != nil {
return xerrors.Errorf("failed to delete deal proposal: %w", err)
if err := proposals.Delete(dealId); err != nil {
return xerrors.Errorf("failed to delete proposal %d : %w", dealId, err)
}
}

Expand Down
7 changes: 4 additions & 3 deletions actors/builtin/market/market_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func TestRemoveAllError(t *testing.T) {
rt := builder.Build(t)
store := adt.AsStore(rt)

smm := market.MakeEmptySetMultimap(store, builtin.DefaultHamtBitwidth)
smm, err := market.MakeEmptySetMultimap(store, builtin.DefaultHamtBitwidth)
require.NoError(t, err)

if err := smm.RemoveAll(42); err != nil {
t.Fatalf("expected no error, got: %s", err)
Expand Down Expand Up @@ -98,7 +99,7 @@ func TestMarketActor(t *testing.T) {
emptyStatesArrayCid, err := adt.StoreEmptyArray(store, market.StatesAmtBitwidth)
assert.NoError(t, err)

emptyMultiMap, err := market.MakeEmptySetMultimap(store, builtin.DefaultHamtBitwidth).Root()
emptyMultiMap, err := market.StoreEmptySetMultimap(store, builtin.DefaultHamtBitwidth)
assert.NoError(t, err)

var state market.State
Expand Down Expand Up @@ -3372,7 +3373,7 @@ func (h *marketActorTestHarness) deleteDealProposal(rt *mock.Runtime, dealId abi
rt.GetState(&st)
deals, err := market.AsDealProposalArray(adt.AsStore(rt), st.Proposals)
require.NoError(h.t, err)
require.NoError(h.t, deals.Delete(uint64(dealId)))
require.NoError(h.t, deals.Delete(dealId))
st.Proposals, err = deals.Root()
require.NoError(h.t, err)
rt.ReplaceState(&st)
Expand Down
30 changes: 20 additions & 10 deletions actors/builtin/market/set_multimap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package market
import (
"reflect"

"github.com/filecoin-project/go-hamt-ipld/v3"
"github.com/filecoin-project/go-state-types/abi"
cid "github.com/ipfs/go-cid"
"github.com/pkg/errors"
Expand Down Expand Up @@ -31,15 +30,21 @@ func AsSetMultimap(s adt.Store, r cid.Cid, outerBitwidth, innerBitwidth int) (*S

// Creates a new map backed by an empty HAMT and flushes it to the store.
// Both inner and outer HAMTs have branching factor 2^bitwidth.
func MakeEmptySetMultimap(s adt.Store, bitwidth int) *SetMultimap {
m := adt.MakeEmptyMap(s, bitwidth)
return &SetMultimap{mp: m, store: s, innerBitwidth: bitwidth}
func MakeEmptySetMultimap(s adt.Store, bitwidth int) (*SetMultimap, error) {
m, err := adt.MakeEmptyMap(s, bitwidth)
if err != nil {
return nil, err
}
return &SetMultimap{mp: m, store: s, innerBitwidth: bitwidth}, nil
}

// Writes a new empty map to the store and returns its CID.
func StoreEmptySetMultimap(s adt.Store, bitwidth int) (cid.Cid, error) {
m := MakeEmptySetMultimap(s, bitwidth)
return m.Root()
mm, err := MakeEmptySetMultimap(s, bitwidth)
if err != nil {
return cid.Undef, err
}
return mm.Root()
}

// Returns the root cid of the underlying HAMT.
Expand All @@ -55,7 +60,10 @@ func (mm *SetMultimap) Put(epoch abi.ChainEpoch, v abi.DealID) error {
return err
}
if !found {
set = adt.MakeEmptySet(mm.store, mm.innerBitwidth)
set, err = adt.MakeEmptySet(mm.store, mm.innerBitwidth)
if err != nil {
return err
}
}

// Add to the set.
Expand Down Expand Up @@ -84,7 +92,10 @@ func (mm *SetMultimap) PutMany(epoch abi.ChainEpoch, vs []abi.DealID) error {
return err
}
if !found {
set = adt.MakeEmptySet(mm.store, mm.innerBitwidth)
set, err = adt.MakeEmptySet(mm.store, mm.innerBitwidth)
if err != nil {
return err
}
}

// Add to the set.
Expand All @@ -109,8 +120,7 @@ func (mm *SetMultimap) PutMany(epoch abi.ChainEpoch, vs []abi.DealID) error {

// Removes all values for a key.
func (mm *SetMultimap) RemoveAll(key abi.ChainEpoch) error {
err := mm.mp.Delete(abi.UIntKey(uint64(key)))
if err != nil && !xerrors.Is(err, hamt.ErrNotFound) {
if _, err := mm.mp.TryDelete(abi.UIntKey(uint64(key))); err != nil {
return xerrors.Errorf("failed to delete set key %v: %w", key, err)
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions actors/builtin/market/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func (t *DealArray) Set(k abi.DealID, value *DealProposal) error {
return t.Array.Set(uint64(k), value)
}

func (t *DealArray) Delete(key uint64) error {
return t.Array.Delete(key)
func (t *DealArray) Delete(id abi.DealID) error {
return t.Array.Delete(uint64(id))
}

// A specialization of a array to deals.
Expand Down
4 changes: 2 additions & 2 deletions actors/builtin/miner/bitfield_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (q BitfieldQueue) Cut(toCut bitfield.BitField) error {
}); err != nil {
return xerrors.Errorf("failed to cut from bitfield queue: %w", err)
}
if err := q.BatchDelete(epochsToRemove); err != nil {
if err := q.BatchDelete(epochsToRemove, true); err != nil {
return xerrors.Errorf("failed to remove empty epochs from bitfield queue: %w", err)
}
return nil
Expand Down Expand Up @@ -141,7 +141,7 @@ func (q BitfieldQueue) PopUntil(until abi.ChainEpoch) (values bitfield.BitField,
return bitfield.New(), false, nil
}

if err = q.BatchDelete(poppedKeys); err != nil {
if err = q.BatchDelete(poppedKeys, true); err != nil {
return bitfield.BitField{}, false, err
}
merged, err := bitfield.MultiMerge(poppedValues...)
Expand Down
13 changes: 4 additions & 9 deletions actors/builtin/miner/deadline_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1142,21 +1142,16 @@ func (dl *Deadline) TakePoStProofs(store adt.Store, idx uint64) (partitions bitf
if err != nil {
return bitfield.New(), nil, xerrors.Errorf("failed to load proofs: %w", err)
}

// Extract and remove the proof from the proofs array, leaving a hole.
// This will not affect concurrent attempts to refute other proofs.
var post WindowedPoSt
found, err := proofArr.Get(idx, &post)
if err != nil {
if found, err := proofArr.Pop(idx, &post); err != nil {
return bitfield.New(), nil, xerrors.Errorf("failed to retrieve proof %d: %w", idx, err)
} else if !found {
return bitfield.New(), nil, xc.ErrIllegalArgument.Wrapf("proof %d not found", idx)
}

// Delete the proof from the proofs array, leaving a hole.
// This will not affect concurrent attempts to refute other proofs.
err = proofArr.Delete(idx)
if err != nil {
return bitfield.New(), nil, xerrors.Errorf("failed to delete proof %d: %w", idx, err)
}

root, err := proofArr.Root()
if err != nil {
return bitfield.New(), nil, xerrors.Errorf("failed to save proofs: %w", err)
Expand Down
6 changes: 3 additions & 3 deletions actors/builtin/miner/expiration_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func (q ExpirationQueue) RescheduleAllAsFaults(faultExpiration abi.ChainEpoch) e
}

// Trim the rescheduled epochs from the queue.
if err = q.BatchDelete(rescheduledEpochs); err != nil {
if err = q.BatchDelete(rescheduledEpochs, true); err != nil {
return err
}

Expand Down Expand Up @@ -564,7 +564,7 @@ func (q ExpirationQueue) PopUntil(until abi.ChainEpoch) (*ExpirationSet, error)
return NewExpirationSetEmpty(), nil
}

if err := q.Array.BatchDelete(poppedKeys); err != nil {
if err := q.Array.BatchDelete(poppedKeys, true); err != nil {
return nil, err
}

Expand Down Expand Up @@ -674,7 +674,7 @@ func (q ExpirationQueue) traverseMutate(f func(epoch abi.ChainEpoch, es *Expirat
}); err != nil && err != errStop {
return err
}
if err := q.Array.BatchDelete(epochsEmptied); err != nil {
if err := q.Array.BatchDelete(epochsEmptied, true); err != nil {
return err
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion actors/builtin/miner/partition_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ func (p *Partition) PopEarlyTerminations(store adt.Store, maxSectors uint64) (re
}

// Update early terminations
err = earlyTerminatedQ.BatchDelete(processed)
err = earlyTerminatedQ.BatchDelete(processed, true)
if err != nil {
return TerminationResult{}, false, xerrors.Errorf("failed to remove entries from early terminations queue: %w", err)
}
Expand Down
Loading

0 comments on commit 2566eb8

Please sign in to comment.