|
|
|
@ -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)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|