-
Notifications
You must be signed in to change notification settings - Fork 214
Implemented the limit per fee currency #2203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
729c5e4
Added MultiGasPool
bebf3ae
Added default configuration
1563a78
Added new flags
45a4394
Implemented the limit per fee currency
d9b093f
Fixed tests
922027c
Fixed PR comments
e219e33
Changed fraction for cREAL
3812345
Fixed a typo
e85b75e
Use gas used to subtract from the gas pool
5bae68a
A nicer way to get gasUsed
6434d6d
Changed the default percentage
347d8bc
Made flag description more specific
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,58 @@ | ||
package core | ||
|
||
import ( | ||
"github.com/celo-org/celo-blockchain/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 any unconfigured currencies and CELO | ||
func NewMultiGasPool( | ||
block_gas_limit 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(block_gas_limit) * fraction), | ||
) | ||
} | ||
|
||
// A special case for CELO which doesn't have a limit | ||
celoPool := new(GasPool).AddGas(block_gas_limit) | ||
|
||
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] | ||
} |
This file contains hidden or 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,139 @@ | ||
package core | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/celo-org/celo-blockchain/common" | ||
) | ||
|
||
func TestMultiCurrencyGasPool(t *testing.T) { | ||
block_gas_limit := uint64(1_000) | ||
sub_gas_amount := 100 | ||
|
||
cusd_token := common.HexToAddress("0x765DE816845861e75A25fCA122bb6898B8B1282a") | ||
ceur_token := common.HexToAddress("0xD8763CBa276a3738E6DE85b4b3bF5FDed6D6cA73") | ||
|
||
testCases := []struct { | ||
name string | ||
whitelist []common.Address | ||
feeCurrency *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, // block_gas_limit - sub_gas_amount | ||
}, | ||
{ | ||
name: "Non-empty whitelist, non-empty mapping, CELO uses default pool", | ||
feeCurrency: nil, | ||
whitelist: []FeeCurrency{ | ||
cusd_token, | ||
}, | ||
defaultLimit: 0.9, | ||
limits: map[FeeCurrency]float64{ | ||
cusd_token: 0.5, | ||
}, | ||
defaultPoolExpected: true, | ||
expectedValue: 900, // block_gas_limit - sub_gas_amount | ||
}, | ||
{ | ||
name: "Empty whitelist, empty mapping, non-whitelisted currency fallbacks to the default pool", | ||
feeCurrency: &cusd_token, | ||
whitelist: []FeeCurrency{}, | ||
defaultLimit: 0.9, | ||
limits: map[FeeCurrency]float64{}, | ||
defaultPoolExpected: true, | ||
expectedValue: 900, // block_gas_limit - sub_gas_amount | ||
}, | ||
{ | ||
name: "Non-empty whitelist, non-empty mapping, non-whitelisted currency uses default pool", | ||
feeCurrency: &ceur_token, | ||
whitelist: []FeeCurrency{ | ||
cusd_token, | ||
}, | ||
defaultLimit: 0.9, | ||
limits: map[FeeCurrency]float64{ | ||
cusd_token: 0.5, | ||
}, | ||
defaultPoolExpected: true, | ||
expectedValue: 900, // block_gas_limit - sub_gas_amount | ||
}, | ||
{ | ||
name: "Non-empty whitelist, empty mapping, whitelisted currency uses default limit", | ||
feeCurrency: &cusd_token, | ||
whitelist: []FeeCurrency{ | ||
cusd_token, | ||
}, | ||
defaultLimit: 0.9, | ||
limits: map[FeeCurrency]float64{}, | ||
defaultPoolExpected: false, | ||
expectedValue: 800, // block_gas_limit * defaultLimit - sub_gas_amount | ||
}, | ||
{ | ||
name: "Non-empty whitelist, non-empty mapping, configured whitelisted currency uses configured limits", | ||
feeCurrency: &cusd_token, | ||
whitelist: []FeeCurrency{ | ||
cusd_token, | ||
}, | ||
defaultLimit: 0.9, | ||
limits: map[FeeCurrency]float64{ | ||
cusd_token: 0.5, | ||
}, | ||
defaultPoolExpected: false, | ||
expectedValue: 400, // block_gas_limit * 0.5 - sub_gas_amount | ||
}, | ||
{ | ||
name: "Non-empty whitelist, non-empty mapping, unconfigured whitelisted currency uses default limit", | ||
feeCurrency: &ceur_token, | ||
whitelist: []FeeCurrency{ | ||
cusd_token, | ||
ceur_token, | ||
}, | ||
defaultLimit: 0.9, | ||
limits: map[FeeCurrency]float64{ | ||
cusd_token: 0.5, | ||
}, | ||
defaultPoolExpected: false, | ||
expectedValue: 800, // block_gas_limit * 0.5 - sub_gas_amount | ||
}, | ||
} | ||
|
||
for _, c := range testCases { | ||
t.Run(c.name, func(t *testing.T) { | ||
mgp := NewMultiGasPool( | ||
block_gas_limit, | ||
c.whitelist, | ||
c.defaultLimit, | ||
c.limits, | ||
) | ||
|
||
pool := mgp.PoolFor(c.feeCurrency) | ||
pool.SubGas(uint64(sub_gas_amount)) | ||
|
||
if c.defaultPoolExpected { | ||
result := mgp.defaultPool.Gas() | ||
if result != c.expectedValue { | ||
t.Error("Default pool expected", c.expectedValue, "got", result) | ||
} | ||
} else { | ||
pool := mgp.pools[*c.feeCurrency] | ||
result := pool.Gas() | ||
|
||
if result != c.expectedValue { | ||
t.Error( | ||
"Expected pool", c.feeCurrency, "value", c.expectedValue, | ||
"got", result, | ||
) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.