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

Update: Implement tests for execution/proof & /errors #90

Closed
wants to merge 10 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
43 changes: 22 additions & 21 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package common
import (
"encoding/json"
"fmt"
"math/big"
"strconv"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/holiman/uint256"
)
Expand Down Expand Up @@ -45,27 +45,28 @@ type Transactions struct {
Hashes [][32]byte
Full []Transaction // transaction needs to be defined
}

// Updated as earlier, txn data fetched from rpc was not able to unmarshal
// into the struct
type Transaction struct {
AccessList types.AccessList
Hash common.Hash
Nonce uint64
BlockHash [32]byte
BlockNumber *uint64
TransactionIndex uint64
From string
To *common.Address
Value *big.Int
GasPrice *big.Int
Gas uint64
Input []byte
ChainID *big.Int
TransactionType uint8
Signature *Signature
MaxFeePerGas *big.Int
MaxPriorityFeePerGas *big.Int
MaxFeePerBlobGas *big.Int
BlobVersionedHashes []common.Hash
AccessList types.AccessList `json:"accessList"`
Hash common.Hash `json:"hash"`
Nonce hexutil.Uint64 `json:"nonce"`
BlockHash string `json:"blockHash"` // Pointer because it's nullable
BlockNumber hexutil.Uint64 `json:"blockNumber"` // Pointer because it's nullable
TransactionIndex hexutil.Uint64 `json:"transactionIndex"`
From string `json:"from"`
To *common.Address `json:"to"` // Pointer because 'to' can be null for contract creation
Value hexutil.Big `json:"value"`
GasPrice hexutil.Big `json:"gasPrice"`
Gas hexutil.Uint64 `json:"gas"`
Input hexutil.Bytes `json:"input"`
ChainID hexutil.Big `json:"chainId"`
TransactionType hexutil.Uint `json:"type"`
Signature *Signature `json:"signature"`
MaxFeePerGas hexutil.Big `json:"maxFeePerGas"`
MaxPriorityFeePerGas hexutil.Big `json:"maxPriorityFeePerGas"`
MaxFeePerBlobGas hexutil.Big `json:"maxFeePerBlobGas"`
BlobVersionedHashes []common.Hash `json:"blobVersionedHashes"`
}

type Signature struct {
Expand Down
36 changes: 21 additions & 15 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/holiman/uint256"
"github.com/pkg/errors"
"github.com/ethereum/go-ethereum/common/hexutil"
)

// Error definitions
Expand Down Expand Up @@ -953,20 +954,24 @@ func processTransaction(txBytes *[1073741824]byte, blockHash consensus_core.Byte
if err != nil {
return common.Transaction{}, fmt.Errorf("failed to decode transaction: %v", err)
}

// Updated due to update in Transaction struct
tx := common.Transaction{
Hash: txEnvelope.Hash(),
Nonce: txEnvelope.Nonce(),
BlockHash: blockHash,
BlockNumber: blockNumber,
TransactionIndex: index,
Nonce: hexutil.Uint64(txEnvelope.Nonce()),
BlockHash: func() string {
data := [32]byte(blockHash)
hexString := hex.EncodeToString(data[:])
return hexString
}(),
BlockNumber: hexutil.Uint64(*blockNumber),
TransactionIndex: hexutil.Uint64(index),
To: txEnvelope.To(),
Value: txEnvelope.Value(),
GasPrice: txEnvelope.GasPrice(),
Gas: txEnvelope.Gas(),
Value: hexutil.Big(*txEnvelope.Value()),
GasPrice: hexutil.Big(*txEnvelope.GasPrice()),
Gas: hexutil.Uint64(txEnvelope.Gas()),
Input: txEnvelope.Data(),
ChainID: txEnvelope.ChainId(),
TransactionType: txEnvelope.Type(),
ChainID: hexutil.Big(*txEnvelope.ChainId()),
TransactionType: hexutil.Uint(uint(txEnvelope.Type())),
}

// Handle signature and transaction type logic
Expand All @@ -990,12 +995,13 @@ func processTransaction(txBytes *[1073741824]byte, blockHash consensus_core.Byte
case types.AccessListTxType:
tx.AccessList = txEnvelope.AccessList()
case types.DynamicFeeTxType:
tx.MaxFeePerGas = new(big.Int).Set(txEnvelope.GasFeeCap())
tx.MaxPriorityFeePerGas = new(big.Int).Set(txEnvelope.GasTipCap())
// Update due to update in Transaction struct
tx.MaxFeePerGas = hexutil.Big(*new(big.Int).Set(txEnvelope.GasFeeCap()))
tx.MaxPriorityFeePerGas = hexutil.Big(*new(big.Int).Set(txEnvelope.GasTipCap()))
case types.BlobTxType:
tx.MaxFeePerGas = new(big.Int).Set(txEnvelope.GasFeeCap())
tx.MaxPriorityFeePerGas = new(big.Int).Set(txEnvelope.GasTipCap())
tx.MaxFeePerBlobGas = new(big.Int).Set(txEnvelope.BlobGasFeeCap())
tx.MaxFeePerGas = hexutil.Big(*new(big.Int).Set(txEnvelope.GasFeeCap()))
tx.MaxPriorityFeePerGas = hexutil.Big(*new(big.Int).Set(txEnvelope.GasTipCap()))
tx.MaxFeePerBlobGas = hexutil.Big(*new(big.Int).Set(txEnvelope.BlobGasFeeCap()))
tx.BlobVersionedHashes = txEnvelope.BlobHashes()
default:
fmt.Println("Unhandled transaction type")
Expand Down
85 changes: 85 additions & 0 deletions execution/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package execution

import (
"errors"
"testing"

"github.com/BlocSoc-iitr/selene/consensus/consensus_core"
"github.com/BlocSoc-iitr/selene/consensus/types"
"github.com/stretchr/testify/assert"
)

func TestExecutionErrors(t *testing.T) {
// Test InvalidAccountProofError
address := types.Address{0x01, 0x02}
err := NewInvalidAccountProofError(address)
assert.EqualError(t, err, "invalid account proof for string: [1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]")

// Test InvalidStorageProofError
slot := consensus_core.Bytes32{0x0a}
err = NewInvalidStorageProofError(address, slot)
assert.EqualError(t, err, "invalid storage proof for string: [1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], slot: [10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]")

// Test CodeHashMismatchError
found := consensus_core.Bytes32{0x03}
expected := consensus_core.Bytes32{0x04}
err = NewCodeHashMismatchError(address, found, expected)
assert.EqualError(t, err, "code hash mismatch for string: [1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], found: [3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], expected: [4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]")

// Test ReceiptRootMismatchError
tx := consensus_core.Bytes32{0x05}
err = NewReceiptRootMismatchError(tx)
assert.EqualError(t, err, "receipt root mismatch for tx: [5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]")

// Test MissingTransactionError
err = NewMissingTransactionError(tx)
assert.EqualError(t, err, "missing transaction for tx: [5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]")

// Test MissingLogError
err = NewMissingLogError(tx, 3)
assert.EqualError(t, err, "missing log for transaction: [5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], index: 3")

// Test TooManyLogsToProveError
err = NewTooManyLogsToProveError(5000, 1000)
assert.EqualError(t, err, "too many logs to prove: 5000, current limit is: 1000")

// Test InvalidBaseGasFeeError
err = NewInvalidBaseGasFeeError(1000, 2000, 123456)
assert.EqualError(t, err, "Invalid base gas fee selene 1000 vs rpc endpoint 2000 at block 123456")

// Test BlockNotFoundError
err = NewBlockNotFoundError(123456)
assert.EqualError(t, err, "Block 123456 not found")

// Test EmptyExecutionPayloadError
err = NewEmptyExecutionPayloadError()
assert.EqualError(t, err, "Selene Execution Payload is empty")
}

func TestEvmErrors(t *testing.T) {
// Test RevertError
data := []byte{0x08, 0xc3, 0x79, 0xa0}
err := NewRevertError(data)
assert.EqualError(t, err, "execution reverted: [8 195 121 160]")

// Test GenericError
err = NewGenericError("generic error")
assert.EqualError(t, err, "evm error: generic error")

// Test RpcError
rpcErr := errors.New("rpc connection failed")
err = NewRpcError(rpcErr)
assert.EqualError(t, err, "rpc error: rpc connection failed")
}

func TestDecodeRevertReason(t *testing.T) {
// Test successful revert reason decoding
reasonData := []byte{0x08, 0xc3, 0x79, 0xa0}
reason := DecodeRevertReason(reasonData)
assert.NotEmpty(t, reason, "Revert reason should be decoded")

// Test invalid revert data
invalidData := []byte{0x00}
reason = DecodeRevertReason(invalidData)
assert.Contains(t, reason, "invalid data for unpacking")
}
Loading
Loading