Skip to content
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

free gas of fork5 #19

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/0xPolygonHermez/zkevm-node/state/runtime/executor"
"github.com/0xPolygonHermez/zkevm-node/synchronizer"
"github.com/ethereum/go-ethereum/common"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -165,7 +166,7 @@ func start(cliCtx *cli.Context) error {
log.Fatal(err)
}
if poolInstance == nil {
poolInstance = createPool(c.Pool, l2ChainID, st, eventLog)
poolInstance = createPool(c.Pool, c.NetworkConfig.L2BridgeAddr, l2ChainID, st, eventLog)
}
seq := createSequencer(*c, poolInstance, ethTxManagerStorage, st, eventLog)
go seq.Start(cliCtx.Context)
Expand All @@ -177,7 +178,7 @@ func start(cliCtx *cli.Context) error {
log.Fatal(err)
}
if poolInstance == nil {
poolInstance = createPool(c.Pool, l2ChainID, st, eventLog)
poolInstance = createPool(c.Pool, c.NetworkConfig.L2BridgeAddr, l2ChainID, st, eventLog)
}
seqSender := createSequenceSender(*c, poolInstance, ethTxManagerStorage, st, eventLog)
go seqSender.Start(cliCtx.Context)
Expand All @@ -189,7 +190,7 @@ func start(cliCtx *cli.Context) error {
log.Fatal(err)
}
if poolInstance == nil {
poolInstance = createPool(c.Pool, l2ChainID, st, eventLog)
poolInstance = createPool(c.Pool, c.NetworkConfig.L2BridgeAddr, l2ChainID, st, eventLog)
}
if c.RPC.EnableL2SuggestedGasPricePolling {
// Needed for rejecting transactions with too low gas price
Expand All @@ -208,7 +209,7 @@ func start(cliCtx *cli.Context) error {
log.Fatal(err)
}
if poolInstance == nil {
poolInstance = createPool(c.Pool, l2ChainID, st, eventLog)
poolInstance = createPool(c.Pool, c.NetworkConfig.L2BridgeAddr, l2ChainID, st, eventLog)
}
go runSynchronizer(*c, etherman, etm, st, poolInstance, eventLog)
case ETHTXMANAGER:
Expand All @@ -228,7 +229,7 @@ func start(cliCtx *cli.Context) error {
log.Fatal(err)
}
if poolInstance == nil {
poolInstance = createPool(c.Pool, l2ChainID, st, eventLog)
poolInstance = createPool(c.Pool, c.NetworkConfig.L2BridgeAddr, l2ChainID, st, eventLog)
}
go runL2GasPriceSuggester(c.L2GasPriceSuggester, st, poolInstance, etherman)
}
Expand Down Expand Up @@ -474,13 +475,13 @@ func newState(ctx context.Context, c *config.Config, l2ChainID uint64, forkIDInt
return st
}

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

Expand Down
2 changes: 2 additions & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ EnableLog = false
MaxConns = 200

[Pool]
FreeClaimGasLimit = 150000
IntervalToRefreshBlockedAddresses = "5m"
IntervalToRefreshGasPrices = "5s"
MaxTxBytesSize=100132
MaxTxDataBytesSize=100000
DefaultMinGasPriceAllowed = 1000000000
MinAllowedGasPriceInterval = "5m"
PollMinAllowedGasPriceInterval = "15s"
FreeGasAddress = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
AccountQueue = 64
GlobalQueue = 1024
[Pool.DB]
Expand Down
2 changes: 2 additions & 0 deletions config/environments/local/local.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ EnableLog = false
MaxConns = 200

[Pool]
FreeClaimGasLimit = 1500000
IntervalToRefreshBlockedAddresses = "5m"
IntervalToRefreshGasPrices = "5s"
MaxTxBytesSize=100132
MaxTxDataBytesSize=100000
DefaultMinGasPriceAllowed = 1000000000
MinAllowedGasPriceInterval = "5m"
PollMinAllowedGasPriceInterval = "15s"
FreeGasAddress = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
[Pool.DB]
User = "pool_user"
Password = "pool_password"
Expand Down
2 changes: 2 additions & 0 deletions config/environments/mainnet/node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ EnableLog = false
MaxConns = 200

[Pool]
FreeClaimGasLimit = 1500000
MaxTxBytesSize=100132
MaxTxDataBytesSize=100000
DefaultMinGasPriceAllowed = 1000000000
MinAllowedGasPriceInterval = "5m"
PollMinAllowedGasPriceInterval = "15s"
FreeGasAddress = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
[Pool.DB]
User = "pool_user"
Password = "pool_password"
Expand Down
2 changes: 2 additions & 0 deletions config/environments/testnet/node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ EnableLog = false
MaxConns = 200

[Pool]
FreeClaimGasLimit = 1500000
IntervalToRefreshBlockedAddresses = "5m"
IntervalToRefreshGasPrices = "5s"
MaxTxBytesSize=100132
MaxTxDataBytesSize=100000
DefaultMinGasPriceAllowed = 1000000000
MinAllowedGasPriceInterval = "5m"
PollMinAllowedGasPriceInterval = "15s"
FreeGasAddress = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
[Pool.DB]
User = "pool_user"
Password = "pool_password"
Expand Down
5 changes: 5 additions & 0 deletions pool/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (

// Config is the pool configuration
type Config struct {
// FreeClaimGasLimit is the max gas allowed use to do a free claim
FreeClaimGasLimit uint64 `mapstructure:"FreeClaimGasLimit"`

// IntervalToRefreshBlockedAddresses is the time it takes to sync the
// blocked address list from db to memory
IntervalToRefreshBlockedAddresses types.Duration `mapstructure:"IntervalToRefreshBlockedAddresses"`
Expand Down Expand Up @@ -37,4 +40,6 @@ type Config struct {

// GlobalQueue represents the maximum number of non-executable transaction slots for all accounts
GlobalQueue uint64 `mapstructure:"GlobalQueue"`

FreeGasAddress string `mapstructure:"FreeGasAddress"`
}
30 changes: 22 additions & 8 deletions pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import (
"github.com/ethereum/go-ethereum/core/types"
)

const (
// BridgeClaimMethodSignature for tracking BridgeClaimMethodSignature method
BridgeClaimMethodSignature = "0x2cffd02e"
)

var (
// ErrNotFound indicates an object has not been found for the search criteria used
ErrNotFound = errors.New("object not found")
Expand All @@ -29,6 +34,9 @@ var (
// ErrReplaceUnderpriced is returned if a transaction is attempted to be replaced
// with a different one without the required price bump.
ErrReplaceUnderpriced = errors.New("replacement transaction underpriced")

// FreeClaimAddress is the default free gas address
FreeClaimAddress = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
)

// Pool is an implementation of the Pool interface
Expand All @@ -45,6 +53,7 @@ type Pool struct {
startTimestamp time.Time
gasPrices GasPrices
gasPricesMux *sync.RWMutex
l2BridgeAddr common.Address
}

type preExecutionResponse struct {
Expand All @@ -63,7 +72,7 @@ type GasPrices struct {
}

// NewPool creates and initializes an instance of Pool
func NewPool(cfg Config, s storage, st stateInterface, chainID uint64, eventLog *event.EventLog) *Pool {
func NewPool(cfg Config, s storage, st stateInterface, l2BridgeAddr common.Address, chainID uint64, eventLog *event.EventLog) *Pool {
startTimestamp := time.Now()
p := &Pool{
cfg: cfg,
Expand All @@ -76,8 +85,11 @@ func NewPool(cfg Config, s storage, st stateInterface, chainID uint64, eventLog
eventLog: eventLog,
gasPrices: GasPrices{0, 0},
gasPricesMux: new(sync.RWMutex),
l2BridgeAddr: l2BridgeAddr,
}

FreeClaimAddress = cfg.FreeGasAddress

p.refreshBlockedAddresses()
go func(cfg *Config, p *Pool) {
for {
Expand Down Expand Up @@ -157,7 +169,7 @@ func (p *Pool) StartPollingMinSuggestedGasPrice(ctx context.Context) {

// AddTx adds a transaction to the pool with the pending state
func (p *Pool) AddTx(ctx context.Context, tx types.Transaction, ip string) error {
poolTx := NewTransaction(tx, ip, false)
poolTx := NewTransaction(tx, ip, false, p)
if err := p.validateTx(ctx, *poolTx); err != nil {
return err
}
Expand Down Expand Up @@ -213,7 +225,7 @@ func (p *Pool) StoreTx(ctx context.Context, tx types.Transaction, ip string, isW
}
}

poolTx := NewTransaction(tx, ip, isWIP)
poolTx := NewTransaction(tx, ip, isWIP, p)
poolTx.ZKCounters = preExecutionResponse.usedZkCounters

return p.storage.AddTx(ctx, *poolTx)
Expand Down Expand Up @@ -387,11 +399,13 @@ func (p *Pool) validateTx(ctx context.Context, poolTx Transaction) error {
}

// Reject transactions with a gas price lower than the minimum gas price
p.minSuggestedGasPriceMux.RLock()
gasPriceCmp := poolTx.GasPrice().Cmp(p.minSuggestedGasPrice)
p.minSuggestedGasPriceMux.RUnlock()
if gasPriceCmp == -1 {
return ErrGasPrice
if from != common.HexToAddress(FreeClaimAddress) || !poolTx.IsClaims {
p.minSuggestedGasPriceMux.RLock()
gasPriceCmp := poolTx.GasPrice().Cmp(p.minSuggestedGasPrice)
p.minSuggestedGasPriceMux.RUnlock()
if gasPriceCmp == -1 {
return ErrGasPrice
}
}

// Transactor should have enough funds to cover the costs
Expand Down
8 changes: 4 additions & 4 deletions pool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func Test_AddTx_OversizedData(t *testing.T) {
require.NoError(t, err)

const chainID = 2576980377
p := pool.NewPool(cfg, s, st, chainID, eventLog)
p := pool.NewPool(cfg, s, st, common.HexToAddress("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"), chainID, eventLog)

b := make([]byte, cfg.MaxTxBytesSize+1)
to := common.HexToAddress(operations.DefaultSequencerAddress)
Expand Down Expand Up @@ -649,7 +649,7 @@ func Test_SetAndGetGasPrice(t *testing.T) {
require.NoError(t, err)
eventLog := event.NewEventLog(event.Config{}, eventStorage)

p := pool.NewPool(cfg, s, nil, chainID.Uint64(), eventLog)
p := pool.NewPool(cfg, s, nil, common.HexToAddress("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"), chainID.Uint64(), eventLog)

nBig, err := rand.Int(rand.Reader, big.NewInt(0).SetUint64(math.MaxUint64))
require.NoError(t, err)
Expand All @@ -674,7 +674,7 @@ func TestDeleteGasPricesHistoryOlderThan(t *testing.T) {
require.NoError(t, err)
eventLog := event.NewEventLog(event.Config{}, eventStorage)

p := pool.NewPool(cfg, s, nil, chainID.Uint64(), eventLog)
p := pool.NewPool(cfg, s, nil, common.HexToAddress("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"), chainID.Uint64(), eventLog)

ctx := context.Background()

Expand Down Expand Up @@ -1788,7 +1788,7 @@ func Test_AddTx_NonceTooHigh(t *testing.T) {
}

func setupPool(t *testing.T, cfg pool.Config, s *pgpoolstorage.PostgresPoolStorage, st *state.State, chainID uint64, ctx context.Context, eventLog *event.EventLog) *pool.Pool {
p := pool.NewPool(cfg, s, st, chainID, eventLog)
p := pool.NewPool(cfg, s, st, common.HexToAddress("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"), chainID, eventLog)

err := p.SetGasPrices(ctx, gasPrice.Uint64(), l1GasPrice.Uint64())
require.NoError(t, err)
Expand Down
24 changes: 23 additions & 1 deletion pool/transaction.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pool

import (
"strings"
"time"

"github.com/0xPolygonHermez/zkevm-node/state"
Expand Down Expand Up @@ -45,10 +46,11 @@ type Transaction struct {
IsWIP bool
IP string
FailedReason *string
IsClaims bool
}

// NewTransaction creates a new transaction
func NewTransaction(tx types.Transaction, ip string, isWIP bool) *Transaction {
func NewTransaction(tx types.Transaction, ip string, isWIP bool, p *Pool) *Transaction {
poolTx := Transaction{
Transaction: tx,
Status: TxStatusPending,
Expand All @@ -57,5 +59,25 @@ func NewTransaction(tx types.Transaction, ip string, isWIP bool) *Transaction {
IP: ip,
}

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

return &poolTx
}

// IsClaimTx checks, if tx is a claim tx
func (tx *Transaction) IsClaimTx(l2BridgeAddr common.Address, freeClaimGasLimit uint64) bool {
if tx.To() == nil {
return false
}

txGas := tx.Gas()
if txGas > freeClaimGasLimit {
return false
}

if *tx.To() == l2BridgeAddr &&
strings.HasPrefix("0x"+common.Bytes2Hex(tx.Data()), BridgeClaimMethodSignature) {
return true
}
return false
}
2 changes: 1 addition & 1 deletion sequencer/dbmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (d *dbManager) loadFromPool() {
}

func (d *dbManager) addTxToWorker(tx pool.Transaction) error {
txTracker, err := d.worker.NewTxTracker(tx.Transaction, tx.ZKCounters, tx.IP)
txTracker, err := d.worker.NewTxTracker(tx, tx.ZKCounters, tx.IP)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion sequencer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type workerInterface interface {
AddPendingTxToStore(txHash common.Hash, addr common.Address)
DeletePendingTxToStore(txHash common.Hash, addr common.Address)
HandleL2Reorg(txHashes []common.Hash)
NewTxTracker(tx types.Transaction, counters state.ZKCounters, ip string) (*TxTracker, error)
NewTxTracker(tx pool.Transaction, counters state.ZKCounters, ip string) (*TxTracker, error)
AddForcedTx(txHash common.Hash, addr common.Address)
DeleteForcedTx(txHash common.Hash, addr common.Address)
}
Expand Down
9 changes: 5 additions & 4 deletions sequencer/mock_worker.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading