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

Difficulty adjustment fix between golang and rusty nodes #62

2 changes: 1 addition & 1 deletion cmd/karlsenminer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {

// Show version at startup.
log.Infof("Version %s", version.Version())
log.Infof("Using KarlsenHashV2 impl: %s", pow.GetHashingAlgoVersion())
log.Infof("Miner compatibility KarlsenHashV1 & KarlsenHashV2 impl: %s", pow.GetHashingAlgoVersion())

// Enable http profiling server if requested.
if cfg.Profile != "" {
Expand Down
2 changes: 1 addition & 1 deletion domain/consensus/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type GHOSTDAGManagerConstructor func(

// DifficultyManagerConstructor is the function signature for a constructor of a type implementing model.DifficultyManager
type DifficultyManagerConstructor func(model.DBReader, model.GHOSTDAGManager, model.GHOSTDAGDataStore,
model.BlockHeaderStore, model.DAABlocksStore, model.DAGTopologyManager, model.DAGTraversalManager, *big.Int, int, bool, time.Duration,
model.BlockHeaderStore, model.DAABlocksStore, model.DAGTopologyManager, model.DAGTraversalManager, *big.Int, int, int, bool, time.Duration,
*externalapi.DomainHash, uint32) model.DifficultyManager

// PastMedianTimeManagerConstructor is the function signature for a constructor of a type implementing model.PastMedianTimeManager
Expand Down
1 change: 1 addition & 0 deletions domain/consensus/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ func (f *factory) NewConsensus(config *Config, db infrastructuredatabase.Databas
dagTraversalManager,
config.PowMax,
config.DifficultyAdjustmentWindowSize,
config.MinDifficultyWindowLen,
config.DisableDifficultyAdjustment,
config.TargetTimePerBlock,
config.GenesisHash,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ type DifficultyManager interface {
RequiredDifficulty(stagingArea *StagingArea, blockHash *externalapi.DomainHash) (uint32, error)
EstimateNetworkHashesPerSecond(startHash *externalapi.DomainHash, windowSize int) (uint64, error)
GenesisDifficulty() uint32
DifficultyAdjustmentWindowSize() int
}
4 changes: 2 additions & 2 deletions domain/consensus/processes/blockbuilder/block_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ func (bb *blockBuilder) buildHeader(stagingArea *model.StagingArea, transactions
}

// adjust the difficulty
/*if daaScore <= (bb.hfDAAScore+10) && daaScore >= bb.hfDAAScore {
if daaScore <= (bb.hfDAAScore+uint64(bb.difficultyManager.DifficultyAdjustmentWindowSize())) && daaScore >= bb.hfDAAScore {
bits = bb.difficultyManager.GenesisDifficulty()
}*/
}

hashMerkleRoot := bb.newBlockHashMerkleRoot(transactions)
acceptedIDMerkleRoot, err := bb.newBlockAcceptedIDMerkleRoot(stagingArea)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (v *blockValidator) validateDifficulty(stagingArea *model.StagingArea,
}

// bypass the difficulty check during HF
if header.DAAScore() <= (v.hfDAAScore+10) && header.DAAScore() >= v.hfDAAScore {
if header.DAAScore() <= (v.hfDAAScore+uint64(v.difficultyManager.DifficultyAdjustmentWindowSize())) && header.DAAScore() >= v.hfDAAScore {
expectedBits = v.difficultyManager.GenesisDifficulty()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func TestValidateDifficulty(t *testing.T) {
genesisBits: consensusConfig.GenesisBlock.Header.Bits()}
factory.SetTestDifficultyManager(func(_ model.DBReader, _ model.GHOSTDAGManager, _ model.GHOSTDAGDataStore,
_ model.BlockHeaderStore, daaBlocksStore model.DAABlocksStore, _ model.DAGTopologyManager,
_ model.DAGTraversalManager, _ *big.Int, _ int, _ bool, _ time.Duration,
_ model.DAGTraversalManager, _ *big.Int, _ int, _ int, _ bool, _ time.Duration,
_ *externalapi.DomainHash, _ uint32) model.DifficultyManager {

mocDifficulty.daaBlocksStore = daaBlocksStore
Expand Down Expand Up @@ -344,11 +344,17 @@ func TestValidateDifficulty(t *testing.T) {
}

type mocDifficultyManager struct {
testDifficulty uint32
testGenesisBits uint32
daaBlocksStore model.DAABlocksStore
genesisDaaScore uint64
genesisBits uint32
testDifficulty uint32
testGenesisBits uint32
daaBlocksStore model.DAABlocksStore
genesisDaaScore uint64
genesisBits uint32
difficultyAdjustmentWindowSize int
}

// DifficultyAdjustmentWindowSize implements model.DifficultyManager.
func (dm *mocDifficultyManager) DifficultyAdjustmentWindowSize() int {
return dm.difficultyAdjustmentWindowSize
}

// GenesisDifficulty implements model.DifficultyManager.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type difficultyManager struct {
genesisHash *externalapi.DomainHash
powMax *big.Int
difficultyAdjustmentWindowSize int
minDifficultyWindowLen int
disableDifficultyAdjustment bool
targetTimePerBlock time.Duration
genesisBits uint32
Expand All @@ -41,6 +42,7 @@ func New(databaseContext model.DBReader,
dagTraversalManager model.DAGTraversalManager,
powMax *big.Int,
difficultyAdjustmentWindowSize int,
minDifficultyWindowLen int,
disableDifficultyAdjustment bool,
targetTimePerBlock time.Duration,
genesisHash *externalapi.DomainHash,
Expand All @@ -55,6 +57,7 @@ func New(databaseContext model.DBReader,
dagTraversalManager: dagTraversalManager,
powMax: powMax,
difficultyAdjustmentWindowSize: difficultyAdjustmentWindowSize,
minDifficultyWindowLen: minDifficultyWindowLen,
disableDifficultyAdjustment: disableDifficultyAdjustment,
targetTimePerBlock: targetTimePerBlock,
genesisHash: genesisHash,
Expand Down Expand Up @@ -105,6 +108,11 @@ func (dm *difficultyManager) GenesisDifficulty() uint32 {
return dm.genesisBits
}

// DifficultyAdjustmentWindowSize returns the size of the window of diff adjustment
func (dm *difficultyManager) DifficultyAdjustmentWindowSize() int {
return dm.difficultyAdjustmentWindowSize
}

func (dm *difficultyManager) requiredDifficultyFromTargetsWindow(targetsWindow blockWindow) (uint32, error) {
if dm.disableDifficultyAdjustment {
return dm.genesisBits, nil
Expand All @@ -117,7 +125,8 @@ func (dm *difficultyManager) requiredDifficultyFromTargetsWindow(targetsWindow b
// We could instead clamp the timestamp difference to `targetTimePerBlock`,
// but then everything will cancel out and we'll get the target from the last block, which will be the same as genesis.
// We add 64 as a safety margin
if len(targetsWindow) < 2 || len(targetsWindow) < dm.difficultyAdjustmentWindowSize {
//if len(targetsWindow) < 2 || len(targetsWindow) < dm.difficultyAdjustmentWindowSize {
if len(targetsWindow) < 2 || len(targetsWindow) < dm.minDifficultyWindowLen {
return dm.genesisBits, nil
}

Expand Down
2 changes: 1 addition & 1 deletion domain/consensus/processes/pruningmanager/pruning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestPruning(t *testing.T) {
dagconfig.MainnetParams.Name: "503",
dagconfig.TestnetParams.Name: "502",
dagconfig.DevnetParams.Name: "503",
dagconfig.SimnetParams.Name: "503",
dagconfig.SimnetParams.Name: "502",
},
}

Expand Down
12 changes: 10 additions & 2 deletions domain/dagconfig/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ type Params struct {
// to calculate the required difficulty of each block.
DifficultyAdjustmentWindowSize int

// MinDifficultyWindowLen is the minimum size of window that can be inspected
// under this value the genesis difficulty is sent.
MinDifficultyWindowLen int

// These fields are related to voting on consensus rule changes as
// defined by BIP0009.
//
Expand Down Expand Up @@ -234,6 +238,7 @@ var MainnetParams = Params{
TargetTimePerBlock: defaultTargetTimePerBlock,
FinalityDuration: defaultFinalityDuration,
DifficultyAdjustmentWindowSize: defaultDifficultyAdjustmentWindowSize,
MinDifficultyWindowLen: 10,
TimestampDeviationTolerance: defaultTimestampDeviationTolerance,

// Consensus rule change deployments.
Expand Down Expand Up @@ -303,6 +308,7 @@ var TestnetParams = Params{
TargetTimePerBlock: defaultTargetTimePerBlock,
FinalityDuration: defaultFinalityDuration,
DifficultyAdjustmentWindowSize: defaultDifficultyAdjustmentWindowSize,
MinDifficultyWindowLen: 10,
TimestampDeviationTolerance: defaultTimestampDeviationTolerance,

// Consensus rule change deployments.
Expand Down Expand Up @@ -372,6 +378,7 @@ var SimnetParams = Params{
TargetTimePerBlock: time.Millisecond,
FinalityDuration: time.Minute,
DifficultyAdjustmentWindowSize: defaultDifficultyAdjustmentWindowSize,
MinDifficultyWindowLen: 10,
TimestampDeviationTolerance: defaultTimestampDeviationTolerance,

// Consensus rule change deployments.
Expand Down Expand Up @@ -410,7 +417,7 @@ var SimnetParams = Params{

MaxBlockLevel: 250,
MergeDepth: defaultMergeDepth,
HFDAAScore: 50,
HFDAAScore: 3600,
}

// DevnetParams defines the network parameters for the development Karlsen network.
Expand All @@ -433,6 +440,7 @@ var DevnetParams = Params{
TargetTimePerBlock: defaultTargetTimePerBlock,
FinalityDuration: defaultFinalityDuration,
DifficultyAdjustmentWindowSize: defaultDifficultyAdjustmentWindowSize,
MinDifficultyWindowLen: 10,
TimestampDeviationTolerance: defaultTimestampDeviationTolerance,

// Consensus rule change deployments.
Expand Down Expand Up @@ -473,7 +481,7 @@ var DevnetParams = Params{

MaxBlockLevel: 250,
MergeDepth: defaultMergeDepth,
HFDAAScore: 50,
HFDAAScore: 3600,
}

// ErrDuplicateNet describes an error where the parameters for a Karlsen
Expand Down
Loading