Skip to content

Commit

Permalink
Merge pull request ethereum#21 from uprendis/develop-1.10.8-updated
Browse files Browse the repository at this point in the history
Develop 1.10.8 updated
  • Loading branch information
quan8 authored Nov 8, 2021
2 parents 4491e2a + a65cf94 commit 4ea903e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
46 changes: 22 additions & 24 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ type Header struct {

// BaseFee was added by EIP-1559 and is ignored in legacy headers.
BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"`

// caches
externalHash atomic.Value `rlp:"-"`
}

// field type overrides for gencodec
Expand All @@ -101,10 +104,21 @@ type headerMarshaling struct {

// Hash returns the block hash of the header, which is simply the keccak256 hash of its
// RLP encoding.
// Also hash of the header could be overridden with external value.
func (h *Header) Hash() common.Hash {
external := h.externalHash.Load()
if external != nil {
return external.(common.Hash)
}

return rlpHash(h)
}

// SetExternalHash overrides hash with external value.
func (h *Header) SetExternalHash(hash common.Hash) {
h.externalHash.Store(hash)
}

var headerSize = common.StorageSize(reflect.TypeOf(Header{}).Size())

// Size returns the approximate memory used by all internal contents. It is used
Expand Down Expand Up @@ -162,7 +176,6 @@ type Block struct {
transactions Transactions

// caches
hash atomic.Value
size atomic.Value

// Td is used by package core to store the total difficulty
Expand Down Expand Up @@ -245,6 +258,13 @@ func CopyHeader(h *Header) *Header {
cpy.Extra = make([]byte, len(h.Extra))
copy(cpy.Extra, h.Extra)
}

external := h.externalHash.Load()
if external != nil {
hash := external.(common.Hash)
cpy.SetExternalHash(hash)
}

return &cpy
}

Expand Down Expand Up @@ -371,31 +391,9 @@ func (b *Block) WithBody(transactions []*Transaction, uncles []*Header) *Block {
return block
}

// WithHash returns a new block with the custom hash.
func (b *Block) WithHash(h common.Hash) *Block {
block := &Block{
header: CopyHeader(b.header),
transactions: make([]*Transaction, len(b.transactions)),
uncles: make([]*Header, len(b.uncles)),
}
copy(block.transactions, b.transactions)
for i, uncle := range b.uncles {
block.uncles[i] = CopyHeader(uncle)
}

block.hash.Store(h)
return block
}

// Hash returns the keccak256 hash of b's header.
// The hash is computed on the first call and cached thereafter.
func (b *Block) Hash() common.Hash {
if hash := b.hash.Load(); hash != nil {
return hash.(common.Hash)
}
v := b.header.Hash()
b.hash.Store(v)
return v
return b.header.Hash()
}

type Blocks []*Block
5 changes: 3 additions & 2 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,10 @@ func (ec *Client) getBlock(ctx context.Context, method string, args ...interface
txs[i] = tx.tx
}

head.SetExternalHash(body.Hash)
block := types.NewBlockWithHeader(head).
WithBody(txs, uncles).
WithHash(body.Hash)
WithBody(txs, uncles)

return block, nil
}

Expand Down

0 comments on commit 4ea903e

Please sign in to comment.