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

fix error msg #212

Merged
merged 12 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
7 changes: 6 additions & 1 deletion jsonrpc/endpoints_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,15 @@ func (e *EthEndpoints) EstimateGas(arg *types.TxArgs, blockArg *types.BlockNumbe
return RPCErrorResponse(types.DefaultErrorCode, "failed to convert arguments into an unsigned transaction", err, false)
}

isGasFreeSender, err := e.pool.IsFreeGasAddr(ctx, sender)
if err != nil {
return nil, types.NewRPCError(types.DefaultErrorCode, "failed to check gas-free", err)
}

t2 := time.Now()
toTxTime := t2.Sub(t1)

gasEstimation, returnValue, err := e.state.EstimateGas(tx, sender, blockToProcess, dbTx)
gasEstimation, returnValue, err := e.state.EstimateGas(tx, sender, isGasFreeSender, blockToProcess, dbTx)
if errors.Is(err, runtime.ErrExecutionReverted) {
data := make([]byte, len(returnValue))
copy(data, returnValue)
Expand Down
7 changes: 6 additions & 1 deletion jsonrpc/endpoints_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,12 @@ func (z *ZKEVMEndpoints) internalEstimateGasPriceAndFee(ctx context.Context, arg
return nil, nil, types.NewRPCError(types.DefaultErrorCode, "failed to convert arguments into an unsigned transaction")
}

gasEstimation, returnValue, err := z.state.EstimateGas(tx, sender, blockToProcess, dbTx)
isGasFreeSender, err := z.pool.IsFreeGasAddr(ctx, sender)
if err != nil {
return nil, nil, types.NewRPCError(types.DefaultErrorCode, "failed to check gas-free", err)
}

gasEstimation, returnValue, err := z.state.EstimateGas(tx, sender, isGasFreeSender, blockToProcess, dbTx)
if errors.Is(err, runtime.ErrExecutionReverted) {
data := make([]byte, len(returnValue))
copy(data, returnValue)
Expand Down
3 changes: 2 additions & 1 deletion jsonrpc/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ type PoolInterface interface {
GetInnerTx(ctx context.Context, txHash common.Hash) (string, error)
GetMinSuggestedGasPriceWithDelta(ctx context.Context, delta time.Duration) (uint64, error)
GetReadyTxCount(ctx context.Context) (uint64, error)
IsFreeGasAddr(ctx context.Context, addr common.Address) (bool, error)
}

// StateInterface gathers the methods required to interact with the state.
type StateInterface interface {
StartToMonitorNewL2Blocks()
BeginStateTransaction(ctx context.Context) (pgx.Tx, error)
DebugTransaction(ctx context.Context, transactionHash common.Hash, traceConfig state.TraceConfig, dbTx pgx.Tx) (*runtime.ExecutionResult, error)
EstimateGas(transaction *types.Transaction, senderAddress common.Address, l2BlockNumber *uint64, dbTx pgx.Tx) (uint64, []byte, error)
EstimateGas(transaction *types.Transaction, senderAddress common.Address, isGasFreeSender bool, l2BlockNumber *uint64, dbTx pgx.Tx) (uint64, []byte, error)
GetBalance(ctx context.Context, address common.Address, root common.Hash) (*big.Int, error)
GetCode(ctx context.Context, address common.Address, root common.Hash) ([]byte, error)
GetL2BlockByHash(ctx context.Context, hash common.Hash, dbTx pgx.Tx) (*state.L2Block, error)
Expand Down
19 changes: 11 additions & 8 deletions pool/pool_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (p *Pool) checkFreeGp(ctx context.Context, poolTx Transaction, from common.
case MainnetChainID:
bridgeURL = MainnetBridgeURL
}
return false, fmt.Errorf("you are unable to initiate a gas-free transaction from this address unless you have previously transferred funds to this address via the X Layer Bridge (%s) or the OKX Exchange, and only the first %d transactions(address nonce less than %d)",
return false, fmt.Errorf("you are unable to initiate a gas-free transaction from this address unless you have previously transferred funds to this address via the X Layer Bridge (%s) or the OKX Exchange, only the first %d transactions (address nonce must be less than %d) can be gas-free",
bridgeURL,
freeGasCountPerAddrConfig,
freeGasCountPerAddrConfig)
Expand All @@ -157,13 +157,16 @@ func (p *Pool) checkAndUpdateFreeGasAddr(ctx context.Context, poolTx Transaction
var freeGpAddr common.Address
inputHex := hex.EncodeToHex(poolTx.Data())
// hard code
if isFreeGasExAddress(p.cfg.FreeGasExAddress, from) &&
strings.HasPrefix(inputHex, ExWithdrawalMethodSignature) &&
len(inputHex) > 74 { // erc20 contract transfer
addrHex := "0x" + inputHex[10:74]
freeGpAddr = common.HexToAddress(addrHex)
} else if poolTx.IsClaims &&
len(inputHex) > 4554 { // bridge contract claim
if isFreeGasExAddress(p.cfg.FreeGasExAddress, from) {
if strings.HasPrefix(inputHex, ExWithdrawalMethodSignature) && len(inputHex) > 74 { // erc20 contract transfer
addrHex := "0x" + inputHex[10:74]
freeGpAddr = common.HexToAddress(addrHex)
} else {
// the to address of any Ex withdrawal okb tx will be considered as a gas-free address
// even if this address is a contract, it will not affect the gas-free
freeGpAddr = *poolTx.To()
}
} else if poolTx.IsClaims && len(inputHex) > 4554 { // bridge contract claim
addrHex := "0x" + inputHex[4490:4554]
freeGpAddr = common.HexToAddress(addrHex)
}
Expand Down
6 changes: 3 additions & 3 deletions state/test/forkid_dragonfruit/dragonfruit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,15 +332,15 @@ func TestExecutorEstimateGas(t *testing.T) {
blockNumber, err := testState.GetLastL2BlockNumber(ctx, nil)
require.NoError(t, err)

estimatedGas, _, err := testState.EstimateGas(signedTx2, sequencerAddress, &blockNumber, nil)
estimatedGas, _, err := testState.EstimateGas(signedTx2, sequencerAddress, false, &blockNumber, nil)
require.NoError(t, err)
log.Debugf("Estimated gas = %v", estimatedGas)

nonce++
tx3 := types.NewTransaction(nonce, scAddress, new(big.Int), 40000, new(big.Int).SetUint64(1), common.Hex2Bytes("4abbb40a"))
signedTx3, err := auth.Signer(auth.From, tx3)
require.NoError(t, err)
_, _, err = testState.EstimateGas(signedTx3, sequencerAddress, &blockNumber, nil)
_, _, err = testState.EstimateGas(signedTx3, sequencerAddress, false, &blockNumber, nil)
require.Error(t, err)
}

Expand Down Expand Up @@ -701,7 +701,7 @@ func TestExecutorGasEstimationMultisig(t *testing.T) {
blockNumber, err := testState.GetLastL2BlockNumber(ctx, nil)
require.NoError(t, err)

estimatedGas, _, err := testState.EstimateGas(signedTx6, sequencerAddress, &blockNumber, nil)
estimatedGas, _, err := testState.EstimateGas(signedTx6, sequencerAddress, false, &blockNumber, nil)
require.NoError(t, err)
log.Debugf("Estimated gas = %v", estimatedGas)

Expand Down
4 changes: 2 additions & 2 deletions state/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ func CheckSupersetBatchTransactions(existingTxHashes []common.Hash, processedTxs
}

// EstimateGas for a transaction
func (s *State) EstimateGas(transaction *types.Transaction, senderAddress common.Address, l2BlockNumber *uint64, dbTx pgx.Tx) (uint64, []byte, error) {
func (s *State) EstimateGas(transaction *types.Transaction, senderAddress common.Address, isGasFreeSender bool, l2BlockNumber *uint64, dbTx pgx.Tx) (uint64, []byte, error) {
const ethTransferGas = 21000

ctx := context.Background()
Expand Down Expand Up @@ -756,7 +756,7 @@ func (s *State) EstimateGas(transaction *types.Transaction, senderAddress common

// if gas price is set, set the highEnd to the max amount
// of the account afford
isGasPriceSet := transaction.GasPrice().BitLen() != 0
isGasPriceSet := !isGasFreeSender && transaction.GasPrice().BitLen() != 0
if isGasPriceSet {
senderBalance, err := s.tree.GetBalance(ctx, senderAddress, l2Block.Root().Bytes())
if errors.Is(err, ErrNotFound) {
Expand Down
Loading