forked from ethereum-optimism/op-geth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port the block limit per fee currency (#119)
* Port the block limit per fee currency feature Closes #65 This implements the block-limit per fee-currency feature. Some parts of this have been directly ported from celo-blockchain (celo-org/celo-blockchain@dc45bdc00). * Copy MultiGasPool in miner environment * Add USDT and USDC to block-limit per currency defaults * Initialize MultiGasPool with currency whitelist
- Loading branch information
Showing
11 changed files
with
370 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package core | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
type FeeCurrency = common.Address | ||
|
||
// MultiGasPool tracks the amount of gas available during execution | ||
// of the transactions in a block per fee currency. The zero value is a pool | ||
// with zero gas available. | ||
type MultiGasPool struct { | ||
pools map[FeeCurrency]*GasPool | ||
defaultPool *GasPool | ||
} | ||
|
||
type FeeCurrencyLimitMapping = map[FeeCurrency]float64 | ||
|
||
// NewMultiGasPool creates a multi-fee currency gas pool and a default fallback | ||
// pool for CELO | ||
func NewMultiGasPool( | ||
blockGasLimit uint64, | ||
whitelist []FeeCurrency, | ||
defaultLimit float64, | ||
limitsMapping FeeCurrencyLimitMapping, | ||
) *MultiGasPool { | ||
pools := make(map[FeeCurrency]*GasPool, len(whitelist)) | ||
|
||
for i := range whitelist { | ||
currency := whitelist[i] | ||
fraction, ok := limitsMapping[currency] | ||
if !ok { | ||
fraction = defaultLimit | ||
} | ||
|
||
pools[currency] = new(GasPool).AddGas( | ||
uint64(float64(blockGasLimit) * fraction), | ||
) | ||
} | ||
|
||
// A special case for CELO which doesn't have a limit | ||
celoPool := new(GasPool).AddGas(blockGasLimit) | ||
|
||
return &MultiGasPool{ | ||
pools: pools, | ||
defaultPool: celoPool, | ||
} | ||
} | ||
|
||
// PoolFor returns a configured pool for the given fee currency or the default | ||
// one otherwise | ||
func (mgp MultiGasPool) PoolFor(feeCurrency *FeeCurrency) *GasPool { | ||
if feeCurrency == nil || mgp.pools[*feeCurrency] == nil { | ||
return mgp.defaultPool | ||
} | ||
|
||
return mgp.pools[*feeCurrency] | ||
} | ||
|
||
func (mgp MultiGasPool) Copy() *MultiGasPool { | ||
pools := make(map[FeeCurrency]*GasPool, len(mgp.pools)) | ||
for fc, gp := range mgp.pools { | ||
gpCpy := *gp | ||
pools[fc] = &gpCpy | ||
} | ||
gpCpy := *mgp.defaultPool | ||
return &MultiGasPool{ | ||
pools: pools, | ||
defaultPool: &gpCpy, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package core | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
func TestMultiCurrencyGasPool(t *testing.T) { | ||
blockGasLimit := uint64(1_000) | ||
subGasAmount := 100 | ||
|
||
cUSDToken := common.HexToAddress("0x765DE816845861e75A25fCA122bb6898B8B1282a") | ||
cEURToken := common.HexToAddress("0xD8763CBa276a3738E6DE85b4b3bF5FDed6D6cA73") | ||
|
||
testCases := []struct { | ||
name string | ||
feeCurrency *FeeCurrency | ||
whitelist []FeeCurrency | ||
defaultLimit float64 | ||
limits FeeCurrencyLimitMapping | ||
defaultPoolExpected bool | ||
expectedValue uint64 | ||
}{ | ||
{ | ||
name: "Empty whitelist, empty mapping, CELO uses default pool", | ||
feeCurrency: nil, | ||
whitelist: []FeeCurrency{}, | ||
defaultLimit: 0.9, | ||
limits: map[FeeCurrency]float64{}, | ||
defaultPoolExpected: true, | ||
expectedValue: 900, // blockGasLimit - subGasAmount | ||
}, | ||
{ | ||
name: "Non-empty whitelist, non-empty mapping, CELO uses default pool", | ||
feeCurrency: nil, | ||
whitelist: []FeeCurrency{ | ||
cUSDToken, | ||
}, | ||
defaultLimit: 0.9, | ||
limits: map[FeeCurrency]float64{ | ||
cUSDToken: 0.5, | ||
}, | ||
defaultPoolExpected: true, | ||
expectedValue: 900, // blockGasLimit - subGasAmount | ||
}, | ||
{ | ||
name: "Empty whitelist, empty mapping, non-whitelisted currency fallbacks to the default pool", | ||
feeCurrency: &cUSDToken, | ||
whitelist: []FeeCurrency{}, | ||
defaultLimit: 0.9, | ||
limits: map[FeeCurrency]float64{}, | ||
defaultPoolExpected: true, | ||
expectedValue: 900, // blockGasLimit - subGasAmount | ||
}, | ||
{ | ||
name: "Non-empty whitelist, non-empty mapping, non-whitelisted currency uses default pool", | ||
feeCurrency: &cEURToken, | ||
whitelist: []FeeCurrency{ | ||
cUSDToken, | ||
}, | ||
defaultLimit: 0.9, | ||
limits: map[FeeCurrency]float64{ | ||
cUSDToken: 0.5, | ||
}, | ||
defaultPoolExpected: true, | ||
expectedValue: 900, // blockGasLimit - subGasAmount | ||
}, | ||
{ | ||
name: "Non-empty whitelist, empty mapping, whitelisted currency uses default limit", | ||
feeCurrency: &cUSDToken, | ||
whitelist: []FeeCurrency{ | ||
cUSDToken, | ||
}, | ||
defaultLimit: 0.9, | ||
limits: map[FeeCurrency]float64{}, | ||
defaultPoolExpected: false, | ||
expectedValue: 800, // blockGasLimit * defaultLimit - subGasAmount | ||
}, | ||
{ | ||
name: "Non-empty whitelist, non-empty mapping, configured whitelisted currency uses configured limits", | ||
feeCurrency: &cUSDToken, | ||
whitelist: []FeeCurrency{ | ||
cUSDToken, | ||
}, | ||
defaultLimit: 0.9, | ||
limits: map[FeeCurrency]float64{ | ||
cUSDToken: 0.5, | ||
}, | ||
defaultPoolExpected: false, | ||
expectedValue: 400, // blockGasLimit * 0.5 - subGasAmount | ||
}, | ||
{ | ||
name: "Non-empty whitelist, non-empty mapping, unconfigured whitelisted currency uses default limit", | ||
feeCurrency: &cEURToken, | ||
whitelist: []FeeCurrency{ | ||
cUSDToken, | ||
cEURToken, | ||
}, | ||
defaultLimit: 0.9, | ||
limits: map[FeeCurrency]float64{ | ||
cUSDToken: 0.5, | ||
}, | ||
defaultPoolExpected: false, | ||
expectedValue: 800, // blockGasLimit * 0.5 - subGasAmount | ||
}, | ||
} | ||
|
||
for _, c := range testCases { | ||
t.Run(c.name, func(t *testing.T) { | ||
mgp := NewMultiGasPool( | ||
blockGasLimit, | ||
c.whitelist, | ||
c.defaultLimit, | ||
c.limits, | ||
) | ||
|
||
pool := mgp.PoolFor(c.feeCurrency) | ||
pool.SubGas(uint64(subGasAmount)) | ||
|
||
if c.defaultPoolExpected { | ||
result := mgp.PoolFor(nil).Gas() | ||
if result != c.expectedValue { | ||
t.Error("Default pool expected", c.expectedValue, "got", result) | ||
} | ||
} else { | ||
result := mgp.PoolFor(c.feeCurrency).Gas() | ||
|
||
if result != c.expectedValue { | ||
t.Error( | ||
"Expected pool", c.feeCurrency, "value", c.expectedValue, | ||
"got", result, | ||
) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package miner | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/params" | ||
) | ||
|
||
// cStables addresses on mainnet | ||
var ( | ||
cUSD_TOKEN = common.HexToAddress("0x765DE816845861e75A25fCA122bb6898B8B1282a") | ||
cEUR_TOKEN = common.HexToAddress("0xD8763CBa276a3738E6DE85b4b3bF5FDed6D6cA73") | ||
cREAL_TOKEN = common.HexToAddress("0xe8537a3d056DA446677B9E9d6c5dB704EaAb4787") | ||
USDC_TOKEN = common.HexToAddress("0xcebA9300f2b948710d2653dD7B07f33A8B32118C") | ||
USDT_TOKEN = common.HexToAddress("0x48065fbBE25f71C9282ddf5e1cD6D6A887483D5e") | ||
) | ||
|
||
// default limits default fraction | ||
const DefaultFeeCurrencyLimit = 0.5 | ||
|
||
// default limits configuration | ||
var DefaultFeeCurrencyLimits = map[uint64]map[common.Address]float64{ | ||
params.CeloMainnetChainID: { | ||
cUSD_TOKEN: 0.9, | ||
USDT_TOKEN: 0.9, | ||
USDC_TOKEN: 0.9, | ||
cEUR_TOKEN: 0.5, | ||
cREAL_TOKEN: 0.5, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.