Skip to content
This repository has been archived by the owner on Jun 6, 2023. It is now read-only.

Commit

Permalink
Small refactors, remove default AMT bitwidth
Browse files Browse the repository at this point in the history
  • Loading branch information
anorth committed Dec 16, 2020
1 parent ca9c602 commit c36f6c3
Show file tree
Hide file tree
Showing 36 changed files with 185 additions and 251 deletions.
4 changes: 2 additions & 2 deletions actors/builtin/init/init_actor_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 7 additions & 15 deletions actors/builtin/market/market_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,44 +61,36 @@ 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)
}

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),
Expand Down
14 changes: 4 additions & 10 deletions actors/builtin/market/market_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
29 changes: 14 additions & 15 deletions actors/builtin/miner/bitfield_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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))
Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
40 changes: 17 additions & 23 deletions actors/builtin/miner/deadline_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -57,6 +56,7 @@ type Deadline struct {
FaultyPower PowerPair
}

const DeadlinePartitionsAmtBitwidth = 3
const DeadlineExpirationAmtBitwidth = 5

//
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 2 additions & 10 deletions actors/builtin/miner/deadline_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions actors/builtin/miner/deadlines.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
Expand Down
Loading

0 comments on commit c36f6c3

Please sign in to comment.