Skip to content
This repository was archived by the owner on Feb 16, 2024. It is now read-only.

Commit cd2a6b4

Browse files
committed
minor dag config changes
1 parent 46143a2 commit cd2a6b4

File tree

11 files changed

+145
-150
lines changed

11 files changed

+145
-150
lines changed

cmd/gen-lookup/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func genCacheLookupTable(initBytes, growthBytes uint64, epochs int) []uint64 {
2121

2222
lookupTable := make([]uint64, epochs)
2323
for i := range lookupTable {
24-
lookupTable[i] = dag.CacheSize(cfg, uint64(i))
24+
lookupTable[i] = cfg.CacheSize(uint64(i))
2525
}
2626

2727
return lookupTable
@@ -35,7 +35,7 @@ func genDatasetLookupTable(initBytes, growthBytes uint64, epochs int) []uint64 {
3535

3636
lookupTable := make([]uint64, epochs)
3737
for i := range lookupTable {
38-
lookupTable[i] = dag.DatasetSize(cfg, uint64(i))
38+
lookupTable[i] = cfg.DatasetSize(uint64(i))
3939
}
4040

4141
return lookupTable

ethash/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ func NewEthereumClassic() *Ethash {
9797
}
9898

9999
func (e *Ethash) Compute(height, nonce uint64, hash []byte) ([]byte, []byte) {
100-
epoch := dag.CalcEpoch(e.cfg, height)
101-
size := dag.DatasetSize(e.cfg, epoch)
100+
epoch := e.cfg.CalcEpoch(height)
101+
size := e.cfg.DatasetSize(epoch)
102102
cache := e.dag.GetCache(epoch)
103103

104104
keccak512Hasher := crypto.NewKeccak512Hasher()
105105
lookup := func(index uint32) []uint32 {
106-
return dag.GenerateDatasetItem512(e.cfg, cache.Cache(), index, keccak512Hasher)
106+
return e.cfg.GenerateDatasetItem512(cache.Cache(), index, keccak512Hasher)
107107
}
108108

109109
mix, digest := hashimoto(hash, nonce, size, lookup)

firopow/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ func NewFiro() *Firopow {
5555
}
5656

5757
func (e *Firopow) Compute(height, nonce uint64, hash []byte) ([]byte, []byte) {
58-
epoch := dag.CalcEpoch(e.cfg, height)
59-
datasetSize := dag.DatasetSize(e.cfg, epoch)
58+
epoch := e.cfg.CalcEpoch(height)
59+
datasetSize := e.cfg.DatasetSize(epoch)
6060
cache := e.dag.GetCache(epoch)
6161

6262
keccak512Hasher := crypto.NewKeccak512Hasher()
6363
lookup := func(index uint32) []uint32 {
64-
return dag.GenerateDatasetItem2048(e.cfg, cache.Cache(), index, keccak512Hasher)
64+
return e.cfg.GenerateDatasetItem2048(cache.Cache(), index, keccak512Hasher)
6565
}
6666

6767
mix, digest := compute(hash, height, nonce, datasetSize, lookup, cache.L1())

internal/dag/cache.go

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,12 @@
1717
package dag
1818

1919
import (
20-
"fmt"
2120
"os"
22-
"path/filepath"
2321
"runtime"
2422
"sync"
2523
"time"
2624
)
2725

28-
func cachePaths(cfg *Config, seed []byte) (string, string) {
29-
cacheName := fmt.Sprintf("cache-%s-R%d-%x", cfg.Name, cfg.Revision, seed)
30-
l1Name := fmt.Sprintf("l1-%s-R%d-%x", cfg.Name, cfg.Revision, seed)
31-
32-
cachePath := filepath.Join(cfg.StorageDir, cacheName)
33-
l1Path := filepath.Join(cfg.StorageDir, l1Name)
34-
35-
return cachePath, l1Path
36-
}
37-
3826
type cache struct {
3927
epoch uint64
4028
once sync.Once
@@ -54,23 +42,24 @@ func (c *cache) L1() []uint32 {
5442
// generate ensures that the cache content is generated before use.
5543
func (c *cache) generate(cfg *Config) {
5644
c.once.Do(func() {
57-
size := CacheSize(cfg, c.epoch)
58-
seed := SeedHash(cfg, c.epoch*cfg.EpochLength+1)
45+
size := cfg.CacheSize(c.epoch)
46+
seed := cfg.SeedHash(c.epoch*cfg.EpochLength + 1)
5947

6048
// If we don't store anything on disk, generate and return.
6149
if cfg.StorageDir == "" {
6250
c.cache.data = make([]uint32, size/4)
63-
generateCache(cfg, c.cache.data, c.epoch, seed)
51+
cfg.generateCache(c.cache.data, c.epoch, seed)
6452

6553
if cfg.L1Enabled {
6654
c.l1.data = make([]uint32, cfg.L1CacheNumItems)
67-
generateL1Cache(cfg, c.l1.data, c.cache.data)
55+
cfg.generateL1Cache(c.l1.data, c.cache.data)
6856
}
6957

7058
return
7159
}
7260

73-
cachePath, l1Path := cachePaths(cfg, seed[:8])
61+
cachePath := cfg.cacheStorageLocation(seed[:8])
62+
l1Path := cfg.l1StorageLocation(seed[:8])
7463

7564
// We're about to mmap the file, ensure that the mapping is cleaned up when the
7665
// cache becomes unused.
@@ -93,28 +82,31 @@ func (c *cache) generate(cfg *Config) {
9382

9483
// No usable previous cache available, create a new cache file to fill
9584
if needsCache {
96-
cacheGenerator := func(buffer []uint32) { generateCache(cfg, buffer, c.epoch, seed) }
85+
cacheGenerator := func(buffer []uint32) { cfg.generateCache(buffer, c.epoch, seed) }
9786
c.cache, err = memoryMapAndGenerate(cachePath, size, cfg.CachesLockMmap, cacheGenerator)
9887
if err != nil {
9988
c.cache.data = make([]uint32, size/4)
100-
generateCache(cfg, c.cache.data, c.epoch, seed)
89+
cfg.generateCache(c.cache.data, c.epoch, seed)
10190
}
10291
}
10392

10493
if needsL1 {
105-
l1Generator := func(buffer []uint32) { generateL1Cache(cfg, buffer, c.cache.data) }
94+
l1Generator := func(buffer []uint32) { cfg.generateL1Cache(buffer, c.cache.data) }
10695
c.l1, err = memoryMapAndGenerate(l1Path, cfg.L1CacheSize, cfg.CachesLockMmap, l1Generator)
10796
if err != nil {
10897
c.l1.data = make([]uint32, cfg.L1CacheNumItems)
109-
generateL1Cache(cfg, c.l1.data, c.cache.data)
98+
cfg.generateL1Cache(c.l1.data, c.cache.data)
11099
}
111100
}
112101

113102
// Iterate over all previous instances and delete old ones
114103
for ep := int(c.epoch) - cfg.CachesCount; ep >= 0; ep-- {
115-
seed := SeedHash(cfg, uint64(ep)*cfg.EpochLength+1)
116-
cachePath, l1Path := cachePaths(cfg, seed[:8])
104+
seed := cfg.SeedHash(uint64(ep)*cfg.EpochLength + 1)
105+
106+
cachePath := cfg.cacheStorageLocation(seed[:8])
117107
os.Remove(cachePath)
108+
109+
l1Path := cfg.l1StorageLocation(seed[:8])
118110
os.Remove(l1Path)
119111
}
120112
})

internal/dag/calculate.go

Lines changed: 0 additions & 81 deletions
This file was deleted.

internal/dag/config.go

Lines changed: 92 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,34 @@
1616

1717
package dag
1818

19+
import (
20+
"fmt"
21+
"math/big"
22+
"path/filepath"
23+
24+
"github.com/sencha-dev/powkit/internal/crypto"
25+
)
26+
1927
const (
2028
mixBytes = 128 // Width of mix
2129
hashBytes = 64 // Hash length in bytes
2230
hashWords = 16 // Number of 32 bit ints in a hash
2331
)
2432

33+
type LookupTable struct {
34+
maxEpoch uint64
35+
table []uint64
36+
}
37+
38+
func NewLookupTable(table []uint64, maxEpoch uint64) *LookupTable {
39+
lookupTable := &LookupTable{
40+
maxEpoch: maxEpoch,
41+
table: table,
42+
}
43+
44+
return lookupTable
45+
}
46+
2547
type Config struct {
2648
Name string
2749
Revision int
@@ -53,16 +75,78 @@ type Config struct {
5375
L1CacheNumItems uint
5476
}
5577

56-
type LookupTable struct {
57-
maxEpoch uint64
58-
table []uint64
78+
/* helpers */
79+
80+
func (cfg *Config) cacheStorageLocation(seed []byte) string {
81+
name := fmt.Sprintf("cache-%s-R%d-%x", cfg.Name, cfg.Revision, seed)
82+
path := filepath.Join(cfg.StorageDir, name)
83+
84+
return path
5985
}
6086

61-
func NewLookupTable(table []uint64, maxEpoch uint64) *LookupTable {
62-
lookupTable := &LookupTable{
63-
maxEpoch: maxEpoch,
64-
table: table,
87+
func (cfg *Config) l1StorageLocation(seed []byte) string {
88+
name := fmt.Sprintf("l1-%s-R%d-%x", cfg.Name, cfg.Revision, seed)
89+
path := filepath.Join(cfg.StorageDir, name)
90+
91+
return path
92+
}
93+
94+
/* calculations */
95+
96+
func (cfg *Config) CalcEpoch(height uint64) uint64 {
97+
epoch := height / cfg.EpochLength
98+
99+
return epoch
100+
}
101+
102+
func (cfg *Config) SeedHash(height uint64) []byte {
103+
seed := make([]byte, 32)
104+
if height < cfg.SeedEpochLength {
105+
return seed
65106
}
66107

67-
return lookupTable
108+
keccak256Hasher := crypto.NewKeccak256Hasher()
109+
for i := 0; i < int(height/cfg.SeedEpochLength); i++ {
110+
keccak256Hasher(seed, seed)
111+
}
112+
113+
return seed
114+
}
115+
116+
func (cfg *Config) CacheSize(epoch uint64) uint64 {
117+
if cfg.CacheSizes != nil && epoch < cfg.CacheSizes.maxEpoch {
118+
return cfg.CacheSizes.table[epoch]
119+
}
120+
121+
return cfg.calcCacheSize(epoch)
122+
}
123+
124+
func (cfg *Config) DatasetSize(epoch uint64) uint64 {
125+
if cfg.DatasetSizes != nil && epoch < cfg.DatasetSizes.maxEpoch {
126+
return cfg.DatasetSizes.table[epoch]
127+
}
128+
129+
return cfg.calcDatasetSize(epoch)
130+
}
131+
132+
func (cfg *Config) calcCacheSize(epoch uint64) uint64 {
133+
size := cfg.CacheInitBytes + cfg.CacheGrowthBytes*epoch - hashBytes
134+
135+
// Always accurate for n < 2^64
136+
for !new(big.Int).SetUint64(size / hashBytes).ProbablyPrime(1) {
137+
size -= 2 * hashBytes
138+
}
139+
140+
return size
141+
}
142+
143+
func (cfg *Config) calcDatasetSize(epoch uint64) uint64 {
144+
size := cfg.DatasetInitBytes + cfg.DatasetGrowthBytes*epoch - mixBytes
145+
146+
// Always accurate for n < 2^64
147+
for !new(big.Int).SetUint64(size / mixBytes).ProbablyPrime(1) {
148+
size -= 2 * mixBytes
149+
}
150+
151+
return size
68152
}

internal/dag/calculate_test.go renamed to internal/dag/config_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func TestEpochNumber(t *testing.T) {
8787
}
8888

8989
for i, tt := range tests {
90-
epoch := CalcEpoch(cfg, tt.height)
90+
epoch := cfg.CalcEpoch(tt.height)
9191

9292
if epoch != tt.epoch {
9393
t.Errorf("failed on %d: epoch mismatch: have %d want %d", i, epoch, tt.epoch)
@@ -153,7 +153,7 @@ func TestSeedHash(t *testing.T) {
153153
}
154154

155155
for i, tt := range tests {
156-
seed := SeedHash(cfg, uint64(tt.epoch)*cfg.EpochLength+1)
156+
seed := cfg.SeedHash(uint64(tt.epoch)*cfg.EpochLength + 1)
157157
if !reflect.DeepEqual(seed, tt.seed) {
158158
t.Errorf("failed on %d: seed mismatch: have %x, want %x", i, seed, tt.seed)
159159
}
@@ -232,7 +232,7 @@ func TestCalcCacheSize(t *testing.T) {
232232
}
233233

234234
for i, tt := range tests {
235-
size := calcCacheSize(cfg, tt.epoch)
235+
size := cfg.calcCacheSize(tt.epoch)
236236
if size != tt.size {
237237
t.Errorf("failed on %d: size mismatch: have %d, want %d", i, size, tt.size)
238238
}
@@ -311,7 +311,7 @@ func TestCalcDatasetSize(t *testing.T) {
311311
}
312312

313313
for i, tt := range tests {
314-
size := calcDatasetSize(cfg, tt.epoch)
314+
size := cfg.calcDatasetSize(tt.epoch)
315315
if size != tt.size {
316316
t.Errorf("failed on %d: size mismatch: have %d, want %d", i, size, tt.size)
317317
}

0 commit comments

Comments
 (0)