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

Mainnet karlsenhashv2 #58

Merged
merged 48 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
707297b
First fishhash tests
Dec 26, 2023
3f0537b
refresh seeder url
Dec 27, 2023
a7eaaea
add minor version
Dec 27, 2023
f239d00
fix logging
Dec 27, 2023
4c1311d
Merge pull request #2 from karlsen-network/master
wam-rd Dec 27, 2023
17b616d
vers
Dec 29, 2023
59e81f9
Fix issue on word32 usage in kernel mixing step
Dec 29, 2023
f19c8a5
Fix the dataset and light cache generation.
Jan 4, 2024
7e8bc86
Last fixes on kernel has
Jan 5, 2024
b6f296a
Code cleanup and dag generation correct logging
Jan 21, 2024
223b0cd
Added lock on mainnet connection
Jan 21, 2024
b9c6d1a
Merge pull request #29 from wam-rd/fishhash
lemois-1337 Jan 21, 2024
c093cef
Merge pull request #5 from karlsen-network/master
okilisan Mar 13, 2024
cd5888a
Fishhash plus implementation with hard fork procedure
Mar 19, 2024
0bed91d
Remove blocking tests on block version because node contains both alg…
Mar 31, 2024
a8405b3
hard fork procedure from khashv1 to khashv2
Jun 26, 2024
5dea9c1
fix critical bug in matrix generation
Jun 28, 2024
e7ce09b
fix lint issues
Jun 28, 2024
39f0f3f
fix lint issues 2
Jun 28, 2024
4ab6b22
HF procedure with diff adjustment
okilisan Aug 11, 2024
687bb34
Merge pull request #52 from okilisan/mainnet_karlsenhashv2
lemois-1337 Aug 23, 2024
5707e8c
Removed periodic race detection workflow
lemois-1337 Aug 23, 2024
e99a2e1
Added 'HFDAAScore' to 'simnet' to pass tests
lemois-1337 Aug 23, 2024
d1e678a
align with rusty block version test
okilisan Aug 23, 2024
866f1d6
Merge pull request #53 from okilisan/mainnet_karlsenhashv2
lemois-1337 Aug 24, 2024
0d50f2b
Merge remote-tracking branch 'refs/remotes/origin/mainnet_karlsenhash…
lemois-1337 Aug 24, 2024
e135ccd
Fixed pruning_test in simnet and devnet genesis from Rust node
lemois-1337 Aug 25, 2024
0cab1d1
Fixed remaining integration tests and Go modules update
lemois-1337 Aug 26, 2024
75d4ccf
Use 4-char abbreviation as rest of KLS logging system (POW->POWK)
lemois-1337 Aug 26, 2024
32a6946
Increase windows runner pagefile to 32gb
lemois-1337 Aug 26, 2024
f8d8eae
Remove Go cache in test workflow due to its constant failures
lemois-1337 Aug 26, 2024
e34b69a
Increase code coverage timeout to 120m due to khashv2.
lemois-1337 Aug 26, 2024
98b0730
Increase timeout in integration tests and sequential execution
lemois-1337 Aug 26, 2024
03a8258
Fixed 'BlockVersionKHashV2' in debug output and removed linebreak
lemois-1337 Aug 27, 2024
629e525
Partially revert e135ccd6ca1dafef9fd06c72639793fe6708647e:
lemois-1337 Aug 27, 2024
2ff2931
Moving khashv2 pre-computed dag file during stability tests
lemois-1337 Aug 27, 2024
8862cc3
Partially revert 0cab1d18a7b5dad418f2aa7683d4af39ac19bb99
lemois-1337 Aug 27, 2024
23b175a
Give orphans stability test more time to process blocks
lemois-1337 Aug 27, 2024
c56f01e
Increase Linux swapfile size in GitHub runner to avoid OOM
lemois-1337 Aug 28, 2024
f2d4fb4
Merge pull request #54 from lemois-1337/mainnet_karlsenhashv2
lemois-1337 Aug 28, 2024
29726b7
Increase swap size for code coverage to support khashv2
lemois-1337 Aug 28, 2024
89f8351
Version bump to 2.1.0 for khashv2
lemois-1337 Aug 28, 2024
68bd3ca
Merge pull request #55 from lemois-1337/mainnet_karlsenhashv2
lemois-1337 Aug 28, 2024
c2d369f
Mainnet HFDAAScore set to 26962009 to switch to khashv2
lemois-1337 Aug 28, 2024
5582011
Updated README.md and added khashv2 paragraph
lemois-1337 Aug 29, 2024
825a94c
Merge pull request #56 from lemois-1337/mainnet_karlsenhashv2
lemois-1337 Aug 29, 2024
bc65e01
Re-enable mainnet sync
lemois-1337 Aug 29, 2024
a133806
Merge pull request #57 from lemois-1337/mainnet_karlsenhashv2
lemois-1337 Aug 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fishhash plus implementation with hard fork procedure
HF defined at DAA score 6,000,000
HF date will be calculated in a future commit
  • Loading branch information
Dev committed Mar 19, 2024
commit cd5888a2a80731383039af4848742bb05aeb8efd
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
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,22 @@ 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 {
return errors.Wrapf(
ruleerrors.ErrWrongBlockVersion, "After HF1 the block version should be %d", constants.BlockVersionAfterHF)
}
} else {
if header.Version() != constants.BlockVersionBeforeHF {
return errors.Wrapf(
ruleerrors.ErrWrongBlockVersion, "Beofre HF1 the block version should be %d", constants.BlockVersionBeforeHF)
}
}
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 @@ -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
BlockVersion uint16 = 1
BlockVersionBeforeHF uint16 = 1
BlockVersionAfterHF uint16 = 2

// MaxTransactionVersion is the current latest supported transaction version.
MaxTransactionVersion uint16 = 0
Expand Down
71 changes: 71 additions & 0 deletions domain/consensus/utils/pow/fishhashplus_kernel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package pow

import (
"github.com/karlsen-network/karlsend/domain/consensus/model/externalapi"

//"crypto/sha3"
"encoding/binary"
)

func fishhashPlusKernel(ctx *fishhashContext, seed hash512) hash256 {
indexLimit := uint32(ctx.FullDatasetNumItems)
mix := mergeHashes(seed, seed)

//log.Debugf("lookup matrix : ")
for i := uint32(0); i < numDatasetAccesses; i++ {

mixGroup := [8]uint32{}
for c := uint32(0); c < 8; c++ {
mixGroup[c] = binary.LittleEndian.Uint32(mix[(4*4*c+0):]) ^ binary.LittleEndian.Uint32(mix[(4*4*c+4):]) ^ binary.LittleEndian.Uint32(mix[(4*4*c+8):]) ^ binary.LittleEndian.Uint32(mix[(4*4*c+12):])
}

p0 := (mixGroup[0] ^ mixGroup[3] ^ mixGroup[6]) % indexLimit
p1 := (mixGroup[1] ^ mixGroup[4] ^ mixGroup[7]) % indexLimit
p2 := (mixGroup[2] ^ mixGroup[5] ^ i) % indexLimit

fetch0 := lookup(ctx, p0)
fetch1 := lookup(ctx, p1)
fetch2 := lookup(ctx, p2)

for j := 0; j < 32; j++ {
binary.LittleEndian.PutUint32(
fetch1[4*j:],
fnv1(binary.LittleEndian.Uint32(mix[4*j:4*j+4]), binary.LittleEndian.Uint32(fetch1[4*j:4*j+4])))
binary.LittleEndian.PutUint32(
fetch2[4*j:],
binary.LittleEndian.Uint32(mix[4*j:4*j+4])^binary.LittleEndian.Uint32(fetch2[4*j:4*j+4]))
}

//fmt.Printf("The NEW fetch1 is : %x \n", fetch1)
//fmt.Printf("The NEW fetch2 is : %x \n", fetch2)

for j := 0; j < 16; j++ {
binary.LittleEndian.PutUint64(
mix[8*j:],
binary.LittleEndian.Uint64(fetch0[8*j:8*j+8])*binary.LittleEndian.Uint64(fetch1[8*j:8*j+8])+binary.LittleEndian.Uint64(fetch2[8*j:8*j+8]))
}
//log.Debugf("\n")
}

mixHash := hash256{}
for i := 0; i < (len(mix) / 4); i += 4 {
j := 4 * i
h1 := fnv1(binary.LittleEndian.Uint32(mix[j:]), binary.LittleEndian.Uint32(mix[j+4:]))
h2 := fnv1(h1, binary.LittleEndian.Uint32(mix[j+8:]))
h3 := fnv1(h2, binary.LittleEndian.Uint32(mix[j+12:]))
binary.LittleEndian.PutUint32(mixHash[i:], h3)
}

return mixHash
}

func fishHashPlus(ctx *fishhashContext, hashin *externalapi.DomainHash) *externalapi.DomainHash {

seed := hash512{}
copy(seed[:], hashin.ByteSlice())

output := fishhashPlusKernel(ctx, seed)
outputArray := [32]byte{}
copy(outputArray[:], output[:])
return externalapi.NewDomainHashFromByteArray(&outputArray)
}
32 changes: 21 additions & 11 deletions domain/consensus/utils/pow/pow.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"math/big"
)

const hashingAlgoVersion = "fishhash-kls-0.0.1"
const hashingAlgoVersion = "fishhash-kls-0.0.2"

// State is an intermediate data structure with pre-computed values to speed up mining.
type State struct {
Expand All @@ -23,7 +23,8 @@ type State struct {
Target big.Int
prePowHash externalapi.DomainHash
//cache cache
context fishhashContext
context fishhashContext
blockVersion uint16
}

// var context *fishhashContext
Expand Down Expand Up @@ -91,14 +92,17 @@ func NewState(header externalapi.MutableBlockHeader, generatedag bool) *State {
header.SetTimeInMilliseconds(timestamp)
header.SetNonce(nonce)

log.Debugf("BlueWork[%s] BlueScore[%d] DAAScore[%d] Version[%d]", header.BlueWork(), header.BlueScore(), header.DAAScore(), header.Version())

return &State{
Target: *target,
prePowHash: *prePowHash,
//will remove matrix opow
//mat: *generateMatrix(prePowHash),
Timestamp: timestamp,
Nonce: nonce,
context: *getContext(generatedag, log),
Timestamp: timestamp,
Nonce: nonce,
context: *getContext(generatedag, log),
blockVersion: header.Version(),
}
}

Expand Down Expand Up @@ -132,19 +136,25 @@ func (state *State) CalculateProofOfWorkValue() *big.Int {
}
//log.Debugf("Hash prePowHash %x\n", state.prePowHash.ByteSlice())
//fmt.Printf("Hash prePowHash %x\n", state.prePowHash.ByteSlice())

powHash := writer.Finalize()

//middleHash := state.mat.HeavyHash(powHash)
//log.Debugf("Hash b3-1: %x\n", powHash.ByteSlice())
//fmt.Printf("Hash b3-1: %x\n", powHash.ByteSlice())
middleHash := fishHash(&state.context, powHash)
//log.Debugf("Hash fish: %x\n", middleHash.ByteSlice())
//fmt.Printf("Hash fish: %x\n", middleHash.ByteSlice())
//log.Infof("Hash b3-1: %x", powHash.ByteSlice())
middleHash := powHash
if state.blockVersion == 1 {
middleHash = fishHash(&state.context, powHash)
} else {
middleHash = fishHashPlus(&state.context, powHash)
}

//log.Infof("Hash fish: %x", middleHash.ByteSlice())

writer2 := hashes.NewPoWHashWriter()
writer2.InfallibleWrite(middleHash.ByteSlice())
finalHash := writer2.Finalize()

//log.Debugf("Hash b3-2: %x\n", finalHash.ByteSlice())
//log.Infof("Hash b3-2: %x", finalHash.ByteSlice())

return toBig(finalHash)
}
Expand Down
3 changes: 3 additions & 0 deletions domain/dagconfig/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ type Params struct {
MaxBlockLevel int

MergeDepth uint64
HFDAAScore uint64
}

// NormalizeRPCServerAddress returns addr with the current network default
Expand Down Expand Up @@ -341,6 +342,8 @@ var TestnetParams = Params{

MaxBlockLevel: 250,
MergeDepth: defaultMergeDepth,
// todo: define the fork date DAAscore
HFDAAScore: 6000000,
}

// SimnetParams defines the network parameters for the simulation test Kaspa
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const validCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrs
const (
appMajor uint = 2
appMinor uint = 0
appPatch uint = 0
appPatch uint = 1
)

// appBuild is defined as a variable so it can be overridden during the build
Expand Down
Loading