eth/catalyst: improve test

This commit is contained in:
Felix Lange 2024-09-12 20:56:24 +02:00
parent 54ac02bf54
commit c6ba1dcd51

View file

@ -20,10 +20,12 @@ import (
"bytes" "bytes"
"context" "context"
crand "crypto/rand" crand "crypto/rand"
"errors"
"fmt" "fmt"
"math/big" "math/big"
"math/rand" "math/rand"
"reflect" "reflect"
"slices"
"sync" "sync"
"testing" "testing"
"time" "time"
@ -1416,8 +1418,8 @@ func TestGetBlockBodiesByHash(t *testing.T) {
for k, test := range tests { for k, test := range tests {
result := api.GetPayloadBodiesByHashV2(test.hashes) result := api.GetPayloadBodiesByHashV2(test.hashes)
for i, r := range result { for i, r := range result {
if !equalBody(test.results[i], r) { if err := checkEqualBody(test.results[i], r); err != nil {
t.Fatalf("test %v: invalid response: expected %+v got %+v", k, test.results[i], r) t.Fatalf("test %v: invalid response: %v\nexpected %+v\ngot %+v", k, err, test.results[i], r)
} }
} }
} }
@ -1494,8 +1496,8 @@ func TestGetBlockBodiesByRange(t *testing.T) {
} }
if len(result) == len(test.results) { if len(result) == len(test.results) {
for i, r := range result { for i, r := range result {
if !equalBody(test.results[i], r) { if err := checkEqualBody(test.results[i], r); err != nil {
t.Fatalf("test %d: invalid response: expected \n%+v\ngot\n%+v", k, test.results[i], r) t.Fatalf("test %d: invalid response: %v\nexpected %+v\ngot %+v", k, err, test.results[i], r)
} }
} }
} else { } else {
@ -1549,33 +1551,32 @@ func TestGetBlockBodiesByRangeInvalidParams(t *testing.T) {
} }
} }
func equalBody(a *types.Body, b *engine.ExecutionPayloadBody) bool { func checkEqualBody(a *types.Body, b *engine.ExecutionPayloadBody) error {
if a == nil && b == nil { if a == nil && b == nil {
return true return nil
} else if a == nil || b == nil { } else if a == nil || b == nil {
return false return errors.New("nil vs. non-nil")
} }
if len(a.Transactions) != len(b.TransactionData) { if len(a.Transactions) != len(b.TransactionData) {
return false return errors.New("transactions length mismatch")
} }
for i, tx := range a.Transactions { for i, tx := range a.Transactions {
data, _ := tx.MarshalBinary() data, _ := tx.MarshalBinary()
if !bytes.Equal(data, b.TransactionData[i]) { if !bytes.Equal(data, b.TransactionData[i]) {
return false return fmt.Errorf("transaction %d mismatch", i)
} }
} }
if !reflect.DeepEqual(a.Withdrawals, b.Withdrawals) { if !reflect.DeepEqual(a.Withdrawals, b.Withdrawals) {
return false return fmt.Errorf("withdrawals mismatch")
} }
var requests [][]byte reqEqual := slices.EqualFunc(a.Requests, b.Requests, func(a []byte, b hexutil.Bytes) bool {
if a.Requests != nil { return bytes.Equal(a, b)
// If requests is non-nil, it means requests are available in block and we })
// should return an empty slice instead of nil if there are no deposits. if !reqEqual {
requests = make([][]byte, 0) return fmt.Errorf("requests mismatch")
} }
return reflect.DeepEqual(requests, b.Requests) return nil
} }
func TestBlockToPayloadWithBlobs(t *testing.T) { func TestBlockToPayloadWithBlobs(t *testing.T) {