Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolay Nedkov <[email protected]>
  • Loading branch information
Psykepro committed Mar 21, 2023
1 parent 9026538 commit aa369f5
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 22 deletions.
6 changes: 5 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func Test_Defaults(t *testing.T) {
},
{
path: "Sequencer.MaxBatchBytesSize",
expectedValue: uint64(150000),
expectedValue: uint64(112640),
},
{
path: "Sequencer.BlocksAmountForTxsToBeDeleted",
Expand Down Expand Up @@ -245,6 +245,10 @@ func Test_Defaults(t *testing.T) {
path: "Pool.FreeClaimGasLimit",
expectedValue: uint64(150000),
},
{
path: "Pool.MaxTxBytesSize",
expectedValue: uint64(102400),
},
{
path: "Pool.DB.User",
expectedValue: "pool_user",
Expand Down
3 changes: 2 additions & 1 deletion config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ MaxConns = 200
[Pool]
FreeClaimGasLimit = 150000
MaxTxBytesSize=102400
[Pool.DB]
User = "pool_user"
Password = "pool_password"
Expand Down Expand Up @@ -69,7 +70,7 @@ LastBatchVirtualizationTimeMaxWaitPeriod = "5s"
BlocksAmountForTxsToBeDeleted = 100
FrequencyToCheckTxsForDelete = "12h"
MaxTxsPerBatch = 150
MaxBatchBytesSize = 150000
MaxBatchBytesSize = 112640
MaxCumulativeGasUsed = 30000000
MaxKeccakHashes = 468
MaxPoseidonHashes = 279620
Expand Down
3 changes: 2 additions & 1 deletion config/environments/local/local.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ MaxConns = 200

[Pool]
FreeClaimGasLimit = 1500000
MaxTxBytesSize=102400
[Pool.DB]
User = "pool_user"
Password = "pool_password"
Expand Down Expand Up @@ -60,7 +61,7 @@ LastBatchVirtualizationTimeMaxWaitPeriod = "5s"
BlocksAmountForTxsToBeDeleted = 100
FrequencyToCheckTxsForDelete = "12h"
MaxTxsPerBatch = 150
MaxBatchBytesSize = 150000
MaxBatchBytesSize = 112640
MaxCumulativeGasUsed = 30000000
MaxKeccakHashes = 468
MaxPoseidonHashes = 279620
Expand Down
1 change: 1 addition & 0 deletions config/environments/public/public.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ MaxConns = 200

[Pool]
FreeClaimGasLimit = 1500000
MaxTxBytesSize=102400
[Pool.DB]
User = "pool_user"
Password = "pool_password"
Expand Down
1 change: 1 addition & 0 deletions pool/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ 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"`
MaxTxBytesSize uint64 `mapstructure:"MaxTxBytesSize"`
DB db.Config `mapstructure:"DB"`
}
14 changes: 1 addition & 13 deletions pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,6 @@ import (
)

const (
// txSlotSize is used to calculate how many data slots a single transaction
// takes up based on its size. The slots are used as DoS protection, ensuring
// that validating a new transaction remains a constant operation (in reality
// O(maxslots), where max slots are 4 currently).
txSlotSize = 32 * 1024

// txMaxSize is the maximum size a single transaction can have. This field has
// non-trivial consequences: larger transactions are significantly harder and
// more expensive to propagate; larger transactions also take more resources
// to validate whether they fit into the pool or not.
txMaxSize = 4 * txSlotSize // 128KB

// bridgeClaimMethodSignature for tracking bridgeClaimMethodSignature method
bridgeClaimMethodSignature = "0x2cffd02e"
)
Expand Down Expand Up @@ -180,7 +168,7 @@ func (p *Pool) validateTx(ctx context.Context, tx types.Transaction) error {
return ErrTxTypeNotSupported
}
// Reject transactions over defined size to prevent DOS attacks
if uint64(tx.Size()) > txMaxSize {
if uint64(tx.Size()) > p.cfg.MaxTxBytesSize {
return ErrOversizedData
}
// Transactions can't be negative. This may never happen using RLP decoded
Expand Down
2 changes: 1 addition & 1 deletion sequencer/closingsignalsmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func setupTest(t *testing.T) {

batchConstraints := batchConstraints{
MaxTxsPerBatch: 150,
MaxBatchBytesSize: 150000,
MaxBatchBytesSize: 112640,
MaxCumulativeGasUsed: 30000000,
MaxKeccakHashes: 468,
MaxPoseidonHashes: 279620,
Expand Down
2 changes: 1 addition & 1 deletion sequencer/dbmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func setupDBManager() {

batchConstraints := batchConstraints{
MaxTxsPerBatch: 150,
MaxBatchBytesSize: 150000,
MaxBatchBytesSize: 112640,
MaxCumulativeGasUsed: 30000000,
MaxKeccakHashes: 468,
MaxPoseidonHashes: 279620,
Expand Down
2 changes: 1 addition & 1 deletion sequencer/finalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var (
dbTxMock = new(DbTxMock)
bc = batchConstraints{
MaxTxsPerBatch: 150,
MaxBatchBytesSize: 150000,
MaxBatchBytesSize: 112640,
MaxCumulativeGasUsed: 30000000,
MaxKeccakHashes: 468,
MaxPoseidonHashes: 279620,
Expand Down
86 changes: 86 additions & 0 deletions sequencer/sequencesender_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package sequencer

import (
"bytes"
"context"
"log"
"math/big"
"os"
"testing"
"time"

ethermanTypes "github.com/0xPolygonHermez/zkevm-node/etherman/types"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/0xPolygonHermez/zkevm-node/test/operations"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/require"
)

var (
s *Sequencer
)

func captureLogOutput(f func()) string {
var buf bytes.Buffer
log.SetOutput(&buf)
f()
log.SetOutput(os.Stderr)
return buf.String()
}

// Test_getSequencesToSend_OversizedDataError tests the error when the data is oversized
func Test_getSequencesToSend_OversizedDataError(t *testing.T) {
stateMock := NewStateMock(t)
ethermanMock := NewEthermanMock(t)
s = &Sequencer{
state: stateMock,
etherman: ethermanMock,
}
ctx := context.Background()
toAddress := common.HexToAddress(operations.DefaultSequencerAddress)
// data is 250 kilobytes
data := make([]byte, txMaxSize+1)
tx := types.NewTransaction(1, toAddress, big.NewInt(1), 21000, big.NewInt(30), data)
batchL2Data, err := state.EncodeTransaction(*tx)
batch := &state.Batch{
BatchNumber: 1,
BatchL2Data: batchL2Data,
Timestamp: time.Now(),
Transactions: []types.Transaction{*tx},
GlobalExitRoot: common.HexToHash("0x1"),
}
sequences := []ethermanTypes.Sequence{
{
GlobalExitRoot: batch.GlobalExitRoot,
Timestamp: batch.Timestamp.Unix(),
BatchL2Data: batch.BatchL2Data,
BatchNumber: batch.BatchNumber,
},
}
stateMock.On("GetLastVirtualBatchNum", ctx, nil).Return(uint64(0), nil)
stateMock.On("IsBatchClosed", ctx, uint64(1), nil).Return(true, nil).Once()
stateMock.On("GetBatchByNumber", ctx, uint64(1), nil).Return(batch, nil)
ethermanMock.On("EstimateGasSequenceBatches", common.HexToAddress("0x0000000000000000000000000000000000000000"), sequences).Return(tx, nil)
stateMock.On("IsBatchClosed", ctx, uint64(2), nil).Return(false, nil).Once()
stateMock.On("GetTimeForLatestBatchVirtualization", ctx, nil).Return(batch.Timestamp, nil)
defer func() {
if r := recover(); r == nil {
t.Error("The code did not panic")
}
}()

var (
seq []ethermanTypes.Sequence
)
logOutput := captureLogOutput(func() {
seq, err = s.getSequencesToSend(ctx)
})
require.Empty(t, seq)
require.Error(t, err)
if logOutput == "" {
t.Error("Expected fatal error did not occur")
} else if !bytes.Contains([]byte(logOutput), []byte("Expected fatal error occurred")) {
t.Error("Expected fatal error did not contain the correct message")
}
}
2 changes: 1 addition & 1 deletion sequencer/txtracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func newTxTracker(tx types.Transaction, isClaim bool, counters state.ZKCounters,

txTracker.IsClaim = isClaim
txTracker.BatchResources.zKCounters = counters
txTracker.BatchResources.bytes = uint64(tx.Size())
txTracker.BatchResources.bytes = tx.Size()
txTracker.HashStr = txTracker.Hash.String()
txTracker.FromStr = txTracker.From.String()
txTracker.Benefit = new(big.Int).Mul(new(big.Int).SetUint64(txTracker.Gas), txTracker.GasPrice)
Expand Down
1 change: 1 addition & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ generate-mocks: ## Generates mocks for the tests, using mockery tool
mockery --name=dbManagerInterface --dir=../sequencer --output=../sequencer --outpkg=sequencer --inpackage --structname=DbManagerMock --filename=mock_db_manager.go
mockery --name=etherman --dir=../sequencer --output=../sequencer --outpkg=sequencer --inpackage --structname=EthermanMock --filename=mock_etherman.go
mockery --name=stateInterface --dir=../sequencer/broadcast --output=../sequencer/broadcast/mocks --outpkg=mocks --structname=StateMock --filename=mock_state.go
mockery --name=etherman --dir=../sequencer --output=../sequencer --outpkg=sequencer --structname=EthermanMock --filename=mock_etherman.go

mockery --name=ethermanInterface --dir=../synchronizer --output=../synchronizer --outpkg=synchronizer --structname=ethermanMock --filename=mock_etherman.go
mockery --name=stateInterface --dir=../synchronizer --output=../synchronizer --outpkg=synchronizer --structname=stateMock --filename=mock_state.go
Expand Down
3 changes: 2 additions & 1 deletion test/config/debug.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ MaxConns = 10

[Pool]
FreeClaimGasLimit = 1500000
MaxTxBytesSize=102400
[Pool.DB]
User = "pool_user"
Password = "pool_password"
Expand Down Expand Up @@ -60,7 +61,7 @@ LastBatchVirtualizationTimeMaxWaitPeriod = "5s"
BlocksAmountForTxsToBeDeleted = 100
FrequencyToCheckTxsForDelete = "12h"
MaxTxsPerBatch = 150
MaxBatchBytesSize = 150000
MaxBatchBytesSize = 112640
MaxCumulativeGasUsed = 30000000
MaxKeccakHashes = 468
MaxPoseidonHashes = 279620
Expand Down
3 changes: 2 additions & 1 deletion test/config/test.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ MaxConns = 200

[Pool]
FreeClaimGasLimit = 1500000
MaxTxBytesSize=102400
[Pool.DB]
User = "pool_user"
Password = "pool_password"
Expand Down Expand Up @@ -60,7 +61,7 @@ LastBatchVirtualizationTimeMaxWaitPeriod = "10s"
BlocksAmountForTxsToBeDeleted = 100
FrequencyToCheckTxsForDelete = "12h"
MaxTxsPerBatch = 150
MaxBatchBytesSize = 150000
MaxBatchBytesSize = 112640
MaxCumulativeGasUsed = 30000000
MaxKeccakHashes = 468
MaxPoseidonHashes = 279620
Expand Down

0 comments on commit aa369f5

Please sign in to comment.