Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix and changes from garden env testing #1665

Merged
merged 11 commits into from
Apr 26, 2024
Merged
9 changes: 9 additions & 0 deletions cmd/go-quai/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"runtime/debug"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -18,6 +19,14 @@ var rootCmd = &cobra.Command{
}

func Execute() error {
defer func() {
if r := recover(); r != nil {
log.Global.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
}).Fatal("Go-Quai Panicked")
}
}()
err := rootCmd.Execute()
if err != nil {
return err
Expand Down
9 changes: 9 additions & 0 deletions cmd/go-quai/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"os/signal"
"path/filepath"
"runtime/debug"
"syscall"
"time"

Expand Down Expand Up @@ -173,6 +174,14 @@ func readWithTimeout(buf *bufio.Reader, ctx context.Context) (string, error) {
defer ticker.Stop()

go func() {
defer func() {
if r := recover(); r != nil {
log.Global.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
}).Fatal("Go-Quai Panicked")
}
}()
response, err := buf.ReadString('\n')
if err != nil {
errChan <- err
Expand Down
8 changes: 8 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,14 @@ func EnablePprof() {
runtime.SetMutexProfileFraction(1)
port := "8085"
go func() {
defer func() {
if r := recover(); r != nil {
log.Global.WithFields(log.Fields{
"error": r,
"stacktrace": string(godebug.Stack()),
}).Error("Go-Quai Panicked")
}
}()
log.Global.Print(http.ListenAndServe("localhost:"+port, nil))
}()
}
Expand Down
17 changes: 17 additions & 0 deletions cmd/utils/hierarchical_coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"fmt"
"runtime/debug"
"sync"
"time"

Expand Down Expand Up @@ -141,6 +142,14 @@ func (hc *HierarchicalCoordinator) startNode(logPath string, quaiBackend quai.Co

go func() {
defer hc.wg.Done()
defer func() {
if r := recover(); r != nil {
logger.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
}).Fatal("Go-Quai Panicked")
}
}()
<-hc.quitCh
logger.Info("Context cancelled, shutting down node")
stack.Close()
Expand All @@ -161,6 +170,14 @@ func (hc *HierarchicalCoordinator) ConsensusBackend() quai.ConsensusAPI {

func (hc *HierarchicalCoordinator) expansionEventLoop() {
defer hc.wg.Done()
defer func() {
if r := recover(); r != nil {
log.Global.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
}).Fatal("Go-Quai Panicked")
}
}()

for {
select {
Expand Down
27 changes: 24 additions & 3 deletions consensus/blake3pow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math/big"
"runtime"
"runtime/debug"
"time"

mapset "github.com/deckarep/golang-set"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/dominant-strategies/go-quai/core/state"
"github.com/dominant-strategies/go-quai/core/types"
"github.com/dominant-strategies/go-quai/core/vm"
"github.com/dominant-strategies/go-quai/log"
"github.com/dominant-strategies/go-quai/params"
"github.com/dominant-strategies/go-quai/trie"
"modernc.org/mathutil"
Expand Down Expand Up @@ -118,6 +120,14 @@ func (blake3pow *Blake3pow) VerifyHeaders(chain consensus.ChainHeaderReader, hea
)
for i := 0; i < workers; i++ {
go func() {
defer func() {
if r := recover(); r != nil {
blake3pow.logger.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
}).Error("Go-Quai Panicked")
}
}()
for index := range inputs {
errors[index] = blake3pow.verifyHeaderWorker(chain, headers, index, unixNow)
done <- index
Expand All @@ -127,6 +137,14 @@ func (blake3pow *Blake3pow) VerifyHeaders(chain consensus.ChainHeaderReader, hea

errorsOut := make(chan error, len(headers))
go func() {
defer func() {
if r := recover(); r != nil {
blake3pow.logger.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
}).Fatal("Go-Quai Panicked")
}
}()
defer close(inputs)
var (
in, out = 0, 0
Expand Down Expand Up @@ -468,7 +486,7 @@ func (blake3pow *Blake3pow) CalcDifficulty(chain consensus.ChainHeaderReader, pa

if chain.IsGenesisHash(parent.Hash()) {
// Divide the parent difficulty by the number of slices running at the time of expansion
return new(big.Int).Div(parent.Difficulty(), big.NewInt(int64((blake3pow.NodeLocation().Region()+1)*(blake3pow.NodeLocation().Zone()+1))))
return parent.Difficulty()
wizeguyy marked this conversation as resolved.
Show resolved Hide resolved
}

parentOfParent := chain.GetHeaderByHash(parent.ParentHash())
Expand Down Expand Up @@ -557,7 +575,7 @@ func (blake3pow *Blake3pow) Finalize(chain consensus.ChainHeaderReader, header *
}
state.CreateAccount(lockupContract)

alloc := core.ReadGenesisAlloc("genallocs/gen_alloc_"+nodeLocation.Name()+".json", blake3pow.logger)
alloc := core.ReadGenesisAlloc("genallocs/gen_quai_alloc_"+nodeLocation.Name()+".json", blake3pow.logger)
blake3pow.logger.WithField("alloc", len(alloc)).Info("Allocating genesis accounts")

for addressString, account := range alloc {
Expand Down Expand Up @@ -593,7 +611,10 @@ func (blake3pow *Blake3pow) FinalizeAndAssemble(chain consensus.ChainHeaderReade
blake3pow.Finalize(chain, header, state)
}

woBody := types.NewWorkObjectBody(header.Header(), txs, etxs, uncles, subManifest, receipts, trie.NewStackTrie(nil), nodeCtx)
woBody, err := types.NewWorkObjectBody(header.Header(), txs, etxs, uncles, subManifest, receipts, trie.NewStackTrie(nil), nodeCtx)
if err != nil {
return nil, err
}
// Header seems complete, assemble into a block and return
return types.NewWorkObject(header.WorkObjectHeader(), woBody, nil, types.BlockObject), nil
}
Expand Down
17 changes: 17 additions & 0 deletions consensus/blake3pow/sealer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/big"
"math/rand"
"runtime"
"runtime/debug"
"sync"

"github.com/dominant-strategies/go-quai/core/types"
Expand Down Expand Up @@ -71,12 +72,28 @@ func (blake3pow *Blake3pow) Seal(header *types.WorkObject, results chan<- *types
for i := 0; i < threads; i++ {
pend.Add(1)
go func(id int, nonce uint64) {
defer func() {
if r := recover(); r != nil {
blake3pow.logger.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
}).Error("Go-Quai Panicked")
}
}()
defer pend.Done()
blake3pow.mine(header, id, nonce, abort, locals)
}(i, uint64(blake3pow.rand.Int63()))
}
// Wait until sealing is terminated or a nonce is found
go func() {
defer func() {
if r := recover(); r != nil {
blake3pow.logger.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
}).Error("Go-Quai Panicked")
}
}()
var result *types.WorkObject
select {
case <-stop:
Expand Down
23 changes: 16 additions & 7 deletions consensus/progpow/algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"math/big"
"reflect"
"runtime"
"runtime/debug"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -143,14 +144,14 @@ func SeedHash(block uint64) []byte {
// algorithm from Strict Memory Hard Hashing Functions (2014). The output is a
// set of 524288 64-byte values.
// This method places the result into dest in machine byte order.
func generateCache(dest []uint32, epoch uint64, seed []byte) {
func generateCache(dest []uint32, epoch uint64, seed []byte, logger *log.Logger) {
// Print some debug logs to allow analysis on low end devices

start := time.Now()
defer func() {
elapsed := time.Since(start)

logEntry := log.Global.WithFields(log.Fields{
logEntry := logger.WithFields(log.Fields{
"elapsed": common.PrettyDuration(elapsed),
})

Expand All @@ -177,12 +178,20 @@ func generateCache(dest []uint32, epoch uint64, seed []byte) {
defer close(done)

go func() {
defer func() {
if r := recover(); r != nil {
logger.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
}).Error("Go-Quai Panicked")
}
}()
for {
select {
case <-done:
return
case <-time.After(3 * time.Second):
log.Global.WithFields(log.Fields{
logger.WithFields(log.Fields{
"percentage": uint64(atomic.LoadUint32(&progress) * 100 / uint32(rows) / 4),
"elapsed": common.PrettyDuration(time.Since(start)),
}).Info("Generating ethash verification cache")
Expand Down Expand Up @@ -222,7 +231,7 @@ func generateCache(dest []uint32, epoch uint64, seed []byte) {

// generateCDag generates the cDag used for progpow. If the 'cDag' is nil, this method is a no-op. Otherwise
// it expects the cDag to be of size progpowCacheWords
func generateCDag(cDag, cache []uint32, epoch uint64) {
func generateCDag(cDag, cache []uint32, epoch uint64, logger *log.Logger) {
if cDag == nil {
return
}
Expand All @@ -238,7 +247,7 @@ func generateCDag(cDag, cache []uint32, epoch uint64) {
}

elapsed := time.Since(start)
log.Global.WithFields(log.Fields{
logger.WithFields(log.Fields{
"elapsed": common.PrettyDuration(elapsed),
"epoch": epoch,
}).Debug("Generated progpow cDag")
Expand Down Expand Up @@ -301,7 +310,7 @@ func generateDatasetItem(cache []uint32, index uint32, keccak512 hasher) []byte

// generateDataset generates the entire ethash dataset for mining.
// This method places the result into dest in machine byte order.
func generateDataset(dest []uint32, epoch uint64, cache []uint32) {
func generateDataset(dest []uint32, epoch uint64, cache []uint32, logger *log.Logger) {
start := time.Now()
defer func() {
elapsed := time.Since(start)
Expand Down Expand Up @@ -358,7 +367,7 @@ func generateDataset(dest []uint32, epoch uint64, cache []uint32) {
copy(dataset[index*hashBytes:], item)

if status := atomic.AddUint32(&progress, 1); status%percent == 0 {
log.Global.WithFields(log.Fields{
logger.WithFields(log.Fields{
"percentage": uint64(status * 100 / uint32(size/hashBytes)),
"elapsed": common.PrettyDuration(time.Since(start)),
}).Info("Generating DAG in progress")
Expand Down
29 changes: 25 additions & 4 deletions consensus/progpow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"math/big"
"runtime"
"runtime/debug"
"time"

mapset "github.com/deckarep/golang-set"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/dominant-strategies/go-quai/core/state"
"github.com/dominant-strategies/go-quai/core/types"
"github.com/dominant-strategies/go-quai/core/vm"
"github.com/dominant-strategies/go-quai/log"
"github.com/dominant-strategies/go-quai/params"
"github.com/dominant-strategies/go-quai/trie"
"modernc.org/mathutil"
Expand Down Expand Up @@ -120,6 +122,14 @@ func (progpow *Progpow) VerifyHeaders(chain consensus.ChainHeaderReader, headers
)
for i := 0; i < workers; i++ {
go func() {
defer func() {
if r := recover(); r != nil {
progpow.logger.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
}).Fatal("Go-Quai Panicked")
}
}()
for index := range inputs {
errors[index] = progpow.verifyHeaderWorker(chain, headers, index, unixNow)
done <- index
Expand All @@ -129,6 +139,14 @@ func (progpow *Progpow) VerifyHeaders(chain consensus.ChainHeaderReader, headers

errorsOut := make(chan error, len(headers))
go func() {
defer func() {
if r := recover(); r != nil {
progpow.logger.WithFields(log.Fields{
"error": r,
"stacktrace": string(debug.Stack()),
}).Fatal("Go-Quai Panicked")
}
}()
defer close(inputs)
var (
in, out = 0, 0
Expand Down Expand Up @@ -467,7 +485,7 @@ func (progpow *Progpow) CalcDifficulty(chain consensus.ChainHeaderReader, parent

if chain.IsGenesisHash(parent.Hash()) {
// Genesis Difficulty is the difficulty in the Genesis Block divided by the number of total slices active
return new(big.Int).Div(parent.Difficulty(), big.NewInt(int64((progpow.NodeLocation().Region()+1)*(progpow.NodeLocation().Zone()+1))))
return parent.Difficulty()
wizeguyy marked this conversation as resolved.
Show resolved Hide resolved
}
parentOfParent := chain.GetHeaderByHash(parent.ParentHash())
if parentOfParent == nil || chain.IsGenesisHash(parentOfParent.Hash()) {
Expand Down Expand Up @@ -510,7 +528,7 @@ func (progpow *Progpow) ComputePowLight(header *types.WorkObjectHeader) (mixHash
ethashCache := progpow.cache(blockNumber)
if ethashCache.cDag == nil {
cDag := make([]uint32, progpowCacheWords)
generateCDag(cDag, ethashCache.cache, blockNumber/epochLength)
generateCDag(cDag, ethashCache.cache, blockNumber/epochLength, progpow.logger)
ethashCache.cDag = cDag
}
return progpowLight(size, cache, hash, nonce, blockNumber, ethashCache.cDag)
Expand Down Expand Up @@ -593,7 +611,7 @@ func (progpow *Progpow) Finalize(chain consensus.ChainHeaderReader, header *type
}
state.CreateAccount(lockupContract)

alloc := core.ReadGenesisAlloc("genallocs/gen_alloc_"+nodeLocation.Name()+".json", progpow.logger)
alloc := core.ReadGenesisAlloc("genallocs/gen_quai_alloc_"+nodeLocation.Name()+".json", progpow.logger)
progpow.logger.WithField("alloc", len(alloc)).Info("Allocating genesis accounts")

for addressString, account := range alloc {
Expand Down Expand Up @@ -629,7 +647,10 @@ func (progpow *Progpow) FinalizeAndAssemble(chain consensus.ChainHeaderReader, h
progpow.Finalize(chain, header, state)
}

woBody := types.NewWorkObjectBody(header.Header(), txs, etxs, uncles, subManifest, receipts, trie.NewStackTrie(nil), nodeCtx)
woBody, err := types.NewWorkObjectBody(header.Header(), txs, etxs, uncles, subManifest, receipts, trie.NewStackTrie(nil), nodeCtx)
if err != nil {
return nil, err
}
// Header seems complete, assemble into a block and return
return types.NewWorkObject(header.WorkObjectHeader(), woBody, nil, types.BlockObject), nil
}
Expand Down
Loading
Loading