Skip to content

Commit

Permalink
test: add tests for calculateMevFromBlock, calculateBlockUncleReward,…
Browse files Browse the repository at this point in the history
… calculateUncleReward & verifyName methods
  • Loading branch information
Monika-Bitfly committed Feb 11, 2025
1 parent 65a2663 commit 07c7784
Showing 1 changed file with 182 additions and 48 deletions.
230 changes: 182 additions & 48 deletions backend/pkg/executionlayer/transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package executionlayer

import (
"bytes"
"fmt"
"math/big"
"testing"

Expand Down Expand Up @@ -950,86 +951,219 @@ func TestCalculateTxFee(t *testing.T) {
}
}

func addPrefix(b []byte, length int) []byte {
for len(b) != length {
b = append([]byte{0}, b...)
func TestCalculateMevFromBlock(t *testing.T) {
tests := []struct {
name string
block *types.Eth1Block
expected *big.Int
}{
{
name: "no MEV",
block: &types.Eth1Block{
Coinbase: []byte("coinbase"),
Transactions: []*types.Eth1Transaction{
{
Itx: []*types.Eth1InternalTransaction{
{
From: alice,
To: common.Address{}.Bytes(),
Value: big.NewInt(100).Bytes(),
},
},
},
},
},
expected: big.NewInt(0),
},
{
name: "MEV from one transaction",
block: &types.Eth1Block{
Coinbase: []byte("coinbase"),
Transactions: []*types.Eth1Transaction{
{
Itx: []*types.Eth1InternalTransaction{
{
From: alice,
To: []byte("coinbase"),
Value: big.NewInt(100).Bytes(),
},
},
},
},
},
expected: big.NewInt(100),
},
}
return b
}

func rightPad(b []byte) []byte {
for len(b) != 32 {
b = append(b, 0)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := calculateMevFromBlock(tt.block)
if result.Cmp(tt.expected) != 0 {
t.Errorf("got %v, want %v", result, tt.expected)
}
})
}
return b
}

func TestTransformer_FromList(t *testing.T) {
func TestCalculateBlockUncleReward(t *testing.T) {
tests := []struct {
name string
want TransformFunc
wantErr bool
name string
block *types.Eth1Block
chainID string
expected *big.Int
}{
{
name: "TransformBlock",
want: TransformBlock,
},
{
name: "TransformTx",
want: TransformTx,
name: "no uncles",
block: &types.Eth1Block{
Uncles: []*types.Eth1Block{},
},
chainID: "1",
expected: big.NewInt(0),
},
{
name: "TransformBlobTx",
want: TransformBlob,
name: "one uncle",
block: &types.Eth1Block{
Number: 10,
Difficulty: big.NewInt(100).Bytes(),
Uncles: []*types.Eth1Block{
{
Number: 1,
},
},
},
chainID: "1",
expected: new(big.Int).Div(eth1BlockReward("1", 10, big.NewInt(100).Bytes()), big.NewInt(32)),
},
{
name: "TransformItx",
want: TransformITx,
name: "two uncles",
block: &types.Eth1Block{
Number: 10,
Difficulty: big.NewInt(100).Bytes(),
Uncles: []*types.Eth1Block{
{
Number: 1,
},
{
Number: 2,
},
},
},
chainID: "1",
expected: new(big.Int).Mul(big.NewInt(2), new(big.Int).Div(eth1BlockReward("1", 10, big.NewInt(100).Bytes()), big.NewInt(32))),
},
{
name: "TransformERC20",
want: TransformERC20,
name: "no uncle rewards",
block: &types.Eth1Block{
Number: 10,
Difficulty: []byte{},
Uncles: []*types.Eth1Block{
{
Number: 1,
},
},
},
chainID: "1",
expected: big.NewInt(0),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := calculateBlockUncleReward(tt.block, tt.chainID)
if result.Cmp(tt.expected) != 0 {
t.Errorf("got %v, want %v", result, tt.expected)
}
})
}
}

func TestCalculateUncleReward(t *testing.T) {
tests := []struct {
name string
block *types.Eth1Block
uncle *types.Eth1Block
chainID string
expected *big.Int
}{
{
name: "TransformERC721",
want: TransformERC721,
name: "no uncles",
block: &types.Eth1Block{
Uncles: []*types.Eth1Block{},
},
chainID: "1",
expected: big.NewInt(0),
},
{
name: "TransformERC1155",
want: TransformERC1155,
name: "one uncle",
block: &types.Eth1Block{
Number: 10,
Difficulty: big.NewInt(100).Bytes(),
},
uncle: &types.Eth1Block{
Number: 1,
},
chainID: "1",
expected: new(big.Int).Div(eth1BlockReward("1", 10, big.NewInt(100).Bytes()), big.NewInt(32)),
},
{
name: "TransformWithdrawals",
want: TransformWithdrawal,
name: "no uncle rewards",
block: &types.Eth1Block{
Number: 10,
Difficulty: []byte{},
},
uncle: &types.Eth1Block{
Number: 1,
},
chainID: "1",
expected: big.NewInt(0),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := calculateUncleReward(tt.block, tt.uncle, tt.chainID)
if result.Cmp(tt.expected) != 0 {
t.Errorf("got %v, want %v", result, tt.expected)
}
})
}
}

func TestVerifyName(t *testing.T) {
tests := []struct {
name string
input string
expected error
}{
{
name: "TransformUncle",
want: TransformUncle,
name: "valid name",
input: "test",
expected: nil,
},
{
name: "TransformEnsNameRegistered",
want: TransformEnsNameRegistered,
name: "empty name",
input: "",
expected: nil,
},
{
name: "TransformContract",
want: TransformContract,
name: "maximum length name",
input: string(make([]byte, 2048)),
expected: nil,
},
{
name: "invalid",
wantErr: true,
name: "name too long",
input: string(make([]byte, 2049)),
expected: fmt.Errorf("name too long: %v", string(make([]byte, 2049))),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := TransformerFromList([]string{tt.name})
if err != nil {
if tt.wantErr {
return
}
t.Errorf("got %v, want nil", err)
}
if got, want := got[0], tt.want; reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
result := verifyName(tt.input)
if (result == nil && tt.expected != nil) || (result != nil && tt.expected == nil) {
t.Errorf("got %v, want %v", result, tt.expected)
} else if result != nil && tt.expected != nil && result.Error() != tt.expected.Error() {
t.Errorf("got %v, want %v", result, tt.expected)
}
})
}
Expand Down

0 comments on commit 07c7784

Please sign in to comment.