Add extra consensus id for temporary testing

This commit is contained in:
DataHoarder 2024-04-10 08:06:40 +02:00
parent decd52c0ca
commit 3e689ae7be
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
7 changed files with 33 additions and 76 deletions

View file

@ -54,7 +54,7 @@ func (p *P2PoolApi) WaitSync() (err error) {
time.Sleep(time.Second * 5)
}
utils.Logf("API", "SYNCHRONIZED (height %d, id %s, blocks %d)", status.Height, status.Id, status.Blocks)
utils.Logf("API", "Consensus id = %s / %s\n", p.Consensus().Id, p.Consensus().MergeMiningId)
utils.Logf("API", "Consensus id = %s\n", p.Consensus().Id)
return nil
}
@ -77,7 +77,7 @@ func (p *P2PoolApi) WaitSyncStart() (err error) {
if status.Synchronized {
utils.Logf("API", "SYNCHRONIZED (height %d, id %s, blocks %d)", status.Height, status.Id, status.Blocks)
}
utils.Logf("API", "Consensus id = %s / %s\n", p.Consensus().Id, p.Consensus().MergeMiningId)
utils.Logf("API", "Consensus id = %s\n", p.Consensus().Id)
return nil
}

View file

@ -67,8 +67,6 @@ type BanEntry struct {
type Server struct {
p2pool P2PoolInterface
tempUseMergeMiningIdAsHandshake bool
peerId uint64
versionInformation p2pooltypes.PeerVersionInformation
@ -122,23 +120,14 @@ func NewServer(p2pool P2PoolInterface, listenAddress string, externalListenPort
return nil, err
}
//TODO: remove this when p2pool has proper hardfork method
var tempUseMergeMiningIdAsHandshake bool
if useMergeMining := ctx.Value("use_merge_mining_id"); useMergeMining != nil {
if v, ok := useMergeMining.(bool); ok {
tempUseMergeMiningIdAsHandshake = v
}
}
s := &Server{
p2pool: p2pool,
tempUseMergeMiningIdAsHandshake: tempUseMergeMiningIdAsHandshake,
listenAddress: addrPort,
externalListenPort: externalListenPort,
peerId: binary.LittleEndian.Uint64(peerId),
MaxOutgoingPeers: min(maxOutgoingPeers, 450),
MaxIncomingPeers: min(maxIncomingPeers, 450),
cachedBlocks: make(map[types.Hash]*sidechain.PoolBlock, p2pool.Consensus().ChainWindowSize*3),
p2pool: p2pool,
listenAddress: addrPort,
externalListenPort: externalListenPort,
peerId: binary.LittleEndian.Uint64(peerId),
MaxOutgoingPeers: min(maxOutgoingPeers, 450),
MaxIncomingPeers: min(maxIncomingPeers, 450),
cachedBlocks: make(map[types.Hash]*sidechain.PoolBlock, p2pool.Consensus().ChainWindowSize*3),
versionInformation: p2pooltypes.PeerVersionInformation{
SoftwareId: p2pooltypes.CurrentSoftwareId,
SoftwareVersion: p2pooltypes.CurrentSoftwareVersion,
@ -907,11 +896,7 @@ func (s *Server) Broadcast(block *sidechain.PoolBlock) {
}
func (s *Server) HandshakeConsensusId() types.Hash {
if s.tempUseMergeMiningIdAsHandshake {
return s.p2pool.Consensus().MergeMiningId
} else {
return s.p2pool.Consensus().Id
}
return s.p2pool.Consensus().Id
}
func (s *Server) Consensus() *sidechain.Consensus {

View file

@ -92,6 +92,9 @@ type Consensus struct {
ChainWindowSize uint64 `json:"pplns_window"`
UnclePenalty uint64 `json:"uncle_penalty"`
// Extra additional string to add for testing usually
Extra string `json:"extra,omitempty"`
// HardFork optional hardfork information for p2pool
// If empty it will be filled with the default hardfork list to the corresponding NetworkType
// Note: this is not supported by p2pool itself
@ -100,14 +103,12 @@ type Consensus struct {
hasher randomx.Hasher
Id types.Hash `json:"id"`
MergeMiningId types.Hash `json:"mm_id"`
}
const SmallestMinimumDifficulty = 1000
const LargestMinimumDifficulty = 1000000000
func NewConsensus(networkType NetworkType, poolName, poolPassword string, targetBlockTime, minimumDifficulty, chainWindowSize, unclePenalty uint64) *Consensus {
func NewConsensus(networkType NetworkType, poolName, poolPassword, extra string, targetBlockTime, minimumDifficulty, chainWindowSize, unclePenalty uint64) *Consensus {
c := &Consensus{
NetworkType: networkType,
PoolName: poolName,
@ -116,6 +117,7 @@ func NewConsensus(networkType NetworkType, poolName, poolPassword string, target
MinimumDifficulty: minimumDifficulty,
ChainWindowSize: chainWindowSize,
UnclePenalty: unclePenalty,
Extra: extra,
}
if !c.verify() {
@ -169,14 +171,10 @@ func (c *Consensus) verify() bool {
}
var emptyHash types.Hash
c.Id = c.CalculateId(false)
c.Id = c.CalculateId()
if c.Id == emptyHash {
return false
}
c.MergeMiningId = c.CalculateId(true)
if c.MergeMiningId == emptyHash {
return false
}
if len(c.HardForks) == 0 {
switch c.NetworkType {
@ -206,28 +204,20 @@ func (c *Consensus) CalculateSideTemplateIdPreAllocated(share *PoolBlock, buf []
_, _ = h.Write(buf)
buf, _ = share.Side.AppendBinary(buf[:0], share.ShareVersion())
_, _ = h.Write(buf)
if share.ShareVersion() > ShareVersion_V2 {
_, _ = h.Write(c.MergeMiningId[:])
} else {
_, _ = h.Write(c.Id[:])
}
_, _ = h.Write(c.Id[:])
crypto.HashFastSum(h, result[:])
return result
}
func (c *Consensus) CalculateSideChainIdFromBlobs(mainBlob, sideBlob []byte, isMergeMining bool) (result types.Hash) {
func (c *Consensus) CalculateSideChainIdFromBlobs(mainBlob, sideBlob []byte) (result types.Hash) {
h := crypto.GetKeccak256Hasher()
defer crypto.PutKeccak256Hasher(h)
_, _ = h.Write(mainBlob)
_, _ = h.Write(sideBlob)
if isMergeMining {
_, _ = h.Write(c.MergeMiningId[:])
} else {
_, _ = h.Write(c.Id[:])
}
_, _ = h.Write(c.Id[:])
crypto.HashFastSum(h, result[:])
return result
}
@ -282,10 +272,11 @@ func (c *Consensus) GetHasher() randomx.Hasher {
return c.hasher
}
func (c *Consensus) CalculateId(mergeMining bool) types.Hash {
func (c *Consensus) CalculateId() types.Hash {
var buf []byte
if mergeMining {
buf = append(buf, 'm', 'm', 0)
if c.Extra != "" {
buf = append(buf, c.Extra...)
buf = append(buf, 0)
}
buf = append(buf, c.NetworkType.String()...)
buf = append(buf, 0)
@ -321,7 +312,6 @@ var ConsensusDefault = &Consensus{
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},
MergeMiningId: types.Hash{107, 177, 178, 129, 71, 2, 66, 207, 56, 145, 102, 187, 105, 128, 102, 27, 68, 29, 81, 92, 114, 214, 215, 125, 158, 40, 117, 207, 32, 182, 142, 101},
}
var ConsensusMini = &Consensus{
@ -333,5 +323,4 @@ var ConsensusMini = &Consensus{
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},
MergeMiningId: types.Hash{215, 23, 207, 132, 167, 193, 162, 243, 66, 3, 228, 99, 238, 140, 39, 46, 112, 158, 200, 37, 62, 100, 138, 59, 183, 233, 136, 91, 198, 34, 19, 39},
}

View file

@ -6,31 +6,22 @@ import (
)
func TestDefaultConsensusId(t *testing.T) {
id := ConsensusMini.CalculateId(false)
id := ConsensusMini.CalculateId()
if id != ConsensusMini.Id {
t.Fatalf("wrong mini sidechain id, expected %s, got %s", ConsensusMini.Id.String(), id.String())
}
id = ConsensusMini.CalculateId(true)
if id != ConsensusMini.MergeMiningId {
t.Fatalf("wrong mini merge mining sidechain id, expected %s, got %s", ConsensusMini.MergeMiningId.String(), id.String())
}
id = ConsensusDefault.CalculateId(false)
id = ConsensusDefault.CalculateId()
if id != ConsensusDefault.Id {
t.Fatalf("wrong default sidechain id, expected %s, got %s", ConsensusDefault.Id.String(), id.String())
}
id = ConsensusDefault.CalculateId(true)
if id != ConsensusDefault.MergeMiningId {
t.Fatalf("wrong default merge mining sidechain id, expected %s, got %s", ConsensusDefault.MergeMiningId.String(), id.String())
}
}
func TestOverlyLongConsensus(t *testing.T) {
c := NewConsensus(NetworkMainnet, strings.Repeat("A", 128), strings.Repeat("A", 128), 10, 100000, 2160, 20)
c := NewConsensus(NetworkMainnet, strings.Repeat("A", 128), strings.Repeat("A", 128), "", 10, 100000, 2160, 20)
c2 := NewConsensus(NetworkMainnet, strings.Repeat("A", 128), strings.Repeat("A", 128), 100, 1000000, 1000, 30)
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())

View file

@ -315,7 +315,7 @@ func (c *SideChain) PoolBlockExternalVerify(block *PoolBlock) (missingBlocks []t
//verify template id against merkle proof
mmTag := block.MergeMiningTag()
auxiliarySlot := merge_mining.GetAuxiliarySlot(c.Consensus().MergeMiningId, mmTag.Nonce, mmTag.NumberAuxiliaryChains)
auxiliarySlot := merge_mining.GetAuxiliarySlot(c.Consensus().Id, mmTag.Nonce, mmTag.NumberAuxiliaryChains)
if !block.Side.MerkleProof.Verify(templateId, int(auxiliarySlot), int(mmTag.NumberAuxiliaryChains), mmTag.RootHash) {
return nil, fmt.Errorf("could not verify template id %s merkle proof against merkle tree root hash %s (number of chains = %d, nonce = %d, auxiliary slot = %d)", templateId, mmTag.RootHash, mmTag.NumberAuxiliaryChains, mmTag.Nonce, auxiliarySlot), true
@ -631,8 +631,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.ShareVersion() < ShareVersion_V3 && block.Side.CoinbasePrivateKeySeed != c.Consensus().Id) ||
(block.ShareVersion() > ShareVersion_V2 && block.Side.CoinbasePrivateKeySeed != c.Consensus().MergeMiningId) {
(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

View file

@ -218,11 +218,7 @@ func (s *Server) fillNewTemplateData(currentDifficulty types.Difficulty) error {
s.newTemplateData.Uncles = s.sidechain.GetPossibleUncles(s.tip, s.newTemplateData.SideHeight)
} else {
s.newTemplateData.PreviousTemplateId = types.ZeroHash
if s.newTemplateData.ShareVersion > sidechain.ShareVersion_V2 {
s.newTemplateData.TransactionPrivateKeySeed = s.sidechain.Consensus().MergeMiningId
} else {
s.newTemplateData.TransactionPrivateKeySeed = s.sidechain.Consensus().Id
}
s.newTemplateData.TransactionPrivateKeySeed = s.sidechain.Consensus().Id
s.newTemplateData.Difficulty = types.DifficultyFrom64(s.sidechain.Consensus().MinimumDifficulty)
s.newTemplateData.CumulativeDifficulty = types.DifficultyFrom64(s.sidechain.Consensus().MinimumDifficulty)
}

View file

@ -106,11 +106,8 @@ func (tpl *Template) TemplateId(hasher *sha3.HasherState, preAllocatedBuffer []b
buf := tpl.Blob(preAllocatedBuffer, 0, 0, sideRandomNumber, sideExtraNonce, types.ZeroHash)
_, _ = hasher.Write(buf)
if tpl.ShareVersion(consensus) > sidechain.ShareVersion_V2 {
_, _ = hasher.Write(consensus.MergeMiningId[:])
} else {
_, _ = hasher.Write(consensus.Id[:])
}
_, _ = hasher.Write(consensus.Id[:])
crypto.HashFastSum(hasher, (*result)[:])
hasher.Reset()
}