Skip to content

Commit

Permalink
- block v13
Browse files Browse the repository at this point in the history
- DA fix
- deprecated 20 second block time adjustment
- minor improvements
  • Loading branch information
IxiAngel committed Jan 27, 2025
1 parent c4d20a1 commit 45d751c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
21 changes: 15 additions & 6 deletions IxianDLT/Block/BlockChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ public ulong getRequiredSignerBits(ulong blockNum)
}
if (blockNum < lastSuperBlock.blockNum)
{
var sb = getBlock((blockNum / ConsensusConfig.superblockInterval) * ConsensusConfig.superblockInterval);
var sb = getBlock((blockNum / ConsensusConfig.superblockInterval) * ConsensusConfig.superblockInterval, true);
if(sb == null || sb.signerBits == 0)
{
return SignerPowSolution.difficultyToBits(ConsensusConfig.minBlockSignerPowDifficulty);
Expand Down Expand Up @@ -750,7 +750,7 @@ public IxiNumber calculateRequiredSignerDifficulty(bool adjustToRatio, int block
}
else
{
newDifficulty = calculateRequiredSignerDifficulty_v2(curBlockTimestamp);
newDifficulty = calculateRequiredSignerDifficulty_v2(blockVersion, curBlockTimestamp);
}

// Limit to max *2, /2
Expand Down Expand Up @@ -843,7 +843,7 @@ private IxiNumber calculateRequiredSignerDifficulty_v1(int blockVersion)
return newDifficulty;
}

private Block findLastDifficultyChangedSuperBlock(ulong blockNum)
private Block findLastDifficultyChangedSuperBlock(ulong blockNum, int blockVersion)
{
// Make sure that it's a superblock
if (blockNum % ConsensusConfig.superblockInterval != 0)
Expand All @@ -863,20 +863,28 @@ private Block findLastDifficultyChangedSuperBlock(ulong blockNum)
{
if (diff != getRequiredSignerDifficulty(sb.lastSuperBlockNum, false, Clock.getNetworkTimestamp()))
{
Logging.warn("DAA: Found diff block #{0}", sb.blockNum);
Logging.trace("DAA: Found diff block #{0}", sb.blockNum);
return sb;
}
if (blockVersion <= BlockVer.v12)
{
// regression fix, which searched only last 10000 blocks
if (blockNum - sb.blockNum == 10000)
{
return sb;
}
}
sb = getSuperBlock(sb.lastSuperBlockNum);
}
return sb;
}

// Time-based DAA
private IxiNumber calculateRequiredSignerDifficulty_v2(long curBlockTimestamp)
private IxiNumber calculateRequiredSignerDifficulty_v2(int blockVersion, long curBlockTimestamp)
{
ulong blockNum = getLastBlockNum() + 1;
Logging.trace("DAA: Calculating diff for block #{0}", blockNum);
Block lastDiffChangeSuperblock = findLastDifficultyChangedSuperBlock(blockNum);
Block lastDiffChangeSuperblock = findLastDifficultyChangedSuperBlock(blockNum, blockVersion);
if (curBlockTimestamp - lastDiffChangeSuperblock.timestamp < ConsensusConfig.difficultyAdjustmentTimeInterval)
{
Logging.trace("DAA: Not enough time has passed to do the calculation, using same difficulty as on previous block.");
Expand Down Expand Up @@ -909,6 +917,7 @@ private IxiNumber calculateRequiredSignerDifficulty_v2(long curBlockTimestamp)
{
break;
}
Logging.trace("DAA: Using block #{0} with diff {1} for DAA calculation", consensusBlockNum, blockTotalSignerDifficulty);

// Smooth-out difficulty spikes
if (maxSingleBlockDifficulty != null
Expand Down
43 changes: 19 additions & 24 deletions IxianDLT/Block/BlockProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ class BlockProcessor

int blockGenerationInterval = ConsensusConfig.blockGenerationInterval;

long averageBlockGenerationInterval = ConsensusConfig.blockGenerationInterval;

public bool firstBlockAfterSync;

private SortedList<ulong, long> fetchingTxForBlocks = new SortedList<ulong, long>();
Expand Down Expand Up @@ -169,7 +167,8 @@ public void onUpdate()
int block_version = Node.blockChain.getLastBlockVersion();

if (block_version < Config.maxBlockVersionToGenerate
&& (last_block_num + 1) % ConsensusConfig.superblockInterval == 0)
&& (last_block_num + 1) % ConsensusConfig.superblockInterval == 0
&& last_block_num >= Config.upgradeBlockHeight)
{
block_version = Config.maxBlockVersionToGenerate;
}
Expand Down Expand Up @@ -767,15 +766,14 @@ public void onBlockReceived(Block b, RemoteEndpoint endpoint = null)

public BlockVerifyStatus verifyBlockBasic(Block b, bool verify_sig = true, RemoteEndpoint endpoint = null)
{
// TODO omega remove bottom if section after v12 upgrade
if (!IxianHandler.isTestNet)
{
// upgrade to v12 at exactly block 4650000
if (b.blockNum < 4650000 && b.version >= BlockVer.v12)
// upgrade to v13
if (b.blockNum < Config.upgradeBlockHeight && b.version >= BlockVer.v13)

Check failure on line 772 in IxianDLT/Block/BlockProcessor.cs

View workflow job for this annotation

GitHub Actions / build

'BlockVer' does not contain a definition for 'v13'

Check failure on line 772 in IxianDLT/Block/BlockProcessor.cs

View workflow job for this annotation

GitHub Actions / build

'BlockVer' does not contain a definition for 'v13'
{
return BlockVerifyStatus.Invalid;
}
if (b.blockNum >= 4650000 && b.version < BlockVer.v12)
if (b.blockNum >= Config.upgradeBlockHeight && b.version < BlockVer.v13)

Check failure on line 776 in IxianDLT/Block/BlockProcessor.cs

View workflow job for this annotation

GitHub Actions / build

'BlockVer' does not contain a definition for 'v13'

Check failure on line 776 in IxianDLT/Block/BlockProcessor.cs

View workflow job for this annotation

GitHub Actions / build

'BlockVer' does not contain a definition for 'v13'
{
return BlockVerifyStatus.Invalid;
}
Expand Down Expand Up @@ -806,10 +804,20 @@ public BlockVerifyStatus verifyBlockBasic(Block b, bool verify_sig = true, Remot
if (b.version >= BlockVer.v7)
{
// if received block's timestamp is lower than previous block's timestamp + 20 seconds
if (b.timestamp < prevBlock.timestamp + ConsensusConfig.minBlockTimeDifference)
if (b.version <= BlockVer.v12)
{
Logging.warn("Received block #{0} with invalid timestamp {1}, expecting at least {2}!", b.blockNum, b.timestamp, prevBlock.timestamp + ConsensusConfig.minBlockTimeDifference);
return BlockVerifyStatus.Invalid;
if (b.timestamp < prevBlock.timestamp + 20)
{
Logging.warn("Received block #{0} with invalid timestamp {1}, expecting at least {2}!", b.blockNum, b.timestamp, prevBlock.timestamp + 20);
return BlockVerifyStatus.Invalid;
}
} else // >= BlockVer.v13
{
if (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.blockGenerationInterval);
return BlockVerifyStatus.Invalid;
}
}
// if received block's timestamp is higher than network time + 60 seconds
if (b.timestamp > Clock.getNetworkTimestamp() + ConsensusConfig.maxBlockNetworkTimeDifference)
Expand Down Expand Up @@ -2307,19 +2315,6 @@ public bool acceptLocalNewBlock()

// Adjust block generation time to get close to the block generation interval target
Block tmp_prev_block = Node.blockChain.getBlock(current_block.blockNum - 1);
if (tmp_prev_block != null)
{
averageBlockGenerationInterval = (averageBlockGenerationInterval + (current_block.timestamp - tmp_prev_block.timestamp)) / 2;

if (averageBlockGenerationInterval > ConsensusConfig.blockGenerationInterval + 1)
{
blockGenerationInterval = ConsensusConfig.minBlockTimeDifference + 1;
}
else if (averageBlockGenerationInterval + 1 < ConsensusConfig.blockGenerationInterval)
{
blockGenerationInterval = ConsensusConfig.blockGenerationInterval;
}
}

if (Node.miner.searchMode == BlockSearchMode.latestBlock)
{
Expand Down Expand Up @@ -4123,7 +4118,7 @@ public ulong determineHighestNetworkBlockNum()
return netBh;
}

ulong maxBlocksGenerated = (ulong)(Clock.getNetworkTimestamp() - lastBlock.timestamp) / (ulong)ConsensusConfig.minBlockTimeDifference;
ulong maxBlocksGenerated = (ulong)(Clock.getNetworkTimestamp() - lastBlock.timestamp) / (ulong)ConsensusConfig.blockGenerationInterval;
ulong maxBlockHeight = lastBlock.blockNum + maxBlocksGenerated;
if (maxBlockHeight < netBh)
{
Expand Down
5 changes: 3 additions & 2 deletions IxianDLT/Meta/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static string dataFolderBlocks
public static ulong forceSyncToBlock = 0;

// Read-only values
public static readonly string version = "xdc-0.9.3e"; // DLT Node version
public static readonly string version = "xdc-0.9.3f-rc"; // DLT Node version

public static readonly string checkVersionUrl = "https://www.ixian.io/update.txt";
public static readonly int checkVersionSeconds = 6 * 60 * 60; // 6 hours
Expand Down Expand Up @@ -131,7 +131,8 @@ public static string dataFolderBlocks
// internal
public static bool changePass = false;

public static int maxBlockVersionToGenerate = BlockVer.v12;
public static int maxBlockVersionToGenerate = BlockVer.v13;

Check failure on line 134 in IxianDLT/Meta/Config.cs

View workflow job for this annotation

GitHub Actions / build

'BlockVer' does not contain a definition for 'v13'

Check failure on line 134 in IxianDLT/Meta/Config.cs

View workflow job for this annotation

GitHub Actions / build

'BlockVer' does not contain a definition for 'v13'
public static ulong upgradeBlockHeight = 4700000;

/// <summary>
/// Command to execute when a new block is accepted.
Expand Down

0 comments on commit 45d751c

Please sign in to comment.