Skip to content

Commit e9a1d8d

Browse files
authored
Merge pull request #16387 from karalabe/evm-polsihes
core: minor evm polishes and optimizations
2 parents 933972d + 1fae50a commit e9a1d8d

File tree

4 files changed

+107
-101
lines changed

4 files changed

+107
-101
lines changed

core/evm.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,26 @@ func NewEVMContext(msg Message, header *types.Header, chain ChainContext, author
6060

6161
// GetHashFn returns a GetHashFunc which retrieves header hashes by number
6262
func GetHashFn(ref *types.Header, chain ChainContext) func(n uint64) common.Hash {
63+
var cache map[uint64]common.Hash
64+
6365
return func(n uint64) common.Hash {
66+
// If there's no hash cache yet, make one
67+
if cache == nil {
68+
cache = map[uint64]common.Hash{
69+
ref.Number.Uint64() - 1: ref.ParentHash,
70+
}
71+
}
72+
// Try to fulfill the request from the cache
73+
if hash, ok := cache[n]; ok {
74+
return hash
75+
}
76+
// Not cached, iterate the blocks and cache the hashes
6477
for header := chain.GetHeader(ref.ParentHash, ref.Number.Uint64()-1); header != nil; header = chain.GetHeader(header.ParentHash, header.Number.Uint64()-1) {
65-
if header.Number.Uint64() == n {
66-
return header.Hash()
78+
cache[header.Number.Uint64()-1] = header.ParentHash
79+
if n == header.Number.Uint64()-1 {
80+
return header.ParentHash
6781
}
6882
}
69-
7083
return common.Hash{}
7184
}
7285
}

core/headerchain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"math"
2424
"math/big"
2525
mrand "math/rand"
26+
"sync/atomic"
2627
"time"
2728

2829
"github.com/ethereum/go-ethereum/common"
@@ -32,7 +33,6 @@ import (
3233
"github.com/ethereum/go-ethereum/log"
3334
"github.com/ethereum/go-ethereum/params"
3435
"github.com/hashicorp/golang-lru"
35-
"sync/atomic"
3636
)
3737

3838
const (

0 commit comments

Comments
 (0)