Skip to content

Commit

Permalink
Add pretty printer for Substates (#919)
Browse files Browse the repository at this point in the history
  • Loading branch information
HerbertJordan authored Dec 15, 2023
1 parent dde9848 commit 5a9b81f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions state/proxy/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,12 @@ func (s *loggingVmStateDb) Prepare(thash common.Hash, ti int) {

func (s *LoggingStateDb) PrepareSubstate(substate *substate.SubstateAlloc, block uint64) {
s.state.PrepareSubstate(substate, block)
s.writeLog("PrepareSubstate, %v", substate)
s.writeLog("PrepareSubstate, %v", PrettySubstateAlloc(*substate))
}

func (s *loggingVmStateDb) GetSubstatePostAlloc() substate.SubstateAlloc {
res := s.db.GetSubstatePostAlloc()
s.writeLog("GetSubstatePostAlloc, %v", res)
s.writeLog("GetSubstatePostAlloc, %v", PrettySubstateAlloc(res))
return res
}

Expand Down
52 changes: 52 additions & 0 deletions state/proxy/substate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package proxy

import (
"fmt"
"sort"
"strings"

substate "github.com/Fantom-foundation/Substate"
"github.com/ethereum/go-ethereum/common"
)

// PrettySubstateAlloc is a wrapper over a SubstateAlloc that adds human
// readable, stable --and thus diff-able -- pretty printing to it.
type PrettySubstateAlloc substate.SubstateAlloc

func (a PrettySubstateAlloc) String() string {
builder := strings.Builder{}
builder.WriteString(fmt.Sprintf("SubstateAlloc{\n\tsize: %d\n", len(a)))
keys := []common.Address{}
for key := range a {
keys = append(keys, key)
}
sort.Slice(keys, func(i, j int) bool { return keys[i].String() < keys[j].String() })

builder.WriteString("\tAccounts:\n")
for _, key := range keys {
builder.WriteString(fmt.Sprintf("\t\t%x: %v\n", key, prettySubstateAccount{a[key]}))
}
builder.WriteString("}")
return builder.String()
}

type prettySubstateAccount struct {
*substate.SubstateAccount
}

func (a prettySubstateAccount) String() string {
builder := strings.Builder{}
builder.WriteString(fmt.Sprintf("Account{\n\t\t\tnonce: %d\n\t\t\tbalance %v\n", a.Nonce, a.Balance))

builder.WriteString("\t\t\tStorage{\n")
keys := []common.Hash{}
for key := range a.Storage {
keys = append(keys, key)
}
sort.Slice(keys, func(i, j int) bool { return keys[i].String() < keys[j].String() })
for _, key := range keys {
builder.WriteString(fmt.Sprintf("\t\t\t\t%v=%v\n", key, a.Storage[key]))
}
builder.WriteString("\t\t\t}\n\t\t}")
return builder.String()
}

0 comments on commit 5a9b81f

Please sign in to comment.