Skip to content

Commit

Permalink
- DA improvements and fixes
Browse files Browse the repository at this point in the history
- other improvements
  • Loading branch information
IxiAngel committed Jan 28, 2025
1 parent 527957b commit a1c954b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 22 deletions.
30 changes: 23 additions & 7 deletions IxianDLT/Block/BlockChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class BlockChain
private int blockCount = 0;

private ulong cachedRequiredSignerDifficultyBlockNum = 0;
private int cachedRequiredSignerDifficultyVersion = 0;
private IxiNumber cachedRequiredSignerDifficulty = 0;

public long Count
Expand Down Expand Up @@ -640,11 +641,16 @@ public int getRequiredConsensusFromStorage(ulong block_num, bool adjusted_to_rat
}
}

public IxiNumber getRequiredSignerDifficulty(ulong blockNum, bool adjustToRatio, long curBlockTimestamp)
public IxiNumber getRequiredSignerDifficulty(ulong blockNum, bool adjustToRatio, int curBlockVersion = 0, long curBlockTimestamp = 0)
{
if (blockNum > lastBlockNum && blockNum % ConsensusConfig.superblockInterval == 0)
{
return calculateRequiredSignerDifficulty(adjustToRatio, lastBlockVersion, curBlockTimestamp);
if (curBlockVersion == 0 || curBlockTimestamp == 0)
{
throw new Exception("Error calculating required signer difficulty, curBlockVersion or curBlockTimestamp is 0.");
}

return calculateRequiredSignerDifficulty(adjustToRatio, curBlockVersion, curBlockTimestamp);
}
else
{
Expand Down Expand Up @@ -720,6 +726,7 @@ public void clearCachedRequiredSignerDifficulty()
lock (blocks)
{
cachedRequiredSignerDifficultyBlockNum = 0;
cachedRequiredSignerDifficultyVersion = 0;
cachedRequiredSignerDifficulty = 0;
}
}
Expand All @@ -733,6 +740,7 @@ public IxiNumber calculateRequiredSignerDifficulty(bool adjustToRatio, int block
if (blockNum < blockOffset + 1) return ConsensusConfig.minBlockSignerPowDifficulty; // special case for first X blocks - since sigFreeze happens n-5 blocks

if (cachedRequiredSignerDifficultyBlockNum == blockNum
&& cachedRequiredSignerDifficultyVersion == blockVersion
&& cachedRequiredSignerDifficulty != 0)
{
if (adjustToRatio)
Expand Down Expand Up @@ -777,6 +785,7 @@ public IxiNumber calculateRequiredSignerDifficulty(bool adjustToRatio, int block
}

cachedRequiredSignerDifficultyBlockNum = blockNum;
cachedRequiredSignerDifficultyVersion = blockVersion;
cachedRequiredSignerDifficulty = newDifficulty;

if (adjustToRatio)
Expand Down Expand Up @@ -861,7 +870,7 @@ private Block findLastDifficultyChangedSuperBlock(ulong blockNum, int blockVersi
IxiNumber diff = getRequiredSignerDifficulty(sb, false);
while (sb.lastSuperBlockNum != 0)
{
if (diff != getRequiredSignerDifficulty(sb.lastSuperBlockNum, false, Clock.getNetworkTimestamp()))
if (diff != getRequiredSignerDifficulty(sb.lastSuperBlockNum, false))
{
Logging.trace("DAA: Found diff block #{0}", sb.blockNum);
return sb;
Expand Down Expand Up @@ -891,10 +900,10 @@ private IxiNumber calculateRequiredSignerDifficulty_v2(int blockVersion, long cu
// Edge case for initial blocks
if (blockNum == ConsensusConfig.superblockInterval)
{
return getRequiredSignerDifficulty(1, false, Clock.getNetworkTimestamp());
return getRequiredSignerDifficulty(1, false);
}

return getRequiredSignerDifficulty(blockNum - ConsensusConfig.superblockInterval, false, Clock.getNetworkTimestamp());
return getRequiredSignerDifficulty(blockNum - ConsensusConfig.superblockInterval, false);
}

ulong blockOffset = 7;
Expand Down Expand Up @@ -1406,6 +1415,13 @@ public bool revertLastBlock(bool blacklist = true, bool legacy_dual_revert = tru
Node.stop();
return false;
}
clearCachedRequiredSignerDifficulty();

var itemsToRemove = blockHashCache.FindAll(x => x.Value.blockNum == block_num_to_revert);
foreach (var item in itemsToRemove)
{
blockHashCache.Remove(item);
}
}

return true;
Expand Down Expand Up @@ -1471,15 +1487,15 @@ public void decreaseSolvedBlocksCount()
}
}

public IxiNumber getMinSignerPowDifficulty(ulong blockNum, long curBlockTimestamp)
public IxiNumber getMinSignerPowDifficulty(ulong blockNum, int curBlockVersion, long curBlockTimestamp)
{
if (Count < 8)
{
return ConsensusConfig.minBlockSignerPowDifficulty;
}
uint minDiffRatio = 7;
ulong frozenSignatureCount = (ulong)ConsensusConfig.maximumBlockSigners;
var difficulty = getRequiredSignerDifficulty(blockNum, true, curBlockTimestamp) / (frozenSignatureCount * minDiffRatio);
var difficulty = getRequiredSignerDifficulty(blockNum, true, curBlockVersion, curBlockTimestamp) / (frozenSignatureCount * minDiffRatio);
if (difficulty < ConsensusConfig.minBlockSignerPowDifficulty)
{
difficulty = ConsensusConfig.minBlockSignerPowDifficulty;
Expand Down
23 changes: 15 additions & 8 deletions IxianDLT/Block/BlockProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void onUpdate()

if (block_version < Config.maxBlockVersionToGenerate
&& (last_block_num + 1) % ConsensusConfig.superblockInterval == 0
&& last_block_num >= Config.upgradeBlockHeight)
&& (IxianHandler.isTestNet || last_block_num >= Config.upgradeBlockHeight))
{
block_version = Config.maxBlockVersionToGenerate;
}
Expand Down Expand Up @@ -228,7 +228,9 @@ public void onUpdate()
//Logging.info(String.Format("Waiting for {0} to generate the next block #{1}. offset {2}", Node.blockChain.getLastElectedNodePubKey(getElectedNodeOffset()), Node.blockChain.getLastBlockNum()+1, getElectedNodeOffset()));
if (generateNextBlock)
{
if (lastUpgradeTry > 0 && Clock.getTimestamp() - lastUpgradeTry < blockGenerationInterval * 120)
if (!Config.forceUpgradeAtBlockHeight
&& lastUpgradeTry > 0
&& Clock.getTimestamp() - lastUpgradeTry < blockGenerationInterval * 120)
{
block_version = Node.blockChain.getLastBlockVersion();
}
Expand Down Expand Up @@ -768,12 +770,17 @@ public BlockVerifyStatus verifyBlockBasic(Block b, bool verify_sig = true, Remot
{
if (!IxianHandler.isTestNet)
{
// upgrade to v13
if (b.blockNum < Config.upgradeBlockHeight && b.version >= BlockVer.v13)
// prevent upgrade under a certain block height
if (b.blockNum < Config.upgradeBlockHeight
&& b.version >= Config.maxBlockVersionToGenerate)
{
return BlockVerifyStatus.Invalid;
}
if (b.blockNum >= Config.upgradeBlockHeight && b.version < BlockVer.v13)

// force upgrade if configured
if (Config.forceUpgradeAtBlockHeight
&& b.blockNum >= Config.upgradeBlockHeight
&& b.version < Config.maxBlockVersionToGenerate)
{
return BlockVerifyStatus.Invalid;
}
Expand Down Expand Up @@ -813,9 +820,9 @@ public BlockVerifyStatus verifyBlockBasic(Block b, bool verify_sig = true, Remot
}
} else // >= BlockVer.v13
{
if (b.timestamp < prevBlock.timestamp + ConsensusConfig.blockGenerationInterval)
if (b.timestamp < prevBlock.timestamp + ConsensusConfig.minBlockTimeDifference)
{
Logging.warn("Received block #{0} with invalid timestamp {1}, expecting at least {2}!", b.blockNum, b.timestamp, prevBlock.timestamp + ConsensusConfig.blockGenerationInterval);
Logging.warn("Received block #{0} with invalid timestamp {1}, expecting at least {2}!", b.blockNum, b.timestamp, prevBlock.timestamp + ConsensusConfig.minBlockTimeDifference);
return BlockVerifyStatus.Invalid;
}
}
Expand Down Expand Up @@ -2097,7 +2104,7 @@ private bool handleBlockchainRecoveryMode(Block curBlock, int requiredSignatureC

if (missingSigs > 0)
{
if (totalSignerDifficulty < recoveryRequiredSignerDifficulty + (missingSigs * IxianHandler.getMinSignerPowDifficulty(curBlock.blockNum, curBlock.timestamp)) * ConsensusConfig.blockChainRecoveryMissingSignerMultiplier)
if (totalSignerDifficulty < recoveryRequiredSignerDifficulty + (missingSigs * IxianHandler.getMinSignerPowDifficulty(curBlock.blockNum, curBlock.version, curBlock.timestamp)) * ConsensusConfig.blockChainRecoveryMissingSignerMultiplier)
{
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions IxianDLT/Meta/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,9 +1168,9 @@ private void devInsertFromJson()
storage.insertBlock(b);
}

public override IxiNumber getMinSignerPowDifficulty(ulong blockNum, long curBlockTimestamp)
public override IxiNumber getMinSignerPowDifficulty(ulong blockNum, int curBlockVersion, long curBlockTimestamp)

Check failure on line 1171 in IxianDLT/Meta/Node.cs

View workflow job for this annotation

GitHub Actions / build

'Node.getMinSignerPowDifficulty(ulong, int, long)': no suitable method found to override

Check failure on line 1171 in IxianDLT/Meta/Node.cs

View workflow job for this annotation

GitHub Actions / build

'Node.getMinSignerPowDifficulty(ulong, int, long)': no suitable method found to override
{
return blockChain.getMinSignerPowDifficulty(blockNum, curBlockTimestamp);
return blockChain.getMinSignerPowDifficulty(blockNum, curBlockVersion, curBlockTimestamp);
}

public override RegisteredNameRecord getRegName(byte[] name, bool useAbsoluteId = true)
Expand Down
4 changes: 2 additions & 2 deletions IxianDLT/Miner/SignerPowMiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private void searchForBlock()
return;
}

solvingDifficulty = Node.blockChain.getMinSignerPowDifficulty(IxianHandler.getLastBlockHeight() + 1, Clock.getNetworkTimestamp());
solvingDifficulty = Node.blockChain.getMinSignerPowDifficulty(IxianHandler.getLastBlockHeight() + 1, IxianHandler.getLastBlockVersion(), Clock.getNetworkTimestamp());

if (solvingDifficulty < 0)
{
Expand Down Expand Up @@ -375,7 +375,7 @@ private void sendFoundSolution()
{
if (newSolution.difficulty <= solution.difficulty
&& solution.blockNum + ConsensusConfig.getPlPowBlocksValidity(IxianHandler.getLastBlockVersion()) - 1 > IxianHandler.getHighestKnownNetworkBlockHeight()
&& solution.difficulty > Node.blockChain.getMinSignerPowDifficulty(IxianHandler.getLastBlockHeight() + 1, Clock.getNetworkTimestamp()))
&& solution.difficulty > Node.blockChain.getMinSignerPowDifficulty(IxianHandler.getLastBlockHeight() + 1, IxianHandler.getLastBlockVersion(), Clock.getNetworkTimestamp()))
{
// If the new solution has a lower difficulty than the previously submitted solution and the previously submitted solution is still valid

Expand Down
2 changes: 1 addition & 1 deletion IxianDLT/Network/PresenceProtocolMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public static void handleGetRandomPresences(byte[] data, RemoteEndpoint endpoint
public static void handleUpdatePresence(byte[] data, RemoteEndpoint endpoint)
{
// Parse the data and update entries in the presence list
Presence updated_presence = PresenceList.updateFromBytes(data, Node.blockChain.getMinSignerPowDifficulty(IxianHandler.getLastBlockHeight() + 1, Clock.getNetworkTimestamp()));
Presence updated_presence = PresenceList.updateFromBytes(data, Node.blockChain.getMinSignerPowDifficulty(IxianHandler.getLastBlockHeight() + 1, IxianHandler.getLastBlockVersion(), Clock.getNetworkTimestamp()));

// If a presence entry was updated, broadcast this message again
if (updated_presence != null)
Expand Down
2 changes: 1 addition & 1 deletion IxianDLT/Tests/BenchmarkNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public override Block getBlockHeader(ulong blockNum)
return BlockHeaderStorage.getBlockHeader(blockNum);
}

public override IxiNumber getMinSignerPowDifficulty(ulong blockNum, long curBlockTimestamp)
public override IxiNumber getMinSignerPowDifficulty(ulong blockNum, int curBlockVersion, long curBlockTimestamp)

Check failure on line 198 in IxianDLT/Tests/BenchmarkNode.cs

View workflow job for this annotation

GitHub Actions / build

'BenchmarkNode.getMinSignerPowDifficulty(ulong, int, long)': no suitable method found to override

Check failure on line 198 in IxianDLT/Tests/BenchmarkNode.cs

View workflow job for this annotation

GitHub Actions / build

'BenchmarkNode.getMinSignerPowDifficulty(ulong, int, long)': no suitable method found to override
{
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion UnitTests/DummyIxianNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public override int getLastBlockVersion()
return Block.maxVersion;
}

public override IxiNumber getMinSignerPowDifficulty(ulong blockNum, long curBlockTimestamp)
public override IxiNumber getMinSignerPowDifficulty(ulong blockNum, int curBlockVersion, long curBlockTimestamp)
{
throw new NotImplementedException();
}
Expand Down

0 comments on commit a1c954b

Please sign in to comment.