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

Commit

Permalink
Instate bitwidth tuning parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenGround0 committed Dec 14, 2020
1 parent 6dc1ff8 commit ca9c602
Show file tree
Hide file tree
Showing 27 changed files with 236 additions and 133 deletions.
25 changes: 19 additions & 6 deletions actors/builtin/market/market_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,23 @@ type State struct {
}

func ConstructState(store adt.Store) (*State, error) {
emptyArray, err := adt.MakeEmptyArray(store, builtin.DefaultAmtBitwidth)
emptyProposalsArray, err := adt.MakeEmptyArray(store, ProposalsAmtBitwidth)
if err != nil {
return nil, xerrors.Errorf("failed to create empty array: %w", err)
}
emptyArrayCid, err := emptyArray.Root()
emptyProposalsArrayCid, err := emptyProposalsArray.Root()
if err != nil {
return nil, xerrors.Errorf("failed to persist empty array: %w", err)
}
emptyStatesArray, err := adt.MakeEmptyArray(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()
if err != nil {
return nil, xerrors.Errorf("failed to create empty map: %w", err)
Expand All @@ -77,13 +86,17 @@ func ConstructState(store adt.Store) (*State, error) {
if err != nil {
return nil, xerrors.Errorf("failed to create empty multiset: %w", err)
}
emptyBalanceTableCid, err := adt.MakeEmptyMap(store, adt.BalanceTableBitwidth).Root()
if err != nil {
return nil, xerrors.Errorf("failed to create empty balance table: %w", err)
}

return &State{
Proposals: emptyArrayCid,
States: emptyArrayCid,
Proposals: emptyProposalsArrayCid,
States: emptyStatesArrayCid,
PendingProposals: emptyMapCid,
EscrowTable: emptyMapCid,
LockedTable: emptyMapCid,
EscrowTable: emptyBalanceTableCid,
LockedTable: emptyBalanceTableCid,
NextID: abi.DealID(0),
DealOpsByEpoch: emptyMSetCid,
LastCron: abi.ChainEpoch(-1),
Expand Down
24 changes: 17 additions & 7 deletions actors/builtin/market/market_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,22 @@ func TestMarketActor(t *testing.T) {

store := adt.AsStore(rt)

emptyBalanceTable, err := adt.MakeEmptyMap(store, adt.BalanceTableBitwidth).Root()
assert.NoError(t, err)

emptyMap, err := adt.MakeEmptyMap(store, builtin.DefaultHamtBitwidth).Root()
assert.NoError(t, err)

emptyArray, err := adt.MakeEmptyArray(store, builtin.DefaultAmtBitwidth)
require.NoError(t, err)
emptyProposalsArray, err := adt.MakeEmptyArray(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)

emptyArrayRoot, err := emptyArray.Root()
emptyStatesArrayCid, err := emptyStatesArray.Root()
assert.NoError(t, err)

emptyMultiMap, err := market.MakeEmptySetMultimap(store, builtin.DefaultHamtBitwidth).Root()
Expand All @@ -97,10 +106,11 @@ func TestMarketActor(t *testing.T) {
var state market.State
rt.GetState(&state)

assert.Equal(t, emptyArrayRoot, state.Proposals)
assert.Equal(t, emptyArrayRoot, state.States)
assert.Equal(t, emptyMap, state.EscrowTable)
assert.Equal(t, emptyMap, state.LockedTable)
assert.Equal(t, emptyProposalsArrayCid, state.Proposals)
assert.Equal(t, emptyStatesArrayCid, state.States)
assert.Equal(t, emptyMap, state.PendingProposals)
assert.Equal(t, emptyBalanceTable, state.EscrowTable)
assert.Equal(t, emptyBalanceTable, state.LockedTable)
assert.Equal(t, abi.DealID(0), state.NextID)
assert.Equal(t, emptyMultiMap, state.DealOpsByEpoch)
assert.Equal(t, abi.ChainEpoch(-1), state.LastCron)
Expand Down
8 changes: 8 additions & 0 deletions actors/builtin/market/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ var DealMaxDuration = abi.ChainEpoch(540 * builtin.EpochsInDay) // PARAM_SPEC
// DealMaxLabelSize is the maximum size of a deal label.
const DealMaxLabelSize = 256

// Bitwidth of proposals AMT determined empirically from mutation patterns and
// projections of mainnet data.
const ProposalsAmtBitwidth = 5

// Bitwidth of states AMT determined empirically from mutation patterns and
// projections of mainnet data.
const StatesAmtBitwidth = 6

// Bounds (inclusive) on deal duration
func DealDurationBounds(_ abi.PaddedPieceSize) (min abi.ChainEpoch, max abi.ChainEpoch) {
return DealMinDuration, DealMaxDuration
Expand Down
4 changes: 2 additions & 2 deletions actors/builtin/market/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func CheckStateInvariants(st *State, store adt.Store, balance abi.TokenAmount, c
expectedDealOps := make(map[abi.DealID]struct{})
totalProposalCollateral := abi.NewTokenAmount(0)

if proposals, err := adt.AsArray(store, st.Proposals, builtin.DefaultAmtBitwidth); err != nil {
if proposals, err := adt.AsArray(store, st.Proposals, ProposalsAmtBitwidth); err != nil {
acc.Addf("error loading proposals: %v", err)
} else {
var proposal DealProposal
Expand Down Expand Up @@ -105,7 +105,7 @@ func CheckStateInvariants(st *State, store adt.Store, balance abi.TokenAmount, c
//

dealStateCount := uint64(0)
if dealStates, err := adt.AsArray(store, st.States, builtin.DefaultAmtBitwidth); err != nil {
if dealStates, err := adt.AsArray(store, st.States, StatesAmtBitwidth); err != nil {
acc.Addf("error loading deal states: %v", err)
} else {
var dealState DealState
Expand Down
5 changes: 2 additions & 3 deletions actors/builtin/market/types.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-state-types/abi"
"github.com/filecoin-project/specs-actors/v3/actors/builtin"
. "github.com/filecoin-project/specs-actors/v3/actors/util/adt"

"github.com/ipfs/go-cid"
Expand All @@ -16,7 +15,7 @@ type DealArray struct {

// Interprets a store as balance table with root `r`.
func AsDealProposalArray(s Store, r cid.Cid) (*DealArray, error) {
a, err := AsArray(s, r, builtin.DefaultAmtBitwidth)
a, err := AsArray(s, r, ProposalsAmtBitwidth)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -57,7 +56,7 @@ type DealState struct {

// Interprets a store as balance table with root `r`.
func AsDealStateArray(s Store, r cid.Cid) (*DealMetaArray, error) {
dsa, err := AsArray(s, r, builtin.DefaultAmtBitwidth)
dsa, err := AsArray(s, r, StatesAmtBitwidth)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions actors/builtin/miner/bitfield_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,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/adt"
)

Expand All @@ -20,8 +19,8 @@ type BitfieldQueue struct {
quant QuantSpec
}

func LoadBitfieldQueue(store adt.Store, root cid.Cid, quant QuantSpec) (BitfieldQueue, error) {
arr, err := adt.AsArray(store, root, builtin.DefaultAmtBitwidth)
func LoadBitfieldQueue(store adt.Store, root cid.Cid, quant QuantSpec, bitwidth int) (BitfieldQueue, error) {
arr, err := adt.AsArray(store, root, bitwidth)
if err != nil {
return BitfieldQueue{}, xerrors.Errorf("failed to load epoch queue %v: %w", root, err)
}
Expand Down
30 changes: 15 additions & 15 deletions actors/builtin/miner/bitfield_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

func TestBitfieldQueue(t *testing.T) {
t.Run("adds values to empty queue", func(t *testing.T) {
queue := emptyBitfieldQueue(t)
queue := emptyBitfieldQueue(t, builtin.DefaultAmtBitwidth)

values := []uint64{1, 2, 3, 4}
epoch := abi.ChainEpoch(42)
Expand All @@ -30,7 +30,7 @@ func TestBitfieldQueue(t *testing.T) {
})

t.Run("adds bitfield to empty queue", func(t *testing.T) {
queue := emptyBitfieldQueue(t)
queue := emptyBitfieldQueue(t, builtin.DefaultAmtBitwidth)

values := []uint64{1, 2, 3, 4}
epoch := abi.ChainEpoch(42)
Expand All @@ -43,7 +43,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))
queue := emptyBitfieldQueueWithQuantizing(t, miner.NewQuantSpec(5, 3), builtin.DefaultAmtBitwidth)

for _, val := range []uint64{0, 2, 3, 4, 7, 8, 9} {
require.NoError(t, queue.AddToQueueValues(abi.ChainEpoch(val), val))
Expand All @@ -58,7 +58,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))
queue := emptyBitfieldQueueWithQuantizing(t, miner.NewQuantSpec(5, 3), builtin.DefaultAmtBitwidth)

for _, val := range []uint64{0, 2, 3, 4, 7, 8, 9} {
err := queue.AddToQueueValues(abi.ChainEpoch(val), val)
Expand All @@ -74,7 +74,7 @@ func TestBitfieldQueue(t *testing.T) {
})

t.Run("merges values withing same epoch", func(t *testing.T) {
queue := emptyBitfieldQueue(t)
queue := emptyBitfieldQueue(t, builtin.DefaultAmtBitwidth)

epoch := abi.ChainEpoch(42)

Expand All @@ -87,7 +87,7 @@ func TestBitfieldQueue(t *testing.T) {
})

t.Run("adds values to different epochs", func(t *testing.T) {
queue := emptyBitfieldQueue(t)
queue := emptyBitfieldQueue(t, builtin.DefaultAmtBitwidth)

epoch1 := abi.ChainEpoch(42)
epoch2 := abi.ChainEpoch(93)
Expand All @@ -102,7 +102,7 @@ func TestBitfieldQueue(t *testing.T) {
})

t.Run("PouUntil from empty queue returns empty bitfield", func(t *testing.T) {
queue := emptyBitfieldQueue(t)
queue := emptyBitfieldQueue(t, builtin.DefaultAmtBitwidth)

// TODO: broken pending https://github.com/filecoin-project/go-amt-ipld/issues/18
//emptyQueue, err := queue.Root()
Expand All @@ -123,7 +123,7 @@ func TestBitfieldQueue(t *testing.T) {
})

t.Run("PopUntil does nothing if 'until' parameter before first value", func(t *testing.T) {
queue := emptyBitfieldQueue(t)
queue := emptyBitfieldQueue(t, builtin.DefaultAmtBitwidth)

epoch1 := abi.ChainEpoch(42)
epoch2 := abi.ChainEpoch(93)
Expand All @@ -149,7 +149,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)
queue := emptyBitfieldQueue(t, builtin.DefaultAmtBitwidth)

epoch1 := abi.ChainEpoch(42)
epoch2 := abi.ChainEpoch(93)
Expand Down Expand Up @@ -207,7 +207,7 @@ func TestBitfieldQueue(t *testing.T) {
})

t.Run("cuts elements", func(t *testing.T) {
queue := emptyBitfieldQueue(t)
queue := emptyBitfieldQueue(t, builtin.DefaultAmtBitwidth)

epoch1 := abi.ChainEpoch(42)
epoch2 := abi.ChainEpoch(93)
Expand All @@ -224,21 +224,21 @@ func TestBitfieldQueue(t *testing.T) {

}

func emptyBitfieldQueueWithQuantizing(t *testing.T, quant miner.QuantSpec) miner.BitfieldQueue {
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, builtin.DefaultAmtBitwidth)
emptyArray, err := adt.MakeEmptyArray(store, bitwidth)
require.NoError(t, err)
root, err := emptyArray.Root()
require.NoError(t, err)

queue, err := miner.LoadBitfieldQueue(store, root, quant)
queue, err := miner.LoadBitfieldQueue(store, root, quant, bitwidth)
require.NoError(t, err)
return queue
}

func emptyBitfieldQueue(t *testing.T) miner.BitfieldQueue {
return emptyBitfieldQueueWithQuantizing(t, miner.NoQuantization)
func emptyBitfieldQueue(t *testing.T, bitwidth int) miner.BitfieldQueue {
return emptyBitfieldQueueWithQuantizing(t, miner.NoQuantization, bitwidth)
}

type bqExpectation struct {
Expand Down
31 changes: 21 additions & 10 deletions actors/builtin/miner/deadline_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type Deadline struct {
FaultyPower PowerPair
}

const DeadlineExpirationAmtBitwidth = 5

//
// Deadlines (plural)
//
Expand Down Expand Up @@ -117,10 +119,10 @@ func (d *Deadlines) UpdateDeadline(store adt.Store, dlIdx uint64, deadline *Dead
// Deadline (singular)
//

func ConstructDeadline(emptyArrayCid cid.Cid) *Deadline {
func ConstructDeadline(emptyPartitionsArrayCid, emptyDeadlineExpirationCid cid.Cid) *Deadline {
return &Deadline{
Partitions: emptyArrayCid,
ExpirationsEpochs: emptyArrayCid,
Partitions: emptyPartitionsArrayCid,
ExpirationsEpochs: emptyDeadlineExpirationCid,
PostSubmissions: bitfield.New(),
EarlyTerminations: bitfield.New(),
LiveSectors: 0,
Expand Down Expand Up @@ -160,7 +162,7 @@ func (d *Deadline) AddExpirationPartitions(store adt.Store, expirationEpoch abi.
return nil
}

queue, err := LoadBitfieldQueue(store, d.ExpirationsEpochs, quant)
queue, err := LoadBitfieldQueue(store, d.ExpirationsEpochs, quant, DeadlineExpirationAmtBitwidth)
if err != nil {
return xerrors.Errorf("failed to load expiration queue: %w", err)
}
Expand Down Expand Up @@ -299,15 +301,24 @@ func (dl *Deadline) AddSectors(
// This case will usually happen zero times.
// It would require adding more than a full partition in one go
// to happen more than once.
emptyArray, err := adt.MakeEmptyArray(store, builtin.DefaultAmtBitwidth)
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
}
emptyArrayRoot, err := emptyArray.Root()
emptyEarlyTerminationArrayRoot, err := emptyEarlyTerminationArray.Root()
if err != nil {
return NewPowerPairZero(), err
}
partition = ConstructPartition(emptyArrayRoot)
partition = ConstructPartition(emptyExpirationArrayRoot, emptyEarlyTerminationArrayRoot)
}

// Figure out which (if any) sectors we want to add to this partition.
Expand Down Expand Up @@ -356,7 +367,7 @@ func (dl *Deadline) AddSectors(

// Next, update the expiration queue.
{
deadlineExpirations, err := LoadBitfieldQueue(store, dl.ExpirationsEpochs, quant)
deadlineExpirations, err := LoadBitfieldQueue(store, dl.ExpirationsEpochs, quant, DeadlineExpirationAmtBitwidth)
if err != nil {
return NewPowerPairZero(), xerrors.Errorf("failed to load expiration epochs: %w", err)
}
Expand Down Expand Up @@ -453,7 +464,7 @@ func (dl *Deadline) PopEarlyTerminations(store adt.Store, maxPartitions, maxSect

// Returns nil if nothing was popped.
func (dl *Deadline) popExpiredPartitions(store adt.Store, until abi.ChainEpoch, quant QuantSpec) (bitfield.BitField, bool, error) {
expirations, err := LoadBitfieldQueue(store, dl.ExpirationsEpochs, quant)
expirations, err := LoadBitfieldQueue(store, dl.ExpirationsEpochs, quant, DeadlineExpirationAmtBitwidth)
if err != nil {
return bitfield.BitField{}, false, err
}
Expand Down Expand Up @@ -664,7 +675,7 @@ func (dl *Deadline) RemovePartitions(store adt.Store, toRemove bitfield.BitField

// Update expiration bitfields.
{
expirationEpochs, err := LoadBitfieldQueue(store, dl.ExpirationsEpochs, quant)
expirationEpochs, err := LoadBitfieldQueue(store, dl.ExpirationsEpochs, quant, DeadlineExpirationAmtBitwidth)
if err != nil {
return bitfield.BitField{}, bitfield.BitField{}, NewPowerPairZero(), xerrors.Errorf("failed to load expiration queue: %w", err)
}
Expand Down
11 changes: 8 additions & 3 deletions actors/builtin/miner/deadline_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,12 +876,17 @@ func TestDeadlines(t *testing.T) {
}

func emptyDeadline(t *testing.T, store adt.Store) *miner.Deadline {
emptyArray, err := adt.MakeEmptyArray(store, builtin.DefaultAmtBitwidth)
emptyPartitionsArray, err := adt.MakeEmptyArray(store, builtin.DefaultAmtBitwidth)
require.NoError(t, err)
root, err := emptyArray.Root()
partitionsRoot, err := emptyPartitionsArray.Root()
require.NoError(t, err)

return miner.ConstructDeadline(root)
emptyDeadlineExpirationArray, err := adt.MakeEmptyArray(store, miner.DeadlineExpirationAmtBitwidth)
require.NoError(t, err)
expirationsRoot, err := emptyDeadlineExpirationArray.Root()
require.NoError(t, err)

return miner.ConstructDeadline(partitionsRoot, expirationsRoot)
}

// Helper type for validating deadline state.
Expand Down
Loading

0 comments on commit ca9c602

Please sign in to comment.