forked from ethereum-optimism/op-geth
-
Notifications
You must be signed in to change notification settings - Fork 2
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 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). However there is a small difference: The `MultiGasPool` does not consider the currency whitelist here because fee-currency validity will be checked by the transaction-validation logic upon adding transactions to the tx-pool. This means that the `GetPool` method will create a GasPool on the fly, if it is not memoized in the internal map yet, no matter if the feeCurrency is whitelisted or not. In practice, this will never happen for non-whitelisted currencies, because those transactions will not make it to the tx-pool.
- Loading branch information
Showing
11 changed files
with
336 additions
and
20 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,66 @@ | ||
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 | ||
|
||
blockGasLimit uint64 | ||
defaultLimit float64 | ||
} | ||
|
||
type FeeCurrencyLimitMapping = map[FeeCurrency]float64 | ||
|
||
// NewMultiGasPool creates a multi-fee currency gas pool and a default fallback | ||
// pool for CELO | ||
func NewMultiGasPool( | ||
blockGasLimit uint64, | ||
defaultLimit float64, | ||
limitsMapping FeeCurrencyLimitMapping, | ||
) *MultiGasPool { | ||
pools := make(map[FeeCurrency]*GasPool, len(limitsMapping)) | ||
// A special case for CELO which doesn't have a limit | ||
celoPool := new(GasPool).AddGas(blockGasLimit) | ||
mgp := &MultiGasPool{ | ||
pools: pools, | ||
defaultPool: celoPool, | ||
blockGasLimit: blockGasLimit, | ||
defaultLimit: defaultLimit, | ||
} | ||
for feeCurrency, fraction := range limitsMapping { | ||
mgp.getOrInitPool(feeCurrency, &fraction) | ||
} | ||
return mgp | ||
} | ||
|
||
func (mgp MultiGasPool) getOrInitPool(c FeeCurrency, fraction *float64) *GasPool { | ||
if gp, ok := mgp.pools[c]; ok { | ||
return gp | ||
} | ||
if fraction == nil { | ||
fraction = &mgp.defaultLimit | ||
} | ||
gp := new(GasPool).AddGas( | ||
uint64(float64(mgp.blockGasLimit) * *fraction), | ||
) | ||
mgp.pools[c] = gp | ||
return gp | ||
} | ||
|
||
// GetPool returns an initialised pool for the given fee currency or | ||
// initialises and returns a new pool with a default limit. | ||
// For a `nil` FeeCurrency value, it returns the default pool. | ||
func (mgp MultiGasPool) GetPool(c *FeeCurrency) *GasPool { | ||
if c == nil { | ||
return mgp.defaultPool | ||
} | ||
// Use the default fraction here because the configured limits' | ||
// pools have been created already in the constructor. | ||
return mgp.getOrInitPool(*c, nil) | ||
} |
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,118 @@ | ||
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 | ||
defaultLimit float64 | ||
limits FeeCurrencyLimitMapping | ||
defaultPoolExpected bool | ||
expectedValue uint64 | ||
}{ | ||
{ | ||
name: "Empty mapping, CELO uses default pool", | ||
feeCurrency: nil, | ||
defaultLimit: 0.9, | ||
limits: map[FeeCurrency]float64{}, | ||
defaultPoolExpected: true, | ||
expectedValue: 900, // blockGasLimit - subGasAmount | ||
}, | ||
{ | ||
name: "Non-empty mapping, CELO uses default pool", | ||
feeCurrency: nil, | ||
defaultLimit: 0.9, | ||
limits: map[FeeCurrency]float64{ | ||
cUSDToken: 0.5, | ||
}, | ||
defaultPoolExpected: true, | ||
expectedValue: 900, // blockGasLimit - subGasAmount | ||
}, | ||
{ | ||
name: "Empty mapping, currency fallbacks to the default limit", | ||
feeCurrency: &cUSDToken, | ||
defaultLimit: 0.9, | ||
limits: map[FeeCurrency]float64{}, | ||
defaultPoolExpected: false, | ||
expectedValue: 800, // blockGasLimit * defaultLimit- subGasAmount | ||
}, | ||
{ | ||
name: "Non-empty mapping, currency uses default limit", | ||
feeCurrency: &cEURToken, | ||
defaultLimit: 0.9, | ||
limits: map[FeeCurrency]float64{ | ||
cUSDToken: 0.5, | ||
}, | ||
defaultPoolExpected: false, | ||
expectedValue: 800, // blockGasLimit * defaultLimit - subGasAmount | ||
}, | ||
{ | ||
name: "Non-empty whitelist, empty mapping, currency uses default limit", | ||
feeCurrency: &cUSDToken, | ||
defaultLimit: 0.9, | ||
limits: map[FeeCurrency]float64{}, | ||
defaultPoolExpected: false, | ||
expectedValue: 800, // blockGasLimit * defaultLimit - subGasAmount | ||
}, | ||
{ | ||
name: "Non-empty mapping, configured currency uses configured limits", | ||
feeCurrency: &cUSDToken, | ||
defaultLimit: 0.9, | ||
limits: map[FeeCurrency]float64{ | ||
cUSDToken: 0.5, | ||
}, | ||
defaultPoolExpected: false, | ||
expectedValue: 400, // blockGasLimit * 0.5 - subGasAmount | ||
}, | ||
{ | ||
name: "Non-empty mapping, unconfigured currency uses default limit", | ||
feeCurrency: &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.defaultLimit, | ||
c.limits, | ||
) | ||
|
||
pool := mgp.GetPool(c.feeCurrency) | ||
pool.SubGas(uint64(subGasAmount)) | ||
|
||
if c.defaultPoolExpected { | ||
result := mgp.GetPool(nil).Gas() | ||
if result != c.expectedValue { | ||
t.Error("Default pool expected", c.expectedValue, "got", result) | ||
} | ||
} else { | ||
result := mgp.GetPool(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,25 @@ | ||
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") | ||
) | ||
|
||
// default limits default fraction | ||
const DefaultFeeCurrencyDefault = 0.5 | ||
|
||
// default limits configuration | ||
var DefaultFeeCurrencyLimits = map[uint64]map[common.Address]float64{ | ||
params.CeloMainnetChainID: { | ||
cUSD_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.