Skip to content

Commit 32b58b4

Browse files
authored
Merge pull request #2304 from CortexFoundation/dev
all: use fmt.Appendf instead of fmt.Sprintf where possible
2 parents 9452576 + 32773cf commit 32b58b4

File tree

5 files changed

+112
-12
lines changed

5 files changed

+112
-12
lines changed

common/math/big.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (i *HexOrDecimal256) MarshalText() ([]byte, error) {
6868
if i == nil {
6969
return []byte("0x0"), nil
7070
}
71-
return []byte(fmt.Sprintf("%#x", (*big.Int)(i))), nil
71+
return fmt.Appendf(nil, "%#x", (*big.Int)(i)), nil
7272
}
7373

7474
// Decimal256 unmarshals big.Int as a decimal string. When unmarshalling,

common/math/integer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (i *HexOrDecimal64) UnmarshalText(input []byte) error {
4848

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

5454
// ParseUint64 parses s as an integer in decimal or hexadecimal syntax.

core/state/snapshot/iterator_test.go

Lines changed: 108 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,27 +211,27 @@ func TestAccountIteratorTraversalValues(t *testing.T) {
211211
h = make(map[common.Hash][]byte)
212212
)
213213
for i := byte(2); i < 0xff; i++ {
214-
a[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 0, i))
214+
a[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 0, i)
215215
if i > 20 && i%2 == 0 {
216-
b[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 1, i))
216+
b[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 1, i)
217217
}
218218
if i%4 == 0 {
219-
c[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 2, i))
219+
c[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 2, i)
220220
}
221221
if i%7 == 0 {
222-
d[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 3, i))
222+
d[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 3, i)
223223
}
224224
if i%8 == 0 {
225-
e[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 4, i))
225+
e[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 4, i)
226226
}
227227
if i > 50 || i < 85 {
228-
f[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 5, i))
228+
f[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 5, i)
229229
}
230230
if i%64 == 0 {
231-
g[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 6, i))
231+
g[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 6, i)
232232
}
233233
if i%128 == 0 {
234-
h[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 7, i))
234+
h[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 7, i)
235235
}
236236
}
237237
// Assemble a stack of snapshots from the account layers
@@ -258,6 +258,106 @@ func TestAccountIteratorTraversalValues(t *testing.T) {
258258
t.Fatalf("hash %x: account mismatch: have %x, want %x", hash, have, want)
259259
}
260260
}
261+
it.Release()
262+
}
263+
264+
func TestStorageIteratorTraversalValues(t *testing.T) {
265+
// Create an empty base layer and a snapshot tree out of it
266+
base := &diskLayer{
267+
diskdb: rawdb.NewMemoryDatabase(),
268+
root: common.HexToHash("0x01"),
269+
cache: fastcache.New(1024 * 500),
270+
}
271+
snaps := &Tree{
272+
layers: map[common.Hash]snapshot{
273+
base.root: base,
274+
},
275+
}
276+
wrapStorage := func(storage map[common.Hash][]byte) map[common.Hash]map[common.Hash][]byte {
277+
return map[common.Hash]map[common.Hash][]byte{
278+
common.HexToHash("0xaa"): storage,
279+
}
280+
}
281+
// Create a batch of storage sets to seed subsequent layers with
282+
var (
283+
a = make(map[common.Hash][]byte)
284+
b = make(map[common.Hash][]byte)
285+
c = make(map[common.Hash][]byte)
286+
d = make(map[common.Hash][]byte)
287+
e = make(map[common.Hash][]byte)
288+
f = make(map[common.Hash][]byte)
289+
g = make(map[common.Hash][]byte)
290+
h = make(map[common.Hash][]byte)
291+
)
292+
for i := byte(2); i < 0xff; i++ {
293+
a[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 0, i)
294+
if i > 20 && i%2 == 0 {
295+
b[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 1, i)
296+
}
297+
if i%4 == 0 {
298+
c[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 2, i)
299+
}
300+
if i%7 == 0 {
301+
d[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 3, i)
302+
}
303+
if i%8 == 0 {
304+
e[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 4, i)
305+
}
306+
if i > 50 || i < 85 {
307+
f[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 5, i)
308+
}
309+
if i%64 == 0 {
310+
g[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 6, i)
311+
}
312+
if i%128 == 0 {
313+
h[common.Hash{i}] = fmt.Appendf(nil, "layer-%d, key %d", 7, i)
314+
}
315+
}
316+
// Assemble a stack of snapshots from the account layers
317+
snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil, randomAccountSet("0xaa"), wrapStorage(a))
318+
snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil, randomAccountSet("0xaa"), wrapStorage(b))
319+
snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), nil, randomAccountSet("0xaa"), wrapStorage(c))
320+
snaps.Update(common.HexToHash("0x05"), common.HexToHash("0x04"), nil, randomAccountSet("0xaa"), wrapStorage(d))
321+
snaps.Update(common.HexToHash("0x06"), common.HexToHash("0x05"), nil, randomAccountSet("0xaa"), wrapStorage(e))
322+
snaps.Update(common.HexToHash("0x07"), common.HexToHash("0x06"), nil, randomAccountSet("0xaa"), wrapStorage(e))
323+
snaps.Update(common.HexToHash("0x08"), common.HexToHash("0x07"), nil, randomAccountSet("0xaa"), wrapStorage(g))
324+
snaps.Update(common.HexToHash("0x09"), common.HexToHash("0x08"), nil, randomAccountSet("0xaa"), wrapStorage(h))
325+
326+
it, _ := snaps.StorageIterator(common.HexToHash("0x09"), common.HexToHash("0xaa"), common.Hash{})
327+
head := snaps.Snapshot(common.HexToHash("0x09"))
328+
for it.Next() {
329+
hash := it.Hash()
330+
want, err := head.Storage(common.HexToHash("0xaa"), hash)
331+
if err != nil {
332+
t.Fatalf("failed to retrieve expected storage slot: %v", err)
333+
}
334+
if have := it.Slot(); !bytes.Equal(want, have) {
335+
t.Fatalf("hash %x: slot mismatch: have %x, want %x", hash, have, want)
336+
}
337+
}
338+
it.Release()
339+
340+
// Test after persist some bottom-most layers into the disk,
341+
// the functionalities still work.
342+
limit := aggregatorMemoryLimit
343+
defer func() {
344+
aggregatorMemoryLimit = limit
345+
}()
346+
aggregatorMemoryLimit = 0 // Force pushing the bottom-most layer into disk
347+
snaps.Cap(common.HexToHash("0x09"), 2)
348+
349+
it, _ = snaps.StorageIterator(common.HexToHash("0x09"), common.HexToHash("0xaa"), common.Hash{})
350+
for it.Next() {
351+
hash := it.Hash()
352+
want, err := head.Storage(common.HexToHash("0xaa"), hash)
353+
if err != nil {
354+
t.Fatalf("failed to retrieve expected slot: %v", err)
355+
}
356+
if have := it.Slot(); !bytes.Equal(want, have) {
357+
t.Fatalf("hash %x: slot mismatch: have %x, want %x", hash, have, want)
358+
}
359+
}
360+
it.Release()
261361
}
262362

263363
// This testcase is notorious, all layers contain the exact same 200 accounts.

p2p/nat/nat.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ type ExtIP net.IP
140140

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

145145
// These do nothing.
146146

p2p/nat/natpmp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (n *pmp) DeleteMapping(protocol string, extport, intport int) (err error) {
7070
}
7171

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

7676
func discoverPMP() Interface {

0 commit comments

Comments
 (0)