Skip to content
Open
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 core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2046,7 +2046,7 @@ func (bc *BlockChain) insertSidechain(block *types.Block, it *insertIterator) (i
// Import all the pruned blocks to make the state available
var (
blocks []*types.Block
memory common.StorageSize
memory uint64
)
for i := len(hashes) - 1; i >= 0; i-- {
// Append the next block to our batch
Expand Down
2 changes: 1 addition & 1 deletion core/txpool/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
return core.ErrTxTypeNotSupported
}
// Reject transactions over defined size to prevent DOS attacks
if uint64(tx.Size()) > txMaxSize {
if tx.Size() > txMaxSize {
return ErrOversizedData
}
// Get current block number
Expand Down
12 changes: 6 additions & 6 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func (b *Block) DecodeRLP(s *rlp.Stream) error {
return err
}
b.header, b.uncles, b.transactions = eb.Header, eb.Uncles, eb.Txs
b.size.Store(common.StorageSize(rlp.ListSize(size)))
b.size.Store(rlp.ListSize(size))
return nil
}

Expand Down Expand Up @@ -412,17 +412,17 @@ func (b *Block) HashNoValidator() common.Hash {

// Size returns the true RLP encoded storage size of the block, either by encoding
// and returning it, or returning a previsouly cached value.
func (b *Block) Size() common.StorageSize {
func (b *Block) Size() uint64 {
if size := b.size.Load(); size != nil {
return size.(common.StorageSize)
return size.(uint64)
}
c := writeCounter(0)
rlp.Encode(&c, b)
b.size.Store(common.StorageSize(c))
return common.StorageSize(c)
b.size.Store(uint64(c))
return uint64(c)
}

type writeCounter common.StorageSize
type writeCounter uint64

func (c *writeCounter) Write(b []byte) (int, error) {
*c += writeCounter(len(b))
Expand Down
4 changes: 2 additions & 2 deletions core/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestBlockEncoding(t *testing.T) {
check("Hash", block.Hash().String(), common.HexToHash("e8d9d473fdeddd3079988fa7be58f582b7b2800e90917d4bb6f11155ce4dba30").String())
check("Nonce", block.Nonce(), uint64(0xa13a5a8c8f2bb1c4))
check("Time", block.Time(), uint64(1426516743))
check("Size", block.Size(), common.StorageSize(len(blockEnc)))
check("Size", block.Size(), uint64(len(blockEnc)))

ourBlockEnc, err := rlp.EncodeToBytes(&block)
if err != nil {
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestEIP2718BlockEncoding(t *testing.T) {
check("Root", block.Root(), common.HexToHash("ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017"))
check("Nonce", block.Nonce(), uint64(0xa13a5a8c8f2bb1c4))
check("Time", block.Time(), uint64(1426516743))
check("Size", block.Size(), common.StorageSize(len(blockEnc)))
check("Size", block.Size(), uint64(len(blockEnc)))

// Create legacy tx.
to := common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87")
Expand Down
8 changes: 4 additions & 4 deletions core/types/gen_access_tuple.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 22 additions & 12 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
var inner LegacyTx
err := s.Decode(&inner)
if err == nil {
tx.setDecoded(&inner, int(rlp.ListSize(size)))
tx.setDecoded(&inner, rlp.ListSize(size))
}
return err
default:
Expand All @@ -157,7 +157,7 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
}
inner, err := tx.decodeTyped(b)
if err == nil {
tx.setDecoded(inner, len(b))
tx.setDecoded(inner, uint64(len(b)))
}
return err
}
Expand All @@ -173,15 +173,15 @@ func (tx *Transaction) UnmarshalBinary(b []byte) error {
if err != nil {
return err
}
tx.setDecoded(&data, len(b))
tx.setDecoded(&data, uint64(len(b)))
return nil
}
// It's an EIP2718 typed transaction envelope.
inner, err := tx.decodeTyped(b)
if err != nil {
return err
}
tx.setDecoded(inner, len(b))
tx.setDecoded(inner, uint64(len(b)))
return nil
}

Expand All @@ -205,11 +205,11 @@ func (tx *Transaction) decodeTyped(b []byte) (TxData, error) {
}

// setDecoded sets the inner transaction and size after decoding.
func (tx *Transaction) setDecoded(inner TxData, size int) {
func (tx *Transaction) setDecoded(inner TxData, size uint64) {
tx.inner = inner
tx.time = time.Now()
if size > 0 {
tx.size.Store(common.StorageSize(size))
tx.size.Store(size)
}
}

Expand Down Expand Up @@ -407,16 +407,26 @@ func (tx *Transaction) Hash() common.Hash {
return h
}

// Size returns the true RLP encoded storage size of the transaction, either by
// encoding and returning it, or returning a previously cached value.
func (tx *Transaction) Size() common.StorageSize {
// Size returns the true encoded storage size of the transaction, either by encoding
// and returning it, or returning a previously cached value.
func (tx *Transaction) Size() uint64 {
if size := tx.size.Load(); size != nil {
return size.(common.StorageSize)
return size.(uint64)
}

// Cache miss, encode and cache.
// Note we rely on the assumption that all tx.inner values are RLP-encoded
c := writeCounter(0)
rlp.Encode(&c, &tx.inner)
tx.size.Store(common.StorageSize(c))
return common.StorageSize(c)
size := uint64(c)

// For typed transactions, the encoding also includes the leading type byte.
if tx.Type() != LegacyTxType {
size++
}

tx.size.Store(size)
return size
}

func (tx *Transaction) EffectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int {
Expand Down
Loading