Skip to content

Commit

Permalink
FreeClaimGasLimit as a config param
Browse files Browse the repository at this point in the history
  • Loading branch information
ARR552 committed Jan 19, 2023
1 parent 30e5bb8 commit 9319003
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 82 deletions.
12 changes: 6 additions & 6 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ func start(cliCtx *cli.Context) error {
go runAggregator(ctx, c.Aggregator, etherman, ethTxManager, st)
case SEQUENCER:
log.Info("Running sequencer")
poolInstance := createPool(c.PoolDB, c.NetworkConfig.L2BridgeAddr, l2ChainID, st)
poolInstance := createPool(c.Pool, c.NetworkConfig.L2BridgeAddr, l2ChainID, st)
gpe := createGasPriceEstimator(c.GasPriceEstimator, st, poolInstance)
seq := createSequencer(*c, poolInstance, st, etherman, ethTxManager, gpe)
go seq.Start(ctx)
case RPC:
log.Info("Running JSON-RPC server")
poolInstance := createPool(c.PoolDB, c.NetworkConfig.L2BridgeAddr, l2ChainID, st)
poolInstance := createPool(c.Pool, c.NetworkConfig.L2BridgeAddr, l2ChainID, st)
gpe := createGasPriceEstimator(c.GasPriceEstimator, st, poolInstance)
apis := map[string]bool{}
for _, a := range cliCtx.StringSlice(config.FlagHTTPAPI) {
Expand Down Expand Up @@ -304,13 +304,13 @@ func newState(ctx context.Context, c *config.Config, l2ChainID uint64, sqlDB *pg
return st
}

func createPool(poolDBConfig db.Config, l2BridgeAddr common.Address, l2ChainID uint64, st *state.State) *pool.Pool {
runPoolMigrations(poolDBConfig)
poolStorage, err := pgpoolstorage.NewPostgresPoolStorage(poolDBConfig)
func createPool(cfgPool pool.Config, l2BridgeAddr common.Address, l2ChainID uint64, st *state.State) *pool.Pool {
runPoolMigrations(cfgPool.DB)
poolStorage, err := pgpoolstorage.NewPostgresPoolStorage(cfgPool.DB)
if err != nil {
log.Fatal(err)
}
poolInstance := pool.NewPool(poolStorage, st, l2BridgeAddr, l2ChainID)
poolInstance := pool.NewPool(cfgPool, poolStorage, st, l2BridgeAddr, l2ChainID)
return poolInstance
}

Expand Down
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/merkletree"
"github.com/0xPolygonHermez/zkevm-node/metrics"
"github.com/0xPolygonHermez/zkevm-node/pool"
"github.com/0xPolygonHermez/zkevm-node/pricegetter"
"github.com/0xPolygonHermez/zkevm-node/sequencer"
"github.com/0xPolygonHermez/zkevm-node/sequencer/broadcast"
Expand Down Expand Up @@ -49,6 +50,7 @@ type Config struct {
Log log.Config
Etherman etherman.Config
EthTxManager ethtxmanager.Config
Pool pool.Config
RPC jsonrpc.Config
Synchronizer synchronizer.Config
Sequencer sequencer.Config
Expand All @@ -60,7 +62,6 @@ type Config struct {
BroadcastServer broadcast.ServerConfig
MTClient merkletree.Config
StateDB db.Config
PoolDB db.Config
Metrics metrics.Config
}

Expand Down
18 changes: 10 additions & 8 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ Port = "5432"
EnableLog = false
MaxConns = 200
[PoolDB]
User = "pool_user"
Password = "pool_password"
Name = "pool_db"
Host = "localhost"
Port = "5432"
EnableLog = false
MaxConns = 200
[Pool]
FreeClaimGasLimit = 150000
[Pool.DB]
User = "pool_user"
Password = "pool_password"
Name = "pool_db"
Host = "localhost"
Port = "5432"
EnableLog = false
MaxConns = 200
[Etherman]
URL = "http://localhost:8545"
Expand Down
18 changes: 10 additions & 8 deletions config/environments/local/local.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ Port = "5432"
EnableLog = false
MaxConns = 200

[PoolDB]
User = "pool_user"
Password = "pool_password"
Name = "pool_db"
Host = "zkevm-pool-db"
Port = "5432"
EnableLog = false
MaxConns = 200
[Pool]
FreeClaimGasLimit = 150000
[Pool.DB]
User = "pool_user"
Password = "pool_password"
Name = "pool_db"
Host = "zkevm-pool-db"
Port = "5432"
EnableLog = false
MaxConns = 200

[Etherman]
URL = "http://your.L1node.url"
Expand Down
18 changes: 10 additions & 8 deletions config/environments/public/public.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ Port = "5432"
EnableLog = false
MaxConns = 200

[PoolDB]
User = "pool_user"
Password = "pool_password"
Name = "pool_db"
Host = "zkevm-pool-db"
Port = "5432"
EnableLog = false
MaxConns = 200
[Pool]
FreeClaimGasLimit = 150000
[Pool.DB]
User = "pool_user"
Password = "pool_password"
Name = "pool_db"
Host = "zkevm-pool-db"
Port = "5432"
EnableLog = false
MaxConns = 200

[Etherman]
URL = "http://your.L1node.url"
Expand Down
9 changes: 9 additions & 0 deletions pool/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package pool

import "github.com/0xPolygonHermez/zkevm-node/db"

type Config struct {
// FreeClaimGasLimit is the max gas allowed use to do a free claim
FreeClaimGasLimit uint64 `mapstructure:"FreeClaimGasLimit"`
DB db.Config `mapstructure:"DB"`
}
6 changes: 4 additions & 2 deletions pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ type Pool struct {
state stateInterface
l2BridgeAddr common.Address
chainID uint64
cfg Config
}

// NewPool creates and initializes an instance of Pool
func NewPool(s storage, st stateInterface, l2BridgeAddr common.Address, chainID uint64) *Pool {
func NewPool(cfg Config, s storage, st stateInterface, l2BridgeAddr common.Address, chainID uint64) *Pool {
return &Pool{
cfg: cfg,
storage: s,
state: st,
l2BridgeAddr: l2BridgeAddr,
Expand All @@ -72,7 +74,7 @@ func (p *Pool) AddTx(ctx context.Context, tx types.Transaction) error {
ReceivedAt: time.Now(),
}

poolTx.IsClaims = poolTx.IsClaimTx(p.l2BridgeAddr)
poolTx.IsClaims = poolTx.IsClaimTx(p.l2BridgeAddr, p.cfg.FreeClaimGasLimit)

return p.storage.AddTx(ctx, poolTx)
}
Expand Down
70 changes: 48 additions & 22 deletions pool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ func Test_AddTx(t *testing.T) {
}

const chainID = 2576980377
p := pool.NewPool(s, st, common.Address{}, chainID)
cfg := pool.Config {
FreeClaimGasLimit: 150000,
}
p := pool.NewPool(cfg, s, st, common.Address{}, chainID)

txRLPHash := "0xf86e8212658082520894fd8b27a263e19f0e9592180e61f0f8c9dfeb1ff6880de0b6b3a764000080850133333355a01eac4c2defc7ed767ae36bbd02613c581b8fb87d0e4f579c9ee3a7cfdb16faa7a043ce30f43d952b9d034cf8f04fecb631192a5dbc7ee2a47f1f49c0d022a8849d"
b, err := hex.DecodeHex(txRLPHash)
Expand Down Expand Up @@ -175,7 +178,10 @@ func Test_GetPendingTxs(t *testing.T) {
t.Error(err)
}

p := pool.NewPool(s, st, common.Address{}, chainID.Uint64())
cfg := pool.Config {
FreeClaimGasLimit: 150000,
}
p := pool.NewPool(cfg, s, st, common.Address{}, chainID.Uint64())

const txsCount = 10
const limit = 5
Expand Down Expand Up @@ -236,8 +242,10 @@ func Test_GetPendingTxsZeroPassed(t *testing.T) {
if err != nil {
t.Error(err)
}

p := pool.NewPool(s, st, common.Address{}, chainID.Uint64())
cfg := pool.Config {
FreeClaimGasLimit: 150000,
}
p := pool.NewPool(cfg, s, st, common.Address{}, chainID.Uint64())

const txsCount = 10
const limit = 0
Expand Down Expand Up @@ -298,8 +306,10 @@ func Test_GetTopPendingTxByProfitabilityAndZkCounters(t *testing.T) {
if err != nil {
t.Error(err)
}

p := pool.NewPool(s, st, common.Address{}, chainID.Uint64())
cfg := pool.Config {
FreeClaimGasLimit: 150000,
}
p := pool.NewPool(cfg, s, st, common.Address{}, chainID.Uint64())

const txsCount = 10

Expand Down Expand Up @@ -353,8 +363,10 @@ func Test_GetTopFailedTxsByProfitabilityAndZkCounters(t *testing.T) {
if err != nil {
t.Error(err)
}

p := pool.NewPool(s, st, common.Address{}, chainID.Uint64())
cfg := pool.Config {
FreeClaimGasLimit: 150000,
}
p := pool.NewPool(cfg, s, st, common.Address{}, chainID.Uint64())

const txsCount = 10

Expand Down Expand Up @@ -421,8 +433,10 @@ func Test_UpdateTxsStatus(t *testing.T) {
if err != nil {
t.Error(err)
}

p := pool.NewPool(s, st, common.Address{}, chainID.Uint64())
cfg := pool.Config {
FreeClaimGasLimit: 150000,
}
p := pool.NewPool(cfg, s, st, common.Address{}, chainID.Uint64())

privateKey, err := crypto.HexToECDSA(strings.TrimPrefix(senderPrivateKey, "0x"))
require.NoError(t, err)
Expand Down Expand Up @@ -492,8 +506,10 @@ func Test_UpdateTxStatus(t *testing.T) {
if err != nil {
t.Error(err)
}

p := pool.NewPool(s, st, common.Address{}, chainID.Uint64())
cfg := pool.Config {
FreeClaimGasLimit: 150000,
}
p := pool.NewPool(cfg, s, st, common.Address{}, chainID.Uint64())

privateKey, err := crypto.HexToECDSA(strings.TrimPrefix(senderPrivateKey, "0x"))
require.NoError(t, err)
Expand Down Expand Up @@ -535,8 +551,10 @@ func Test_SetAndGetGasPrice(t *testing.T) {
if err != nil {
t.Error(err)
}

p := pool.NewPool(s, nil, common.Address{}, chainID.Uint64())
cfg := pool.Config {
FreeClaimGasLimit: 150000,
}
p := pool.NewPool(cfg, s, nil, common.Address{}, chainID.Uint64())

nBig, err := rand.Int(rand.Reader, big.NewInt(0).SetUint64(math.MaxUint64))
if err != nil {
Expand Down Expand Up @@ -586,8 +604,10 @@ func TestMarkReorgedTxsAsPending(t *testing.T) {
if err != nil {
t.Error(err)
}

p := pool.NewPool(s, st, common.Address{}, chainID.Uint64())
cfg := pool.Config {
FreeClaimGasLimit: 150000,
}
p := pool.NewPool(cfg, s, st, common.Address{}, chainID.Uint64())

privateKey, err := crypto.HexToECDSA(strings.TrimPrefix(senderPrivateKey, "0x"))
require.NoError(t, err)
Expand Down Expand Up @@ -650,8 +670,10 @@ func TestGetPendingTxSince(t *testing.T) {
if err != nil {
t.Error(err)
}

p := pool.NewPool(s, st, common.Address{}, chainID.Uint64())
cfg := pool.Config {
FreeClaimGasLimit: 150000,
}
p := pool.NewPool(cfg, s, st, common.Address{}, chainID.Uint64())

const txsCount = 10

Expand Down Expand Up @@ -753,8 +775,10 @@ func Test_DeleteTxsByHashes(t *testing.T) {
if err != nil {
t.Error(err)
}

p := pool.NewPool(s, st, common.Address{}, chainID.Uint64())
cfg := pool.Config {
FreeClaimGasLimit: 150000,
}
p := pool.NewPool(cfg, s, st, common.Address{}, chainID.Uint64())

privateKey, err := crypto.HexToECDSA(strings.TrimPrefix(senderPrivateKey, "0x"))
require.NoError(t, err)
Expand Down Expand Up @@ -907,11 +931,13 @@ func Test_TryAddIncompatibleTxs(t *testing.T) {
expectedError: fmt.Errorf("chain id higher than allowed, max allowed is %v", uint64(math.MaxUint64)),
},
}

cfg := pool.Config {
FreeClaimGasLimit: 150000,
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
incompatibleTx := testCase.createIncompatibleTx()
p := pool.NewPool(s, st, common.Address{}, incompatibleTx.ChainId().Uint64())
p := pool.NewPool(cfg, s, st, common.Address{}, incompatibleTx.ChainId().Uint64())
err = p.AddTx(ctx, incompatibleTx)
assert.Equal(t, testCase.expectedError, err)
})
Expand Down
5 changes: 1 addition & 4 deletions pool/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ const (
TxStatusSelected TxStatus = "selected"
// TxStatusFailed represents a tx that has been failed after processing, but can be processed in the future
TxStatusFailed TxStatus = "failed"

// freeClaimGasLimit is the max gas allowed use to do a free claim
freeClaimGasLimit uint64 = 150000
)

// TxStatus represents the state of a tx
Expand Down Expand Up @@ -77,7 +74,7 @@ func (zkc *ZkCounters) SumUpZkCounters(txZkCounters ZkCounters) {
}

// IsClaimTx checks, if tx is a claim tx
func (tx *Transaction) IsClaimTx(l2BridgeAddr common.Address) bool {
func (tx *Transaction) IsClaimTx(l2BridgeAddr common.Address, freeClaimGasLimit uint64) bool {
if tx.To() == nil {
return false
}
Expand Down
4 changes: 2 additions & 2 deletions pool/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ func Test_IsClaimTx(t *testing.T) {
expectedResult: true,
},
}

const freeClaimGasLimit uint64 = 150000
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
result := testCase.Tx.IsClaimTx(l2BridgeAddr)
result := testCase.Tx.IsClaimTx(l2BridgeAddr, freeClaimGasLimit)
if result != testCase.expectedResult {
t.Errorf("Invalid result, expected: %v, found: %v", testCase.expectedResult, result)
}
Expand Down
12 changes: 8 additions & 4 deletions sequencer/pendingtxsqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ func TestQueue_AddAndPopTx(t *testing.T) {
if err != nil {
panic(err)
}

p := pool.NewPool(s, st, common.Address{}, chainID.Uint64())
cfgPool := pool.Config {
FreeClaimGasLimit: 150000,
}
p := pool.NewPool(cfgPool, s, st, common.Address{}, chainID.Uint64())

const txsCount = 10

Expand Down Expand Up @@ -156,8 +158,10 @@ func TestQueue_AddOneTx(t *testing.T) {
if err != nil {
panic(err)
}

p := pool.NewPool(s, st, common.Address{}, chainID.Uint64())
cfgPool := pool.Config {
FreeClaimGasLimit: 150000,
}
p := pool.NewPool(cfgPool, s, st, common.Address{}, chainID.Uint64())

const txsCount = 1

Expand Down
5 changes: 4 additions & 1 deletion test/benchmarks/sequencer/pool_processing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ func setup(ctx context.Context, b *testing.B) (*operations.Manager, *ethclient.C
st := opsman.State()
s, err := pgpoolstorage.NewPostgresPoolStorage(poolDbConfig)
require.NoError(b, err)
pl := pool.NewPool(s, st, common.Address{}, chainID)
cfg := pool.Config {
FreeClaimGasLimit: 150000,
}
pl := pool.NewPool(cfg, s, st, common.Address{}, chainID)

// Print Info before send
senderBalance, err := client.BalanceAt(ctx, auth.From, nil)
Expand Down
Loading

0 comments on commit 9319003

Please sign in to comment.