Skip to content

Commit 4f52647

Browse files
committed
test: add tests for calculateMevFromBlock, calculateBlockUncleReward, calculateUncleReward & verifyName methods
1 parent b2e9786 commit 4f52647

File tree

1 file changed

+219
-0
lines changed

1 file changed

+219
-0
lines changed

backend/pkg/executionlayer/transformer_test.go

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package executionlayer
22

33
import (
44
"bytes"
5+
"fmt"
56
"math/big"
67
"reflect"
78
"testing"
@@ -1958,3 +1959,221 @@ func TestCalculateTxFee(t *testing.T) {
19581959
})
19591960
}
19601961
}
1962+
1963+
func TestCalculateMevFromBlock(t *testing.T) {
1964+
tests := []struct {
1965+
name string
1966+
block *types.Eth1Block
1967+
expected *big.Int
1968+
}{
1969+
{
1970+
name: "no MEV",
1971+
block: &types.Eth1Block{
1972+
Coinbase: []byte("coinbase"),
1973+
Transactions: []*types.Eth1Transaction{
1974+
{
1975+
Itx: []*types.Eth1InternalTransaction{
1976+
{
1977+
From: alice,
1978+
To: common.Address{}.Bytes(),
1979+
Value: big.NewInt(100).Bytes(),
1980+
},
1981+
},
1982+
},
1983+
},
1984+
},
1985+
expected: big.NewInt(0),
1986+
},
1987+
{
1988+
name: "MEV from one transaction",
1989+
block: &types.Eth1Block{
1990+
Coinbase: []byte("coinbase"),
1991+
Transactions: []*types.Eth1Transaction{
1992+
{
1993+
Itx: []*types.Eth1InternalTransaction{
1994+
{
1995+
From: alice,
1996+
To: []byte("coinbase"),
1997+
Value: big.NewInt(100).Bytes(),
1998+
},
1999+
},
2000+
},
2001+
},
2002+
},
2003+
expected: big.NewInt(100),
2004+
},
2005+
}
2006+
2007+
for _, tt := range tests {
2008+
t.Run(tt.name, func(t *testing.T) {
2009+
result := calculateMevFromBlock(tt.block)
2010+
if result.Cmp(tt.expected) != 0 {
2011+
t.Errorf("got %v, want %v", result, tt.expected)
2012+
}
2013+
})
2014+
}
2015+
}
2016+
2017+
func TestCalculateBlockUncleReward(t *testing.T) {
2018+
tests := []struct {
2019+
name string
2020+
block *types.Eth1Block
2021+
chainID string
2022+
expected *big.Int
2023+
}{
2024+
{
2025+
name: "no uncles",
2026+
block: &types.Eth1Block{
2027+
Uncles: []*types.Eth1Block{},
2028+
},
2029+
chainID: "1",
2030+
expected: big.NewInt(0),
2031+
},
2032+
{
2033+
name: "one uncle",
2034+
block: &types.Eth1Block{
2035+
Number: 10,
2036+
Difficulty: big.NewInt(100).Bytes(),
2037+
Uncles: []*types.Eth1Block{
2038+
{
2039+
Number: 1,
2040+
},
2041+
},
2042+
},
2043+
chainID: "1",
2044+
expected: new(big.Int).Div(eth1BlockReward("1", 10, big.NewInt(100).Bytes()), big.NewInt(32)),
2045+
},
2046+
{
2047+
name: "two uncles",
2048+
block: &types.Eth1Block{
2049+
Number: 10,
2050+
Difficulty: big.NewInt(100).Bytes(),
2051+
Uncles: []*types.Eth1Block{
2052+
{
2053+
Number: 1,
2054+
},
2055+
{
2056+
Number: 2,
2057+
},
2058+
},
2059+
},
2060+
chainID: "1",
2061+
expected: new(big.Int).Mul(big.NewInt(2), new(big.Int).Div(eth1BlockReward("1", 10, big.NewInt(100).Bytes()), big.NewInt(32))),
2062+
},
2063+
{
2064+
name: "no uncle rewards",
2065+
block: &types.Eth1Block{
2066+
Number: 10,
2067+
Difficulty: []byte{},
2068+
Uncles: []*types.Eth1Block{
2069+
{
2070+
Number: 1,
2071+
},
2072+
},
2073+
},
2074+
chainID: "1",
2075+
expected: big.NewInt(0),
2076+
},
2077+
}
2078+
2079+
for _, tt := range tests {
2080+
t.Run(tt.name, func(t *testing.T) {
2081+
result := calculateBlockUncleReward(tt.block, tt.chainID)
2082+
if result.Cmp(tt.expected) != 0 {
2083+
t.Errorf("got %v, want %v", result, tt.expected)
2084+
}
2085+
})
2086+
}
2087+
}
2088+
2089+
func TestCalculateUncleReward(t *testing.T) {
2090+
tests := []struct {
2091+
name string
2092+
block *types.Eth1Block
2093+
uncle *types.Eth1Block
2094+
chainID string
2095+
expected *big.Int
2096+
}{
2097+
{
2098+
name: "no uncles",
2099+
block: &types.Eth1Block{
2100+
Uncles: []*types.Eth1Block{},
2101+
},
2102+
chainID: "1",
2103+
expected: big.NewInt(0),
2104+
},
2105+
{
2106+
name: "one uncle",
2107+
block: &types.Eth1Block{
2108+
Number: 10,
2109+
Difficulty: big.NewInt(100).Bytes(),
2110+
},
2111+
uncle: &types.Eth1Block{
2112+
Number: 1,
2113+
},
2114+
chainID: "1",
2115+
expected: new(big.Int).Div(eth1BlockReward("1", 10, big.NewInt(100).Bytes()), big.NewInt(32)),
2116+
},
2117+
{
2118+
name: "no uncle rewards",
2119+
block: &types.Eth1Block{
2120+
Number: 10,
2121+
Difficulty: []byte{},
2122+
},
2123+
uncle: &types.Eth1Block{
2124+
Number: 1,
2125+
},
2126+
chainID: "1",
2127+
expected: big.NewInt(0),
2128+
},
2129+
}
2130+
2131+
for _, tt := range tests {
2132+
t.Run(tt.name, func(t *testing.T) {
2133+
result := calculateUncleReward(tt.block, tt.uncle, tt.chainID)
2134+
if result.Cmp(tt.expected) != 0 {
2135+
t.Errorf("got %v, want %v", result, tt.expected)
2136+
}
2137+
})
2138+
}
2139+
}
2140+
2141+
func TestVerifyName(t *testing.T) {
2142+
tests := []struct {
2143+
name string
2144+
input string
2145+
expected error
2146+
}{
2147+
{
2148+
name: "valid name",
2149+
input: "test",
2150+
expected: nil,
2151+
},
2152+
{
2153+
name: "empty name",
2154+
input: "",
2155+
expected: nil,
2156+
},
2157+
{
2158+
name: "maximum length name",
2159+
input: string(make([]byte, 2048)),
2160+
expected: nil,
2161+
},
2162+
{
2163+
name: "name too long",
2164+
input: string(make([]byte, 2049)),
2165+
expected: fmt.Errorf("name too long: %v", string(make([]byte, 2049))),
2166+
},
2167+
}
2168+
2169+
for _, tt := range tests {
2170+
t.Run(tt.name, func(t *testing.T) {
2171+
result := verifyName(tt.input)
2172+
if (result == nil && tt.expected != nil) || (result != nil && tt.expected == nil) {
2173+
t.Errorf("got %v, want %v", result, tt.expected)
2174+
} else if result != nil && tt.expected != nil && result.Error() != tt.expected.Error() {
2175+
t.Errorf("got %v, want %v", result, tt.expected)
2176+
}
2177+
})
2178+
}
2179+
}

0 commit comments

Comments
 (0)