Skip to content

Commit

Permalink
Merge pull request 0xPolygon#3 from 0xPolygon/feature/gasless
Browse files Browse the repository at this point in the history
Feature/gasless
  • Loading branch information
arnaubennassar authored Jul 11, 2023
2 parents a59d0c7 + 2855144 commit 2fe5745
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 4 deletions.
1 change: 1 addition & 0 deletions ci/e2e-group1/gasless_test.go
1 change: 1 addition & 0 deletions pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func NewPool(cfg Config, s storage, st stateInterface, l2BridgeAddr common.Addre
state: st,
chainID: chainID,
blockedAddresses: sync.Map{},
minSuggestedGasPrice: big.NewInt(int64(cfg.DefaultMinGasPriceAllowed)),
minSuggestedGasPriceMux: new(sync.RWMutex),
eventLog: eventLog,
}
Expand Down
1 change: 0 additions & 1 deletion test/config/debug.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ EnableLog = true
MaxConns = 10

[Pool]
FreeClaimGasLimit = 1500000
IntervalToRefreshBlockedAddresses = "5m"
MaxTxBytesSize=30132
MaxTxDataBytesSize=30000
Expand Down
1 change: 0 additions & 1 deletion test/config/test.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ EnableLog = false
MaxConns = 200

[Pool]
FreeClaimGasLimit = 1500000
IntervalToRefreshBlockedAddresses = "5m"
MaxTxBytesSize=100132
MaxTxDataBytesSize=100000
Expand Down
96 changes: 96 additions & 0 deletions test/e2e/gasless_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package e2e

import (
"context"
"fmt"
"math/big"
"os/exec"
"testing"
"time"

"github.com/0xPolygon/supernets2-node/log"
"github.com/0xPolygon/supernets2-node/test/operations"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/require"
)

func TestEthTransferGasless(t *testing.T) {
if testing.Short() {
t.Skip()
}
// Edit config
out, err := exec.Command("pwd").CombinedOutput()
fmt.Println(string(out))
require.NoError(t, err)
const path = "../../test/config/test.node.config.toml"
require.NoError(t,
exec.Command("sed", "-i", "s/DefaultMinGasPriceAllowed = 1000000000/DefaultMinGasPriceAllowed = 0/g", path).Run(),
)
require.NoError(t,
exec.Command("sed", "-i", "s/EnableL2SuggestedGasPricePolling = true/EnableL2SuggestedGasPricePolling = false/g", path).Run(),
)
// Undo edit config
defer func() {
require.NoError(t,
exec.Command("sed", "-i", "s/DefaultMinGasPriceAllowed = 0/DefaultMinGasPriceAllowed = 1000000000/g", path).Run(),
)
require.NoError(t,
exec.Command("sed", "-i", "s/EnableL2SuggestedGasPricePolling = false/EnableL2SuggestedGasPricePolling = true/g", path).Run(),
)
}()

ctx := context.Background()
defer func() { require.NoError(t, operations.Teardown()) }()

err = operations.Teardown()
require.NoError(t, err)
opsCfg := operations.GetDefaultOperationsConfig()
opsCfg.State.MaxCumulativeGasUsed = 80000000000
opsman, err := operations.NewManager(ctx, opsCfg)
require.NoError(t, err)
err = opsman.Setup()
require.NoError(t, err)
time.Sleep(5 * time.Second)
// Load account with balance on local genesis
pk, err := crypto.GenerateKey()
require.NoError(t, err)
auth, err := bind.NewKeyedTransactorWithChainID(pk, big.NewInt(0).SetUint64(operations.DefaultL2ChainID))
require.NoError(t, err)
// Load eth client
client, err := ethclient.Dial(operations.DefaultL2NetworkURL)
require.NoError(t, err)
// Send txs
nTxs := 10
amount := big.NewInt(0)
toAddress := common.HexToAddress("0x70997970C51812dc3A010C7d01b50e0d17dc79C8")
senderBalance, err := client.BalanceAt(ctx, auth.From, nil)
require.NoError(t, err)
senderNonce, err := client.PendingNonceAt(ctx, auth.From)
require.NoError(t, err)

log.Infof("Receiver Addr: %v", toAddress.String())
log.Infof("Sender Addr: %v", auth.From.String())
log.Infof("Sender Balance: %v", senderBalance.String())
log.Infof("Sender Nonce: %v", senderNonce)

gasLimit, err := client.EstimateGas(ctx, ethereum.CallMsg{From: auth.From, To: &toAddress, Value: amount})
require.NoError(t, err)

gasPrice := big.NewInt(0)
nonce, err := client.PendingNonceAt(ctx, auth.From)
require.NoError(t, err)

txs := make([]*types.Transaction, 0, nTxs)
for i := 0; i < nTxs; i++ {
tx := types.NewTransaction(nonce+uint64(i), toAddress, amount, gasLimit, gasPrice, nil)
txs = append(txs, tx)
}

_, err = operations.ApplyL2Txs(ctx, txs, auth, client, operations.VerifiedConfirmationLevel)
require.NoError(t, err)
}
11 changes: 9 additions & 2 deletions test/operations/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ func ApplyL2Txs(ctx context.Context, txs []*types.Transaction, auth *bind.Transa
}
}
waitToBeMined := confirmationLevel != PoolConfirmationLevel
var initialNonce uint64
if waitToBeMined {
initialNonce, err = client.NonceAt(ctx, auth.From, nil)
if err != nil {
return nil, err
}
}
sentTxs, err := applyTxs(ctx, txs, auth, client, waitToBeMined)
if err != nil {
return nil, err
Expand All @@ -226,7 +233,7 @@ func ApplyL2Txs(ctx context.Context, txs []*types.Transaction, auth *bind.Transa
}

l2BlockNumbers := make([]*big.Int, 0, len(sentTxs))
for _, tx := range sentTxs {
for i, tx := range sentTxs {
// check transaction nonce against transaction reported L2 block number
receipt, err := client.TransactionReceipt(ctx, tx.Hash())
if err != nil {
Expand All @@ -235,7 +242,7 @@ func ApplyL2Txs(ctx context.Context, txs []*types.Transaction, auth *bind.Transa

// get L2 block number
l2BlockNumbers = append(l2BlockNumbers, receipt.BlockNumber)
expectedNonce := receipt.BlockNumber.Uint64() - 1 + 8 //nolint:gomnd
expectedNonce := initialNonce + uint64(i)
if tx.Nonce() != expectedNonce {
return nil, fmt.Errorf("mismatching nonce for tx %v: want %d, got %d\n", tx.Hash(), expectedNonce, tx.Nonce())
}
Expand Down

0 comments on commit 2fe5745

Please sign in to comment.