-
Notifications
You must be signed in to change notification settings - Fork 43
/
mining_pow.go
38 lines (36 loc) · 992 Bytes
/
mining_pow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"encoding/binary"
"encoding/hex"
"os"
"time"
)
// mineSqlite3Database mines a SQLite3 database file, by adjusting the user_version field
// in the database header as a "nonce", and using SHA256 for the actual hashing. The file
// must exist and must be closed.
func mineSqlite3Database(fileName string, difficultyBits int) (string, error) {
startNonce := uint32(time.Now().Unix())
f, err := os.OpenFile(fileName, os.O_RDWR, 0)
if err != nil {
return "", err
}
defer f.Close()
b := make([]byte, 4)
for nonce := startNonce + 1; nonce != startNonce; nonce++ {
binary.LittleEndian.PutUint32(b, nonce)
_, err := f.WriteAt(b, 60) // https://www.sqlite.org/fileformat2.html#database_header
if err != nil {
return "", err
}
f.Sync()
hash, err := hashFileToBytes(fileName)
if err != nil {
return "", err
}
nZeroes := countStartZeroBits(hash)
if nZeroes == difficultyBits {
return hex.EncodeToString(hash), nil
}
}
return "", nil
}