Skip to content

Commit

Permalink
HF procedure with diff adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
okilisan committed Aug 11, 2024
1 parent 39f0f3f commit 4ab6b22
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ type DifficultyManager interface {
StageDAADataAndReturnRequiredDifficulty(stagingArea *StagingArea, blockHash *externalapi.DomainHash, isBlockWithTrustedData bool) (uint32, error)
RequiredDifficulty(stagingArea *StagingArea, blockHash *externalapi.DomainHash) (uint32, error)
EstimateNetworkHashesPerSecond(startHash *externalapi.DomainHash, windowSize int) (uint64, error)
GenesisDifficulty() uint32
}
10 changes: 8 additions & 2 deletions domain/consensus/processes/blockbuilder/block_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ func (bb *blockBuilder) buildHeader(stagingArea *model.StagingArea, transactions
if err != nil {
return nil, err
}

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

hashMerkleRoot := bb.newBlockHashMerkleRoot(transactions)
acceptedIDMerkleRoot, err := bb.newBlockAcceptedIDMerkleRoot(stagingArea)
if err != nil {
Expand All @@ -228,9 +234,9 @@ func (bb *blockBuilder) buildHeader(stagingArea *model.StagingArea, transactions
return nil, err
}

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

return blockheader.NewImmutableBlockHeader(
Expand Down
4 changes: 2 additions & 2 deletions domain/consensus/processes/blockbuilder/test_block_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ func (bb *testBlockBuilder) buildUTXOInvalidHeader(stagingArea *model.StagingAre
})
}

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

bb.nonceCounter++
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.BlockVersionBeforeHF,
constants.BlockVersionKHashV1,
[]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,9 +109,9 @@ func TestCheckParentsIncest(t *testing.T) {
t.Fatalf("AddBlock: %+v", err)
}

version := constants.BlockVersionBeforeHF
version := constants.BlockVersionKHashV1
if consensusConfig.HFDAAScore == 0 {
version = constants.BlockVersionAfterHF
version = constants.BlockVersionKHashV2
}
directParentsRelationBlock := &externalapi.DomainBlock{
Header: blockheader.NewImmutableBlockHeader(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ func (v *blockValidator) checkBlockVersion(header externalapi.BlockHeader) error
}
*/
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())
if header.Version() != constants.BlockVersionKHashV2 {
log.Warnf("After HF1 the block version should be %d - block[%d][v%d]", constants.BlockVersionKHashV2, 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())
if header.Version() != constants.BlockVersionKHashV1 {
log.Warnf("Before HF1 the block version should be %d - block[%d][v%d]", constants.BlockVersionKHashV1, header.DAAScore(), header.Version())
}
}
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ func CheckBlockVersion(t *testing.T, tc testapi.TestConsensus, consensusConfig *
t.Fatalf("BuildBlockWithParents: %+v", err)
}

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

block.Header = blockheader.NewImmutableBlockHeader(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ func (v *blockValidator) validateDifficulty(stagingArea *model.StagingArea,
return err
}

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

if header.Bits() != expectedBits {
return errors.Wrapf(ruleerrors.ErrUnexpectedDifficulty, "block difficulty of %d is not the expected value of %d", header.Bits(), expectedBits)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,9 @@ func TestCheckPruningPointViolation(t *testing.T) {
func TestValidateDifficulty(t *testing.T) {
testutils.ForAllNets(t, true, func(t *testing.T, consensusConfig *consensus.Config) {
factory := consensus.NewFactory()
mocDifficulty := &mocDifficultyManager{genesisDaaScore: consensusConfig.GenesisBlock.Header.DAAScore()}
mocDifficulty := &mocDifficultyManager{
genesisDaaScore: consensusConfig.GenesisBlock.Header.DAAScore(),
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,
Expand Down Expand Up @@ -346,6 +348,12 @@ type mocDifficultyManager struct {
testGenesisBits uint32
daaBlocksStore model.DAABlocksStore
genesisDaaScore uint64
genesisBits uint32
}

// GenesisDifficulty implements model.DifficultyManager.
func (dm *mocDifficultyManager) GenesisDifficulty() uint32 {
return dm.genesisBits
}

// RequiredDifficulty returns the difficulty required for the test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ func (dm *difficultyManager) RequiredDifficulty(stagingArea *model.StagingArea,
return dm.requiredDifficultyFromTargetsWindow(targetsWindow)
}

// GenesisDifficulty returns the difficulty current network genesis block
func (dm *difficultyManager) GenesisDifficulty() uint32 {
return dm.genesisBits
}

func (dm *difficultyManager) requiredDifficultyFromTargetsWindow(targetsWindow blockWindow) (uint32, error) {
if dm.disableDifficultyAdjustment {
return dm.genesisBits, nil
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.BlockVersionBeforeHF,
constants.BlockVersionKHashV1,
[]externalapi.BlockLevelParents{StringToDomainHashSlice(testBlockData.Parents)},
nil,
nil,
Expand Down
8 changes: 4 additions & 4 deletions domain/consensus/utils/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package constants
import "math"

const (
// BlockVersionBeforeHF represents the block version before the HF
BlockVersionBeforeHF uint16 = 1
// BlockVersionKHashV1 represents the block version before the HF
BlockVersionKHashV1 uint16 = 1

// BlockVersionAfterHF represents the block version after the HF
BlockVersionAfterHF uint16 = 2
// BlockVersionKHashV2 represents the block version after the HF
BlockVersionKHashV2 uint16 = 2

// MaxTransactionVersion is the current latest supported transaction version.
MaxTransactionVersion uint16 = 0
Expand Down
6 changes: 3 additions & 3 deletions domain/consensus/utils/pow/pow.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ func (state *State) CalculateProofOfWorkValue() *big.Int {
//middleHash := state.mat.HeavyHash(powHash)
//log.Infof("Hash b3-1: %x", powHash.ByteSlice())
finalHash := powHash
if state.blockVersion == constants.BlockVersionBeforeHF {
log.Infof("Using khashv1 %d %d\n", state.blockVersion, constants.BlockVersionBeforeHF)
if state.blockVersion == constants.BlockVersionKHashV1 {
log.Debugf("Using khashv1 %d %d\n", state.blockVersion, constants.BlockVersionKHashV1)
finalHash = state.mat.HeavyHash(powHash)
} else {
log.Infof("Using khashv2 %d %d\n", state.blockVersion, constants.BlockVersionBeforeHF)
log.Debugf("Using khashv2 %d %d\n", state.blockVersion, constants.BlockVersionKHashV1)
middleHash := fishHashPlus(&state.context, powHash)
writer2 := hashes.NewPoWHashWriter()
writer2.InfallibleWrite(middleHash.ByteSlice())
Expand Down
2 changes: 1 addition & 1 deletion domain/dagconfig/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ var MainnetParams = Params{
MaxBlockLevel: 225,
MergeDepth: defaultMergeDepth,
// todo: define the fork date DAAscore
HFDAAScore: 22000042,
HFDAAScore: 42000042,
}

// TestnetParams defines the network parameters for the test Karlsen network.
Expand Down

0 comments on commit 4ab6b22

Please sign in to comment.