Skip to content

Commit c4a6621

Browse files
karalabeholiman
andauthored
core, eth: for types with accurate size calcs, return uint64, not float (#26046)
* core, eth: for types with accurate size calcs, return uint64, not float * core/types: proper tx size tests * core/types: extend tx size test with decoded sizes, fix error * core/txpool: fix linter Co-authored-by: Martin Holst Swende <martin@swende.se>
1 parent 5bed24d commit c4a6621

File tree

10 files changed

+137
-64
lines changed

10 files changed

+137
-64
lines changed

core/blockchain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1913,7 +1913,7 @@ func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator) (i
19131913
// Import all the pruned blocks to make the state available
19141914
var (
19151915
blocks []*types.Block
1916-
memory common.StorageSize
1916+
memory uint64
19171917
)
19181918
for i := len(hashes) - 1; i >= 0; i-- {
19191919
// Append the next block to our batch

core/txpool/txpool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
595595
return core.ErrTxTypeNotSupported
596596
}
597597
// Reject transactions over defined size to prevent DOS attacks
598-
if uint64(tx.Size()) > txMaxSize {
598+
if tx.Size() > txMaxSize {
599599
return ErrOversizedData
600600
}
601601
// Transactions can't be negative. This may never happen using RLP decoded

core/types/block.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ func (b *Block) DecodeRLP(s *rlp.Stream) error {
263263
return err
264264
}
265265
b.header, b.uncles, b.transactions = eb.Header, eb.Uncles, eb.Txs
266-
b.size.Store(common.StorageSize(rlp.ListSize(size)))
266+
b.size.Store(rlp.ListSize(size))
267267
return nil
268268
}
269269

@@ -322,14 +322,14 @@ func (b *Block) Body() *Body { return &Body{b.transactions, b.uncles} }
322322

323323
// Size returns the true RLP encoded storage size of the block, either by encoding
324324
// and returning it, or returning a previously cached value.
325-
func (b *Block) Size() common.StorageSize {
325+
func (b *Block) Size() uint64 {
326326
if size := b.size.Load(); size != nil {
327-
return size.(common.StorageSize)
327+
return size.(uint64)
328328
}
329329
c := writeCounter(0)
330330
rlp.Encode(&c, b)
331-
b.size.Store(common.StorageSize(c))
332-
return common.StorageSize(c)
331+
b.size.Store(uint64(c))
332+
return uint64(c)
333333
}
334334

335335
// SanityCheck can be used to prevent that unbounded fields are
@@ -338,7 +338,7 @@ func (b *Block) SanityCheck() error {
338338
return b.header.SanityCheck()
339339
}
340340

341-
type writeCounter common.StorageSize
341+
type writeCounter uint64
342342

343343
func (c *writeCounter) Write(b []byte) (int, error) {
344344
*c += writeCounter(len(b))

core/types/block_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestBlockEncoding(t *testing.T) {
5353
check("Hash", block.Hash(), common.HexToHash("0a5843ac1cb04865017cb35a57b50b07084e5fcee39b5acadade33149f4fff9e"))
5454
check("Nonce", block.Nonce(), uint64(0xa13a5a8c8f2bb1c4))
5555
check("Time", block.Time(), uint64(1426516743))
56-
check("Size", block.Size(), common.StorageSize(len(blockEnc)))
56+
check("Size", block.Size(), uint64(len(blockEnc)))
5757

5858
tx1 := NewTransaction(0, common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), big.NewInt(10), 50000, big.NewInt(10), nil)
5959
tx1, _ = tx1.WithSignature(HomesteadSigner{}, common.Hex2Bytes("9bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094f8a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b100"))
@@ -90,7 +90,7 @@ func TestEIP1559BlockEncoding(t *testing.T) {
9090
check("Hash", block.Hash(), common.HexToHash("c7252048cd273fe0dac09650027d07f0e3da4ee0675ebbb26627cea92729c372"))
9191
check("Nonce", block.Nonce(), uint64(0xa13a5a8c8f2bb1c4))
9292
check("Time", block.Time(), uint64(1426516743))
93-
check("Size", block.Size(), common.StorageSize(len(blockEnc)))
93+
check("Size", block.Size(), uint64(len(blockEnc)))
9494
check("BaseFee", block.BaseFee(), new(big.Int).SetUint64(params.InitialBaseFee))
9595

9696
tx1 := NewTransaction(0, common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), big.NewInt(10), 50000, big.NewInt(10), nil)
@@ -153,7 +153,7 @@ func TestEIP2718BlockEncoding(t *testing.T) {
153153
check("Root", block.Root(), common.HexToHash("ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017"))
154154
check("Nonce", block.Nonce(), uint64(0xa13a5a8c8f2bb1c4))
155155
check("Time", block.Time(), uint64(1426516743))
156-
check("Size", block.Size(), common.StorageSize(len(blockEnc)))
156+
check("Size", block.Size(), uint64(len(blockEnc)))
157157

158158
// Create legacy tx.
159159
to := common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87")

core/types/transaction.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
131131
var inner LegacyTx
132132
err := s.Decode(&inner)
133133
if err == nil {
134-
tx.setDecoded(&inner, int(rlp.ListSize(size)))
134+
tx.setDecoded(&inner, rlp.ListSize(size))
135135
}
136136
return err
137137
default:
@@ -142,7 +142,7 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
142142
}
143143
inner, err := tx.decodeTyped(b)
144144
if err == nil {
145-
tx.setDecoded(inner, len(b))
145+
tx.setDecoded(inner, uint64(len(b)))
146146
}
147147
return err
148148
}
@@ -158,15 +158,15 @@ func (tx *Transaction) UnmarshalBinary(b []byte) error {
158158
if err != nil {
159159
return err
160160
}
161-
tx.setDecoded(&data, len(b))
161+
tx.setDecoded(&data, uint64(len(b)))
162162
return nil
163163
}
164164
// It's an EIP2718 typed transaction envelope.
165165
inner, err := tx.decodeTyped(b)
166166
if err != nil {
167167
return err
168168
}
169-
tx.setDecoded(inner, len(b))
169+
tx.setDecoded(inner, uint64(len(b)))
170170
return nil
171171
}
172172

@@ -190,11 +190,11 @@ func (tx *Transaction) decodeTyped(b []byte) (TxData, error) {
190190
}
191191

192192
// setDecoded sets the inner transaction and size after decoding.
193-
func (tx *Transaction) setDecoded(inner TxData, size int) {
193+
func (tx *Transaction) setDecoded(inner TxData, size uint64) {
194194
tx.inner = inner
195195
tx.time = time.Now()
196196
if size > 0 {
197-
tx.size.Store(common.StorageSize(size))
197+
tx.size.Store(size)
198198
}
199199
}
200200

@@ -372,16 +372,21 @@ func (tx *Transaction) Hash() common.Hash {
372372
return h
373373
}
374374

375-
// Size returns the true RLP encoded storage size of the transaction, either by
376-
// encoding and returning it, or returning a previously cached value.
377-
func (tx *Transaction) Size() common.StorageSize {
375+
// Size returns the true encoded storage size of the transaction, either by encoding
376+
// and returning it, or returning a previously cached value.
377+
func (tx *Transaction) Size() uint64 {
378378
if size := tx.size.Load(); size != nil {
379-
return size.(common.StorageSize)
379+
return size.(uint64)
380380
}
381381
c := writeCounter(0)
382382
rlp.Encode(&c, &tx.inner)
383-
tx.size.Store(common.StorageSize(c))
384-
return common.StorageSize(c)
383+
384+
size := uint64(c)
385+
if tx.Type() != LegacyTxType {
386+
size += 1 // type byte
387+
}
388+
tx.size.Store(size)
389+
return size
385390
}
386391

387392
// WithSignature returns a new transaction with the given signature.

core/types/transaction_marshalling.go

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -51,55 +51,55 @@ type txJSON struct {
5151
}
5252

5353
// MarshalJSON marshals as JSON with a hash.
54-
func (t *Transaction) MarshalJSON() ([]byte, error) {
54+
func (tx *Transaction) MarshalJSON() ([]byte, error) {
5555
var enc txJSON
5656
// These are set for all tx types.
57-
enc.Hash = t.Hash()
58-
enc.Type = hexutil.Uint64(t.Type())
57+
enc.Hash = tx.Hash()
58+
enc.Type = hexutil.Uint64(tx.Type())
5959

6060
// Other fields are set conditionally depending on tx type.
61-
switch tx := t.inner.(type) {
61+
switch itx := tx.inner.(type) {
6262
case *LegacyTx:
63-
enc.Nonce = (*hexutil.Uint64)(&tx.Nonce)
64-
enc.Gas = (*hexutil.Uint64)(&tx.Gas)
65-
enc.GasPrice = (*hexutil.Big)(tx.GasPrice)
66-
enc.Value = (*hexutil.Big)(tx.Value)
67-
enc.Data = (*hexutil.Bytes)(&tx.Data)
68-
enc.To = t.To()
69-
enc.V = (*hexutil.Big)(tx.V)
70-
enc.R = (*hexutil.Big)(tx.R)
71-
enc.S = (*hexutil.Big)(tx.S)
63+
enc.Nonce = (*hexutil.Uint64)(&itx.Nonce)
64+
enc.Gas = (*hexutil.Uint64)(&itx.Gas)
65+
enc.GasPrice = (*hexutil.Big)(itx.GasPrice)
66+
enc.Value = (*hexutil.Big)(itx.Value)
67+
enc.Data = (*hexutil.Bytes)(&itx.Data)
68+
enc.To = tx.To()
69+
enc.V = (*hexutil.Big)(itx.V)
70+
enc.R = (*hexutil.Big)(itx.R)
71+
enc.S = (*hexutil.Big)(itx.S)
7272
case *AccessListTx:
73-
enc.ChainID = (*hexutil.Big)(tx.ChainID)
74-
enc.AccessList = &tx.AccessList
75-
enc.Nonce = (*hexutil.Uint64)(&tx.Nonce)
76-
enc.Gas = (*hexutil.Uint64)(&tx.Gas)
77-
enc.GasPrice = (*hexutil.Big)(tx.GasPrice)
78-
enc.Value = (*hexutil.Big)(tx.Value)
79-
enc.Data = (*hexutil.Bytes)(&tx.Data)
80-
enc.To = t.To()
81-
enc.V = (*hexutil.Big)(tx.V)
82-
enc.R = (*hexutil.Big)(tx.R)
83-
enc.S = (*hexutil.Big)(tx.S)
73+
enc.ChainID = (*hexutil.Big)(itx.ChainID)
74+
enc.AccessList = &itx.AccessList
75+
enc.Nonce = (*hexutil.Uint64)(&itx.Nonce)
76+
enc.Gas = (*hexutil.Uint64)(&itx.Gas)
77+
enc.GasPrice = (*hexutil.Big)(itx.GasPrice)
78+
enc.Value = (*hexutil.Big)(itx.Value)
79+
enc.Data = (*hexutil.Bytes)(&itx.Data)
80+
enc.To = tx.To()
81+
enc.V = (*hexutil.Big)(itx.V)
82+
enc.R = (*hexutil.Big)(itx.R)
83+
enc.S = (*hexutil.Big)(itx.S)
8484
case *DynamicFeeTx:
85-
enc.ChainID = (*hexutil.Big)(tx.ChainID)
86-
enc.AccessList = &tx.AccessList
87-
enc.Nonce = (*hexutil.Uint64)(&tx.Nonce)
88-
enc.Gas = (*hexutil.Uint64)(&tx.Gas)
89-
enc.MaxFeePerGas = (*hexutil.Big)(tx.GasFeeCap)
90-
enc.MaxPriorityFeePerGas = (*hexutil.Big)(tx.GasTipCap)
91-
enc.Value = (*hexutil.Big)(tx.Value)
92-
enc.Data = (*hexutil.Bytes)(&tx.Data)
93-
enc.To = t.To()
94-
enc.V = (*hexutil.Big)(tx.V)
95-
enc.R = (*hexutil.Big)(tx.R)
96-
enc.S = (*hexutil.Big)(tx.S)
85+
enc.ChainID = (*hexutil.Big)(itx.ChainID)
86+
enc.AccessList = &itx.AccessList
87+
enc.Nonce = (*hexutil.Uint64)(&itx.Nonce)
88+
enc.Gas = (*hexutil.Uint64)(&itx.Gas)
89+
enc.MaxFeePerGas = (*hexutil.Big)(itx.GasFeeCap)
90+
enc.MaxPriorityFeePerGas = (*hexutil.Big)(itx.GasTipCap)
91+
enc.Value = (*hexutil.Big)(itx.Value)
92+
enc.Data = (*hexutil.Bytes)(&itx.Data)
93+
enc.To = tx.To()
94+
enc.V = (*hexutil.Big)(itx.V)
95+
enc.R = (*hexutil.Big)(itx.R)
96+
enc.S = (*hexutil.Big)(itx.S)
9797
}
9898
return json.Marshal(&enc)
9999
}
100100

101101
// UnmarshalJSON unmarshals from JSON.
102-
func (t *Transaction) UnmarshalJSON(input []byte) error {
102+
func (tx *Transaction) UnmarshalJSON(input []byte) error {
103103
var dec txJSON
104104
if err := json.Unmarshal(input, &dec); err != nil {
105105
return err
@@ -268,7 +268,7 @@ func (t *Transaction) UnmarshalJSON(input []byte) error {
268268
}
269269

270270
// Now set the inner transaction.
271-
t.setDecoded(inner, 0)
271+
tx.setDecoded(inner, 0)
272272

273273
// TODO: check hash here?
274274
return nil

core/types/transaction_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,3 +531,71 @@ func assertEqual(orig *Transaction, cpy *Transaction) error {
531531
}
532532
return nil
533533
}
534+
535+
func TestTransactionSizes(t *testing.T) {
536+
signer := NewLondonSigner(big.NewInt(123))
537+
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
538+
to := common.HexToAddress("0x01")
539+
for i, txdata := range []TxData{
540+
&AccessListTx{
541+
ChainID: big.NewInt(123),
542+
Nonce: 0,
543+
To: nil,
544+
Value: big.NewInt(1000),
545+
Gas: 21000,
546+
GasPrice: big.NewInt(100000),
547+
},
548+
&LegacyTx{
549+
Nonce: 1,
550+
GasPrice: big.NewInt(500),
551+
Gas: 1000000,
552+
To: &to,
553+
Value: big.NewInt(1),
554+
},
555+
&AccessListTx{
556+
ChainID: big.NewInt(123),
557+
Nonce: 1,
558+
GasPrice: big.NewInt(500),
559+
Gas: 1000000,
560+
To: &to,
561+
Value: big.NewInt(1),
562+
AccessList: AccessList{
563+
AccessTuple{
564+
Address: common.HexToAddress("0x01"),
565+
StorageKeys: []common.Hash{common.HexToHash("0x01")},
566+
}},
567+
},
568+
&DynamicFeeTx{
569+
ChainID: big.NewInt(123),
570+
Nonce: 1,
571+
Gas: 1000000,
572+
To: &to,
573+
Value: big.NewInt(1),
574+
GasTipCap: big.NewInt(500),
575+
GasFeeCap: big.NewInt(500),
576+
},
577+
} {
578+
tx, err := SignNewTx(key, signer, txdata)
579+
if err != nil {
580+
t.Fatalf("test %d: %v", i, err)
581+
}
582+
bin, _ := tx.MarshalBinary()
583+
584+
// Check initial calc
585+
if have, want := int(tx.Size()), len(bin); have != want {
586+
t.Errorf("test %d: size wrong, have %d want %d", i, have, want)
587+
}
588+
// Check cached version too
589+
if have, want := int(tx.Size()), len(bin); have != want {
590+
t.Errorf("test %d: (cached) size wrong, have %d want %d", i, have, want)
591+
}
592+
// Check unmarshalled version too
593+
utx := new(Transaction)
594+
if err := utx.UnmarshalBinary(bin); err != nil {
595+
t.Fatalf("test %d: failed to unmarshal tx: %v", i, err)
596+
}
597+
if have, want := int(utx.Size()), len(bin); have != want {
598+
t.Errorf("test %d: (unmarshalled) size wrong, have %d want %d", i, have, want)
599+
}
600+
}
601+
}

eth/downloader/queue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ func (q *queue) Results(block bool) []*fetchResult {
369369
size += receipt.Size()
370370
}
371371
for _, tx := range result.Transactions {
372-
size += tx.Size()
372+
size += common.StorageSize(tx.Size())
373373
}
374374
q.resultSize = common.StorageSize(blockCacheSizeWeight)*size +
375375
(1-common.StorageSize(blockCacheSizeWeight))*q.resultSize

eth/protocols/eth/broadcast.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (p *Peer) broadcastTransactions() {
8282
for i := 0; i < len(queue) && size < maxTxPacketSize; i++ {
8383
if tx := p.txpool.Get(queue[i]); tx != nil {
8484
txs = append(txs, tx)
85-
size += tx.Size()
85+
size += common.StorageSize(tx.Size())
8686
}
8787
hashesCount++
8888
}

les/downloader/queue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ func (q *queue) Results(block bool) []*fetchResult {
373373
size += receipt.Size()
374374
}
375375
for _, tx := range result.Transactions {
376-
size += tx.Size()
376+
size += common.StorageSize(tx.Size())
377377
}
378378
q.resultSize = common.StorageSize(blockCacheSizeWeight)*size +
379379
(1-common.StorageSize(blockCacheSizeWeight))*q.resultSize

0 commit comments

Comments
 (0)