Skip to content

Commit

Permalink
feat: add basic hash calculations
Browse files Browse the repository at this point in the history
- fix config type

Signed-off-by: Ales Verbic <[email protected]>
  • Loading branch information
verbotenj committed Sep 1, 2023
1 parent b13ea5f commit 21fd0d7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
7 changes: 7 additions & 0 deletions cmd/bluefin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/blinklabs-io/bluefin/internal/config"
"github.com/blinklabs-io/bluefin/internal/indexer"
"github.com/blinklabs-io/bluefin/internal/logging"
"github.com/blinklabs-io/bluefin/internal/miner"
"github.com/blinklabs-io/bluefin/internal/storage"
"github.com/blinklabs-io/bluefin/internal/wallet"
"github.com/blinklabs-io/bluefin/internal/worker"
Expand Down Expand Up @@ -76,6 +77,12 @@ func main() {
// This should be started by the indexer reaching chain tip
worker.GetManager().Start(worker.WorkerParams{})

// Start miner
logger.Infof("starting miner on %s", cfg.Indexer.Network)
if err := miner.New().Start(); err != nil {
logger.Fatalf("failed to start miner: %s", err)
}

// Wait forever
select {}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/blinklabs-io/snek v0.7.0
github.com/dgraph-io/badger/v4 v4.2.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/minio/sha256-simd v1.0.1
go.uber.org/automaxprocs v1.5.3
go.uber.org/zap v1.25.0
gopkg.in/yaml.v2 v2.4.0
Expand All @@ -34,6 +35,7 @@ require (
github.com/google/flatbuffers v1.12.1 // indirect
github.com/jinzhu/copier v0.4.0 // indirect
github.com/klauspost/compress v1.12.3 // indirect
github.com/klauspost/cpuid/v2 v2.2.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
github.com/klauspost/compress v1.12.3 h1:G5AfA94pHPysR56qqrkO2pxEexdDzrpFJ6yt/VqWxVU=
github.com/klauspost/compress v1.12.3/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
github.com/klauspost/cpuid/v2 v2.2.3 h1:sxCkb+qR91z4vsqw4vGGZlDgPz3G7gjaLyK3V8y70BU=
github.com/klauspost/cpuid/v2 v2.2.3/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM=
github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
Expand Down Expand Up @@ -136,6 +140,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
63 changes: 61 additions & 2 deletions internal/miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,81 @@
package miner

import (
"encoding/hex"
"fmt"
"strings"

"github.com/blinklabs-io/bluefin/internal/config"
"github.com/blinklabs-io/bluefin/internal/logging"
"github.com/minio/sha256-simd"
)

type Miner struct {
Config *config.Config
Logger *logging.Logger
}

type State struct {
Nonce int
BlockNumber int
CurrentHash int
LeadingZeros string
DifficultyNumber int
EpochTime int
}

func New() *Miner {
return &Miner{
Config: config.GetConfig(),
Logger: logging.GetLogger(),
}
}

func (m *Miner) Start() {
// TODO
func (m *Miner) Start() error {
// TODO add real state
state := State{
Nonce: 0,
BlockNumber: 1,
CurrentHash: 1234567890,
LeadingZeros: "0xabcdef",
DifficultyNumber: 1,
EpochTime: 1627890123,
}

hash, nonce := calculateHash(state)
fmt.Printf("Hash with leading zeros: %s\n", hash)
fmt.Printf("Nonce: %d\n", nonce)
return nil
}

func calculateHash(state State) (string, int) {
nonce := state.Nonce
difficulty := state.DifficultyNumber

for {
// Construct the input using the state fields and nonce
input := fmt.Sprintf("%x%d%d%x%d%d",
nonce,
state.BlockNumber,
state.CurrentHash,
state.LeadingZeros,
state.DifficultyNumber,
state.EpochTime,
)

// Calculate the hash
hasher := sha256.New()
hasher.Write([]byte(input))
hash := hasher.Sum(nil)

// Convert the hash to a hexadecimal string
hashStr := hex.EncodeToString(hash)

// Check if the hash has the required number of leading zeros
if strings.HasPrefix(hashStr, strings.Repeat("00", difficulty)) {
return hashStr, nonce
}

nonce++
}
}

0 comments on commit 21fd0d7

Please sign in to comment.