diff --git a/actors/builtin/init/init_actor_state.go b/actors/builtin/init/init_actor_state.go index 89b8d8e62d..c7256db486 100644 --- a/actors/builtin/init/init_actor_state.go +++ b/actors/builtin/init/init_actor_state.go @@ -18,13 +18,13 @@ type State struct { } func ConstructState(store adt.Store, networkName string) (*State, error) { - emptyMapCid, err := adt.MakeEmptyMap(store, builtin.DefaultHamtBitwidth).Root() + emptyAddressMapCid, err := adt.StoreEmptyMap(store, builtin.DefaultHamtBitwidth) if err != nil { return nil, xerrors.Errorf("failed to create empty map: %w", err) } return &State{ - AddressMap: emptyMapCid, + AddressMap: emptyAddressMapCid, NextID: abi.ActorID(builtin.FirstNonSingletonActorId), NetworkName: networkName, }, nil diff --git a/actors/builtin/market/market_state.go b/actors/builtin/market/market_state.go index 8803224e93..90aaaae5e7 100644 --- a/actors/builtin/market/market_state.go +++ b/actors/builtin/market/market_state.go @@ -61,32 +61,24 @@ type State struct { } func ConstructState(store adt.Store) (*State, error) { - emptyProposalsArray, err := adt.MakeEmptyArray(store, ProposalsAmtBitwidth) + emptyProposalsArrayCid, err := adt.StoreEmptyArray(store, ProposalsAmtBitwidth) if err != nil { return nil, xerrors.Errorf("failed to create empty array: %w", err) } - emptyProposalsArrayCid, err := emptyProposalsArray.Root() - if err != nil { - return nil, xerrors.Errorf("failed to persist empty array: %w", err) - } - emptyStatesArray, err := adt.MakeEmptyArray(store, StatesAmtBitwidth) + emptyStatesArrayCid, err := adt.StoreEmptyArray(store, StatesAmtBitwidth) if err != nil { return nil, xerrors.Errorf("failed to create empty states array: %w", err) } - emptyStatesArrayCid, err := emptyStatesArray.Root() - if err != nil { - return nil, xerrors.Errorf("failed to persist empty states array: %w", err) - } - emptyMapCid, err := adt.MakeEmptyMap(store, builtin.DefaultHamtBitwidth).Root() + emptyPendingProposalsMapCid, err := adt.StoreEmptyMap(store, builtin.DefaultHamtBitwidth) if err != nil { return nil, xerrors.Errorf("failed to create empty map: %w", err) } - emptyMSetCid, err := MakeEmptySetMultimap(store, builtin.DefaultHamtBitwidth).Root() + emptyDealOpsHamtCid, err := MakeEmptySetMultimap(store, builtin.DefaultHamtBitwidth).Root() if err != nil { return nil, xerrors.Errorf("failed to create empty multiset: %w", err) } - emptyBalanceTableCid, err := adt.MakeEmptyMap(store, adt.BalanceTableBitwidth).Root() + emptyBalanceTableCid, err := adt.StoreEmptyMap(store, adt.BalanceTableBitwidth) if err != nil { return nil, xerrors.Errorf("failed to create empty balance table: %w", err) } @@ -94,11 +86,11 @@ func ConstructState(store adt.Store) (*State, error) { return &State{ Proposals: emptyProposalsArrayCid, States: emptyStatesArrayCid, - PendingProposals: emptyMapCid, + PendingProposals: emptyPendingProposalsMapCid, EscrowTable: emptyBalanceTableCid, LockedTable: emptyBalanceTableCid, NextID: abi.DealID(0), - DealOpsByEpoch: emptyMSetCid, + DealOpsByEpoch: emptyDealOpsHamtCid, LastCron: abi.ChainEpoch(-1), TotalClientLockedCollateral: abi.NewTokenAmount(0), diff --git a/actors/builtin/market/market_test.go b/actors/builtin/market/market_test.go index f4dc1c34fb..af5a09369f 100644 --- a/actors/builtin/market/market_test.go +++ b/actors/builtin/market/market_test.go @@ -82,22 +82,16 @@ func TestMarketActor(t *testing.T) { store := adt.AsStore(rt) - emptyBalanceTable, err := adt.MakeEmptyMap(store, adt.BalanceTableBitwidth).Root() + emptyBalanceTable, err := adt.StoreEmptyMap(store, adt.BalanceTableBitwidth) assert.NoError(t, err) - emptyMap, err := adt.MakeEmptyMap(store, builtin.DefaultHamtBitwidth).Root() + emptyMap, err := adt.StoreEmptyMap(store, builtin.DefaultHamtBitwidth) assert.NoError(t, err) - emptyProposalsArray, err := adt.MakeEmptyArray(store, market.ProposalsAmtBitwidth) + emptyProposalsArrayCid, err := adt.StoreEmptyArray(store, market.ProposalsAmtBitwidth) assert.NoError(t, err) - emptyProposalsArrayCid, err := emptyProposalsArray.Root() - assert.NoError(t, err) - - emptyStatesArray, err := adt.MakeEmptyArray(store, market.StatesAmtBitwidth) - assert.NoError(t, err) - - emptyStatesArrayCid, err := emptyStatesArray.Root() + emptyStatesArrayCid, err := adt.StoreEmptyArray(store, market.StatesAmtBitwidth) assert.NoError(t, err) emptyMultiMap, err := market.MakeEmptySetMultimap(store, builtin.DefaultHamtBitwidth).Root() diff --git a/actors/builtin/miner/bitfield_queue_test.go b/actors/builtin/miner/bitfield_queue_test.go index de178cf8f1..df43684264 100644 --- a/actors/builtin/miner/bitfield_queue_test.go +++ b/actors/builtin/miner/bitfield_queue_test.go @@ -10,15 +10,16 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/filecoin-project/specs-actors/v3/actors/builtin" "github.com/filecoin-project/specs-actors/v3/actors/builtin/miner" "github.com/filecoin-project/specs-actors/v3/actors/util/adt" "github.com/filecoin-project/specs-actors/v3/support/mock" ) +const testAmtBitwidth = 3 + func TestBitfieldQueue(t *testing.T) { t.Run("adds values to empty queue", func(t *testing.T) { - queue := emptyBitfieldQueue(t, builtin.DefaultAmtBitwidth) + queue := emptyBitfieldQueue(t, testAmtBitwidth) values := []uint64{1, 2, 3, 4} epoch := abi.ChainEpoch(42) @@ -30,7 +31,7 @@ func TestBitfieldQueue(t *testing.T) { }) t.Run("adds bitfield to empty queue", func(t *testing.T) { - queue := emptyBitfieldQueue(t, builtin.DefaultAmtBitwidth) + queue := emptyBitfieldQueue(t, testAmtBitwidth) values := []uint64{1, 2, 3, 4} epoch := abi.ChainEpoch(42) @@ -43,7 +44,7 @@ func TestBitfieldQueue(t *testing.T) { }) t.Run("quantizes added epochs according to quantization spec", func(t *testing.T) { - queue := emptyBitfieldQueueWithQuantizing(t, miner.NewQuantSpec(5, 3), builtin.DefaultAmtBitwidth) + queue := emptyBitfieldQueueWithQuantizing(t, miner.NewQuantSpec(5, 3), testAmtBitwidth) for _, val := range []uint64{0, 2, 3, 4, 7, 8, 9} { require.NoError(t, queue.AddToQueueValues(abi.ChainEpoch(val), val)) @@ -58,7 +59,7 @@ func TestBitfieldQueue(t *testing.T) { }) t.Run("quantizes added epochs according to quantization spec", func(t *testing.T) { - queue := emptyBitfieldQueueWithQuantizing(t, miner.NewQuantSpec(5, 3), builtin.DefaultAmtBitwidth) + queue := emptyBitfieldQueueWithQuantizing(t, miner.NewQuantSpec(5, 3), testAmtBitwidth) for _, val := range []uint64{0, 2, 3, 4, 7, 8, 9} { err := queue.AddToQueueValues(abi.ChainEpoch(val), val) @@ -74,7 +75,7 @@ func TestBitfieldQueue(t *testing.T) { }) t.Run("merges values withing same epoch", func(t *testing.T) { - queue := emptyBitfieldQueue(t, builtin.DefaultAmtBitwidth) + queue := emptyBitfieldQueue(t, testAmtBitwidth) epoch := abi.ChainEpoch(42) @@ -87,7 +88,7 @@ func TestBitfieldQueue(t *testing.T) { }) t.Run("adds values to different epochs", func(t *testing.T) { - queue := emptyBitfieldQueue(t, builtin.DefaultAmtBitwidth) + queue := emptyBitfieldQueue(t, testAmtBitwidth) epoch1 := abi.ChainEpoch(42) epoch2 := abi.ChainEpoch(93) @@ -102,7 +103,7 @@ func TestBitfieldQueue(t *testing.T) { }) t.Run("PouUntil from empty queue returns empty bitfield", func(t *testing.T) { - queue := emptyBitfieldQueue(t, builtin.DefaultAmtBitwidth) + queue := emptyBitfieldQueue(t, testAmtBitwidth) // TODO: broken pending https://github.com/filecoin-project/go-amt-ipld/issues/18 //emptyQueue, err := queue.Root() @@ -123,7 +124,7 @@ func TestBitfieldQueue(t *testing.T) { }) t.Run("PopUntil does nothing if 'until' parameter before first value", func(t *testing.T) { - queue := emptyBitfieldQueue(t, builtin.DefaultAmtBitwidth) + queue := emptyBitfieldQueue(t, testAmtBitwidth) epoch1 := abi.ChainEpoch(42) epoch2 := abi.ChainEpoch(93) @@ -149,7 +150,7 @@ func TestBitfieldQueue(t *testing.T) { }) t.Run("PopUntil removes and returns entries before and including target epoch", func(t *testing.T) { - queue := emptyBitfieldQueue(t, builtin.DefaultAmtBitwidth) + queue := emptyBitfieldQueue(t, testAmtBitwidth) epoch1 := abi.ChainEpoch(42) epoch2 := abi.ChainEpoch(93) @@ -207,7 +208,7 @@ func TestBitfieldQueue(t *testing.T) { }) t.Run("cuts elements", func(t *testing.T) { - queue := emptyBitfieldQueue(t, builtin.DefaultAmtBitwidth) + queue := emptyBitfieldQueue(t, testAmtBitwidth) epoch1 := abi.ChainEpoch(42) epoch2 := abi.ChainEpoch(93) @@ -227,12 +228,10 @@ func TestBitfieldQueue(t *testing.T) { func emptyBitfieldQueueWithQuantizing(t *testing.T, quant miner.QuantSpec, bitwidth int) miner.BitfieldQueue { rt := mock.NewBuilder(context.Background(), address.Undef).Build(t) store := adt.AsStore(rt) - emptyArray, err := adt.MakeEmptyArray(store, bitwidth) - require.NoError(t, err) - root, err := emptyArray.Root() + emptyArray, err := adt.StoreEmptyArray(store, bitwidth) require.NoError(t, err) - queue, err := miner.LoadBitfieldQueue(store, root, quant, bitwidth) + queue, err := miner.LoadBitfieldQueue(store, emptyArray, quant, bitwidth) require.NoError(t, err) return queue } diff --git a/actors/builtin/miner/deadline_state.go b/actors/builtin/miner/deadline_state.go index 3c950d259e..4d480a91e2 100644 --- a/actors/builtin/miner/deadline_state.go +++ b/actors/builtin/miner/deadline_state.go @@ -12,7 +12,6 @@ import ( cbg "github.com/whyrusleeping/cbor-gen" "golang.org/x/xerrors" - "github.com/filecoin-project/specs-actors/v3/actors/builtin" "github.com/filecoin-project/specs-actors/v3/actors/util/adt" ) @@ -57,6 +56,7 @@ type Deadline struct { FaultyPower PowerPair } +const DeadlinePartitionsAmtBitwidth = 3 const DeadlineExpirationAmtBitwidth = 5 // @@ -119,20 +119,29 @@ func (d *Deadlines) UpdateDeadline(store adt.Store, dlIdx uint64, deadline *Dead // Deadline (singular) // -func ConstructDeadline(emptyPartitionsArrayCid, emptyDeadlineExpirationCid cid.Cid) *Deadline { +func ConstructDeadline(store adt.Store) (*Deadline, error) { + emptyPartitionsArrayCid, err := adt.StoreEmptyArray(store, DeadlinePartitionsAmtBitwidth) + if err != nil { + return nil, xerrors.Errorf("failed to construct empty partitions array: %w", err) + } + emptyDeadlineExpirationArrayCid, err := adt.StoreEmptyArray(store, DeadlineExpirationAmtBitwidth) + if err != nil { + return nil, xerrors.Errorf("failed to construct empty deadline expiration array: %w", err) + } + return &Deadline{ Partitions: emptyPartitionsArrayCid, - ExpirationsEpochs: emptyDeadlineExpirationCid, + ExpirationsEpochs: emptyDeadlineExpirationArrayCid, PostSubmissions: bitfield.New(), EarlyTerminations: bitfield.New(), LiveSectors: 0, TotalSectors: 0, FaultyPower: NewPowerPairZero(), - } + }, nil } func (d *Deadline) PartitionsArray(store adt.Store) (*adt.Array, error) { - arr, err := adt.AsArray(store, d.Partitions, builtin.DefaultAmtBitwidth) + arr, err := adt.AsArray(store, d.Partitions, DeadlinePartitionsAmtBitwidth) if err != nil { return nil, xc.ErrIllegalState.Wrapf("failed to load partitions: %w", err) } @@ -299,26 +308,11 @@ func (dl *Deadline) AddSectors( return NewPowerPairZero(), err } else if !found { // This case will usually happen zero times. - // It would require adding more than a full partition in one go - // to happen more than once. - emptyExpirationArray, err := adt.MakeEmptyArray(store, PartitionExpirationAmtBitwidth) - if err != nil { - return NewPowerPairZero(), err - } - emptyExpirationArrayRoot, err := emptyExpirationArray.Root() - if err != nil { - return NewPowerPairZero(), err - } - - emptyEarlyTerminationArray, err := adt.MakeEmptyArray(store, builtin.DefaultAmtBitwidth) - if err != nil { - return NewPowerPairZero(), err - } - emptyEarlyTerminationArrayRoot, err := emptyEarlyTerminationArray.Root() + // It would require adding more than a full partition in one go to happen more than once. + partition, err = ConstructPartition(store) if err != nil { return NewPowerPairZero(), err } - partition = ConstructPartition(emptyExpirationArrayRoot, emptyEarlyTerminationArrayRoot) } // Figure out which (if any) sectors we want to add to this partition. @@ -585,7 +579,7 @@ func (dl *Deadline) RemovePartitions(store adt.Store, toRemove bitfield.BitField return bitfield.BitField{}, bitfield.BitField{}, NewPowerPairZero(), xerrors.Errorf("cannot remove partitions from deadline with early terminations: %w", err) } - newPartitions, err := adt.MakeEmptyArray(store, builtin.DefaultAmtBitwidth) + newPartitions, err := adt.MakeEmptyArray(store, DeadlinePartitionsAmtBitwidth) if err != nil { return bitfield.BitField{}, bitfield.BitField{}, NewPowerPairZero(), xerrors.Errorf("failed to create empty array for initializing partitions: %w", err) } diff --git a/actors/builtin/miner/deadline_state_test.go b/actors/builtin/miner/deadline_state_test.go index 76c73857a4..5788120966 100644 --- a/actors/builtin/miner/deadline_state_test.go +++ b/actors/builtin/miner/deadline_state_test.go @@ -876,17 +876,9 @@ func TestDeadlines(t *testing.T) { } func emptyDeadline(t *testing.T, store adt.Store) *miner.Deadline { - emptyPartitionsArray, err := adt.MakeEmptyArray(store, builtin.DefaultAmtBitwidth) + dl, err := miner.ConstructDeadline(store) require.NoError(t, err) - partitionsRoot, err := emptyPartitionsArray.Root() - require.NoError(t, err) - - emptyDeadlineExpirationArray, err := adt.MakeEmptyArray(store, miner.DeadlineExpirationAmtBitwidth) - require.NoError(t, err) - expirationsRoot, err := emptyDeadlineExpirationArray.Root() - require.NoError(t, err) - - return miner.ConstructDeadline(partitionsRoot, expirationsRoot) + return dl } // Helper type for validating deadline state. diff --git a/actors/builtin/miner/deadlines.go b/actors/builtin/miner/deadlines.go index 565642115a..bef627966e 100644 --- a/actors/builtin/miner/deadlines.go +++ b/actors/builtin/miner/deadlines.go @@ -7,7 +7,6 @@ import ( "github.com/filecoin-project/go-state-types/dline" "golang.org/x/xerrors" - "github.com/filecoin-project/specs-actors/v3/actors/builtin" "github.com/filecoin-project/specs-actors/v3/actors/util/adt" ) @@ -29,7 +28,7 @@ func FindSector(store adt.Store, deadlines *Deadlines, sectorNum abi.SectorNumbe return 0, 0, err } - partitions, err := adt.AsArray(store, dl.Partitions, builtin.DefaultAmtBitwidth) + partitions, err := adt.AsArray(store, dl.Partitions, DeadlinePartitionsAmtBitwidth) if err != nil { return 0, 0, err } diff --git a/actors/builtin/miner/expiration_queue_test.go b/actors/builtin/miner/expiration_queue_test.go index fb1d9e9ace..fcdaa620e3 100644 --- a/actors/builtin/miner/expiration_queue_test.go +++ b/actors/builtin/miner/expiration_queue_test.go @@ -12,7 +12,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/filecoin-project/specs-actors/v3/actors/builtin" "github.com/filecoin-project/specs-actors/v3/actors/builtin/miner" "github.com/filecoin-project/specs-actors/v3/actors/util/adt" "github.com/filecoin-project/specs-actors/v3/support/mock" @@ -242,7 +241,7 @@ func TestExpirationQueue(t *testing.T) { }) t.Run("quantizes added sectors by expiration", func(t *testing.T) { - queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(5, 3), builtin.DefaultAmtBitwidth) + queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(5, 3), testAmtBitwidth) secNums, power, pledge, err := queue.AddActiveSectors(sectors, sectorSize) require.NoError(t, err) assertBitfieldEquals(t, secNums, 1, 2, 3, 4, 5, 6) @@ -343,7 +342,7 @@ func TestExpirationQueue(t *testing.T) { t.Run("reschedules sectors as faults", func(t *testing.T) { // Create 3 expiration sets with 2 sectors apiece - queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), builtin.DefaultAmtBitwidth) + queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), testAmtBitwidth) _, _, _, err := queue.AddActiveSectors(sectors, sectorSize) require.NoError(t, err) @@ -404,7 +403,7 @@ func TestExpirationQueue(t *testing.T) { t.Run("reschedules all sectors as faults", func(t *testing.T) { // Create expiration 3 sets with 2 sectors apiece - queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), builtin.DefaultAmtBitwidth) + queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), testAmtBitwidth) _, _, _, err := queue.AddActiveSectors(sectors, sectorSize) require.NoError(t, err) @@ -465,7 +464,7 @@ func TestExpirationQueue(t *testing.T) { t.Run("reschedule expirations then reschedule as fault", func(t *testing.T) { // Create expiration 3 sets with 2 sectors apiece - queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), builtin.DefaultAmtBitwidth) + queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), testAmtBitwidth) _, _, _, err := queue.AddActiveSectors(sectors, sectorSize) require.NoError(t, err) @@ -502,7 +501,7 @@ func TestExpirationQueue(t *testing.T) { t.Run("reschedule recover restores all sector stats", func(t *testing.T) { // Create expiration 3 sets with 2 sectors apiece - queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), builtin.DefaultAmtBitwidth) + queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), testAmtBitwidth) _, _, _, err := queue.AddActiveSectors(sectors, sectorSize) require.NoError(t, err) @@ -567,7 +566,7 @@ func TestExpirationQueue(t *testing.T) { t.Run("replaces sectors with new sectors", func(t *testing.T) { // Create expiration 3 sets - queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), builtin.DefaultAmtBitwidth) + queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), testAmtBitwidth) // add sectors to each set _, _, _, err := queue.AddActiveSectors([]*miner.SectorOnChainInfo{sectors[0], sectors[1], sectors[3], sectors[5]}, sectorSize) @@ -621,7 +620,7 @@ func TestExpirationQueue(t *testing.T) { t.Run("removes sectors", func(t *testing.T) { // add all sectors into 3 sets - queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), builtin.DefaultAmtBitwidth) + queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), testAmtBitwidth) _, _, _, err := queue.AddActiveSectors(sectors, sectorSize) require.NoError(t, err) @@ -683,21 +682,21 @@ func TestExpirationQueue(t *testing.T) { }) t.Run("adding no sectors leaves the queue empty", func(t *testing.T) { - queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), builtin.DefaultAmtBitwidth) + queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), testAmtBitwidth) _, _, _, err := queue.AddActiveSectors(nil, sectorSize) require.NoError(t, err) assert.Zero(t, queue.Length()) }) t.Run("rescheduling no expirations leaves the queue empty", func(t *testing.T) { - queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), builtin.DefaultAmtBitwidth) + queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), testAmtBitwidth) err := queue.RescheduleExpirations(10, nil, sectorSize) require.NoError(t, err) assert.Zero(t, queue.Length()) }) t.Run("rescheduling no expirations as faults leaves the queue empty", func(t *testing.T) { - queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), builtin.DefaultAmtBitwidth) + queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), testAmtBitwidth) _, _, _, err := queue.AddActiveSectors(sectors, sectorSize) require.NoError(t, err) @@ -710,7 +709,7 @@ func TestExpirationQueue(t *testing.T) { }) t.Run("rescheduling all expirations as faults leaves the queue empty if it was empty", func(t *testing.T) { - queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), builtin.DefaultAmtBitwidth) + queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), testAmtBitwidth) _, _, _, err := queue.AddActiveSectors(sectors, sectorSize) require.NoError(t, err) @@ -723,7 +722,7 @@ func TestExpirationQueue(t *testing.T) { }) t.Run("rescheduling no sectors as recovered leaves the queue empty", func(t *testing.T) { - queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), builtin.DefaultAmtBitwidth) + queue := emptyExpirationQueueWithQuantizing(t, miner.NewQuantSpec(4, 1), testAmtBitwidth) _, err := queue.RescheduleRecovered(nil, sectorSize) require.NoError(t, err) assert.Zero(t, queue.Length()) @@ -755,16 +754,14 @@ func requireNoExpirationGroupsBefore(t *testing.T, epoch abi.ChainEpoch, queue m func emptyExpirationQueueWithQuantizing(t *testing.T, quant miner.QuantSpec, bitwidth int) miner.ExpirationQueue { rt := mock.NewBuilder(context.Background(), address.Undef).Build(t) store := adt.AsStore(rt) - emptyArray, err := adt.MakeEmptyArray(store, builtin.DefaultAmtBitwidth) - require.NoError(t, err) - root, err := emptyArray.Root() + emptyArray, err := adt.StoreEmptyArray(store, testAmtBitwidth) require.NoError(t, err) - queue, err := miner.LoadExpirationQueue(store, root, quant, bitwidth) + queue, err := miner.LoadExpirationQueue(store, emptyArray, quant, bitwidth) require.NoError(t, err) return queue } func emptyExpirationQueue(t *testing.T) miner.ExpirationQueue { - return emptyExpirationQueueWithQuantizing(t, miner.NoQuantization, builtin.DefaultAmtBitwidth) + return emptyExpirationQueueWithQuantizing(t, miner.NoQuantization, testAmtBitwidth) } diff --git a/actors/builtin/miner/miner_state.go b/actors/builtin/miner/miner_state.go index a44a673ad8..30e9b85422 100644 --- a/actors/builtin/miner/miner_state.go +++ b/actors/builtin/miner/miner_state.go @@ -168,49 +168,28 @@ type SectorOnChainInfo struct { } func ConstructState(store adt.Store, infoCid cid.Cid, periodStart abi.ChainEpoch, deadlineIndex uint64) (*State, error) { - emptyMapCid, err := adt.MakeEmptyMap(store, builtin.DefaultHamtBitwidth).Root() + emptyPrecommitMapCid, err := adt.StoreEmptyMap(store, builtin.DefaultHamtBitwidth) if err != nil { return nil, xerrors.Errorf("failed to construct empty map: %w", err) } - emptyPrecommitsExpiryArray, err := adt.MakeEmptyArray(store, PrecommitExpiryAmtBitwidth) + emptyPrecommitsExpiryArrayCid, err := adt.StoreEmptyArray(store, PrecommitExpiryAmtBitwidth) if err != nil { return nil, xerrors.Errorf("failed to construct empty precommits array: %w", err) } - emptyPrecommitsArrayCid, err := emptyPrecommitsExpiryArray.Root() - if err != nil { - return nil, xerrors.Errorf("failed to persist empty precommits array: %w", err) - } - emptySectorsArray, err := adt.MakeEmptyArray(store, SectorsAmtBitwidth) + emptySectorsArrayCid, err := adt.StoreEmptyArray(store, SectorsAmtBitwidth) if err != nil { return nil, xerrors.Errorf("failed to construct empty sectors array: %w", err) } - emptySectorsArrayCid, err := emptySectorsArray.Root() - if err != nil { - return nil, xerrors.Errorf("failed to persist empty sectors array: %w", err) - } - emptyPartitionsArray, err := adt.MakeEmptyArray(store, builtin.DefaultAmtBitwidth) - if err != nil { - return nil, xerrors.Errorf("failed to construct empty partitions array: %w", err) - } - emptyPartitionsArrayCid, err := emptyPartitionsArray.Root() - if err != nil { - return nil, xerrors.Errorf("failed to persist empty partitions array: %w", err) - } - emptyDeadlineExpirationArray, err := adt.MakeEmptyArray(store, DeadlineExpirationAmtBitwidth) - if err != nil { - return nil, xerrors.Errorf("failed to construct empty deadline expiration array: %w", err) - } - emptyDeadlineExpirationArrayCid, err := emptyDeadlineExpirationArray.Root() - if err != nil { - return nil, xerrors.Errorf("failed to persist empty deadline expiration array: %w", err) - } emptyBitfield := bitfield.NewFromSet(nil) emptyBitfieldCid, err := store.Put(store.Context(), emptyBitfield) if err != nil { return nil, xerrors.Errorf("failed to construct empty bitfield: %w", err) } - emptyDeadline := ConstructDeadline(emptyPartitionsArrayCid, emptyDeadlineExpirationArrayCid) + emptyDeadline, err := ConstructDeadline(store) + if err != nil { + return nil, xerrors.Errorf("failed to construct empty deadline: %w", err) + } emptyDeadlineCid, err := store.Put(store.Context(), emptyDeadline) if err != nil { return nil, xerrors.Errorf("failed to construct empty deadline: %w", err) @@ -220,8 +199,7 @@ func ConstructState(store adt.Store, infoCid cid.Cid, periodStart abi.ChainEpoch if err != nil { return nil, xerrors.Errorf("failed to construct empty deadlines: %w", err) } - emptyVestingFunds := ConstructVestingFunds() - emptyVestingFundsCid, err := store.Put(store.Context(), emptyVestingFunds) + emptyVestingFundsCid, err := store.Put(store.Context(), ConstructVestingFunds()) if err != nil { return nil, xerrors.Errorf("failed to construct empty vesting funds: %w", err) } @@ -237,8 +215,8 @@ func ConstructState(store adt.Store, infoCid cid.Cid, periodStart abi.ChainEpoch InitialPledge: abi.NewTokenAmount(0), - PreCommittedSectors: emptyMapCid, - PreCommittedSectorsExpiry: emptyPrecommitsArrayCid, + PreCommittedSectors: emptyPrecommitMapCid, + PreCommittedSectorsExpiry: emptyPrecommitsExpiryArrayCid, AllocatedSectors: emptyBitfieldCid, Sectors: emptySectorsArrayCid, ProvingPeriodStart: periodStart, diff --git a/actors/builtin/miner/partition_state.go b/actors/builtin/miner/partition_state.go index db07b41414..7f1599ae12 100644 --- a/actors/builtin/miner/partition_state.go +++ b/actors/builtin/miner/partition_state.go @@ -10,7 +10,6 @@ import ( "github.com/ipfs/go-cid" "golang.org/x/xerrors" - "github.com/filecoin-project/specs-actors/v3/actors/builtin" "github.com/filecoin-project/specs-actors/v3/actors/util" "github.com/filecoin-project/specs-actors/v3/actors/util/adt" ) @@ -55,6 +54,8 @@ type Partition struct { // patterns and projections of mainnet data. const PartitionExpirationAmtBitwidth = 4 +const PartitionEarlyTerminationArrayAmtBitwidth = 3 + // Value type for a pair of raw and QA power. type PowerPair struct { Raw abi.StoragePower @@ -62,20 +63,29 @@ type PowerPair struct { } // A set of sectors associated with a given epoch. -func ConstructPartition(emptyExpirationArray, emptyEarlyTerminationArray cid.Cid) *Partition { +func ConstructPartition(store adt.Store) (*Partition, error) { + emptyExpirationArrayRoot, err := adt.StoreEmptyArray(store, PartitionExpirationAmtBitwidth) + if err != nil { + return nil, err + } + emptyEarlyTerminationArrayRoot, err := adt.StoreEmptyArray(store, PartitionEarlyTerminationArrayAmtBitwidth) + if err != nil { + return nil, err + } + return &Partition{ Sectors: bitfield.New(), Unproven: bitfield.New(), Faults: bitfield.New(), Recoveries: bitfield.New(), Terminated: bitfield.New(), - ExpirationsEpochs: emptyExpirationArray, - EarlyTerminated: emptyEarlyTerminationArray, + ExpirationsEpochs: emptyExpirationArrayRoot, + EarlyTerminated: emptyEarlyTerminationArrayRoot, LivePower: NewPowerPairZero(), UnprovenPower: NewPowerPairZero(), FaultyPower: NewPowerPairZero(), RecoveringPower: NewPowerPairZero(), - } + }, nil } // Live sectors are those that are not terminated (but may be faulty). @@ -513,7 +523,7 @@ func (p *Partition) ReplaceSectors(store adt.Store, oldSectors, newSectors []*Se // Record the epoch of any sectors expiring early, for termination fee calculation later. func (p *Partition) recordEarlyTermination(store adt.Store, epoch abi.ChainEpoch, sectors bitfield.BitField) error { - etQueue, err := LoadBitfieldQueue(store, p.EarlyTerminated, NoQuantization, builtin.DefaultAmtBitwidth) + etQueue, err := LoadBitfieldQueue(store, p.EarlyTerminated, NoQuantization, PartitionEarlyTerminationArrayAmtBitwidth) if err != nil { return xerrors.Errorf("failed to load early termination queue: %w", err) } @@ -738,7 +748,7 @@ func (p *Partition) PopEarlyTerminations(store adt.Store, maxSectors uint64) (re stopErr := errors.New("stop iter") // Load early terminations. - earlyTerminatedQ, err := LoadBitfieldQueue(store, p.EarlyTerminated, NoQuantization, builtin.DefaultAmtBitwidth) + earlyTerminatedQ, err := LoadBitfieldQueue(store, p.EarlyTerminated, NoQuantization, PartitionEarlyTerminationArrayAmtBitwidth) if err != nil { return TerminationResult{}, false, err } diff --git a/actors/builtin/miner/partition_state_test.go b/actors/builtin/miner/partition_state_test.go index f1c31bdb91..0963b35a18 100644 --- a/actors/builtin/miner/partition_state_test.go +++ b/actors/builtin/miner/partition_state_test.go @@ -449,7 +449,7 @@ func TestPartitions(t *testing.T) { }) // sectors should be added to early termination bitfield queue - queue, err := miner.LoadBitfieldQueue(store, partition.EarlyTerminated, miner.NoQuantization, builtin.DefaultAmtBitwidth) + queue, err := miner.LoadBitfieldQueue(store, partition.EarlyTerminated, miner.NoQuantization, miner.PartitionEarlyTerminationArrayAmtBitwidth) require.NoError(t, err) ExpectBQ(). @@ -542,7 +542,7 @@ func TestPartitions(t *testing.T) { }) // sectors should be added to early termination bitfield queue - queue, err := miner.LoadBitfieldQueue(store, partition.EarlyTerminated, miner.NoQuantization, builtin.DefaultAmtBitwidth) + queue, err := miner.LoadBitfieldQueue(store, partition.EarlyTerminated, miner.NoQuantization, miner.PartitionEarlyTerminationArrayAmtBitwidth) require.NoError(t, err) // only early termination appears in bitfield queue @@ -663,7 +663,7 @@ func TestPartitions(t *testing.T) { assert.True(t, hasMore) // expect terminations to still contain 3 and 5 - queue, err := miner.LoadBitfieldQueue(store, partition.EarlyTerminated, miner.NoQuantization, builtin.DefaultAmtBitwidth) + queue, err := miner.LoadBitfieldQueue(store, partition.EarlyTerminated, miner.NoQuantization, miner.PartitionEarlyTerminationArrayAmtBitwidth) require.NoError(t, err) // only early termination appears in bitfield queue @@ -682,7 +682,7 @@ func TestPartitions(t *testing.T) { assert.False(t, hasMore) // expect early terminations to be empty - queue, err = miner.LoadBitfieldQueue(store, partition.EarlyTerminated, miner.NoQuantization, builtin.DefaultAmtBitwidth) + queue, err = miner.LoadBitfieldQueue(store, partition.EarlyTerminated, miner.NoQuantization, miner.PartitionEarlyTerminationArrayAmtBitwidth) require.NoError(t, err) ExpectBQ().Equals(t, queue) }) @@ -923,17 +923,9 @@ func selectSectors(t *testing.T, sectors []*miner.SectorOnChainInfo, field bitfi } func emptyPartition(t *testing.T, store adt.Store) *miner.Partition { - emptyExpirationArray, err := adt.MakeEmptyArray(store, miner.PartitionExpirationAmtBitwidth) + p, err := miner.ConstructPartition(store) require.NoError(t, err) - expirationRoot, err := emptyExpirationArray.Root() - require.NoError(t, err) - - emptyEarlyTerminationArray, err := adt.MakeEmptyArray(store, builtin.DefaultAmtBitwidth) - require.NoError(t, err) - earlyTerminationRoot, err := emptyEarlyTerminationArray.Root() - require.NoError(t, err) - - return miner.ConstructPartition(expirationRoot, earlyTerminationRoot) + return p } func rescheduleSectors(t *testing.T, target abi.ChainEpoch, sectors []*miner.SectorOnChainInfo, filter bitfield.BitField) []*miner.SectorOnChainInfo { diff --git a/actors/builtin/miner/sectors_test.go b/actors/builtin/miner/sectors_test.go index 2933941566..bff561a6ac 100644 --- a/actors/builtin/miner/sectors_test.go +++ b/actors/builtin/miner/sectors_test.go @@ -9,7 +9,6 @@ import ( "github.com/filecoin-project/go-state-types/big" "github.com/stretchr/testify/require" - "github.com/filecoin-project/specs-actors/v3/actors/builtin" "github.com/filecoin-project/specs-actors/v3/actors/builtin/miner" "github.com/filecoin-project/specs-actors/v3/actors/util/adt" "github.com/filecoin-project/specs-actors/v3/support/ipld" @@ -17,7 +16,7 @@ import ( ) func sectorsArr(t *testing.T, store adt.Store, sectors []*miner.SectorOnChainInfo) miner.Sectors { - emptyArray, err := adt.MakeEmptyArray(store, builtin.DefaultAmtBitwidth) + emptyArray, err := adt.MakeEmptyArray(store, miner.SectorsAmtBitwidth) require.NoError(t, err) sectorArr := miner.Sectors{emptyArray} require.NoError(t, sectorArr.Store(sectors...)) diff --git a/actors/builtin/miner/testing.go b/actors/builtin/miner/testing.go index e2067fda7c..340867a2b5 100644 --- a/actors/builtin/miner/testing.go +++ b/actors/builtin/miner/testing.go @@ -439,7 +439,7 @@ func CheckPartitionStateInvariants( // Validate the early termination queue. earlyTerminationCount := 0 - if earlyQ, err := LoadBitfieldQueue(store, partition.EarlyTerminated, NoQuantization, builtin.DefaultAmtBitwidth); err != nil { + if earlyQ, err := LoadBitfieldQueue(store, partition.EarlyTerminated, NoQuantization, PartitionEarlyTerminationArrayAmtBitwidth); err != nil { acc.Addf("error loading early termination queue: %v", err) } else { earlyTerminationCount = CheckEarlyTerminationQueue(earlyQ, partition.Terminated, acc) diff --git a/actors/builtin/multisig/multisig_actor.go b/actors/builtin/multisig/multisig_actor.go index cc56a4017a..416521e8b0 100644 --- a/actors/builtin/multisig/multisig_actor.go +++ b/actors/builtin/multisig/multisig_actor.go @@ -120,7 +120,7 @@ func (a Actor) Constructor(rt runtime.Runtime, params *ConstructorParams) *abi.E rt.Abortf(exitcode.ErrIllegalArgument, "negative unlock duration disallowed") } - pending, err := adt.MakeEmptyMap(adt.AsStore(rt), builtin.DefaultHamtBitwidth).Root() + pending, err := adt.StoreEmptyMap(adt.AsStore(rt), builtin.DefaultHamtBitwidth) if err != nil { rt.Abortf(exitcode.ErrIllegalState, "failed to create empty map: %v", err) } diff --git a/actors/builtin/multisig/multisig_test.go b/actors/builtin/multisig/multisig_test.go index 1830244b5b..b9ec19eee1 100644 --- a/actors/builtin/multisig/multisig_test.go +++ b/actors/builtin/multisig/multisig_test.go @@ -120,7 +120,7 @@ func TestConstruction(t *testing.T) { assert.Equal(t, abi.ChainEpoch(1234), st.StartEpoch) // assert no transactions - empty, err := adt.MakeEmptyMap(rt.AdtStore(), builtin.DefaultHamtBitwidth).Root() + empty, err := adt.StoreEmptyMap(rt.AdtStore(), builtin.DefaultHamtBitwidth) require.NoError(t, err) assert.Equal(t, empty, st.PendingTxns) diff --git a/actors/builtin/paych/paych_actor.go b/actors/builtin/paych/paych_actor.go index 4ba99021e3..49e27bd035 100644 --- a/actors/builtin/paych/paych_actor.go +++ b/actors/builtin/paych/paych_actor.go @@ -61,7 +61,7 @@ func (pca *Actor) Constructor(rt runtime.Runtime, params *ConstructorParams) *ab from, err := pca.resolveAccount(rt, params.From) builtin.RequireNoErr(rt, err, exitcode.Unwrap(err, exitcode.ErrIllegalState), "failed to resolve from address: %s", params.From) - emptyArr, err := adt.MakeEmptyArray(adt.AsStore(rt), builtin.DefaultAmtBitwidth) + emptyArr, err := adt.MakeEmptyArray(adt.AsStore(rt), LaneStatesAmtBitwidth) builtin.RequireNoErr(rt, err, exitcode.ErrIllegalState, "failed to create empty array") emptyArrCid, err := emptyArr.Root() builtin.RequireNoErr(rt, err, exitcode.ErrIllegalState, "failed to persist empty array") @@ -225,7 +225,7 @@ func (pca Actor) UpdateChannelState(rt runtime.Runtime, params *UpdateChannelSta rt.StateTransaction(&st, func() { laneFound := true - lstates, err := adt.AsArray(adt.AsStore(rt), st.LaneStates, builtin.DefaultAmtBitwidth) + lstates, err := adt.AsArray(adt.AsStore(rt), st.LaneStates, LaneStatesAmtBitwidth) builtin.RequireNoErr(rt, err, exitcode.ErrIllegalState, "failed to load lanes") // Find the voucher lane, creating if necessary. diff --git a/actors/builtin/paych/paych_state.go b/actors/builtin/paych/paych_state.go index c8c6ef7a49..85c4efa4c4 100644 --- a/actors/builtin/paych/paych_state.go +++ b/actors/builtin/paych/paych_state.go @@ -35,6 +35,8 @@ type LaneState struct { Nonce uint64 } +const LaneStatesAmtBitwidth = 3 + func ConstructState(from addr.Address, to addr.Address, emptyArrCid cid.Cid) *State { return &State{ From: from, diff --git a/actors/builtin/paych/paych_test.go b/actors/builtin/paych/paych_test.go index 50679ca35a..8c33aecd04 100644 --- a/actors/builtin/paych/paych_test.go +++ b/actors/builtin/paych/paych_test.go @@ -271,7 +271,7 @@ func TestPaymentChannelActor_CreateLane(t *testing.T) { rt.Call(actor.UpdateChannelState, ucp) var st State rt.GetState(&st) - lstates, err := adt.AsArray(adt.AsStore(rt), st.LaneStates, builtin.DefaultAmtBitwidth) + lstates, err := adt.AsArray(adt.AsStore(rt), st.LaneStates, LaneStatesAmtBitwidth) assert.NoError(t, err) assert.Equal(t, uint64(1), lstates.Length()) @@ -297,14 +297,14 @@ func TestPaymentChannelActor_CreateLane(t *testing.T) { func assertLaneStatesLength(t *testing.T, rt *mock.Runtime, rcid cid.Cid, l int) { t.Helper() - arr, err := adt.AsArray(adt.AsStore(rt), rcid, builtin.DefaultAmtBitwidth) + arr, err := adt.AsArray(adt.AsStore(rt), rcid, LaneStatesAmtBitwidth) assert.NoError(t, err) assert.Equal(t, arr.Length(), uint64(l)) } func constructLaneStateAMT(t *testing.T, rt *mock.Runtime, lss []*LaneState) cid.Cid { t.Helper() - arr, err := adt.MakeEmptyArray(adt.AsStore(rt), builtin.DefaultAmtBitwidth) + arr, err := adt.MakeEmptyArray(adt.AsStore(rt), LaneStatesAmtBitwidth) require.NoError(t, err) for i, ls := range lss { err := arr.Set(uint64(i), ls) @@ -318,7 +318,7 @@ func constructLaneStateAMT(t *testing.T, rt *mock.Runtime, lss []*LaneState) cid } func getLaneState(t *testing.T, rt *mock.Runtime, rcid cid.Cid, lane uint64) *LaneState { - arr, err := adt.AsArray(adt.AsStore(rt), rcid, builtin.DefaultAmtBitwidth) + arr, err := adt.AsArray(adt.AsStore(rt), rcid, LaneStatesAmtBitwidth) assert.NoError(t, err) var out LaneState @@ -965,11 +965,9 @@ func (h *pcActorHarness) checkState(rt *mock.Runtime) { func verifyInitialState(t *testing.T, rt *mock.Runtime, sender, receiver addr.Address) { var st State rt.GetState(&st) - emptyArray, err := adt.MakeEmptyArray(adt.AsStore(rt), builtin.DefaultAmtBitwidth) + emptyArray, err := adt.StoreEmptyArray(adt.AsStore(rt), LaneStatesAmtBitwidth) require.NoError(t, err) - emptyArrCid, err := emptyArray.Root() - assert.NoError(t, err) - expectedState := State{From: sender, To: receiver, ToSend: abi.NewTokenAmount(0), LaneStates: emptyArrCid} + expectedState := State{From: sender, To: receiver, ToSend: abi.NewTokenAmount(0), LaneStates: emptyArray} verifyState(t, rt, -1, expectedState) } @@ -985,11 +983,9 @@ func verifyState(t *testing.T, rt *mock.Runtime, expLanes int, expectedState Sta assertLaneStatesLength(t, rt, st.LaneStates, expLanes) assert.True(t, reflect.DeepEqual(expectedState.LaneStates, st.LaneStates)) } else { - emptyArray, err := adt.MakeEmptyArray(adt.AsStore(rt), builtin.DefaultAmtBitwidth) - require.NoError(t, err) - ecid, err := emptyArray.Root() + emptyArray, err := adt.StoreEmptyArray(adt.AsStore(rt), LaneStatesAmtBitwidth) assert.NoError(t, err) - assert.Equal(t, st.LaneStates, ecid) + assert.Equal(t, st.LaneStates, emptyArray) } } diff --git a/actors/builtin/paych/testing.go b/actors/builtin/paych/testing.go index e9117e972a..29435c7795 100644 --- a/actors/builtin/paych/testing.go +++ b/actors/builtin/paych/testing.go @@ -24,7 +24,7 @@ func CheckStateInvariants(st *State, store adt.Store, balance abi.TokenAmount) ( acc.Require(st.SettlingAt >= st.MinSettleHeight, "channel is setting at epoch %d before min settle height %d", st.SettlingAt, st.MinSettleHeight) - if lanes, err := adt.AsArray(store, st.LaneStates, builtin.DefaultAmtBitwidth); err != nil { + if lanes, err := adt.AsArray(store, st.LaneStates, LaneStatesAmtBitwidth); err != nil { acc.Addf("error loading lanes: %v", err) } else { var lane LaneState diff --git a/actors/builtin/power/policy.go b/actors/builtin/power/policy.go index b97c77bbc9..c135bd9c60 100644 --- a/actors/builtin/power/policy.go +++ b/actors/builtin/power/policy.go @@ -11,14 +11,3 @@ const ConsensusMinerMinMiners = 4 // PARAM_SPEC // Onboarding 1EiB/year requires at least 32 prove-commits per epoch. const MaxMinerProveCommitsPerEpoch = 200 // PARAM_SPEC -// Bitwidth of CronEventQueue HAMT determined empirically from mutation -// patterns and projections of mainnet data. -const CronQueueHamtBitwidth = 6 - -// Bitwidth of CronEventQueue AMT determined empirically from mutation -// patterns and projections of mainnet data. -const CronQueueAmtBitwidth = 6 - -// Bitwidth of ProofValidationBatch AMT determined empirically from mutation -// pattersn and projections of mainnet data. -const ProofValidationBatchAmtBitwidth = 4 diff --git a/actors/builtin/power/power_state.go b/actors/builtin/power/power_state.go index 670ef451b2..66af510642 100644 --- a/actors/builtin/power/power_state.go +++ b/actors/builtin/power/power_state.go @@ -23,6 +23,18 @@ var InitialQAPowerEstimatePosition = big.Mul(big.NewInt(750_000), big.NewInt(1<< // max chain throughput in bytes per epoch = 120 ProveCommits / epoch = 3,840 GiB var InitialQAPowerEstimateVelocity = big.Mul(big.NewInt(3_840), big.NewInt(1<<30)) +// Bitwidth of CronEventQueue HAMT determined empirically from mutation +// patterns and projections of mainnet data. +const CronQueueHamtBitwidth = 6 + +// Bitwidth of CronEventQueue AMT determined empirically from mutation +// patterns and projections of mainnet data. +const CronQueueAmtBitwidth = 6 + +// Bitwidth of ProofValidationBatch AMT determined empirically from mutation +// pattersn and projections of mainnet data. +const ProofValidationBatchAmtBitwidth = 4 + type State struct { TotalRawBytePower abi.StoragePower // TotalBytesCommitted includes claims from miners below min power threshold @@ -73,7 +85,7 @@ type CronEvent struct { } func ConstructState(store adt.Store) (*State, error) { - emptyMapCid, err := adt.MakeEmptyMap(store, builtin.DefaultHamtBitwidth).Root() + emptyClaimsMapCid, err := adt.StoreEmptyMap(store, builtin.DefaultHamtBitwidth) if err != nil { return nil, xerrors.Errorf("failed to create empty map: %w", err) } @@ -94,7 +106,7 @@ func ConstructState(store adt.Store) (*State, error) { ThisEpochQAPowerSmoothed: smoothing.NewEstimate(InitialQAPowerEstimatePosition, InitialQAPowerEstimateVelocity), FirstCronEpoch: 0, CronEventQueue: emptyCronQueueMMapCid, - Claims: emptyMapCid, + Claims: emptyClaimsMapCid, MinerCount: 0, MinerAboveMinPowerCount: 0, }, nil diff --git a/actors/builtin/power/power_test.go b/actors/builtin/power/power_test.go index d9d51a54cf..5016c177f5 100644 --- a/actors/builtin/power/power_test.go +++ b/actors/builtin/power/power_test.go @@ -8,12 +8,11 @@ import ( "strings" "testing" - "github.com/filecoin-project/go-state-types/network" - addr "github.com/filecoin-project/go-address" "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/big" "github.com/filecoin-project/go-state-types/exitcode" + "github.com/filecoin-project/go-state-types/network" cid "github.com/ipfs/go-cid" assert "github.com/stretchr/testify/assert" require "github.com/stretchr/testify/require" diff --git a/actors/builtin/shared.go b/actors/builtin/shared.go index b928d2a9aa..ed15baa56a 100644 --- a/actors/builtin/shared.go +++ b/actors/builtin/shared.go @@ -21,10 +21,6 @@ import ( // This value has been empirically chosen, but the optimal value for maps with different mutation profiles may differ. const DefaultHamtBitwidth = 5 -// Default log2 of branching factor for AMTs. -// This value has been empirically chosen, but the optimal value for arrays with different mutation profiles may differ. -const DefaultAmtBitwidth = 3 - type BigFrac struct { Numerator big.Int Denominator big.Int diff --git a/actors/builtin/verifreg/verified_registry_state.go b/actors/builtin/verifreg/verified_registry_state.go index a3b25b9750..f7ff49f058 100644 --- a/actors/builtin/verifreg/verified_registry_state.go +++ b/actors/builtin/verifreg/verified_registry_state.go @@ -31,7 +31,7 @@ var MinVerifiedDealSize = abi.NewStoragePower(1 << 20) // rootKeyAddress comes from genesis. func ConstructState(store adt.Store, rootKeyAddress addr.Address) (*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) } diff --git a/actors/builtin/verifreg/verified_registry_test.go b/actors/builtin/verifreg/verified_registry_test.go index 65453a85aa..000d924d99 100644 --- a/actors/builtin/verifreg/verified_registry_test.go +++ b/actors/builtin/verifreg/verified_registry_test.go @@ -40,7 +40,7 @@ func TestConstruction(t *testing.T) { actor := verifRegActorTestHarness{t: t, rootkey: raddr} actor.constructAndVerify(rt) - emptyMap, err := adt.MakeEmptyMap(rt.AdtStore(), builtin.DefaultHamtBitwidth).Root() + emptyMap, err := adt.StoreEmptyMap(rt.AdtStore(), builtin.DefaultHamtBitwidth) require.NoError(t, err) state := actor.state(rt) @@ -60,7 +60,7 @@ func TestConstruction(t *testing.T) { actor := verifRegActorTestHarness{t: t, rootkey: raddr} actor.constructAndVerify(rt) - emptyMap, err := adt.MakeEmptyMap(rt.AdtStore(), builtin.DefaultHamtBitwidth).Root() + emptyMap, err := adt.StoreEmptyMap(rt.AdtStore(), builtin.DefaultHamtBitwidth) require.NoError(t, err) var state verifreg.State diff --git a/actors/migration/nv9/init.go b/actors/migration/nv9/init.go index 31c78b4d92..b7d1340ede 100644 --- a/actors/migration/nv9/init.go +++ b/actors/migration/nv9/init.go @@ -8,7 +8,6 @@ import ( builtin3 "github.com/filecoin-project/specs-actors/v3/actors/builtin" init3 "github.com/filecoin-project/specs-actors/v3/actors/builtin/init" - adt3 "github.com/filecoin-project/specs-actors/v3/actors/util/adt" ) type initMigrator struct{} @@ -19,7 +18,7 @@ func (m initMigrator) MigrateState(ctx context.Context, store cbor.IpldStore, in return nil, err } - addressMapOut, err := migrateHAMTRaw(ctx, store, inState.AddressMap, adt3.DefaultHamtOptionsWithDefaultBitwidth) + addressMapOut, err := migrateHAMTRaw(ctx, store, inState.AddressMap, builtin3.DefaultHamtBitwidth) if err != nil { return nil, err } diff --git a/actors/migration/nv9/market.go b/actors/migration/nv9/market.go index 2d8a4afa79..e8837e2bd7 100644 --- a/actors/migration/nv9/market.go +++ b/actors/migration/nv9/market.go @@ -6,8 +6,6 @@ import ( cid "github.com/ipfs/go-cid" cbor "github.com/ipfs/go-ipld-cbor" - amt3 "github.com/filecoin-project/go-amt-ipld/v3" - hamt3 "github.com/filecoin-project/go-hamt-ipld/v3" market2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/market" adt2 "github.com/filecoin-project/specs-actors/v2/actors/util/adt" @@ -28,23 +26,23 @@ func (m marketMigrator) MigrateState(ctx context.Context, store cbor.IpldStore, if err != nil { return nil, err } - proposalsCidOut, err := migrateAMTRaw(ctx, store, inState.Proposals, []amt3.Option{amt3.UseTreeBitWidth(market3.ProposalsAmtBitwidth)}) + proposalsCidOut, err := migrateAMTRaw(ctx, store, inState.Proposals, market3.ProposalsAmtBitwidth) if err != nil { return nil, err } - statesCidOut, err := migrateAMTRaw(ctx, store, inState.States, []amt3.Option{amt3.UseTreeBitWidth(market3.StatesAmtBitwidth)}) + statesCidOut, err := migrateAMTRaw(ctx, store, inState.States, market3.StatesAmtBitwidth) if err != nil { return nil, err } - escrowTableCidOut, err := migrateHAMTRaw(ctx, store, inState.EscrowTable, append(adt3.DefaultHamtOptions, hamt3.UseTreeBitWidth(adt3.BalanceTableBitwidth))) + escrowTableCidOut, err := migrateHAMTRaw(ctx, store, inState.EscrowTable, adt3.BalanceTableBitwidth) if err != nil { return nil, err } - lockedTableCidOut, err := migrateHAMTRaw(ctx, store, inState.LockedTable, append(adt3.DefaultHamtOptions, hamt3.UseTreeBitWidth(adt3.BalanceTableBitwidth))) + lockedTableCidOut, err := migrateHAMTRaw(ctx, store, inState.LockedTable, adt3.BalanceTableBitwidth) if err != nil { return nil, err } - dobeCidOut, err := migrateHAMTHAMTRaw(ctx, store, inState.DealOpsByEpoch, adt3.DefaultHamtOptionsWithDefaultBitwidth, adt3.DefaultHamtOptionsWithDefaultBitwidth) + dobeCidOut, err := migrateHAMTHAMTRaw(ctx, store, inState.DealOpsByEpoch, builtin3.DefaultHamtBitwidth, builtin3.DefaultHamtBitwidth) if err != nil { return nil, err } diff --git a/actors/migration/nv9/miner.go b/actors/migration/nv9/miner.go index bb195811f7..83132c0bdd 100644 --- a/actors/migration/nv9/miner.go +++ b/actors/migration/nv9/miner.go @@ -3,17 +3,15 @@ package nv9 import ( "context" - amt3 "github.com/filecoin-project/go-amt-ipld/v3" miner2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/miner" adt2 "github.com/filecoin-project/specs-actors/v2/actors/util/adt" - miner3 "github.com/filecoin-project/specs-actors/v3/actors/builtin/miner" - + cid "github.com/ipfs/go-cid" + cbor "github.com/ipfs/go-ipld-cbor" "golang.org/x/xerrors" builtin3 "github.com/filecoin-project/specs-actors/v3/actors/builtin" + miner3 "github.com/filecoin-project/specs-actors/v3/actors/builtin/miner" adt3 "github.com/filecoin-project/specs-actors/v3/actors/util/adt" - cid "github.com/ipfs/go-cid" - cbor "github.com/ipfs/go-ipld-cbor" ) type minerMigrator struct{} @@ -24,15 +22,15 @@ func (m minerMigrator) MigrateState(ctx context.Context, store cbor.IpldStore, i return nil, err } - preCommittedSectorsOut, err := migrateHAMTRaw(ctx, store, inState.PreCommittedSectors, adt3.DefaultHamtOptionsWithDefaultBitwidth) + preCommittedSectorsOut, err := migrateHAMTRaw(ctx, store, inState.PreCommittedSectors, builtin3.DefaultHamtBitwidth) if err != nil { return nil, err } - preCommittedSectorsExpiryOut, err := migrateAMTRaw(ctx, store, inState.PreCommittedSectorsExpiry, append(adt3.DefaultAmtOptions, amt3.UseTreeBitWidth(miner3.PrecommitExpiryAmtBitwidth))) + preCommittedSectorsExpiryOut, err := migrateAMTRaw(ctx, store, inState.PreCommittedSectorsExpiry, miner3.PrecommitExpiryAmtBitwidth) if err != nil { return nil, err } - sectorsOut, err := migrateAMTRaw(ctx, store, inState.Sectors, append(adt3.DefaultAmtOptions, amt3.UseTreeBitWidth(miner3.SectorsAmtBitwidth))) + sectorsOut, err := migrateAMTRaw(ctx, store, inState.Sectors, miner3.SectorsAmtBitwidth) if err != nil { return nil, err } @@ -90,7 +88,7 @@ func (m *minerMigrator) migrateDeadlines(ctx context.Context, store cbor.IpldSto return cid.Undef, xerrors.Errorf("partitions: %w", err) } - expirationEpochs, err := migrateAMTRaw(ctx, store, inDeadline.ExpirationsEpochs, append(adt3.DefaultAmtOptions, amt3.UseTreeBitWidth(miner3.DeadlineExpirationAmtBitwidth))) + expirationEpochs, err := migrateAMTRaw(ctx, store, inDeadline.ExpirationsEpochs, miner3.DeadlineExpirationAmtBitwidth) if err != nil { return cid.Undef, xerrors.Errorf("bitfield queue: %w", err) } @@ -121,19 +119,19 @@ func (m *minerMigrator) migratePartitions(ctx context.Context, store cbor.IpldSt if err != nil { return cid.Undef, err } - outArray, err := adt3.MakeEmptyArray(adt2.WrapStore(ctx, store), builtin3.DefaultAmtBitwidth) + outArray, err := adt3.MakeEmptyArray(adt2.WrapStore(ctx, store), miner3.DeadlinePartitionsAmtBitwidth) if err != nil { return cid.Undef, err } var inPartition miner2.Partition if err = inArray.ForEach(&inPartition, func(i int64) error { - expirationEpochs, err := migrateAMTRaw(ctx, store, inPartition.ExpirationsEpochs, append(adt3.DefaultAmtOptions, amt3.UseTreeBitWidth(miner3.PartitionExpirationAmtBitwidth))) + expirationEpochs, err := migrateAMTRaw(ctx, store, inPartition.ExpirationsEpochs, miner3.PartitionExpirationAmtBitwidth) if err != nil { return xerrors.Errorf("expiration queue: %w", err) } - earlyTerminated, err := migrateAMTRaw(ctx, store, inPartition.EarlyTerminated, adt3.DefaultAmtOptions) + earlyTerminated, err := migrateAMTRaw(ctx, store, inPartition.EarlyTerminated, miner3.PartitionEarlyTerminationArrayAmtBitwidth) if err != nil { return xerrors.Errorf("early termination queue: %w", err) } diff --git a/actors/migration/nv9/multisig.go b/actors/migration/nv9/multisig.go index 44505c7d67..bef5833389 100644 --- a/actors/migration/nv9/multisig.go +++ b/actors/migration/nv9/multisig.go @@ -8,7 +8,6 @@ import ( builtin3 "github.com/filecoin-project/specs-actors/v3/actors/builtin" multisig3 "github.com/filecoin-project/specs-actors/v3/actors/builtin/multisig" - adt3 "github.com/filecoin-project/specs-actors/v3/actors/util/adt" ) type multisigMigrator struct{} @@ -19,7 +18,7 @@ func (m multisigMigrator) MigrateState(ctx context.Context, store cbor.IpldStore return nil, err } - pendingTxnsOut, err := migrateHAMTRaw(ctx, store, inState.PendingTxns, adt3.DefaultHamtOptionsWithDefaultBitwidth) + pendingTxnsOut, err := migrateHAMTRaw(ctx, store, inState.PendingTxns, builtin3.DefaultHamtBitwidth) if err != nil { return nil, err } diff --git a/actors/migration/nv9/paych.go b/actors/migration/nv9/paych.go index ca7b9ff068..49ccedc621 100644 --- a/actors/migration/nv9/paych.go +++ b/actors/migration/nv9/paych.go @@ -8,7 +8,6 @@ import ( builtin3 "github.com/filecoin-project/specs-actors/v3/actors/builtin" paych3 "github.com/filecoin-project/specs-actors/v3/actors/builtin/paych" - adt3 "github.com/filecoin-project/specs-actors/v3/actors/util/adt" ) type paychMigrator struct{} @@ -19,7 +18,7 @@ func (m paychMigrator) MigrateState(ctx context.Context, store cbor.IpldStore, i return nil, err } - laneStatesOut, err := migrateAMTRaw(ctx, store, inState.LaneStates, adt3.DefaultAmtOptions) + laneStatesOut, err := migrateAMTRaw(ctx, store, inState.LaneStates, paych3.LaneStatesAmtBitwidth) if err != nil { return nil, err } diff --git a/actors/migration/nv9/power.go b/actors/migration/nv9/power.go index cb6db63c17..a604c67b4b 100644 --- a/actors/migration/nv9/power.go +++ b/actors/migration/nv9/power.go @@ -3,15 +3,12 @@ package nv9 import ( "context" - amt3 "github.com/filecoin-project/go-amt-ipld/v3" - hamt3 "github.com/filecoin-project/go-hamt-ipld/v3" power2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/power" cid "github.com/ipfs/go-cid" cbor "github.com/ipfs/go-ipld-cbor" builtin3 "github.com/filecoin-project/specs-actors/v3/actors/builtin" power3 "github.com/filecoin-project/specs-actors/v3/actors/builtin/power" - adt3 "github.com/filecoin-project/specs-actors/v3/actors/util/adt" smoothing3 "github.com/filecoin-project/specs-actors/v3/actors/util/smoothing" ) @@ -25,22 +22,19 @@ func (m powerMigrator) MigrateState(ctx context.Context, store cbor.IpldStore, i var proofValidationBatchOut *cid.Cid if inState.ProofValidationBatch != nil { - proofValidationBatchAmtOpts := append(adt3.DefaultAmtOptions, amt3.UseTreeBitWidth(power3.ProofValidationBatchAmtBitwidth)) - proofValidationBatchOutCID, err := migrateHAMTAMTRaw(ctx, store, *inState.ProofValidationBatch, adt3.DefaultHamtOptionsWithDefaultBitwidth, proofValidationBatchAmtOpts) + proofValidationBatchOutCID, err := migrateHAMTAMTRaw(ctx, store, *inState.ProofValidationBatch, builtin3.DefaultHamtBitwidth, power3.ProofValidationBatchAmtBitwidth) if err != nil { return nil, err } proofValidationBatchOut = &proofValidationBatchOutCID } - claimsOut, err := migrateHAMTRaw(ctx, store, inState.Claims, adt3.DefaultHamtOptionsWithDefaultBitwidth) + claimsOut, err := migrateHAMTRaw(ctx, store, inState.Claims, builtin3.DefaultHamtBitwidth) if err != nil { return nil, err } - cronEventQueueHamtOpts := append(adt3.DefaultHamtOptions, hamt3.UseTreeBitWidth(power3.CronQueueHamtBitwidth)) - cronEventQueueAmtOpts := append(adt3.DefaultAmtOptions, amt3.UseTreeBitWidth(power3.CronQueueAmtBitwidth)) - cronEventQueueOut, err := migrateHAMTAMTRaw(ctx, store, inState.CronEventQueue, cronEventQueueHamtOpts, cronEventQueueAmtOpts) + cronEventQueueOut, err := migrateHAMTAMTRaw(ctx, store, inState.CronEventQueue, power3.CronQueueHamtBitwidth, power3.CronQueueAmtBitwidth) if err != nil { return nil, err } diff --git a/actors/migration/nv9/util.go b/actors/migration/nv9/util.go index 7d0e48f3ae..a803619f11 100644 --- a/actors/migration/nv9/util.go +++ b/actors/migration/nv9/util.go @@ -8,21 +8,23 @@ import ( amt3 "github.com/filecoin-project/go-amt-ipld/v3" hamt2 "github.com/filecoin-project/go-hamt-ipld/v2" hamt3 "github.com/filecoin-project/go-hamt-ipld/v3" + adt2 "github.com/filecoin-project/specs-actors/v2/actors/util/adt" cid "github.com/ipfs/go-cid" cbor "github.com/ipfs/go-ipld-cbor" cbg "github.com/whyrusleeping/cbor-gen" - adt2 "github.com/filecoin-project/specs-actors/v2/actors/util/adt" + adt3 "github.com/filecoin-project/specs-actors/v3/actors/util/adt" ) -// Migrates a HAMT from v2 v3 without re-encoding keys or values. The new hamt +// Migrates a HAMT from v2 to v3 without re-encoding keys or values. The new hamt // configuration is specified by newOpts. -func migrateHAMTRaw(ctx context.Context, store cbor.IpldStore, root cid.Cid, newOpts []hamt3.Option) (cid.Cid, error) { +func migrateHAMTRaw(ctx context.Context, store cbor.IpldStore, root cid.Cid, newBitwidth int) (cid.Cid, error) { inRootNode, err := hamt2.LoadNode(ctx, store, root, adt2.HamtOptions...) if err != nil { return cid.Undef, err } + newOpts := append(adt3.DefaultHamtOptions, hamt3.UseTreeBitWidth(newBitwidth)) outRootNode := hamt3.NewNode(store, newOpts...) if err = inRootNode.ForEach(ctx, func(k string, val interface{}) error { @@ -39,12 +41,13 @@ func migrateHAMTRaw(ctx context.Context, store cbor.IpldStore, root cid.Cid, new } // Migrates an AMT from v2 to v3 without re-encoding values. The new amt configuration is specified by newOpts -func migrateAMTRaw(ctx context.Context, store cbor.IpldStore, root cid.Cid, newOpts []amt3.Option) (cid.Cid, error) { +func migrateAMTRaw(ctx context.Context, store cbor.IpldStore, root cid.Cid, newBitwidth int) (cid.Cid, error) { inRootNode, err := amt2.LoadAMT(ctx, store, root) if err != nil { return cid.Undef, err } + newOpts := append(adt3.DefaultAmtOptions, amt3.UseTreeBitWidth(newBitwidth)) outRootNode, err := amt3.NewAMT(store, newOpts...) if err != nil { return cid.Undef, err @@ -62,11 +65,13 @@ func migrateAMTRaw(ctx context.Context, store cbor.IpldStore, root cid.Cid, newO // Migrates a HAMT of HAMTs from v2 to v3 without re-encoding leaf keys or values. // The new outer hamt configurion is newOptsOuter, the new inner hamt configuration // is newOptsInner. -func migrateHAMTHAMTRaw(ctx context.Context, store cbor.IpldStore, root cid.Cid, newOptsOuter, newOptsInner []hamt3.Option) (cid.Cid, error) { +func migrateHAMTHAMTRaw(ctx context.Context, store cbor.IpldStore, root cid.Cid, newOuterBitwidth, newInnerBitwidth int) (cid.Cid, error) { inRootNodeOuter, err := hamt2.LoadNode(ctx, store, root) if err != nil { return cid.Undef, err } + + newOptsOuter := append(adt3.DefaultHamtOptions, hamt3.UseTreeBitWidth(newOuterBitwidth)) outRootNodeOuter := hamt3.NewNode(store, newOptsOuter...) if err = inRootNodeOuter.ForEach(ctx, func(k string, val interface{}) error { @@ -74,7 +79,7 @@ func migrateHAMTHAMTRaw(ctx context.Context, store cbor.IpldStore, root cid.Cid, if err := inInner.UnmarshalCBOR(bytes.NewReader(val.(*cbg.Deferred).Raw)); err != nil { return err } - outInner, err := migrateHAMTRaw(ctx, store, cid.Cid(inInner), newOptsInner) + outInner, err := migrateHAMTRaw(ctx, store, cid.Cid(inInner), newInnerBitwidth) if err != nil { return err } @@ -93,11 +98,12 @@ func migrateHAMTHAMTRaw(ctx context.Context, store cbor.IpldStore, root cid.Cid, // Migrates a HAMT of AMTs from v2 to v3 without re-encoding values. // The new outer hamt configuraTIon is newOptsOuter, the new inner amt configuration // is newOptsInner -func migrateHAMTAMTRaw(ctx context.Context, store cbor.IpldStore, root cid.Cid, newOptsOuter []hamt3.Option, newOptsInner []amt3.Option) (cid.Cid, error) { +func migrateHAMTAMTRaw(ctx context.Context, store cbor.IpldStore, root cid.Cid, newOuterBitwidth, newInnerBitwidth int) (cid.Cid, error) { inRootNodeOuter, err := hamt2.LoadNode(ctx, store, root) if err != nil { return cid.Undef, err } + newOptsOuter := append(adt3.DefaultHamtOptions, hamt3.UseTreeBitWidth(newOuterBitwidth)) outRootNodeOuter := hamt3.NewNode(store, newOptsOuter...) if err = inRootNodeOuter.ForEach(ctx, func(k string, val interface{}) error { @@ -105,7 +111,7 @@ func migrateHAMTAMTRaw(ctx context.Context, store cbor.IpldStore, root cid.Cid, if err := inInner.UnmarshalCBOR(bytes.NewReader(val.(*cbg.Deferred).Raw)); err != nil { return err } - outInner, err := migrateAMTRaw(ctx, store, cid.Cid(inInner), newOptsInner) + outInner, err := migrateAMTRaw(ctx, store, cid.Cid(inInner), newInnerBitwidth) if err != nil { return err } diff --git a/actors/migration/nv9/verifreg.go b/actors/migration/nv9/verifreg.go index b7bd64a721..10cae215f0 100644 --- a/actors/migration/nv9/verifreg.go +++ b/actors/migration/nv9/verifreg.go @@ -8,7 +8,6 @@ import ( builtin3 "github.com/filecoin-project/specs-actors/v3/actors/builtin" verifreg3 "github.com/filecoin-project/specs-actors/v3/actors/builtin/verifreg" - adt3 "github.com/filecoin-project/specs-actors/v3/actors/util/adt" ) type verifregMigrator struct{} @@ -19,11 +18,11 @@ func (m verifregMigrator) MigrateState(ctx context.Context, store cbor.IpldStore return nil, err } - verifiersCIDOut, err := migrateHAMTRaw(ctx, store, inState.Verifiers, adt3.DefaultHamtOptionsWithDefaultBitwidth) + verifiersCIDOut, err := migrateHAMTRaw(ctx, store, inState.Verifiers, builtin3.DefaultHamtBitwidth) if err != nil { return nil, err } - verifiedClientsCIDOut, err := migrateHAMTRaw(ctx, store, inState.Verifiers, adt3.DefaultHamtOptionsWithDefaultBitwidth) + verifiedClientsCIDOut, err := migrateHAMTRaw(ctx, store, inState.Verifiers, builtin3.DefaultHamtBitwidth) if err != nil { return nil, err } diff --git a/actors/util/adt/array.go b/actors/util/adt/array.go index 71f96248b2..0cf9696ad6 100644 --- a/actors/util/adt/array.go +++ b/actors/util/adt/array.go @@ -34,7 +34,7 @@ func AsArray(s Store, r cid.Cid, bitwidth int) (*Array, error) { }, nil } -// Creates a new map backed by an empty HAMT and flushes it to the store. +// Creates a new array backed by an empty AMT. func MakeEmptyArray(s Store, bitwidth int) (*Array, error) { options := append(DefaultAmtOptions, amt.UseTreeBitWidth(bitwidth)) root, err := amt.NewAMT(s, options...) @@ -47,6 +47,15 @@ func MakeEmptyArray(s Store, bitwidth int) (*Array, error) { }, nil } +// Writes a new empty array to the store, returning its CID. +func StoreEmptyArray(s Store, bitwidth int) (cid.Cid, error) { + arr, err := MakeEmptyArray(s, bitwidth) + if err != nil { + return cid.Undef, err + } + return arr.Root() +} + // Returns the root CID of the underlying AMT. func (a *Array) Root() (cid.Cid, error) { return a.root.Flush(a.store.Context()) diff --git a/actors/util/adt/array_test.go b/actors/util/adt/array_test.go index 300af1e589..984098c2e9 100644 --- a/actors/util/adt/array_test.go +++ b/actors/util/adt/array_test.go @@ -7,7 +7,6 @@ import ( "github.com/filecoin-project/go-address" "github.com/stretchr/testify/require" - "github.com/filecoin-project/specs-actors/v3/actors/builtin" "github.com/filecoin-project/specs-actors/v3/actors/util/adt" "github.com/filecoin-project/specs-actors/v3/support/mock" ) @@ -15,7 +14,7 @@ import ( func TestArrayNotFound(t *testing.T) { rt := mock.NewBuilder(context.Background(), address.Undef).Build(t) store := adt.AsStore(rt) - arr, err := adt.MakeEmptyArray(store, builtin.DefaultAmtBitwidth) + arr, err := adt.MakeEmptyArray(store, 3) require.NoError(t, err) found, err := arr.Get(7, nil) diff --git a/actors/util/adt/map.go b/actors/util/adt/map.go index 5dcacc5b83..b898be544d 100644 --- a/actors/util/adt/map.go +++ b/actors/util/adt/map.go @@ -7,7 +7,6 @@ import ( hamt "github.com/filecoin-project/go-hamt-ipld/v3" "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/cbor" - "github.com/filecoin-project/specs-actors/v3/actors/builtin" cid "github.com/ipfs/go-cid" errors "github.com/pkg/errors" cbg "github.com/whyrusleeping/cbor-gen" @@ -23,16 +22,6 @@ var DefaultHamtOptions = []hamt.Option{ }), } -// DefaultHamtOptionsWithDefaultBitwidth specifies DefaultHamtOptions plus -// a bitwidh of the default hamt bitwidth (5) -var DefaultHamtOptionsWithDefaultBitwidth = []hamt.Option{ - hamt.UseHashFunction(func(input []byte) []byte { - res := sha256.Sum256(input) - return res[:] - }), - hamt.UseTreeBitWidth(builtin.DefaultHamtBitwidth), -} - // Map stores key-value pairs in a HAMT. type Map struct { lastCid cid.Cid @@ -57,8 +46,7 @@ func AsMap(s Store, root cid.Cid, bitwidth int) (*Map, error) { }, nil } -// Creates a new map backed by an empty HAMT and flushes it to the store. -// The hamt +// Creates a new map backed by an empty HAMT. func MakeEmptyMap(s Store, bitwidth int) *Map { options := append(DefaultHamtOptions, hamt.UseTreeBitWidth(bitwidth)) nd := hamt.NewNode(s, options...) @@ -69,6 +57,12 @@ func MakeEmptyMap(s Store, bitwidth int) *Map { } } +// Creates and stores a new empty map, returning its CID. +func StoreEmptyMap(s Store, bitwidth int) (cid.Cid, error) { + m := MakeEmptyMap(s, bitwidth) + return m.Root() +} + // Returns the root cid of underlying HAMT. func (m *Map) Root() (cid.Cid, error) { if err := m.root.Flush(m.store.Context()); err != nil {