From c841f26256e2abcb3f0a555699773de09c9d89cc Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Tue, 18 Aug 2020 15:19:24 +0200 Subject: [PATCH 1/2] Basefee change depends on unique messages not all messages Signed-off-by: Jakub Sztandera --- build/params_shared_vals.go | 3 +++ chain/store/basefee.go | 34 +++++++++++++++++++++++++++++++--- chain/store/basefee_test.go | 6 +++--- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/build/params_shared_vals.go b/build/params_shared_vals.go index cfcab0140..2fce61ee7 100644 --- a/build/params_shared_vals.go +++ b/build/params_shared_vals.go @@ -89,11 +89,14 @@ const VerifSigCacheSize = 32000 // TODO: If this is gonna stay, it should move to specs-actors const BlockMessageLimit = 10000 + const BlockGasLimit = 10_000_000_000 const BlockGasTarget = BlockGasLimit / 2 const BaseFeeMaxChangeDenom = 8 // 12.5% const InitialBaseFee = 100e6 const MinimumBaseFee = 100 +const PackingEfficiencyNum = 4 +const PackingEfficiencyDenom = 5 // Actor consts // TODO: Pull from actors when its made not private diff --git a/chain/store/basefee.go b/chain/store/basefee.go index 7148ff9ba..3873f3945 100644 --- a/chain/store/basefee.go +++ b/chain/store/basefee.go @@ -7,11 +7,26 @@ import ( "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/specs-actors/actors/abi" "github.com/filecoin-project/specs-actors/actors/abi/big" + "github.com/ipfs/go-cid" "golang.org/x/xerrors" ) func computeNextBaseFee(baseFee types.BigInt, gasLimitUsed int64, noOfBlocks int) types.BigInt { - delta := gasLimitUsed/int64(noOfBlocks) - build.BlockGasTarget + // deta := 1/PackingEfficiency * gasLimitUsed/noOfBlocks - build.BlockGasTarget + // change := baseFee * deta / BlockGasTarget / BaseFeeMaxChangeDenom + // nextBaseFee = baseFee + change + // nextBaseFee = max(nextBaseFee, build.MinimumBaseFee) + + delta := build.PackingEfficiencyDenom * gasLimitUsed / int64(noOfBlocks*build.PackingEfficiencyNum) + delta -= build.BlockGasTarget + + // cap change at 12.5% (BaseFeeMaxChangeDenom) by capping delta + if delta > build.BlockGasTarget { + delta = build.BlockGasTarget + } + if delta < -build.BlockGasTarget { + delta = -build.BlockGasTarget + } change := big.Mul(baseFee, big.NewInt(delta)) change = big.Div(change, big.NewInt(build.BlockGasTarget)) @@ -26,17 +41,30 @@ func computeNextBaseFee(baseFee types.BigInt, gasLimitUsed int64, noOfBlocks int func (cs *ChainStore) ComputeBaseFee(ctx context.Context, ts *types.TipSet) (abi.TokenAmount, error) { zero := abi.NewTokenAmount(0) + + // totalLimit is sum of GasLimits of unique messages in a tipset totalLimit := int64(0) + + seen := make(map[cid.Cid]struct{}) + for _, b := range ts.Blocks() { msg1, msg2, err := cs.MessagesForBlock(b) if err != nil { return zero, xerrors.Errorf("error getting messages for: %s: %w", b.Cid(), err) } for _, m := range msg1 { - totalLimit += m.GasLimit + c := m.Cid() + if _, ok := seen[c]; !ok { + totalLimit += m.GasLimit + seen[c] = struct{}{} + } } for _, m := range msg2 { - totalLimit += m.Message.GasLimit + c := m.Cid() + if _, ok := seen[c]; !ok { + totalLimit += m.Message.GasLimit + seen[c] = struct{}{} + } } } parentBaseFee := ts.Blocks()[0].ParentBaseFee diff --git a/chain/store/basefee_test.go b/chain/store/basefee_test.go index c2186a711..7a7cae911 100644 --- a/chain/store/basefee_test.go +++ b/chain/store/basefee_test.go @@ -18,10 +18,10 @@ func TestBaseFee(t *testing.T) { }{ {100e6, 0, 1, 87.5e6}, {100e6, 0, 5, 87.5e6}, - {100e6, build.BlockGasTarget, 1, 100e6}, - {100e6, build.BlockGasTarget * 2, 2, 100e6}, + {100e6, build.BlockGasTarget, 1, 103.125e6}, + {100e6, build.BlockGasTarget * 2, 2, 103.125e6}, {100e6, build.BlockGasLimit * 2, 2, 112.5e6}, - {100e6, build.BlockGasLimit * 1.5, 2, 106.25e6}, + {100e6, build.BlockGasLimit * 1.5, 2, 110937500}, } for _, test := range tests { From c9fdd4bee279a05d9364ca4ef71dfb1683ac6ed4 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Tue, 18 Aug 2020 17:40:53 +0200 Subject: [PATCH 2/2] Fix testgrounds build Signed-off-by: Jakub Sztandera --- build/params_testground.go | 3 +++ chain/store/basefee.go | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/build/params_testground.go b/build/params_testground.go index ffd5ac9a4..bdd56fbb1 100644 --- a/build/params_testground.go +++ b/build/params_testground.go @@ -66,4 +66,7 @@ var ( // Actor consts // TODO: Pull from actors when its made not private MinDealDuration = abi.ChainEpoch(180 * builtin.EpochsInDay) + + PackingEfficiencyNum int64 = 4 + PackingEfficiencyDenom int64 = 5 ) diff --git a/chain/store/basefee.go b/chain/store/basefee.go index 3873f3945..de3f90a8f 100644 --- a/chain/store/basefee.go +++ b/chain/store/basefee.go @@ -17,7 +17,7 @@ func computeNextBaseFee(baseFee types.BigInt, gasLimitUsed int64, noOfBlocks int // nextBaseFee = baseFee + change // nextBaseFee = max(nextBaseFee, build.MinimumBaseFee) - delta := build.PackingEfficiencyDenom * gasLimitUsed / int64(noOfBlocks*build.PackingEfficiencyNum) + delta := build.PackingEfficiencyDenom * gasLimitUsed / (int64(noOfBlocks) * build.PackingEfficiencyNum) delta -= build.BlockGasTarget // cap change at 12.5% (BaseFeeMaxChangeDenom) by capping delta