Skip to content

all: use fmt.Appendf instead of fmt.Sprintf where possible #2304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/math/big.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (i *HexOrDecimal256) MarshalText() ([]byte, error) {
if i == nil {
return []byte("0x0"), nil
}
return []byte(fmt.Sprintf("%#x", (*big.Int)(i))), nil
return fmt.Appendf(nil, "%#x", (*big.Int)(i)), nil
}

// Decimal256 unmarshals big.Int as a decimal string. When unmarshalling,
Expand Down
2 changes: 1 addition & 1 deletion common/math/integer.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (i *HexOrDecimal64) UnmarshalText(input []byte) error {

// MarshalText implements encoding.TextMarshaler.
func (i HexOrDecimal64) MarshalText() ([]byte, error) {
return []byte(fmt.Sprintf("%#x", uint64(i))), nil
return fmt.Appendf(nil, "%#x", uint64(i)), nil
}

// ParseUint64 parses s as an integer in decimal or hexadecimal syntax.
Expand Down
116 changes: 108 additions & 8 deletions core/state/snapshot/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,27 +211,27 @@ func TestAccountIteratorTraversalValues(t *testing.T) {
h = make(map[common.Hash][]byte)
)
for i := byte(2); i < 0xff; i++ {
a[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 0, i))
a[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 0, i)
if i > 20 && i%2 == 0 {
b[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 1, i))
b[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 1, i)
}
if i%4 == 0 {
c[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 2, i))
c[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 2, i)
}
if i%7 == 0 {
d[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 3, i))
d[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 3, i)
}
if i%8 == 0 {
e[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 4, i))
e[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 4, i)
}
if i > 50 || i < 85 {
f[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 5, i))
f[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 5, i)
}
if i%64 == 0 {
g[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 6, i))
g[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 6, i)
}
if i%128 == 0 {
h[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 7, i))
h[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 7, i)
}
}
// Assemble a stack of snapshots from the account layers
Expand All @@ -258,6 +258,106 @@ func TestAccountIteratorTraversalValues(t *testing.T) {
t.Fatalf("hash %x: account mismatch: have %x, want %x", hash, have, want)
}
}
it.Release()
}

func TestStorageIteratorTraversalValues(t *testing.T) {
// Create an empty base layer and a snapshot tree out of it
base := &diskLayer{
diskdb: rawdb.NewMemoryDatabase(),
root: common.HexToHash("0x01"),
cache: fastcache.New(1024 * 500),
}
snaps := &Tree{
layers: map[common.Hash]snapshot{
base.root: base,
},
}
wrapStorage := func(storage map[common.Hash][]byte) map[common.Hash]map[common.Hash][]byte {
return map[common.Hash]map[common.Hash][]byte{
common.HexToHash("0xaa"): storage,
}
}
// Create a batch of storage sets to seed subsequent layers with
var (
a = make(map[common.Hash][]byte)
b = make(map[common.Hash][]byte)
c = make(map[common.Hash][]byte)
d = make(map[common.Hash][]byte)
e = make(map[common.Hash][]byte)
f = make(map[common.Hash][]byte)
g = make(map[common.Hash][]byte)
h = make(map[common.Hash][]byte)
)
for i := byte(2); i < 0xff; i++ {
a[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 0, i)
if i > 20 && i%2 == 0 {
b[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 1, i)
}
if i%4 == 0 {
c[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 2, i)
}
if i%7 == 0 {
d[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 3, i)
}
if i%8 == 0 {
e[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 4, i)
}
if i > 50 || i < 85 {
f[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 5, i)
}
if i%64 == 0 {
g[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 6, i)
}
if i%128 == 0 {
h[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 7, i)
}
}
// Assemble a stack of snapshots from the account layers
snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil, randomAccountSet("0xaa"), wrapStorage(a))
snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil, randomAccountSet("0xaa"), wrapStorage(b))
snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), nil, randomAccountSet("0xaa"), wrapStorage(c))
snaps.Update(common.HexToHash("0x05"), common.HexToHash("0x04"), nil, randomAccountSet("0xaa"), wrapStorage(d))
snaps.Update(common.HexToHash("0x06"), common.HexToHash("0x05"), nil, randomAccountSet("0xaa"), wrapStorage(e))
snaps.Update(common.HexToHash("0x07"), common.HexToHash("0x06"), nil, randomAccountSet("0xaa"), wrapStorage(e))
snaps.Update(common.HexToHash("0x08"), common.HexToHash("0x07"), nil, randomAccountSet("0xaa"), wrapStorage(g))
snaps.Update(common.HexToHash("0x09"), common.HexToHash("0x08"), nil, randomAccountSet("0xaa"), wrapStorage(h))

it, _ := snaps.StorageIterator(common.HexToHash("0x09"), common.HexToHash("0xaa"), common.Hash{})
head := snaps.Snapshot(common.HexToHash("0x09"))
for it.Next() {
hash := it.Hash()
want, err := head.Storage(common.HexToHash("0xaa"), hash)
if err != nil {
t.Fatalf("failed to retrieve expected storage slot: %v", err)
}
if have := it.Slot(); !bytes.Equal(want, have) {
t.Fatalf("hash %x: slot mismatch: have %x, want %x", hash, have, want)
}
}
it.Release()

// Test after persist some bottom-most layers into the disk,
// the functionalities still work.
limit := aggregatorMemoryLimit
defer func() {
aggregatorMemoryLimit = limit
}()
aggregatorMemoryLimit = 0 // Force pushing the bottom-most layer into disk
snaps.Cap(common.HexToHash("0x09"), 2)

it, _ = snaps.StorageIterator(common.HexToHash("0x09"), common.HexToHash("0xaa"), common.Hash{})
for it.Next() {
hash := it.Hash()
want, err := head.Storage(common.HexToHash("0xaa"), hash)
if err != nil {
t.Fatalf("failed to retrieve expected slot: %v", err)
}
if have := it.Slot(); !bytes.Equal(want, have) {
t.Fatalf("hash %x: slot mismatch: have %x, want %x", hash, have, want)
}
}
it.Release()
}

// This testcase is notorious, all layers contain the exact same 200 accounts.
Expand Down
2 changes: 1 addition & 1 deletion p2p/nat/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ type ExtIP net.IP

func (n ExtIP) ExternalIP() (net.IP, error) { return net.IP(n), nil }
func (n ExtIP) String() string { return fmt.Sprintf("ExtIP(%v)", net.IP(n)) }
func (n ExtIP) MarshalText() ([]byte, error) { return []byte(fmt.Sprintf("extip:%v", net.IP(n))), nil }
func (n ExtIP) MarshalText() ([]byte, error) { return fmt.Appendf(nil, "extip:%v", net.IP(n)), nil }

// These do nothing.

Expand Down
2 changes: 1 addition & 1 deletion p2p/nat/natpmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (n *pmp) DeleteMapping(protocol string, extport, intport int) (err error) {
}

func (n *pmp) MarshalText() ([]byte, error) {
return []byte(fmt.Sprintf("natpmp:%v", n.gw)), nil
return fmt.Appendf(nil, "natpmp:%v", n.gw), nil
}

func discoverPMP() Interface {
Expand Down