Skip to content

Commit

Permalink
fix: adding last fixes.
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolay Nedkov <nikolai_nedkov@yahoo.com>
  • Loading branch information
Psykepro committed Mar 23, 2023
1 parent c56af06 commit 252ccca
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 56 deletions.
9 changes: 7 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,17 @@ func Test_Defaults(t *testing.T) {
path: "Pool.MaxTxDataBytesSize",
expectedValue: 30000,
},

{
path: "Pool.DefaultMinGasPriceAllowed",
expectedValue: uint64(1000000000),
},
{
path: "Pool.MinSuggestedGasPriceInterval",
path: "Pool.MinAllowedGasPriceInterval",
expectedValue: types.NewDuration(5 * time.Minute),
},
{
path: "Pool.PollMinSuggestedGasPriceInterval",
path: "Pool.PollMinAllowedGasPriceInterval",
expectedValue: types.NewDuration(15 * time.Second),
},
{
Expand Down
5 changes: 3 additions & 2 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ MaxConns = 200
FreeClaimGasLimit = 150000
MaxTxBytesSize=30132
MaxTxDataBytesSize=30000
MinSuggestedGasPriceInterval = "5m"
PollMinSuggestedGasPriceInterval = "15s"
DefaultMinGasPriceAllowed = 1000000000
MinAllowedGasPriceInterval = "5m"
PollMinAllowedGasPriceInterval = "15s"
[Pool.DB]
User = "pool_user"
Password = "pool_password"
Expand Down
5 changes: 3 additions & 2 deletions config/environments/local/local.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ MaxConns = 200
FreeClaimGasLimit = 1500000
MaxTxBytesSize=30132
MaxTxDataBytesSize=30000
MinSuggestedGasPriceInterval = "5m"
PollMinSuggestedGasPriceInterval = "15s"
DefaultMinGasPriceAllowed = 1000000000
MinAllowedGasPriceInterval = "5m"
PollMinAllowedGasPriceInterval = "15s"
[Pool.DB]
User = "pool_user"
Password = "pool_password"
Expand Down
5 changes: 3 additions & 2 deletions config/environments/public/public.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ MaxConns = 200
FreeClaimGasLimit = 1500000
MaxTxBytesSize=30132
MaxTxDataBytesSize=30000
MinSuggestedGasPriceInterval = "5m"
PollMinSuggestedGasPriceInterval = "15s"
DefaultMinGasPriceAllowed = 1000000000
MinAllowedGasPriceInterval = "5m"
PollMinAllowedGasPriceInterval = "15s"
[Pool.DB]
User = "pool_user"
Password = "pool_password"
Expand Down
11 changes: 7 additions & 4 deletions pool/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ type Config struct {
// DB is the database configuration
DB db.Config `mapstructure:"DB"`

// MinSuggestedGasPriceInterval is the interval to look back of the suggested min gas price for a tx
MinSuggestedGasPriceInterval types.Duration `mapstructure:"MinSuggestedGasPriceInterval"`
// DefaultMinGasPriceAllowed is the default min gas price to suggest
DefaultMinGasPriceAllowed uint64 `mapstructure:"DefaultMinGasPriceAllowed"`

// PollMinSuggestedGasPriceInterval is the interval to poll the suggested min gas price for a tx
PollMinSuggestedGasPriceInterval types.Duration `mapstructure:"PollMinSuggestedGasPriceInterval"`
// MinAllowedGasPriceInterval is the interval to look back of the suggested min gas price for a tx
MinAllowedGasPriceInterval types.Duration `mapstructure:"MinAllowedGasPriceInterval"`

// PollMinAllowedGasPriceInterval is the interval to poll the suggested min gas price for a tx
PollMinAllowedGasPriceInterval types.Duration `mapstructure:"PollMinAllowedGasPriceInterval"`
}
48 changes: 20 additions & 28 deletions pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
const (
// bridgeClaimMethodSignature for tracking bridgeClaimMethodSignature method
bridgeClaimMethodSignature = "0x2cffd02e"
retryInterval = 2 * time.Second
)

var (
Expand Down Expand Up @@ -57,28 +56,14 @@ func NewPool(cfg Config, s storage, st stateInterface, l2BridgeAddr common.Addre

// StartPollingMinSuggestedGasPrice starts polling the minimum suggested gas price
func (p *Pool) StartPollingMinSuggestedGasPrice(ctx context.Context) {
err := p.pollMinSuggestedGasPrice(ctx)
if err != nil && err != state.ErrNotFound {
log.Fatalf("Error polling min suggested gas price: %v", err)
}

// Retrying until we have a valid minSuggestedGasPrice
for err == state.ErrNotFound {
err = p.pollMinSuggestedGasPrice(ctx)
log.Infof("Retrying to poll min suggested gas price ...")
time.Sleep(retryInterval)
}

p.pollMinSuggestedGasPrice(ctx)
go func() {
for {
select {
case <-ctx.Done():
return
case <-time.After(p.cfg.PollMinSuggestedGasPriceInterval.Duration):
err = p.pollMinSuggestedGasPrice(ctx)
if err != nil {
log.Errorf("Error polling min suggested gas price: %v", err)
}
case <-time.After(p.cfg.PollMinAllowedGasPriceInterval.Duration):
p.pollMinSuggestedGasPrice(ctx)
}
}
}()
Expand Down Expand Up @@ -201,17 +186,20 @@ func (p *Pool) validateTx(ctx context.Context, tx types.Transaction) error {
if tx.Type() != types.LegacyTxType {
return ErrTxTypeNotSupported
}

// Reject transactions over defined size to prevent DOS attacks
if tx.Size() > p.cfg.MaxTxBytesSize {
return ErrOversizedData
}

// Reject transactions with a gas price lower than the minimum gas price
p.minSuggestedGasPriceMux.RLock()
gasPriceCmp := tx.GasPrice().Cmp(p.minSuggestedGasPrice)
p.minSuggestedGasPriceMux.RUnlock()
if gasPriceCmp == -1 {
return ErrGasPrice
}

// Transactions can't be negative. This may never happen using RLP decoded
// transactions but may occur if you create a transaction using the RPC.
if tx.Value().Sign() < 0 {
Expand Down Expand Up @@ -297,23 +285,27 @@ func (p *Pool) validateTx(ctx context.Context, tx types.Transaction) error {
return nil
}

func (p *Pool) pollMinSuggestedGasPrice(ctx context.Context) error {
fromTimestamp := time.Now().UTC().Add(-p.cfg.MinSuggestedGasPriceInterval.Duration)
func (p *Pool) pollMinSuggestedGasPrice(ctx context.Context) {
fromTimestamp := time.Now().UTC().Add(-p.cfg.MinAllowedGasPriceInterval.Duration)
gasPrice, err := p.storage.MinGasPriceSince(ctx, fromTimestamp)
if err == state.ErrNotFound {
log.Warnf("No suggested min gas price since: %v", fromTimestamp)
return err
} else if err != nil {
log.Errorf("Error getting min gas price since: %v", fromTimestamp)
return err
if err != nil {
p.minSuggestedGasPriceMux.Lock()
// Ensuring we always have suggested minimum gas price
if p.minSuggestedGasPrice == nil {
p.minSuggestedGasPrice = big.NewInt(0).SetUint64(p.cfg.DefaultMinGasPriceAllowed)
}
p.minSuggestedGasPriceMux.Unlock()
if err == state.ErrNotFound {
log.Warnf("No suggested min gas price since: %v", fromTimestamp)
} else {
log.Errorf("Error getting min gas price since: %v", fromTimestamp)
}
} else {
p.minSuggestedGasPriceMux.Lock()
p.minSuggestedGasPrice = big.NewInt(0).SetUint64(gasPrice)
p.minSuggestedGasPriceMux.Unlock()
log.Infof("Min suggested gas price updated to: %d", gasPrice)
}

return nil
}

// checkTxFieldCompatibilityWithExecutor checks the field sizes of the transaction to make sure
Expand Down
11 changes: 6 additions & 5 deletions pool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ var (
},
}
cfg = pool.Config{
FreeClaimGasLimit: 150000,
MaxTxBytesSize: 30132,
MaxTxDataBytesSize: 30000,
MinSuggestedGasPriceInterval: cfgTypes.NewDuration(5 * time.Minute),
PollMinSuggestedGasPriceInterval: cfgTypes.NewDuration(15 * time.Second),
FreeClaimGasLimit: 150000,
MaxTxBytesSize: 30132,
MaxTxDataBytesSize: 30000,
MinAllowedGasPriceInterval: cfgTypes.NewDuration(5 * time.Minute),
PollMinAllowedGasPriceInterval: cfgTypes.NewDuration(15 * time.Second),
DefaultMinGasPriceAllowed: 1000000000,
}
gasPrice = big.NewInt(1000000000)
gasLimit = uint64(21000)
Expand Down
14 changes: 7 additions & 7 deletions test/benchmarks/sequencer/common/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
)

const (
sleepDuration = 5 * time.Second
minSuggestedGasPriceIntervalMinutes = 5
pollMinSuggestedGasPriceIntervalSeconds = 15
sleepDuration = 5 * time.Second
minAllowedGasPriceIntervalMinutes = 5
pollMinAllowedGasPriceIntervalSeconds = 15
)

// Environment sets up the environment for the benchmark
Expand Down Expand Up @@ -53,10 +53,10 @@ func Environment(ctx context.Context, b *testing.B) (*operations.Manager, *ethcl
s, err := pgpoolstorage.NewPostgresPoolStorage(params.PoolDbConfig)
require.NoError(b, err)
config := pool.Config{
FreeClaimGasLimit: 1000000, //nolint:gomnd
DB: params.PoolDbConfig,
MinSuggestedGasPriceInterval: types.NewDuration(minSuggestedGasPriceIntervalMinutes * time.Minute),
PollMinSuggestedGasPriceInterval: types.NewDuration(pollMinSuggestedGasPriceIntervalSeconds * time.Second),
FreeClaimGasLimit: 1000000, //nolint:gomnd
DB: params.PoolDbConfig,
MinAllowedGasPriceInterval: types.NewDuration(minAllowedGasPriceIntervalMinutes * time.Minute),
PollMinAllowedGasPriceInterval: types.NewDuration(pollMinAllowedGasPriceIntervalSeconds * time.Second),
}

pl := pool.NewPool(config, s, st, common.Address{}, params.ChainID)
Expand Down
5 changes: 3 additions & 2 deletions test/config/debug.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ MaxConns = 10
FreeClaimGasLimit = 1500000
MaxTxBytesSize=30132
MaxTxDataBytesSize=30000
MinSuggestedGasPriceInterval = "5m"
PollMinSuggestedGasPriceInterval = "15s"
DefaultMinGasPriceAllowed = 1000000000
MinAllowedGasPriceInterval = "5m"
PollMinAllowedGasPriceInterval = "15s"
[Pool.DB]
User = "pool_user"
Password = "pool_password"
Expand Down
5 changes: 3 additions & 2 deletions test/config/test.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ MaxConns = 200
FreeClaimGasLimit = 1500000
MaxTxBytesSize=30132
MaxTxDataBytesSize=30000
MinSuggestedGasPriceInterval = "5m"
PollMinSuggestedGasPriceInterval = "15s"
DefaultMinGasPriceAllowed = 1000000000
MinAllowedGasPriceInterval = "5m"
PollMinAllowedGasPriceInterval = "15s"
[Pool.DB]
User = "pool_user"
Password = "pool_password"
Expand Down

0 comments on commit 252ccca

Please sign in to comment.