forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ut and adjust the code about kafka
- Loading branch information
Showing
4 changed files
with
238 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package gasprice | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
"testing" | ||
"time" | ||
|
||
"github.com/0xPolygonHermez/zkevm-node/config/types" | ||
"github.com/0xPolygonHermez/zkevm-node/log" | ||
) | ||
|
||
func init() { | ||
log.Init(log.Config{ | ||
Level: "debug", | ||
Outputs: []string{"stdout"}, | ||
}) | ||
} | ||
|
||
func TestUpdateGasPriceFixed(t *testing.T) { | ||
ctx := context.Background() | ||
var d time.Duration = 1000000000 | ||
|
||
cfg := Config{ | ||
Type: FixedType, | ||
DefaultGasPriceWei: 1000000000, | ||
UpdatePeriod: types.NewDuration(d), | ||
Factor: 0.5, | ||
KafkaURL: "127.0.0.1:9092", | ||
Topic: "middle_coinPrice_push", | ||
DefaultL2CoinPrice: 40, | ||
GasPriceUsdt: 0.001, | ||
} | ||
l1GasPrice := big.NewInt(10000000000) | ||
l2GasPrice := uint64(25000000000000) | ||
poolM := new(poolMock) | ||
ethM := new(ethermanMock) | ||
ethM.On("GetL1GasPrice", ctx).Return(l1GasPrice).Once() | ||
poolM.On("SetGasPrices", ctx, l2GasPrice, l1GasPrice.Uint64()).Return(nil).Once() | ||
f := newFixedGasPriceSuggester(ctx, cfg, poolM, ethM) | ||
|
||
ethM.On("GetL1GasPrice", ctx).Return(l1GasPrice, l1GasPrice).Once() | ||
poolM.On("SetGasPrices", ctx, l2GasPrice, l1GasPrice.Uint64()).Return(nil).Once() | ||
f.UpdateGasPriceAvg() | ||
} | ||
|
||
func TestUpdateGasPriceAvgCases(t *testing.T) { | ||
var d time.Duration = 1000000000 | ||
testcases := []struct { | ||
cfg Config | ||
l1GasPrice *big.Int | ||
l2GasPrice uint64 | ||
}{ | ||
{ | ||
cfg: Config{ | ||
Type: FixedType, | ||
DefaultGasPriceWei: 1000000000, | ||
UpdatePeriod: types.NewDuration(d), | ||
KafkaURL: "127.0.0.1:9092", | ||
Topic: "middle_coinPrice_push", | ||
DefaultL2CoinPrice: 40, | ||
GasPriceUsdt: 0.001, | ||
}, | ||
l1GasPrice: big.NewInt(10000000000), | ||
l2GasPrice: uint64(25000000000000), | ||
}, | ||
{ | ||
cfg: Config{ | ||
Type: FixedType, | ||
DefaultGasPriceWei: 1000000000, | ||
UpdatePeriod: types.NewDuration(d), | ||
KafkaURL: "127.0.0.1:9092", | ||
Topic: "middle_coinPrice_push", | ||
DefaultL2CoinPrice: 1e-19, | ||
GasPriceUsdt: 0.001, | ||
}, | ||
l1GasPrice: big.NewInt(10000000000), | ||
l2GasPrice: uint64(25000000000000), | ||
}, | ||
{ // the gas price small than the min gas price | ||
cfg: Config{ | ||
Type: FixedType, | ||
DefaultGasPriceWei: 26000000000000, | ||
UpdatePeriod: types.NewDuration(d), | ||
KafkaURL: "127.0.0.1:9092", | ||
Topic: "middle_coinPrice_push", | ||
DefaultL2CoinPrice: 40, | ||
GasPriceUsdt: 0.001, | ||
}, | ||
l1GasPrice: big.NewInt(10000000000), | ||
l2GasPrice: uint64(26000000000000), | ||
}, | ||
{ // the gas price bigger than the max gas price | ||
cfg: Config{ | ||
Type: FixedType, | ||
DefaultGasPriceWei: 1000000000000, | ||
MaxGasPriceWei: 23000000000000, | ||
UpdatePeriod: types.NewDuration(d), | ||
KafkaURL: "127.0.0.1:9092", | ||
Topic: "middle_coinPrice_push", | ||
DefaultL2CoinPrice: 40, | ||
GasPriceUsdt: 0.001, | ||
}, | ||
l1GasPrice: big.NewInt(10000000000), | ||
l2GasPrice: uint64(23000000000000), | ||
}, | ||
{ | ||
cfg: Config{ | ||
Type: FixedType, | ||
DefaultGasPriceWei: 1000000000, | ||
UpdatePeriod: types.NewDuration(d), | ||
KafkaURL: "127.0.0.1:9092", | ||
Topic: "middle_coinPrice_push", | ||
DefaultL2CoinPrice: 30, | ||
GasPriceUsdt: 0.001, | ||
}, | ||
l1GasPrice: big.NewInt(10000000000), | ||
l2GasPrice: uint64(33300000000000), | ||
}, | ||
{ | ||
cfg: Config{ | ||
Type: FixedType, | ||
DefaultGasPriceWei: 10, | ||
UpdatePeriod: types.NewDuration(d), | ||
KafkaURL: "127.0.0.1:9092", | ||
Topic: "middle_coinPrice_push", | ||
DefaultL2CoinPrice: 30, | ||
GasPriceUsdt: 1e-15, | ||
}, | ||
l1GasPrice: big.NewInt(10000000000), | ||
l2GasPrice: uint64(33), | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
ctx := context.Background() | ||
poolM := new(poolMock) | ||
ethM := new(ethermanMock) | ||
ethM.On("GetL1GasPrice", ctx).Return(tc.l1GasPrice).Twice() | ||
poolM.On("SetGasPrices", ctx, tc.l2GasPrice, tc.l1GasPrice.Uint64()).Return(nil).Twice() | ||
f := newFixedGasPriceSuggester(ctx, tc.cfg, poolM, ethM) | ||
f.UpdateGasPriceAvg() | ||
} | ||
} | ||
|
||
func TestLimitMasGasPriceFixed(t *testing.T) { | ||
ctx := context.Background() | ||
var d time.Duration = 1000000000 | ||
|
||
cfg := Config{ | ||
Type: FollowerType, | ||
DefaultGasPriceWei: 100000000, | ||
MaxGasPriceWei: 50000000, | ||
UpdatePeriod: types.NewDuration(d), | ||
Factor: 0.5, | ||
} | ||
l1GasPrice := big.NewInt(1000000000) | ||
poolM := new(poolMock) | ||
ethM := new(ethermanMock) | ||
ethM.On("GetL1GasPrice", ctx).Return(l1GasPrice) | ||
// Ensure SetGasPrices is called with the MaxGasPriceWei | ||
poolM.On("SetGasPrices", ctx, cfg.MaxGasPriceWei, l1GasPrice.Uint64()).Return(nil) | ||
f := newFollowerGasPriceSuggester(ctx, cfg, poolM, ethM) | ||
f.UpdateGasPriceAvg() | ||
} |
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,57 @@ | ||
package gasprice | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestCalculateRate(t *testing.T) { | ||
testcases := []struct { | ||
l2CoinId int | ||
msg string | ||
check func(rate float64, err error) | ||
}{ | ||
{ | ||
// error | ||
l2CoinId: okbcoinId, | ||
msg: fmt.Sprintf("{\"topic\":\"middle_coinPrice_push\"}"), | ||
check: func(rate float64, err error) { | ||
require.Error(t, err) | ||
}, | ||
}, | ||
{ | ||
// error | ||
l2CoinId: okbcoinId, | ||
msg: fmt.Sprintf("{\"topic\":\"middle_coinPrice_push\",\"source\":null,\"type\":null,\"data\":{\"priceList\":[{\"coinId\":%d,\"price\":0.02}],\"id\":\"98a797ce-f61b-4e90-87ac-445e77ad3599\"}}", okbcoinId+1), | ||
check: func(rate float64, err error) { | ||
require.Error(t, err) | ||
}, | ||
}, | ||
{ | ||
// correct | ||
l2CoinId: okbcoinId, | ||
msg: fmt.Sprintf("{\"topic\":\"middle_coinPrice_push\",\"source\":null,\"type\":null,\"data\":{\"priceList\":[{\"coinId\":%d,\"price\":0.02}, {\"coinId\":%d,\"price\":0.002}],\"id\":\"98a797ce-f61b-4e90-87ac-445e77ad3599\"}}", 1, okbcoinId), | ||
check: func(rate float64, err error) { | ||
require.Equal(t, rate, 0.002) | ||
require.NoError(t, err) | ||
}, | ||
}, | ||
{ | ||
// correct | ||
l2CoinId: okbcoinId, | ||
msg: fmt.Sprintf("{\"topic\":\"middle_coinPrice_push\",\"source\":null,\"type\":null,\"data\":{\"priceList\":[{\"coinId\":%d,\"price\":0.02}, {\"coinId\":%d,\"price\":10}],\"id\":\"98a797ce-f61b-4e90-87ac-445e77ad3599\"}}", 1, okbcoinId), | ||
check: func(rate float64, err error) { | ||
require.Equal(t, rate, float64(10)) | ||
require.NoError(t, err) | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
rp := newKafkaProcessor(Config{Topic: "middle_coinPrice_push", L2CoinId: tc.l2CoinId}, context.Background()) | ||
rt, err := rp.parseL2CoinPrice([]byte(tc.msg)) | ||
tc.check(rt, err) | ||
} | ||
} |