Browse Source

Only compare same number of blocks when comparing peer chains

chain-weight-consensus
catbref 4 years ago
parent
commit
9b0e88ca87
  1. 5
      src/main/java/org/qortal/block/Block.java
  2. 7
      src/main/java/org/qortal/block/BlockChain.java
  3. 3
      src/main/resources/blockchain.json
  4. 23
      src/test/java/org/qortal/test/ChainWeightTests.java
  5. 4
      src/test/resources/test-chain-v2-founder-rewards.json
  6. 4
      src/test/resources/test-chain-v2-leftover-reward.json
  7. 4
      src/test/resources/test-chain-v2-minting.json
  8. 4
      src/test/resources/test-chain-v2-qora-holder-extremes.json
  9. 4
      src/test/resources/test-chain-v2-qora-holder.json
  10. 4
      src/test/resources/test-chain-v2-reward-scaling.json
  11. 4
      src/test/resources/test-chain-v2.json

5
src/main/java/org/qortal/block/Block.java

@ -822,10 +822,9 @@ public class Block {
parentHeight = blockSummaryData.getHeight();
parentBlockSignature = blockSummaryData.getSignature();
/* Potential future consensus change: only comparing the same number of blocks.
if (parentHeight >= maxHeight)
// After this timestamp, we only compare the same number of blocks
if (NTP.getTime() >= BlockChain.getInstance().getCalcChainWeightTimestamp() && parentHeight >= maxHeight)
break;
*/
}
return cumulativeWeight;

7
src/main/java/org/qortal/block/BlockChain.java

@ -70,7 +70,8 @@ public class BlockChain {
private GenesisBlock.GenesisInfo genesisInfo;
public enum FeatureTrigger {
atFindNextTransactionFix;
atFindNextTransactionFix,
calcChainWeightTimestamp;
}
/** Map of which blockchain features are enabled when (height/timestamp) */
@ -376,6 +377,10 @@ public class BlockChain {
return this.featureTriggers.get(FeatureTrigger.atFindNextTransactionFix.name()).intValue();
}
public long getCalcChainWeightTimestamp() {
return this.featureTriggers.get(FeatureTrigger.calcChainWeightTimestamp.name()).longValue();
}
// More complex getters for aspects that change by height or timestamp
public long getRewardAtHeight(int ourHeight) {

3
src/main/resources/blockchain.json

@ -48,7 +48,8 @@
"minutesPerBlock": 1
},
"featureTriggers": {
"atFindNextTransactionFix": 275000
"atFindNextTransactionFix": 275000,
"calcChainWeightTimestamp": 1616000000000,
},
"genesisInfo": {
"version": 4,

23
src/test/java/org/qortal/test/ChainWeightTests.java

@ -11,6 +11,7 @@ import java.util.Random;
import org.qortal.account.Account;
import org.qortal.block.Block;
import org.qortal.block.BlockChain;
import org.qortal.data.block.BlockSummaryData;
import org.qortal.repository.DataException;
import org.qortal.repository.Repository;
@ -19,7 +20,9 @@ import org.qortal.test.common.Common;
import org.qortal.test.common.TestAccount;
import org.qortal.transform.Transformer;
import org.qortal.transform.block.BlockTransformer;
import org.qortal.utils.NTP;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class ChainWeightTests extends Common {
@ -27,6 +30,12 @@ public class ChainWeightTests extends Common {
private static final Random RANDOM = new Random();
private static final NumberFormat FORMATTER = new DecimalFormat("0.###E0");
@BeforeClass
public static void beforeClass() {
// We need this so that NTP.getTime() in Block.calcChainWeight() doesn't return null, causing NPE
NTP.setFixedOffset(0L);
}
@Before
public void beforeTest() throws DataException {
Common.useSettings("test-settings-v2-minting.json");
@ -182,7 +191,7 @@ public class ChainWeightTests extends Common {
return BigInteger.valueOf(blockSummaryData.getOnlineAccountsCount()).shiftLeft(accountsCountShift).add(keyDistance);
}
// Check that a longer chain beats a shorter chain
// Check that a longer chain has same weight as shorter/truncated chain
@Test
public void testLongerChain() throws DataException {
try (final Repository repository = RepositoryManager.getRepository()) {
@ -190,18 +199,20 @@ public class ChainWeightTests extends Common {
BlockSummaryData commonBlockSummary = genBlockSummary(repository, commonBlockHeight);
byte[] commonBlockGeneratorKey = commonBlockSummary.getMinterPublicKey();
List<BlockSummaryData> shorterChain = genBlockSummaries(repository, 3, commonBlockSummary);
List<BlockSummaryData> longerChain = genBlockSummaries(repository, shorterChain.size() + 1, commonBlockSummary);
populateBlockSummariesMinterLevels(repository, shorterChain);
List<BlockSummaryData> longerChain = genBlockSummaries(repository, 6, commonBlockSummary);
populateBlockSummariesMinterLevels(repository, longerChain);
List<BlockSummaryData> shorterChain = longerChain.subList(0, longerChain.size() / 2);
final int mutualHeight = commonBlockHeight - 1 + Math.min(shorterChain.size(), longerChain.size());
BigInteger shorterChainWeight = Block.calcChainWeight(commonBlockHeight, commonBlockGeneratorKey, shorterChain, mutualHeight);
BigInteger longerChainWeight = Block.calcChainWeight(commonBlockHeight, commonBlockGeneratorKey, longerChain, mutualHeight);
assertEquals("longer chain should have greater weight", 1, longerChainWeight.compareTo(shorterChainWeight));
if (NTP.getTime() >= BlockChain.getInstance().getCalcChainWeightTimestamp())
assertEquals("longer chain should have same weight", 0, longerChainWeight.compareTo(shorterChainWeight));
else
assertEquals("longer chain should have greater weight", 1, longerChainWeight.compareTo(shorterChainWeight));
}
}

4
src/test/resources/test-chain-v2-founder-rewards.json

@ -44,7 +44,9 @@
"powfixTimestamp": 0,
"qortalTimestamp": 0,
"newAssetPricingTimestamp": 0,
"groupApprovalTimestamp": 0
"groupApprovalTimestamp": 0,
"atFindNextTransactionFix": 0,
"calcChainWeightTimestamp": 0
},
"genesisInfo": {
"version": 4,

4
src/test/resources/test-chain-v2-leftover-reward.json

@ -44,7 +44,9 @@
"powfixTimestamp": 0,
"qortalTimestamp": 0,
"newAssetPricingTimestamp": 0,
"groupApprovalTimestamp": 0
"groupApprovalTimestamp": 0,
"atFindNextTransactionFix": 0,
"calcChainWeightTimestamp": 0
},
"genesisInfo": {
"version": 4,

4
src/test/resources/test-chain-v2-minting.json

@ -44,7 +44,9 @@
"powfixTimestamp": 0,
"qortalTimestamp": 0,
"newAssetPricingTimestamp": 0,
"groupApprovalTimestamp": 0
"groupApprovalTimestamp": 0,
"atFindNextTransactionFix": 0,
"calcChainWeightTimestamp": 0
},
"genesisInfo": {
"version": 4,

4
src/test/resources/test-chain-v2-qora-holder-extremes.json

@ -44,7 +44,9 @@
"powfixTimestamp": 0,
"qortalTimestamp": 0,
"newAssetPricingTimestamp": 0,
"groupApprovalTimestamp": 0
"groupApprovalTimestamp": 0,
"atFindNextTransactionFix": 0,
"calcChainWeightTimestamp": 0
},
"genesisInfo": {
"version": 4,

4
src/test/resources/test-chain-v2-qora-holder.json

@ -44,7 +44,9 @@
"powfixTimestamp": 0,
"qortalTimestamp": 0,
"newAssetPricingTimestamp": 0,
"groupApprovalTimestamp": 0
"groupApprovalTimestamp": 0,
"atFindNextTransactionFix": 0,
"calcChainWeightTimestamp": 0
},
"genesisInfo": {
"version": 4,

4
src/test/resources/test-chain-v2-reward-scaling.json

@ -44,7 +44,9 @@
"powfixTimestamp": 0,
"qortalTimestamp": 0,
"newAssetPricingTimestamp": 0,
"groupApprovalTimestamp": 0
"groupApprovalTimestamp": 0,
"atFindNextTransactionFix": 0,
"calcChainWeightTimestamp": 0
},
"genesisInfo": {
"version": 4,

4
src/test/resources/test-chain-v2.json

@ -44,7 +44,9 @@
"powfixTimestamp": 0,
"qortalTimestamp": 0,
"newAssetPricingTimestamp": 0,
"groupApprovalTimestamp": 0
"groupApprovalTimestamp": 0,
"atFindNextTransactionFix": 0,
"calcChainWeightTimestamp": 0
},
"genesisInfo": {
"version": 4,

Loading…
Cancel
Save