Skip to content

Commit

Permalink
Merge pull request #19411 from holiman/uncle_abort_early
Browse files Browse the repository at this point in the history
consensus,core: shortcut uncle validation
  • Loading branch information
karalabe authored Apr 8, 2019
2 parents 2a8a07c + d763b49 commit 3996bc1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ func (ethash *Ethash) VerifyUncles(chain consensus.ChainReader, block *types.Blo
if len(block.Uncles()) > maxUncles {
return errTooManyUncles
}
if len(block.Uncles()) == 0 {
return nil
}
// Gather the set of past uncles and ancestors
uncles, ancestors := mapset.NewSet(), make(map[common.Hash]*types.Header)

Expand Down
5 changes: 4 additions & 1 deletion core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (

var (
EmptyRootHash = DeriveSha(Transactions{})
EmptyUncleHash = CalcUncleHash(nil)
EmptyUncleHash = rlpHash([]*Header(nil))
)

// A BlockNonce is a 64-bit hash which proves (combined with the
Expand Down Expand Up @@ -324,6 +324,9 @@ func (c *writeCounter) Write(b []byte) (int, error) {
}

func CalcUncleHash(uncles []*Header) common.Hash {
if len(uncles) == 0 {
return EmptyUncleHash
}
return rlpHash(uncles)
}

Expand Down
16 changes: 16 additions & 0 deletions core/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,19 @@ func TestBlockEncoding(t *testing.T) {
t.Errorf("encoded block mismatch:\ngot: %x\nwant: %x", ourBlockEnc, blockEnc)
}
}

func TestUncleHash(t *testing.T) {
uncles := make([]*Header, 0)
h := CalcUncleHash(uncles)
exp := common.HexToHash("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")
if h != exp {
t.Fatalf("empty uncle hash is wrong, got %x != %x", h, exp)
}
}
func BenchmarkUncleHash(b *testing.B) {
uncles := make([]*Header, 0)
b.ResetTimer()
for i := 0; i < b.N; i++ {
CalcUncleHash(uncles)
}
}

0 comments on commit 3996bc1

Please sign in to comment.