Skip to content

Commit

Permalink
eth/tracers: various small nits
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Nov 20, 2024
1 parent e6463e2 commit 36fb2cf
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 79 deletions.
91 changes: 41 additions & 50 deletions eth/tracers/live/supply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"path/filepath"
"strings"
"testing"
"unicode"

"github.com/ethereum/go-ethereum/tests"
)
Expand Down Expand Up @@ -62,7 +61,6 @@ func TestSupplyTracerBlockchain(t *testing.T) {
if !strings.HasSuffix(file.Name(), ".json") {
continue
}
file := file // capture range variable
var testcases map[string]*blockTest
var blob []byte
// Tracer test found, read if from disk
Expand All @@ -72,58 +70,51 @@ func TestSupplyTracerBlockchain(t *testing.T) {
if err := json.Unmarshal(blob, &testcases); err != nil {
t.Fatalf("failed to parse testcase %s: %v", file.Name(), err)
}
for testname, test := range testcases {
t.Run(fmt.Sprintf("%s/%s", camel(strings.TrimSuffix(file.Name(), ".json")), testname), func(t *testing.T) {
t.Parallel()

traceOutputPath := filepath.ToSlash(t.TempDir())
traceOutputFilename := path.Join(traceOutputPath, "supply.jsonl")
// Load supply tracer
tracer, err := newSupply(json.RawMessage(fmt.Sprintf(`{"path":"%s"}`, traceOutputPath)))
if err != nil {
t.Fatalf("failed to create tracer: %v", err)
}
if err := test.bt.Run(false, "path", false, tracer, nil); err != nil {
t.Errorf("failed to run test: %v\n", err)
}
// Check and compare the results
file, err := os.OpenFile(traceOutputFilename, os.O_RDONLY, 0666)
if err != nil {
t.Fatalf("failed to open output file: %v", err)
}
defer file.Close()

var (
output []supplyInfo
scanner = bufio.NewScanner(file)
)
for scanner.Scan() {
blockBytes := scanner.Bytes()
var info supplyInfo
if err := json.Unmarshal(blockBytes, &info); err != nil {
t.Fatalf("failed to unmarshal result: %v", err)
}
output = append(output, info)
}
if len(output) != len(test.Expected) {
fmt.Printf("output: %v\n", output)
t.Fatalf("expected %d supply infos, got %d", len(test.Expected), len(output))
}
for i, expected := range test.Expected {
compareAsJSON(t, expected, output[i])
}
})
for testname, blockTest := range testcases {
t.Run(fmt.Sprintf("%s/%s", file.Name(), testname),
func(t *testing.T) { runBlockTest(t, blockTest) })
}
}
}

// 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:]
func runBlockTest(t *testing.T, test *blockTest) {
t.Parallel()

traceOutputPath := filepath.ToSlash(t.TempDir())
traceOutputFilename := path.Join(traceOutputPath, "supply.jsonl")
// Load supply tracer
tracer, err := newSupply(json.RawMessage(fmt.Sprintf(`{"path":"%s"}`, traceOutputPath)))
if err != nil {
t.Fatalf("failed to create tracer: %v", err)
}
if err := test.bt.Run(false, "path", false, tracer, nil); err != nil {
t.Errorf("failed to run test: %v\n", err)
}
// Check and compare the results
file, err := os.OpenFile(traceOutputFilename, os.O_RDONLY, 0666)
if err != nil {
t.Fatalf("failed to open output file: %v", err)
}
defer file.Close()

var (
output []supplyInfo
scanner = bufio.NewScanner(file)
)
for scanner.Scan() {
var info supplyInfo
if err := json.Unmarshal(scanner.Bytes(), &info); err != nil {
t.Fatalf("failed to unmarshal result: %v", err)
}
output = append(output, info)
}
if len(output) != len(test.Expected) {
fmt.Printf("output: %v\n", output)
t.Fatalf("expected %d supply infos, got %d", len(test.Expected), len(output))
}
for i, expected := range test.Expected {
compareAsJSON(t, expected, output[i])
}
return strings.Join(pieces, "")
}

func compareAsJSON(t *testing.T, expected interface{}, actual interface{}) {
Expand All @@ -136,6 +127,6 @@ func compareAsJSON(t *testing.T, expected interface{}, actual interface{}) {
t.Fatalf("failed to marshal actual value to JSON: %v", err)
}
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))
}
}
38 changes: 19 additions & 19 deletions params/forks/forks.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,43 +44,43 @@ const (
func (f Fork) String() string {
switch f {
case Prague:
return "prague"
return "Prague"
case Cancun:
return "cancun"
return "Cancun"
case Shanghai:
return "shanghai"
return "Shanghai"
case Paris:
return "paris"
return "Paris"
case GrayGlacier:
return "grayGlacier"
return "GrayGlacier"
case ArrowGlacier:
return "arrowGlacier"
return "ArrowGlacier"
case London:
return "london"
return "London"
case Berlin:
return "berlin"
return "Berlin"
case MuirGlacier:
return "muirGlacier"
return "MuirGlacier"
case Istanbul:
return "istanbul"
return "Istanbul"
case Petersburg:
return "petersburg"
return "Petersburg"
case Constantinople:
return "constantinople"
return "Constantinople"
case Byzantium:
return "byzantium"
return "Byzantium"
case SpuriousDragon:
return "spuriousDragon"
return "SpuriousDragon"
case TangerineWhistle:
return "tangerineWhistle"
return "TangerineWhistle"
case DAO:
return "dao"
return "Dao"
case Homestead:
return "homestead"
return "Homestead"
case FrontierThawing:
return "frontierThawing"
return "FrontierThawing"
case Frontier:
return "frontier"
return "Frontier"
default:
panic("unknown fork")
}
Expand Down
11 changes: 1 addition & 10 deletions tests/block_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"math/big"
"os"
"reflect"
"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -88,7 +87,7 @@ func FromChain(db ethdb.Database, chain *core.BlockChain, post *types.GenesisAll
post = &types.GenesisAlloc{}
}
bt.json = btJSON{
Network: capitalize(chain.Config().LatestFork(head.Number, head.Time).String()),
Network: chain.Config().LatestFork(head.Number, head.Time).String(),
Blocks: blocks,
Genesis: FromHeader(chain.Genesis().Header()),
Pre: alloc,
Expand Down Expand Up @@ -460,11 +459,3 @@ func (bb *btBlock) decode() (*types.Block, error) {
err = rlp.DecodeBytes(data, &b)
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:]
}

0 comments on commit 36fb2cf

Please sign in to comment.