Skip to content

Commit

Permalink
Merge branch 'merge-upstream-1.2.0' into merge-v1.2.0-fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
okilisan committed Apr 5, 2024
2 parents 3c63c5e + 0bed91d commit 75d1433
Show file tree
Hide file tree
Showing 30 changed files with 681 additions and 38 deletions.
7 changes: 7 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"runtime"
"time"

"github.com/karlsen-network/karlsend/domain/consensus/utils/pow"
"github.com/karlsen-network/karlsend/infrastructure/config"
"github.com/karlsen-network/karlsend/infrastructure/db/database"
"github.com/karlsen-network/karlsend/infrastructure/db/database/ldb"
Expand Down Expand Up @@ -82,6 +83,12 @@ func (app *karlsendApp) main(startedChan chan<- struct{}) error {

// Show version at startup.
log.Infof("Version %s", version.Version())
log.Infof("Using KarlsenHashV2 impl: %s", pow.GetHashingAlgoVersion())
if !app.cfg.Testnet && !app.cfg.Devnet && !app.cfg.Simnet {
log.Warnf("You are trying to connect to Mainnet")
log.Errorf("This version is using KarlsenHashV2, please add --testnet parameter")
os.Exit(42)
}

// Enable http profiling server if requested.
if app.cfg.Profile != "" {
Expand Down
3 changes: 3 additions & 0 deletions app/protocol/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func (m *Manager) AddTransaction(tx *externalapi.DomainTransaction, allowOrphan

// AddBlock adds the given block to the DAG and propagates it.
func (m *Manager) AddBlock(block *externalapi.DomainBlock) error {
//TODO switch this to debug level
log.Infof("NEW BLOCK ADDED ***************************************")
log.Infof("BlueWork[%s] BlueScore[%d] DAAScore[%d] Bits[%d] Version[%d]", block.Header.BlueWork(), block.Header.BlueScore(), block.Header.DAAScore(), block.Header.Bits(), block.Header.Version())
return m.context.AddBlock(block)
}

Expand Down
7 changes: 7 additions & 0 deletions cmd/karlsenminer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

_ "net/http/pprof"

"github.com/karlsen-network/karlsend/domain/consensus/utils/pow"
"github.com/karlsen-network/karlsend/infrastructure/os/signal"
"github.com/karlsen-network/karlsend/util/panics"
"github.com/karlsen-network/karlsend/util/profiling"
Expand All @@ -29,6 +30,12 @@ func main() {

// Show version at startup.
log.Infof("Version %s", version.Version())
log.Infof("Using KarlsenHashV2 impl: %s", pow.GetHashingAlgoVersion())
if !cfg.Testnet && !cfg.Devnet && !cfg.Simnet {
log.Warnf("You are trying to connect to Mainnet")
log.Errorf("This version is using KarlsenHashV2, please add --testnet parameter")
os.Exit(42)
}

// Enable http profiling server if requested.
if cfg.Profile != "" {
Expand Down
17 changes: 15 additions & 2 deletions cmd/karlsenminer/mineloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
)

var hashesTried uint64
var dagReady = false

const logHashRateInterval = 10 * time.Second

Expand Down Expand Up @@ -97,6 +98,12 @@ func logHashRate() {
spawn("logHashRate", func() {
lastCheck := time.Now()
for range time.Tick(logHashRateInterval) {

if !dagReady {
log.Infof("Generating DAG, please wait ...")
continue
}

currentHashesTried := atomic.LoadUint64(&hashesTried)
currentTime := time.Now()
kiloHashesTried := float64(currentHashesTried) / 1000.0
Expand Down Expand Up @@ -138,7 +145,11 @@ func handleFoundBlock(client *minerClient, block *externalapi.DomainBlock) error
func mineNextBlock(mineWhenNotSynced bool) *externalapi.DomainBlock {
nonce := rand.Uint64() // Use the global concurrent-safe random source.
for {
if !dagReady {
continue
}
nonce++
//fmt.Printf("mineNextBlock -- log1\n")
// For each nonce we try to build a block from the most up to date
// block template.
// In the rare case where the nonce space is exhausted for a specific
Expand All @@ -165,7 +176,6 @@ func getBlockForMining(mineWhenNotSynced bool) (*externalapi.DomainBlock, *pow.S

for {
tryCount++

shouldLog := (tryCount-1)%10 == 0
template, state, isSynced := templatemanager.Get()
if template == nil {
Expand Down Expand Up @@ -207,7 +217,10 @@ func templatesLoop(client *minerClient, miningAddr util.Address, errChan chan er
errChan <- errors.Wrapf(err, "Error getting block template from %s", client.Address())
return
}
err = templatemanager.Set(template)
err = templatemanager.Set(template, backendLog)
// after first template DAG is supposed to be ready
// TODO: refresh dag status in real time
dagReady = true
if err != nil {
errChan <- errors.Wrapf(err, "Error setting block template from %s", client.Address())
return
Expand Down
6 changes: 4 additions & 2 deletions cmd/karlsenminer/templatemanager/templatemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/karlsen-network/karlsend/app/appmessage"
"github.com/karlsen-network/karlsend/domain/consensus/model/externalapi"
"github.com/karlsen-network/karlsend/domain/consensus/utils/pow"
"github.com/karlsen-network/karlsend/infrastructure/logger"
)

var currentTemplate *externalapi.DomainBlock
Expand All @@ -27,15 +28,16 @@ func Get() (*externalapi.DomainBlock, *pow.State, bool) {
}

// Set sets the current template to work on
func Set(template *appmessage.GetBlockTemplateResponseMessage) error {
func Set(template *appmessage.GetBlockTemplateResponseMessage, backendLog *logger.Backend) error {
block, err := appmessage.RPCBlockToDomainBlock(template.Block)
if err != nil {
return err
}
lock.Lock()
defer lock.Unlock()
currentTemplate = block
currentState = pow.NewState(block.Header.ToMutable())
pow.SetLogger(backendLog, logger.LevelTrace)
currentState = pow.NewState(block.Header.ToMutable(), true)
isSynced = template.IsSynced
return nil
}
2 changes: 2 additions & 0 deletions domain/consensus/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ func (f *factory) NewConsensus(config *Config, db infrastructuredatabase.Databas
config.MaxBlockParents,
config.TimestampDeviationTolerance,
config.TargetTimePerBlock,
config.HFDAAScore,
config.MaxBlockLevel,

dbManager,
Expand Down Expand Up @@ -397,6 +398,7 @@ func (f *factory) NewConsensus(config *Config, db infrastructuredatabase.Databas
blockBuilder := blockbuilder.New(
dbManager,
genesisHash,
config.HFDAAScore,

difficultyManager,
pastMedianTimeManager,
Expand Down
10 changes: 9 additions & 1 deletion domain/consensus/processes/blockbuilder/block_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
type blockBuilder struct {
databaseContext model.DBManager
genesisHash *externalapi.DomainHash
hfDAAScore uint64

difficultyManager model.DifficultyManager
pastMedianTimeManager model.PastMedianTimeManager
Expand All @@ -42,6 +43,7 @@ type blockBuilder struct {
func New(
databaseContext model.DBManager,
genesisHash *externalapi.DomainHash,
hfDAAScore uint64,

difficultyManager model.DifficultyManager,
pastMedianTimeManager model.PastMedianTimeManager,
Expand All @@ -63,6 +65,7 @@ func New(
return &blockBuilder{
databaseContext: databaseContext,
genesisHash: genesisHash,
hfDAAScore: hfDAAScore,

difficultyManager: difficultyManager,
pastMedianTimeManager: pastMedianTimeManager,
Expand Down Expand Up @@ -225,8 +228,13 @@ func (bb *blockBuilder) buildHeader(stagingArea *model.StagingArea, transactions
return nil, err
}

version := constants.BlockVersionBeforeHF
if daaScore >= bb.hfDAAScore {
version = constants.BlockVersionAfterHF
}

return blockheader.NewImmutableBlockHeader(
constants.BlockVersion,
version,
parents,
hashMerkleRoot,
acceptedIDMerkleRoot,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,14 @@ func (bb *testBlockBuilder) buildUTXOInvalidHeader(stagingArea *model.StagingAre
})
}

version := constants.BlockVersionBeforeHF
if daaScore >= bb.hfDAAScore {
version = constants.BlockVersionAfterHF
}

bb.nonceCounter++
return blockheader.NewImmutableBlockHeader(
constants.BlockVersion,
version,
parents,
hashMerkleRoot,
&externalapi.DomainHash{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ func initBlockWithFirstTransactionDifferentThanCoinbase(consensusConfig *consens

return &externalapi.DomainBlock{
Header: blockheader.NewImmutableBlockHeader(
constants.BlockVersion,
constants.BlockVersionBeforeHF,
[]externalapi.BlockLevelParents{[]*externalapi.DomainHash{consensusConfig.GenesisHash}},
merkle.CalculateHashMerkleRoot([]*externalapi.DomainTransaction{tx}),
&externalapi.DomainHash{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ func TestCheckParentsIncest(t *testing.T) {
t.Fatalf("AddBlock: %+v", err)
}

version := constants.BlockVersion
version := constants.BlockVersionBeforeHF
if consensusConfig.HFDAAScore == 0 {
version = constants.BlockVersionAfterHF
}
directParentsRelationBlock := &externalapi.DomainBlock{
Header: blockheader.NewImmutableBlockHeader(
version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,20 @@ func (v *blockValidator) checkParentsLimit(header externalapi.BlockHeader) error
}

func (v *blockValidator) checkBlockVersion(header externalapi.BlockHeader) error {
if header.Version() != constants.BlockVersion {
return errors.Wrapf(
ruleerrors.ErrWrongBlockVersion, "The block version should be %d", constants.BlockVersion)
/*
if header.Version() != constants.BlockVersion {
return errors.Wrapf(
ruleerrors.ErrWrongBlockVersion, "The block version should be %d", constants.BlockVersion)
}
*/
if header.DAAScore() >= v.hfDAAScore {
if header.Version() != constants.BlockVersionAfterHF {
log.Warnf("After HF1 the block version should be %d - block[%d][v%d]", constants.BlockVersionAfterHF, header.DAAScore(), header.Version())
}
} else {
if header.Version() != constants.BlockVersionBeforeHF {
log.Warnf("Before HF1 the block version should be %d - block[%d][v%d]", constants.BlockVersionBeforeHF, header.DAAScore(), header.Version())
}
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ func CheckBlockVersion(t *testing.T, tc testapi.TestConsensus, consensusConfig *
t.Fatalf("BuildBlockWithParents: %+v", err)
}

expectedVersion := constants.BlockVersion
expectedVersion := constants.BlockVersionBeforeHF
if consensusConfig.HFDAAScore == 0 {
expectedVersion = constants.BlockVersionAfterHF
}

block.Header = blockheader.NewImmutableBlockHeader(
expectedVersion+1,
block.Header.Parents(),
Expand Down
3 changes: 3 additions & 0 deletions domain/consensus/processes/blockvalidator/blockvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type blockValidator struct {
maxBlockParents externalapi.KType
timestampDeviationTolerance int
targetTimePerBlock time.Duration
hfDAAScore uint64
maxBlockLevel int

databaseContext model.DBReader
Expand Down Expand Up @@ -63,6 +64,7 @@ func New(powMax *big.Int,
maxBlockParents externalapi.KType,
timestampDeviationTolerance int,
targetTimePerBlock time.Duration,
hfDAAScore uint64,
maxBlockLevel int,

databaseContext model.DBReader,
Expand Down Expand Up @@ -102,6 +104,7 @@ func New(powMax *big.Int,
maxBlockMass: maxBlockMass,
mergeSetSizeLimit: mergeSetSizeLimit,
maxBlockParents: maxBlockParents,
hfDAAScore: hfDAAScore,
maxBlockLevel: maxBlockLevel,

timestampDeviationTolerance: timestampDeviationTolerance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (v *blockValidator) validateDifficulty(stagingArea *model.StagingArea,
// difficulty is not performed.
func (v *blockValidator) checkProofOfWork(header externalapi.BlockHeader) error {
// The target difficulty must be larger than zero.
state := pow.NewState(header.ToMutable())
state := pow.NewState(header.ToMutable(), false)
target := &state.Target
if target.Sign() <= 0 {
return errors.Wrapf(ruleerrors.ErrNegativeTarget, "block target difficulty of %064x is too low",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestPOW(t *testing.T) {
// solveBlockWithWrongPOW increments the given block's nonce until it gets wrong POW (for test!).
func solveBlockWithWrongPOW(block *externalapi.DomainBlock) *externalapi.DomainBlock {
header := block.Header.ToMutable()
state := pow.NewState(header)
state := pow.NewState(header, false)
for i := uint64(0); i < math.MaxUint64; i++ {
state.Nonce = i
if !state.CheckProofOfWork() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestGHOSTDAG(t *testing.T) {
blockID := StringToDomainHash(testBlockData.ID)
dagTopology.parentsMap[*blockID] = StringToDomainHashSlice(testBlockData.Parents)
blockHeadersStore.dagMap[*blockID] = blockheader.NewImmutableBlockHeader(
constants.BlockVersion,
constants.BlockVersionBeforeHF,
[]externalapi.BlockLevelParents{StringToDomainHashSlice(testBlockData.Parents)},
nil,
nil,
Expand Down
3 changes: 2 additions & 1 deletion domain/consensus/utils/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import "math"

const (
// BlockVersion represents the current block version

Check failure on line 6 in domain/consensus/utils/constants/constants.go

View workflow job for this annotation

GitHub Actions / Tests, ubuntu-latest

comment on exported const BlockVersionBeforeHF should be of the form "BlockVersionBeforeHF ..."

Check failure on line 6 in domain/consensus/utils/constants/constants.go

View workflow job for this annotation

GitHub Actions / Tests, windows-latest

comment on exported const BlockVersionBeforeHF should be of the form "BlockVersionBeforeHF ..."

Check failure on line 6 in domain/consensus/utils/constants/constants.go

View workflow job for this annotation

GitHub Actions / Tests, macos-latest

comment on exported const BlockVersionBeforeHF should be of the form "BlockVersionBeforeHF ..."
BlockVersion uint16 = 1
BlockVersionBeforeHF uint16 = 1
BlockVersionAfterHF uint16 = 2

Check failure on line 8 in domain/consensus/utils/constants/constants.go

View workflow job for this annotation

GitHub Actions / Tests, ubuntu-latest

exported const BlockVersionAfterHF should have comment (or a comment on this block) or be unexported

Check failure on line 8 in domain/consensus/utils/constants/constants.go

View workflow job for this annotation

GitHub Actions / Tests, windows-latest

exported const BlockVersionAfterHF should have comment (or a comment on this block) or be unexported

Check failure on line 8 in domain/consensus/utils/constants/constants.go

View workflow job for this annotation

GitHub Actions / Tests, macos-latest

exported const BlockVersionAfterHF should have comment (or a comment on this block) or be unexported

// MaxTransactionVersion is the current latest supported transaction version.
MaxTransactionVersion uint16 = 0
Expand Down
2 changes: 1 addition & 1 deletion domain/consensus/utils/mining/solve.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// SolveBlock increments the given block's nonce until it matches the difficulty requirements in its bits field
func SolveBlock(block *externalapi.DomainBlock, rd *rand.Rand) {
header := block.Header.ToMutable()
state := pow.NewState(header)
state := pow.NewState(header, false)
for state.Nonce = rd.Uint64(); state.Nonce < math.MaxUint64; state.Nonce++ {
if state.CheckProofOfWork() {
header.SetNonce(state.Nonce)
Expand Down
Loading

0 comments on commit 75d1433

Please sign in to comment.