eth/tracers: various small nits

This commit is contained in:
Martin Holst Swende 2024-11-20 12:33:06 +01:00
parent f9a1c7bf54
commit 8296be8909
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
3 changed files with 61 additions and 79 deletions

View file

@ -26,7 +26,6 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
"unicode"
"github.com/ethereum/go-ethereum/tests" "github.com/ethereum/go-ethereum/tests"
) )
@ -62,7 +61,6 @@ func TestSupplyTracerBlockchain(t *testing.T) {
if !strings.HasSuffix(file.Name(), ".json") { if !strings.HasSuffix(file.Name(), ".json") {
continue continue
} }
file := file // capture range variable
var testcases map[string]*blockTest var testcases map[string]*blockTest
var blob []byte var blob []byte
// Tracer test found, read if from disk // Tracer test found, read if from disk
@ -72,8 +70,14 @@ func TestSupplyTracerBlockchain(t *testing.T) {
if err := json.Unmarshal(blob, &testcases); err != nil { if err := json.Unmarshal(blob, &testcases); err != nil {
t.Fatalf("failed to parse testcase %s: %v", file.Name(), err) t.Fatalf("failed to parse testcase %s: %v", file.Name(), err)
} }
for testname, test := range testcases { for testname, blockTest := range testcases {
t.Run(fmt.Sprintf("%s/%s", camel(strings.TrimSuffix(file.Name(), ".json")), testname), func(t *testing.T) { t.Run(fmt.Sprintf("%s/%s", file.Name(), testname),
func(t *testing.T) { runBlockTest(t, blockTest) })
}
}
}
func runBlockTest(t *testing.T, test *blockTest) {
t.Parallel() t.Parallel()
traceOutputPath := filepath.ToSlash(t.TempDir()) traceOutputPath := filepath.ToSlash(t.TempDir())
@ -98,9 +102,8 @@ func TestSupplyTracerBlockchain(t *testing.T) {
scanner = bufio.NewScanner(file) scanner = bufio.NewScanner(file)
) )
for scanner.Scan() { for scanner.Scan() {
blockBytes := scanner.Bytes()
var info supplyInfo var info supplyInfo
if err := json.Unmarshal(blockBytes, &info); err != nil { if err := json.Unmarshal(scanner.Bytes(), &info); err != nil {
t.Fatalf("failed to unmarshal result: %v", err) t.Fatalf("failed to unmarshal result: %v", err)
} }
output = append(output, info) output = append(output, info)
@ -112,18 +115,6 @@ func TestSupplyTracerBlockchain(t *testing.T) {
for i, expected := range test.Expected { for i, expected := range test.Expected {
compareAsJSON(t, expected, output[i]) compareAsJSON(t, expected, output[i])
} }
})
}
}
}
// camel converts a snake cased input string into a camel cased output.
func camel(str string) string {
pieces := strings.Split(str, "_")
for i := 1; i < len(pieces); i++ {
pieces[i] = string(unicode.ToUpper(rune(pieces[i][0]))) + pieces[i][1:]
}
return strings.Join(pieces, "")
} }
func compareAsJSON(t *testing.T, expected interface{}, actual interface{}) { func compareAsJSON(t *testing.T, expected interface{}, actual interface{}) {
@ -136,6 +127,6 @@ func compareAsJSON(t *testing.T, expected interface{}, actual interface{}) {
t.Fatalf("failed to marshal actual value to JSON: %v", err) t.Fatalf("failed to marshal actual value to JSON: %v", err)
} }
if !bytes.Equal(want, have) { if !bytes.Equal(want, have) {
t.Fatalf("incorrect supply info:\nexpected:\n%s\ngot:\n%s", string(want), string(have)) t.Fatalf("incorrect supply info:\nhave: %s\nwant: %s", string(have), string(want))
} }
} }

View file

@ -44,43 +44,43 @@ const (
func (f Fork) String() string { func (f Fork) String() string {
switch f { switch f {
case Prague: case Prague:
return "prague" return "Prague"
case Cancun: case Cancun:
return "cancun" return "Cancun"
case Shanghai: case Shanghai:
return "shanghai" return "Shanghai"
case Paris: case Paris:
return "paris" return "Paris"
case GrayGlacier: case GrayGlacier:
return "grayGlacier" return "GrayGlacier"
case ArrowGlacier: case ArrowGlacier:
return "arrowGlacier" return "ArrowGlacier"
case London: case London:
return "london" return "London"
case Berlin: case Berlin:
return "berlin" return "Berlin"
case MuirGlacier: case MuirGlacier:
return "muirGlacier" return "MuirGlacier"
case Istanbul: case Istanbul:
return "istanbul" return "Istanbul"
case Petersburg: case Petersburg:
return "petersburg" return "Petersburg"
case Constantinople: case Constantinople:
return "constantinople" return "Constantinople"
case Byzantium: case Byzantium:
return "byzantium" return "Byzantium"
case SpuriousDragon: case SpuriousDragon:
return "spuriousDragon" return "SpuriousDragon"
case TangerineWhistle: case TangerineWhistle:
return "tangerineWhistle" return "TangerineWhistle"
case DAO: case DAO:
return "dao" return "Dao"
case Homestead: case Homestead:
return "homestead" return "Homestead"
case FrontierThawing: case FrontierThawing:
return "frontierThawing" return "FrontierThawing"
case Frontier: case Frontier:
return "frontier" return "Frontier"
default: default:
panic("unknown fork") panic("unknown fork")
} }

View file

@ -26,7 +26,6 @@ import (
"math/big" "math/big"
"os" "os"
"reflect" "reflect"
"strings"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
@ -89,7 +88,7 @@ func FromChain(db ethdb.Database, chain *core.BlockChain, post *types.GenesisAll
post = &types.GenesisAlloc{} post = &types.GenesisAlloc{}
} }
bt.json = btJSON{ bt.json = btJSON{
Network: capitalize(chain.Config().LatestFork(head.Number, head.Time).String()), Network: chain.Config().LatestFork(head.Number, head.Time).String(),
Blocks: blocks, Blocks: blocks,
Genesis: FromHeader(chain.Genesis().Header()), Genesis: FromHeader(chain.Genesis().Header()),
Pre: alloc, Pre: alloc,
@ -466,11 +465,3 @@ func (bb *btBlock) decode() (*types.Block, error) {
err = rlp.DecodeBytes(data, &b) err = rlp.DecodeBytes(data, &b)
return &b, err return &b, err
} }
func capitalize(s string) string {
// Check if the string is empty
if len(s) == 0 {
return s
}
return strings.ToUpper(string(s[0])) + s[1:]
}