MCPcopy Create free account
hub / github.com/CPChain/chain / GetHashFn

Function GetHashFn

core/evm.go:62–85  ·  view source on GitHub ↗

GetHashFn returns a GetHashFunc which retrieves header hashes by number

(ref *types.Header, chain ChainContext)

Source from the content-addressed store, hash-verified

60
61// GetHashFn returns a GetHashFunc which retrieves header hashes by number
62func GetHashFn(ref *types.Header, chain ChainContext) func(n uint64) common.Hash {
63 var cache map[uint64]common.Hash
64
65 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
77 for header := chain.GetHeader(ref.ParentHash, ref.Number.Uint64()-1); header != nil; header = chain.GetHeader(header.ParentHash, header.Number.Uint64()-1) {
78 cache[header.Number.Uint64()-1] = header.ParentHash
79 if n == header.Number.Uint64()-1 {
80 return header.ParentHash
81 }
82 }
83 return common.Hash{}
84 }
85}
86
87// CanTransfer checks wether there are enough funds in the address' account to make a transfer.
88// This does not take the necessary gas in to account to make the transfer valid.

Callers 1

NewEVMContextFunction · 0.85

Calls 2

Uint64Method · 0.80
GetHeaderMethod · 0.65

Tested by

no test coverage detected