@@ -19,6 +19,7 @@ package ethash
1919import (
2020 "encoding/binary"
2121 "hash"
22+ "math/big"
2223 "reflect"
2324 "runtime"
2425 "sync"
@@ -47,6 +48,48 @@ const (
4748 loopAccesses = 64 // Number of accesses in hashimoto loop
4849)
4950
51+ // cacheSize returns the size of the ethash verification cache that belongs to a certain
52+ // block number.
53+ func cacheSize (block uint64 ) uint64 {
54+ epoch := int (block / epochLength )
55+ if epoch < maxEpoch {
56+ return cacheSizes [epoch ]
57+ }
58+ return calcCacheSize (epoch )
59+ }
60+
61+ // calcCacheSize calculates the cache size for epoch. The cache size grows linearly,
62+ // however, we always take the highest prime below the linearly growing threshold in order
63+ // to reduce the risk of accidental regularities leading to cyclic behavior.
64+ func calcCacheSize (epoch int ) uint64 {
65+ size := cacheInitBytes + cacheGrowthBytes * uint64 (epoch ) - hashBytes
66+ for ! new (big.Int ).SetUint64 (size / hashBytes ).ProbablyPrime (1 ) { // Always accurate for n < 2^64
67+ size -= 2 * hashBytes
68+ }
69+ return size
70+ }
71+
72+ // datasetSize returns the size of the ethash mining dataset that belongs to a certain
73+ // block number.
74+ func datasetSize (block uint64 ) uint64 {
75+ epoch := int (block / epochLength )
76+ if epoch < maxEpoch {
77+ return datasetSizes [epoch ]
78+ }
79+ return calcDatasetSize (epoch )
80+ }
81+
82+ // calcDatasetSize calculates the dataset size for epoch. The dataset size grows linearly,
83+ // however, we always take the highest prime below the linearly growing threshold in order
84+ // to reduce the risk of accidental regularities leading to cyclic behavior.
85+ func calcDatasetSize (epoch int ) uint64 {
86+ size := datasetInitBytes + datasetGrowthBytes * uint64 (epoch ) - mixBytes
87+ for ! new (big.Int ).SetUint64 (size / mixBytes ).ProbablyPrime (1 ) { // Always accurate for n < 2^64
88+ size -= 2 * mixBytes
89+ }
90+ return size
91+ }
92+
5093// hasher is a repetitive hasher allowing the same hash data structures to be
5194// reused between hash runs instead of requiring new ones to be created.
5295type hasher func (dest []byte , data []byte )
0 commit comments