Skip to content

Commit

Permalink
fix claims (#1977)
Browse files Browse the repository at this point in the history
  • Loading branch information
tclemos authored Mar 31, 2023
1 parent c55a798 commit 474ebc9
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 53 deletions.
17 changes: 15 additions & 2 deletions jsonrpc/endpoints_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (e *EthEndpoints) Call(arg *types.TxArgs, blockArg *types.BlockNumberOrHash

var blockNumber uint64
var err error
var blockToProcess *uint64
if blockArg.IsHash() {
block, err := e.state.GetL2BlockByHash(ctx, blockArg.Hash().Hash(), dbTx)
if errors.Is(err, state.ErrNotFound) {
Expand All @@ -72,6 +73,7 @@ func (e *EthEndpoints) Call(arg *types.TxArgs, blockArg *types.BlockNumberOrHash
return rpcErrorResponse(types.DefaultErrorCode, errMsg, err)
}
blockNumber = block.Number().Uint64()
blockToProcess = &blockNumber
} else {
var rpcErr types.Error
blockNumber, rpcErr = blockArg.Number().GetNumericBlockNumber(ctx, e.state, dbTx)
Expand All @@ -85,6 +87,10 @@ func (e *EthEndpoints) Call(arg *types.TxArgs, blockArg *types.BlockNumberOrHash
errMsg := fmt.Sprintf("failed to get block by number %v", blockNumber)
return rpcErrorResponse(types.DefaultErrorCode, errMsg, err)
}
blockToProcess = &blockNumber
if *blockArg.Number() == types.LatestBlockNumber || *blockArg.Number() == types.PendingBlockNumber {
blockToProcess = nil
}
}

// If the caller didn't supply the gas limit in the message, then we set it to maximum possible => block gas limit
Expand All @@ -104,7 +110,7 @@ func (e *EthEndpoints) Call(arg *types.TxArgs, blockArg *types.BlockNumberOrHash
return rpcErrorResponse(types.DefaultErrorCode, "failed to convert arguments into an unsigned transaction", err)
}

result, err := e.state.ProcessUnsignedTransaction(ctx, tx, sender, blockNumber, false, dbTx)
result, err := e.state.ProcessUnsignedTransaction(ctx, tx, sender, blockToProcess, false, dbTx)
if err != nil {
return rpcErrorResponse(types.DefaultErrorCode, "failed to execute the unsigned transaction", err)
}
Expand Down Expand Up @@ -140,11 +146,13 @@ func (e *EthEndpoints) EstimateGas(arg *types.TxArgs, blockArg *types.BlockNumbe

var blockNumber uint64
var err error
var blockToProcess *uint64
if blockArg == nil {
blockNumber, err = e.state.GetLastL2BlockNumber(ctx, dbTx)
if err != nil {
return rpcErrorResponse(types.DefaultErrorCode, "failed to get last block from state", err)
}
blockToProcess = nil
} else {
if blockArg.IsHash() {
block, err := e.state.GetL2BlockByHash(ctx, blockArg.Hash().Hash(), dbTx)
Expand All @@ -153,12 +161,17 @@ func (e *EthEndpoints) EstimateGas(arg *types.TxArgs, blockArg *types.BlockNumbe
return rpcErrorResponse(types.DefaultErrorCode, errMsg, err)
}
blockNumber = block.Number().Uint64()
blockToProcess = &blockNumber
} else {
var rpcErr types.Error
blockNumber, rpcErr = blockArg.Number().GetNumericBlockNumber(ctx, e.state, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
blockToProcess = &blockNumber
if *blockArg.Number() == types.LatestBlockNumber || *blockArg.Number() == types.PendingBlockNumber {
blockToProcess = nil
}
}
}

Expand All @@ -168,7 +181,7 @@ func (e *EthEndpoints) EstimateGas(arg *types.TxArgs, blockArg *types.BlockNumbe
return rpcErrorResponse(types.DefaultErrorCode, "failed to convert arguments into an unsigned transaction", err)
}

gasEstimation, err := e.state.EstimateGas(tx, sender, blockNumber, dbTx)
gasEstimation, err := e.state.EstimateGas(tx, sender, blockToProcess, dbTx)
if err != nil {
return rpcErrorResponse(types.DefaultErrorCode, err.Error(), nil)
}
Expand Down
31 changes: 18 additions & 13 deletions jsonrpc/endpoints_eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestCall(t *testing.T) {
})
m.State.On("GetNonce", context.Background(), *txArgs.From, blockNumber, m.DbTx).Return(nonce, nil).Once()
m.State.
On("ProcessUnsignedTransaction", context.Background(), txMatchBy, *txArgs.From, blockNumber, false, m.DbTx).
On("ProcessUnsignedTransaction", context.Background(), txMatchBy, *txArgs.From, &blockNumber, false, m.DbTx).
Return(&runtime.ExecutionResult{ReturnValue: testCase.expectedResult}, nil).
Once()
},
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestCall(t *testing.T) {
})
m.State.On("GetNonce", context.Background(), *txArgs.From, blockNumber, m.DbTx).Return(nonce, nil).Once()
m.State.
On("ProcessUnsignedTransaction", context.Background(), txMatchBy, *txArgs.From, blockNumber, false, m.DbTx).
On("ProcessUnsignedTransaction", context.Background(), txMatchBy, *txArgs.From, &blockNumber, false, m.DbTx).
Return(&runtime.ExecutionResult{ReturnValue: testCase.expectedResult}, nil).
Once()
},
Expand Down Expand Up @@ -245,8 +245,9 @@ func TestCall(t *testing.T) {
return match
})
m.State.On("GetNonce", context.Background(), *txArgs.From, blockNumber, m.DbTx).Return(nonce, nil).Once()
var nilBn *uint64
m.State.
On("ProcessUnsignedTransaction", context.Background(), txMatchBy, *txArgs.From, blockNumber, false, m.DbTx).
On("ProcessUnsignedTransaction", context.Background(), txMatchBy, *txArgs.From, nilBn, false, m.DbTx).
Return(&runtime.ExecutionResult{ReturnValue: testCase.expectedResult}, nil).
Once()
},
Expand Down Expand Up @@ -289,7 +290,7 @@ func TestCall(t *testing.T) {
})
m.State.On("GetNonce", context.Background(), *txArgs.From, blockNumber, m.DbTx).Return(nonce, nil).Once()
m.State.
On("ProcessUnsignedTransaction", context.Background(), txMatchBy, *txArgs.From, blockNumber, false, m.DbTx).
On("ProcessUnsignedTransaction", context.Background(), txMatchBy, *txArgs.From, &blockNumber, false, m.DbTx).
Return(&runtime.ExecutionResult{ReturnValue: testCase.expectedResult}, nil).
Once()
},
Expand Down Expand Up @@ -330,7 +331,7 @@ func TestCall(t *testing.T) {
})
m.State.On("GetNonce", context.Background(), *txArgs.From, blockNumber, m.DbTx).Return(nonce, nil).Once()
m.State.
On("ProcessUnsignedTransaction", context.Background(), txMatchBy, *txArgs.From, blockNumber, false, m.DbTx).
On("ProcessUnsignedTransaction", context.Background(), txMatchBy, *txArgs.From, &blockNumber, false, m.DbTx).
Return(&runtime.ExecutionResult{ReturnValue: testCase.expectedResult}, nil).
Once()
},
Expand Down Expand Up @@ -369,8 +370,9 @@ func TestCall(t *testing.T) {
dataMatch := hex.EncodeToHex(tx.Data()) == hex.EncodeToHex(*txArgs.Data)
return hasTx && gasMatch && toMatch && gasPriceMatch && valueMatch && dataMatch
})
var nilBn *uint64
m.State.
On("ProcessUnsignedTransaction", context.Background(), txMatchBy, common.HexToAddress(c.DefaultSenderAddress), blockNumber, false, m.DbTx).
On("ProcessUnsignedTransaction", context.Background(), txMatchBy, common.HexToAddress(c.DefaultSenderAddress), nilBn, false, m.DbTx).
Return(&runtime.ExecutionResult{ReturnValue: testCase.expectedResult}, nil).
Once()
},
Expand Down Expand Up @@ -409,8 +411,9 @@ func TestCall(t *testing.T) {
dataMatch := hex.EncodeToHex(tx.Data()) == hex.EncodeToHex(*txArgs.Data)
return hasTx && gasMatch && toMatch && gasPriceMatch && valueMatch && dataMatch
})
var nilBn *uint64
m.State.
On("ProcessUnsignedTransaction", context.Background(), txMatchBy, common.HexToAddress(c.DefaultSenderAddress), blockNumber, false, m.DbTx).
On("ProcessUnsignedTransaction", context.Background(), txMatchBy, common.HexToAddress(c.DefaultSenderAddress), nilBn, false, m.DbTx).
Return(&runtime.ExecutionResult{ReturnValue: testCase.expectedResult}, nil).
Once()
},
Expand Down Expand Up @@ -475,8 +478,9 @@ func TestCall(t *testing.T) {
return hasTx && gasMatch && toMatch && gasPriceMatch && valueMatch && dataMatch && nonceMatch
})
m.State.On("GetNonce", context.Background(), *txArgs.From, blockNumber, m.DbTx).Return(nonce, nil).Once()
var nilBn *uint64
m.State.
On("ProcessUnsignedTransaction", context.Background(), txMatchBy, *txArgs.From, blockNumber, false, m.DbTx).
On("ProcessUnsignedTransaction", context.Background(), txMatchBy, *txArgs.From, nilBn, false, m.DbTx).
Return(&runtime.ExecutionResult{Err: errors.New("failed to process unsigned transaction")}, nil).
Once()
},
Expand Down Expand Up @@ -518,8 +522,9 @@ func TestCall(t *testing.T) {
return hasTx && gasMatch && toMatch && gasPriceMatch && valueMatch && dataMatch && nonceMatch
})
m.State.On("GetNonce", context.Background(), *txArgs.From, blockNumber, m.DbTx).Return(nonce, nil).Once()
var nilBn *uint64
m.State.
On("ProcessUnsignedTransaction", context.Background(), txMatchBy, *txArgs.From, blockNumber, false, m.DbTx).
On("ProcessUnsignedTransaction", context.Background(), txMatchBy, *txArgs.From, nilBn, false, m.DbTx).
Return(&runtime.ExecutionResult{Err: runtime.ErrExecutionReverted}, nil).
Once()
},
Expand Down Expand Up @@ -624,9 +629,9 @@ func TestEstimateGas(t *testing.T) {
On("GetNonce", context.Background(), *txArgs.From, blockNumber, m.DbTx).
Return(nonce, nil).
Once()

var nilBn *uint64
m.State.
On("EstimateGas", txMatchBy, *txArgs.From, blockNumber, m.DbTx).
On("EstimateGas", txMatchBy, *txArgs.From, nilBn, m.DbTx).
Return(*testCase.expectedResult, nil).
Once()
},
Expand Down Expand Up @@ -668,9 +673,9 @@ func TestEstimateGas(t *testing.T) {
On("GetLastL2BlockNumber", context.Background(), m.DbTx).
Return(blockNumber, nil).
Once()

var nilBn *uint64
m.State.
On("EstimateGas", txMatchBy, common.HexToAddress(c.DefaultSenderAddress), blockNumber, m.DbTx).
On("EstimateGas", txMatchBy, common.HexToAddress(c.DefaultSenderAddress), nilBn, m.DbTx).
Return(*testCase.expectedResult, nil).
Once()
},
Expand Down
16 changes: 8 additions & 8 deletions jsonrpc/mocks/mock_state.go

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

4 changes: 2 additions & 2 deletions jsonrpc/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type StateInterface interface {
PrepareWebSocket()
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, error)
EstimateGas(transaction *types.Transaction, senderAddress common.Address, l2BlockNumber *uint64, dbTx pgx.Tx) (uint64, error)
GetBalance(ctx context.Context, address common.Address, blockNumber uint64, dbTx pgx.Tx) (*big.Int, error)
GetCode(ctx context.Context, address common.Address, blockNumber uint64, dbTx pgx.Tx) ([]byte, error)
GetL2BlockByHash(ctx context.Context, hash common.Hash, dbTx pgx.Tx) (*types.Block, error)
Expand All @@ -52,7 +52,7 @@ type StateInterface interface {
GetTransactionReceipt(ctx context.Context, transactionHash common.Hash, dbTx pgx.Tx) (*types.Receipt, error)
IsL2BlockConsolidated(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (bool, error)
IsL2BlockVirtualized(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) (bool, error)
ProcessUnsignedTransaction(ctx context.Context, tx *types.Transaction, senderAddress common.Address, l2BlockNumber uint64, noZKEVMCounters bool, dbTx pgx.Tx) (*runtime.ExecutionResult, error)
ProcessUnsignedTransaction(ctx context.Context, tx *types.Transaction, senderAddress common.Address, l2BlockNumber *uint64, noZKEVMCounters bool, dbTx pgx.Tx) (*runtime.ExecutionResult, error)
RegisterNewL2BlockEventHandler(h state.NewL2BlockEventHandler)
GetLastVirtualBatchNum(ctx context.Context, dbTx pgx.Tx) (uint64, error)
GetLastVerifiedBatch(ctx context.Context, dbTx pgx.Tx) (*state.VerifiedBatch, error)
Expand Down
36 changes: 17 additions & 19 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,15 @@ func (s *State) GetStorageAt(ctx context.Context, address common.Address, positi
}

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

var lowEnd uint64
var highEnd uint64

ctx := context.Background()

lastBatches, l2BlockStateRoot, err := s.PostgresStorage.GetLastNBatchesByL2BlockNumber(ctx, &l2BlockNumber, two, dbTx)
lastBatches, l2BlockStateRoot, err := s.PostgresStorage.GetLastNBatchesByL2BlockNumber(ctx, l2BlockNumber, two, dbTx)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -1207,12 +1207,7 @@ func (s *State) PreProcessTransaction(ctx context.Context, tx *types.Transaction
return nil, err
}

lastL2BlockNumber, err := s.GetLastL2BlockNumber(ctx, dbTx)
if err != nil {
return nil, err
}

response, err := s.internalProcessUnsignedTransaction(ctx, tx, sender, lastL2BlockNumber, false, dbTx)
response, err := s.internalProcessUnsignedTransaction(ctx, tx, sender, nil, false, dbTx)
if err != nil {
return nil, err
}
Expand All @@ -1221,7 +1216,7 @@ func (s *State) PreProcessTransaction(ctx context.Context, tx *types.Transaction
}

// ProcessUnsignedTransaction processes the given unsigned transaction.
func (s *State) ProcessUnsignedTransaction(ctx context.Context, tx *types.Transaction, senderAddress common.Address, l2BlockNumber uint64, noZKEVMCounters bool, dbTx pgx.Tx) (*runtime.ExecutionResult, error) {
func (s *State) ProcessUnsignedTransaction(ctx context.Context, tx *types.Transaction, senderAddress common.Address, l2BlockNumber *uint64, noZKEVMCounters bool, dbTx pgx.Tx) (*runtime.ExecutionResult, error) {
result := new(runtime.ExecutionResult)
response, err := s.internalProcessUnsignedTransaction(ctx, tx, senderAddress, l2BlockNumber, noZKEVMCounters, dbTx)
if err != nil {
Expand All @@ -1245,23 +1240,26 @@ func (s *State) ProcessUnsignedTransaction(ctx context.Context, tx *types.Transa
}

// ProcessUnsignedTransaction processes the given unsigned transaction.
func (s *State) internalProcessUnsignedTransaction(ctx context.Context, tx *types.Transaction, senderAddress common.Address, l2BlockNumber uint64, noZKEVMCounters bool, dbTx pgx.Tx) (*ProcessBatchResponse, error) {
lastBatches, _, err := s.PostgresStorage.GetLastNBatchesByL2BlockNumber(ctx, &l2BlockNumber, two, dbTx)
func (s *State) internalProcessUnsignedTransaction(ctx context.Context, tx *types.Transaction, senderAddress common.Address, l2BlockNumber *uint64, noZKEVMCounters bool, dbTx pgx.Tx) (*ProcessBatchResponse, error) {
lastBatches, l2BlockStateRoot, err := s.PostgresStorage.GetLastNBatchesByL2BlockNumber(ctx, l2BlockNumber, two, dbTx)
if err != nil {
return nil, err
}

l2Block, err := s.GetL2BlockByNumber(ctx, l2BlockNumber, dbTx)
if err != nil {
log.Errorf("error getting l2 block", err)
return nil, err
stateRoot := l2BlockStateRoot
if l2BlockNumber != nil {
l2Block, err := s.GetL2BlockByNumber(ctx, *l2BlockNumber, dbTx)
if err != nil {
return nil, err
}
stateRoot = l2Block.Root()
}

nonce, err := s.tree.GetNonce(ctx, senderAddress, l2Block.Root().Bytes())
loadedNonce, err := s.tree.GetNonce(ctx, senderAddress, stateRoot.Bytes())
if err != nil {
return nil, err
}
forcedNonce := nonce.Uint64()
nonce := loadedNonce.Uint64()

// Get latest batch from the database to get globalExitRoot and Timestamp
lastBatch := lastBatches[0]
Expand All @@ -1272,7 +1270,7 @@ func (s *State) internalProcessUnsignedTransaction(ctx context.Context, tx *type
previousBatch = lastBatches[1]
}

batchL2Data, err := EncodeUnsignedTransaction(*tx, s.cfg.ChainID, &forcedNonce)
batchL2Data, err := EncodeUnsignedTransaction(*tx, s.cfg.ChainID, &nonce)
if err != nil {
log.Errorf("error encoding unsigned transaction ", err)
return nil, err
Expand All @@ -1284,7 +1282,7 @@ func (s *State) internalProcessUnsignedTransaction(ctx context.Context, tx *type
OldBatchNum: lastBatch.BatchNumber,
BatchL2Data: batchL2Data,
From: senderAddress.String(),
OldStateRoot: l2Block.Root().Bytes(),
OldStateRoot: stateRoot.Bytes(),
GlobalExitRoot: lastBatch.GlobalExitRoot.Bytes(),
OldAccInputHash: previousBatch.AccInputHash.Bytes(),
EthTimestamp: uint64(lastBatch.Timestamp.Unix()),
Expand Down
Loading

0 comments on commit 474ebc9

Please sign in to comment.