Changed how consensus Id is generated

This commit is contained in:
DataHoarder 2023-06-05 23:41:32 +02:00
parent 330d3fe3aa
commit 1b04de234c
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
7 changed files with 23 additions and 33 deletions

View file

@ -180,7 +180,7 @@ func main() {
log.Panic(err)
}
log.Printf("Consensus id = %s", consensus.Id())
log.Printf("Consensus id = %s", consensus.Id)
var lastPoolInfo atomic.Pointer[cmdutils.PoolInfoResult]

View file

@ -55,7 +55,7 @@ func (p *P2PoolApi) WaitSync() (err error) {
time.Sleep(time.Second * 5)
}
log.Printf("[API] SYNCHRONIZED (height %d, id %s, blocks %d)", status.Height, status.Id, status.Blocks)
log.Printf("[API] Consensus id = %s\n", p.Consensus().Id())
log.Printf("[API] Consensus id = %s\n", p.Consensus().Id)
return nil
}
@ -78,7 +78,7 @@ func (p *P2PoolApi) WaitSyncStart() (err error) {
if status.Synchronized {
log.Printf("[API] SYNCHRONIZED (height %d, id %s, blocks %d)", status.Height, status.Id, status.Blocks)
}
log.Printf("[API] Consensus id = %s\n", p.Consensus().Id())
log.Printf("[API] Consensus id = %s\n", p.Consensus().Id)
return nil
}

View file

@ -20,7 +20,7 @@ func TestFindChallengeSolution(t *testing.T) {
var stop atomic.Bool
if solution, hash, ok := FindChallengeSolution(handshakeChallenge, sidechain.ConsensusDefault.Id(), &stop); !ok {
if solution, hash, ok := FindChallengeSolution(handshakeChallenge, sidechain.ConsensusDefault.Id, &stop); !ok {
t.Fatalf("No solution for %s", hex.EncodeToString(handshakeChallenge[:]))
} else {
t.Logf("Solution for %s is %d (hash %s)", hex.EncodeToString(handshakeChallenge[:]), solution, hash.String())

View file

@ -416,7 +416,7 @@ func (c *Client) OnConnection() {
}
if c.IsIncomingConnection {
if hash, ok := CalculateChallengeHash(c.handshakeChallenge, c.Owner.Consensus().Id(), solution); !ok {
if hash, ok := CalculateChallengeHash(c.handshakeChallenge, c.Owner.Consensus().Id, solution); !ok {
//not enough PoW
c.Ban(DefaultBanTime, fmt.Errorf("not enough PoW on HANDSHAKE_SOLUTION, challenge = %s, solution = %d, calculated hash = %s, expected hash = %s", hex.EncodeToString(c.handshakeChallenge[:]), solution, hash.String(), challengeHash.String()))
return
@ -426,7 +426,7 @@ func (c *Client) OnConnection() {
return
}
} else {
if hash, _ := CalculateChallengeHash(c.handshakeChallenge, c.Owner.Consensus().Id(), solution); hash != challengeHash {
if hash, _ := CalculateChallengeHash(c.handshakeChallenge, c.Owner.Consensus().Id, solution); hash != challengeHash {
//wrong hash
c.Ban(DefaultBanTime, fmt.Errorf("wrong hash HANDSHAKE_SOLUTION, challenge = %s, solution = %d, calculated hash = %s, expected hash = %s", hex.EncodeToString(c.handshakeChallenge[:]), solution, hash.String(), challengeHash.String()))
return
@ -909,7 +909,7 @@ func (c *Client) sendHandshakeSolution(challenge HandshakeChallenge) {
stop.Store(true)
}
if solution, hash, ok := FindChallengeSolution(challenge, c.Owner.Consensus().Id(), stop); ok || c.IsIncomingConnection {
if solution, hash, ok := FindChallengeSolution(challenge, c.Owner.Consensus().Id, stop); ok || c.IsIncomingConnection {
var buf [HandshakeChallengeSize + types.HashSize]byte
copy(buf[:], hash[:])

View file

@ -98,7 +98,7 @@ type Consensus struct {
hasher randomx.Hasher
id types.Hash
Id types.Hash
}
const SmallestMinimumDifficulty = 100000
@ -166,8 +166,8 @@ func (c *Consensus) verify() bool {
}
var emptyHash types.Hash
c.id = c.CalculateId()
if c.id == emptyHash {
c.Id = c.CalculateId()
if c.Id == emptyHash {
return false
}
@ -200,7 +200,7 @@ func (c *Consensus) CalculateSideTemplateIdPreAllocated(share *PoolBlock, buf []
buf, _ = share.Side.AppendBinary(buf[:0], share.ShareVersion())
_, _ = h.Write(buf)
_, _ = h.Write(c.id[:])
_, _ = h.Write(c.Id[:])
crypto.HashFastSum(h, result[:])
return result
}
@ -212,27 +212,17 @@ func (c *Consensus) CalculateSideChainIdFromBlobs(mainBlob, sideBlob []byte) (re
_, _ = h.Write(mainBlob)
_, _ = h.Write(sideBlob)
_, _ = h.Write(c.id[:])
_, _ = h.Write(c.Id[:])
crypto.HashFastSum(h, result[:])
return result
}
func (c *Consensus) Id() types.Hash {
var h types.Hash
if c.id == h {
//this data race is fine
c.id = c.CalculateId()
return c.id
}
return c.id
}
func (c *Consensus) IsDefault() bool {
return c.id == ConsensusDefault.id
return c.Id == ConsensusDefault.Id
}
func (c *Consensus) IsMini() bool {
return c.id == ConsensusMini.id
return c.Id == ConsensusMini.Id
}
func (c *Consensus) DefaultPort() uint16 {
@ -290,5 +280,5 @@ func (c *Consensus) CalculateId() types.Hash {
return randomx.ConsensusHash(buf)
}
var ConsensusDefault = &Consensus{NetworkType: NetworkMainnet, PoolName: "mainnet test 2", TargetBlockTime: 10, MinimumDifficulty: 100000, ChainWindowSize: 2160, UnclePenalty: 20, HardForks: p2poolMainNetHardForks, id: types.Hash{34, 175, 126, 231, 181, 11, 104, 146, 227, 153, 218, 107, 44, 108, 68, 39, 178, 81, 4, 212, 169, 4, 142, 0, 177, 110, 157, 240, 68, 7, 249, 24}}
var ConsensusMini = &Consensus{NetworkType: NetworkMainnet, PoolName: "mini", TargetBlockTime: 10, MinimumDifficulty: 100000, ChainWindowSize: 2160, UnclePenalty: 20, HardForks: p2poolMainNetHardForks, id: types.Hash{57, 130, 201, 26, 149, 174, 199, 250, 66, 80, 189, 18, 108, 216, 194, 220, 136, 23, 63, 24, 64, 113, 221, 44, 219, 86, 39, 163, 53, 24, 126, 196}}
var ConsensusDefault = &Consensus{NetworkType: NetworkMainnet, PoolName: "mainnet test 2", TargetBlockTime: 10, MinimumDifficulty: 100000, ChainWindowSize: 2160, UnclePenalty: 20, HardForks: p2poolMainNetHardForks, Id: types.Hash{34, 175, 126, 231, 181, 11, 104, 146, 227, 153, 218, 107, 44, 108, 68, 39, 178, 81, 4, 212, 169, 4, 142, 0, 177, 110, 157, 240, 68, 7, 249, 24}}
var ConsensusMini = &Consensus{NetworkType: NetworkMainnet, PoolName: "mini", TargetBlockTime: 10, MinimumDifficulty: 100000, ChainWindowSize: 2160, UnclePenalty: 20, HardForks: p2poolMainNetHardForks, Id: types.Hash{57, 130, 201, 26, 149, 174, 199, 250, 66, 80, 189, 18, 108, 216, 194, 220, 136, 23, 63, 24, 64, 113, 221, 44, 219, 86, 39, 163, 53, 24, 126, 196}}

View file

@ -7,13 +7,13 @@ import (
func TestDefaultConsensusId(t *testing.T) {
id := ConsensusMini.CalculateId()
if id != ConsensusMini.Id() {
t.Fatalf("wrong mini sidechain id, expected %s, got %s", ConsensusMini.Id().String(), id.String())
if id != ConsensusMini.Id {
t.Fatalf("wrong mini sidechain id, expected %s, got %s", ConsensusMini.Id.String(), id.String())
}
id = ConsensusDefault.CalculateId()
if id != ConsensusDefault.Id() {
t.Fatalf("wrong default sidechain id, expected %s, got %s", ConsensusDefault.Id().String(), id.String())
if id != ConsensusDefault.Id {
t.Fatalf("wrong default sidechain id, expected %s, got %s", ConsensusDefault.Id.String(), id.String())
}
}
@ -23,7 +23,7 @@ func TestOverlyLongConsensus(t *testing.T) {
c2 := NewConsensus(NetworkMainnet, strings.Repeat("A", 128), strings.Repeat("A", 128), 100, 1000000, 1000, 30)
if c.Id() == c2.Id() {
t.Fatalf("consensus is different but ids are equal, %s, %s", c.Id().String(), c2.Id().String())
if c.Id == c2.Id {
t.Fatalf("consensus is different but ids are equal, %s, %s", c.Id.String(), c2.Id.String())
}
}

View file

@ -465,7 +465,7 @@ func (c *SideChain) verifyBlock(block *PoolBlock) (verification error, invalid e
len(block.Side.Uncles) != 0 ||
block.Side.Difficulty.Cmp64(c.Consensus().MinimumDifficulty) != 0 ||
block.Side.CumulativeDifficulty.Cmp64(c.Consensus().MinimumDifficulty) != 0 ||
(block.ShareVersion() > ShareVersion_V1 && block.Side.CoinbasePrivateKeySeed != c.Consensus().Id()) {
(block.ShareVersion() > ShareVersion_V1 && block.Side.CoinbasePrivateKeySeed != c.Consensus().Id) {
return nil, errors.New("genesis block has invalid parameters")
}
//this does not verify coinbase outputs, but that's fine