mirror of
https://github.com/Qortal/qortal.git
synced 2025-07-23 04:36:50 +00:00
Cancel reward-shares with NEGATIVE share instead of ZERO. Also: bug-fixes!
Now reward-shares with zero percent are valid, to allow the 'recipient' party to gain "number of minted blocks" but no actual block reward. Correspondly, the special zero share used to cancel reward-shares has been changed to be any negative value. Block rewards, founder 'leftovers': if founder is minter account in any online reward shares, then the per-founder-share is spread across their online reward-shares, otherwise it's simply/wholy given to that founder. Created a new DB table to hold "next block height", updated via triggers on Blocks. This is so various sub-queries can simply read the next-block-height value instead of complex IFNULL(MAX(height),0)+1 or SELECT height FROM Blocks ORDER BY height DESC. Prior code was also broken in edge cases, e.g. no genesis block, or ran slow. Added tests to cover above. Deleted BTC tests as they're obsolete. Added/improved other tests.
This commit is contained in:
@@ -96,6 +96,11 @@ public class AccountBalanceTests extends Common {
|
||||
|
||||
BigDecimal initialBalance = testNewerBalance(repository, alice);
|
||||
|
||||
// Fetch all historic balances
|
||||
List<AccountBalanceData> historicBalances = repository.getAccountRepository().getHistoricBalances(alice.getAddress(), Asset.QORT);
|
||||
for (AccountBalanceData historicBalance : historicBalances)
|
||||
System.out.println(String.format("Balance at height %d: %s", historicBalance.getHeight(), historicBalance.getBalance().toPlainString()));
|
||||
|
||||
// Fetch balance at height 1, even though newer balance exists
|
||||
AccountBalanceData accountBalanceData = repository.getAccountRepository().getBalance(alice.getAddress(), Asset.QORT, 1);
|
||||
BigDecimal genesisBalance = accountBalanceData.getBalance();
|
||||
@@ -115,6 +120,7 @@ public class AccountBalanceTests extends Common {
|
||||
|
||||
try (final Repository repository = RepositoryManager.getRepository()) {
|
||||
PublicKeyAccount recipientAccount = new PublicKeyAccount(repository, publicKey);
|
||||
System.out.println(String.format("Test recipient: %s", recipientAccount.getAddress()));
|
||||
|
||||
// Mint a few blocks
|
||||
for (int i = 0; i < 10; ++i)
|
||||
@@ -124,6 +130,12 @@ public class AccountBalanceTests extends Common {
|
||||
BigDecimal balance = recipientAccount.getConfirmedBalance(Asset.QORT);
|
||||
assertEqualBigDecimals("recipient's balance should be zero", BigDecimal.ZERO, balance);
|
||||
|
||||
// Confirm recipient has no historic balances
|
||||
List<AccountBalanceData> historicBalances = repository.getAccountRepository().getHistoricBalances(recipientAccount.getAddress(), Asset.QORT);
|
||||
for (AccountBalanceData historicBalance : historicBalances)
|
||||
System.err.println(String.format("Block %d: %s", historicBalance.getHeight(), historicBalance.getBalance().toPlainString()));
|
||||
assertTrue("recipient should not have historic balances yet", historicBalances.isEmpty());
|
||||
|
||||
// Send 1 QORT to recipient
|
||||
TestAccount sendingAccount = Common.getTestAccount(repository, "alice");
|
||||
pay(repository, sendingAccount, recipientAccount, BigDecimal.ONE);
|
||||
@@ -145,7 +157,7 @@ public class AccountBalanceTests extends Common {
|
||||
balance = recipientAccount.getConfirmedBalance(Asset.QORT);
|
||||
assertEqualBigDecimals("recipient's balance incorrect", totalAmount, balance);
|
||||
|
||||
List<AccountBalanceData> historicBalances = repository.getAccountRepository().getHistoricBalances(recipientAccount.getAddress(), Asset.QORT);
|
||||
historicBalances = repository.getAccountRepository().getHistoricBalances(recipientAccount.getAddress(), Asset.QORT);
|
||||
for (AccountBalanceData historicBalance : historicBalances)
|
||||
System.out.println(String.format("Block %d: %s", historicBalance.getHeight(), historicBalance.getBalance().toPlainString()));
|
||||
|
||||
@@ -172,6 +184,12 @@ public class AccountBalanceTests extends Common {
|
||||
// Re-check balance from (now) invalid height
|
||||
accountBalanceData = repository.getAccountRepository().getBalance(recipientAccount.getAddress(), Asset.QORT, height - 2);
|
||||
assertNull("recipient's invalid-height balance data should be null", accountBalanceData);
|
||||
|
||||
// Confirm recipient has no historic balances
|
||||
historicBalances = repository.getAccountRepository().getHistoricBalances(recipientAccount.getAddress(), Asset.QORT);
|
||||
for (AccountBalanceData historicBalance : historicBalances)
|
||||
System.err.println(String.format("Block %d: %s", historicBalance.getHeight(), historicBalance.getBalance().toPlainString()));
|
||||
assertTrue("recipient should have no remaining historic balances", historicBalances.isEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -191,7 +209,7 @@ public class AccountBalanceTests extends Common {
|
||||
@Test
|
||||
public void testRepositorySpeed() throws DataException, SQLException {
|
||||
Random random = new Random();
|
||||
final long MAX_QUERY_TIME = 100L; // ms
|
||||
final long MAX_QUERY_TIME = 80L; // ms
|
||||
|
||||
try (final Repository repository = RepositoryManager.getRepository()) {
|
||||
System.out.println("Creating random accounts...");
|
||||
|
@@ -1,62 +0,0 @@
|
||||
package org.qora.test;
|
||||
|
||||
import org.bitcoinj.script.Script;
|
||||
import org.bitcoinj.script.ScriptBuilder;
|
||||
import org.junit.Test;
|
||||
import org.qora.crosschain.BTC;
|
||||
|
||||
import com.google.common.hash.HashCode;
|
||||
|
||||
public class BTCTests {
|
||||
|
||||
@Test
|
||||
public void testWatchAddress() throws Exception {
|
||||
// String testAddress = "mrTDPdM15cFWJC4g223BXX5snicfVJBx6M";
|
||||
String testAddress = "1GRENT17xMQe2ukPhwAeZU1TaUUon1Qc65";
|
||||
|
||||
long testStartTime = 1539000000L;
|
||||
|
||||
BTC btc = BTC.getInstance();
|
||||
|
||||
// Disabled for now, pending further work
|
||||
// btc.watch(testAddress, testStartTime);
|
||||
|
||||
// Disabled for now, pending further work
|
||||
// Thread.sleep(5000);
|
||||
|
||||
// Disabled for now, pending further work
|
||||
// btc.watch(testAddress, testStartTime);
|
||||
|
||||
btc.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWatchScript() throws Exception {
|
||||
long testStartTime = 1539000000L;
|
||||
|
||||
BTC btc = BTC.getInstance();
|
||||
|
||||
byte[] redeemScriptHash = HashCode.fromString("3dbcc35e69ebc449f616fa3eb3723dfad9cbb5b3").asBytes();
|
||||
Script redeemScript = ScriptBuilder.createP2SHOutputScript(redeemScriptHash);
|
||||
redeemScript.setCreationTimeSeconds(testStartTime);
|
||||
|
||||
// Disabled for now, pending further work
|
||||
// btc.watch(redeemScript);
|
||||
|
||||
// Disabled for now, pending further work
|
||||
Thread.sleep(5000);
|
||||
|
||||
// Disabled for now, pending further work
|
||||
// btc.watch(redeemScript);
|
||||
|
||||
btc.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateCheckpoints() throws Exception {
|
||||
BTC btc = BTC.getInstance();
|
||||
|
||||
btc.updateCheckpoints();
|
||||
}
|
||||
|
||||
}
|
@@ -50,6 +50,9 @@ public class EPCTests {
|
||||
}
|
||||
|
||||
private void testEPC(ExecuteProduceConsume testEPC) throws InterruptedException {
|
||||
final int runTime = 60; // seconds
|
||||
System.out.println(String.format("Testing EPC for %s seconds:", runTime));
|
||||
|
||||
final long start = System.currentTimeMillis();
|
||||
testEPC.start();
|
||||
|
||||
@@ -67,7 +70,7 @@ public class EPCTests {
|
||||
}, 1L, 1L, TimeUnit.SECONDS);
|
||||
|
||||
// Let it run for a minute
|
||||
Thread.sleep(60_000L);
|
||||
Thread.sleep(runTime * 1000L);
|
||||
statusExecutor.shutdownNow();
|
||||
|
||||
final long before = System.currentTimeMillis();
|
||||
|
@@ -24,7 +24,7 @@ public class MemoryPoWTests {
|
||||
Integer nonce = MemoryPoW.compute(data, start, range, difficulty);
|
||||
long finishTime = System.currentTimeMillis();
|
||||
|
||||
System.out.println(String.format("Memory-hard PoW took %dms", finishTime - startTime));
|
||||
System.out.println(String.format("Memory-hard PoW (buffer size: %dKB, range: %d, leading zeros: %d) took %dms", MemoryPoW.WORK_BUFFER_LENGTH / 1024, range, difficulty, finishTime - startTime));
|
||||
|
||||
assertNotNull(nonce);
|
||||
|
||||
|
@@ -7,11 +7,14 @@ import org.qora.asset.Asset;
|
||||
import org.qora.repository.DataException;
|
||||
import org.qora.repository.Repository;
|
||||
import org.qora.repository.RepositoryManager;
|
||||
import org.qora.repository.hsqldb.HSQLDBRepository;
|
||||
import org.qora.test.common.BlockUtils;
|
||||
import org.qora.test.common.Common;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@@ -90,4 +93,52 @@ public class RepositoryTests extends Common {
|
||||
}
|
||||
}
|
||||
|
||||
/** Check that the <i>sub-query</i> used to fetch highest block height is optimized by HSQLDB. */
|
||||
@Test
|
||||
public void testBlockHeightSpeed() throws DataException, SQLException {
|
||||
try (final Repository repository = RepositoryManager.getRepository()) {
|
||||
// Mint some blocks
|
||||
System.out.println("Minting test blocks - should take approx. 30 seconds...");
|
||||
for (int i = 0; i < 30000; ++i)
|
||||
BlockUtils.mintBlock(repository);
|
||||
|
||||
final HSQLDBRepository hsqldb = (HSQLDBRepository) repository;
|
||||
|
||||
// Too slow:
|
||||
testSql(hsqldb, "SELECT IFNULL(MAX(height), 0) + 1 FROM Blocks", false);
|
||||
|
||||
// Fast but if there are no rows, then no result is returned, which causes some triggers to fail:
|
||||
testSql(hsqldb, "SELECT IFNULL(height, 0) + 1 FROM (SELECT height FROM Blocks ORDER BY height DESC LIMIT 1)", true);
|
||||
|
||||
// Too slow:
|
||||
testSql(hsqldb, "SELECT COUNT(*) + 1 FROM Blocks", false);
|
||||
|
||||
// 2-stage, using cached value:
|
||||
hsqldb.prepareStatement("DROP TABLE IF EXISTS TestNextBlockHeight").execute();
|
||||
hsqldb.prepareStatement("CREATE TABLE TestNextBlockHeight (height INT NOT NULL)").execute();
|
||||
hsqldb.prepareStatement("INSERT INTO TestNextBlockHeight VALUES (SELECT IFNULL(MAX(height), 0) + 1 FROM Blocks)").execute();
|
||||
|
||||
// 1: Check fetching cached next block height is fast:
|
||||
testSql(hsqldb, "SELECT height from TestNextBlockHeight", true);
|
||||
|
||||
// 2: Check updating NextBlockHeight (typically called via trigger) is fast:
|
||||
testSql(hsqldb, "UPDATE TestNextBlockHeight SET height = (SELECT height FROM Blocks ORDER BY height DESC LIMIT 1)", true);
|
||||
}
|
||||
}
|
||||
|
||||
private void testSql(HSQLDBRepository hsqldb, String sql, boolean isFast) throws DataException, SQLException {
|
||||
// Execute query to prime caches
|
||||
hsqldb.prepareStatement(sql).execute();
|
||||
|
||||
// Execute again for a slightly more accurate timing
|
||||
final long start = System.currentTimeMillis();
|
||||
hsqldb.prepareStatement(sql).execute();
|
||||
|
||||
final long executionTime = System.currentTimeMillis() - start;
|
||||
System.out.println(String.format("%s: [%d ms] SQL: %s", (isFast ? "fast": "slow"), executionTime, sql));
|
||||
|
||||
final long threshold = 3; // ms
|
||||
assertTrue( isFast ? executionTime < threshold : executionTime > threshold);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -117,16 +117,16 @@ public class SerializationTests extends Common {
|
||||
public void benchmarkBitSetCompression() {
|
||||
Random random = new Random();
|
||||
|
||||
System.out.println(String.format("Known Online UncompressedBitSet UncompressedIntList Compressed"));
|
||||
System.out.println(String.format("Known Online UncompressedBitSet UncompressedIntList Compressed"));
|
||||
|
||||
for (int run = 0; run < 1000; ++run) {
|
||||
for (int run = 0; run < 100; ++run) {
|
||||
final int numberOfKnownAccounts = random.nextInt(1 << 17) + 1;
|
||||
|
||||
// 5% to 25%
|
||||
final int numberOfAccountsToEncode = random.nextInt((numberOfKnownAccounts / 20) + numberOfKnownAccounts / 5);
|
||||
// 3% to 23%
|
||||
final int numberOfAccountsToEncode = random.nextInt((numberOfKnownAccounts / 20) + numberOfKnownAccounts / 3);
|
||||
|
||||
// Enough uncompressed bytes to fit one bit per known account
|
||||
final int uncompressedBitSetSize = ((numberOfKnownAccounts - 1) >> 3) + 1;
|
||||
final int uncompressedBitSetSize = ((numberOfKnownAccounts - 1) >> 3) + 1; // the >> 3 is to scale size from 8 bits to 1 byte
|
||||
|
||||
// Size of a simple list of ints
|
||||
final int uncompressedIntListSize = numberOfAccountsToEncode * 4;
|
||||
@@ -138,7 +138,7 @@ public class SerializationTests extends Common {
|
||||
|
||||
int compressedSize = compressedSet.toByteBuffer().remaining();
|
||||
|
||||
System.out.println(String.format("%d %d %d %d %d", numberOfKnownAccounts, numberOfAccountsToEncode, uncompressedBitSetSize, uncompressedIntListSize, compressedSize));
|
||||
System.out.println(String.format("%6d %6d %18d %19d %10d", numberOfKnownAccounts, numberOfAccountsToEncode, uncompressedBitSetSize, uncompressedIntListSize, compressedSize));
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -23,6 +23,8 @@ import org.qora.utils.Base58;
|
||||
|
||||
public class RewardShareTests extends Common {
|
||||
|
||||
private static final BigDecimal CANCEL_SHARE_PERCENT = BigDecimal.ONE.negate();
|
||||
|
||||
@Before
|
||||
public void beforeTest() throws DataException {
|
||||
Common.useDefaultSettings();
|
||||
@@ -60,7 +62,7 @@ public class RewardShareTests extends Common {
|
||||
assertEqualBigDecimals("Incorrect share percentage", sharePercent, rewardShareData.getSharePercent());
|
||||
|
||||
// Delete reward-share
|
||||
byte[] newRewardSharePrivateKey = AccountUtils.rewardShare(repository, "alice", "bob", BigDecimal.ZERO);
|
||||
byte[] newRewardSharePrivateKey = AccountUtils.rewardShare(repository, "alice", "bob", CANCEL_SHARE_PERCENT);
|
||||
PrivateKeyAccount newRewardShareAccount = new PrivateKeyAccount(repository, newRewardSharePrivateKey);
|
||||
|
||||
// Confirm reward-share keys match
|
||||
@@ -112,10 +114,10 @@ public class RewardShareTests extends Common {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZeroInitialShareInvalid() throws DataException {
|
||||
public void testNegativeInitialShareInvalid() throws DataException {
|
||||
try (final Repository repository = RepositoryManager.getRepository()) {
|
||||
// Create invalid REWARD_SHARE transaction with initial 0% reward share
|
||||
TransactionData transactionData = AccountUtils.createRewardShare(repository, "alice", "bob", BigDecimal.ZERO);
|
||||
// Create invalid REWARD_SHARE transaction with initial negative reward share
|
||||
TransactionData transactionData = AccountUtils.createRewardShare(repository, "alice", "bob", CANCEL_SHARE_PERCENT);
|
||||
|
||||
// Confirm transaction is invalid
|
||||
Transaction transaction = Transaction.fromData(repository, transactionData);
|
||||
@@ -162,8 +164,8 @@ public class RewardShareTests extends Common {
|
||||
validationResult = newTransaction.isValidUnconfirmed();
|
||||
assertNotSame("Subsequent zero-fee self-share should be invalid", ValidationResult.OK, validationResult);
|
||||
|
||||
// Subsequent terminating (0% share) self-reward-share should be OK
|
||||
newTransactionData = AccountUtils.createRewardShare(repository, testAccountName, testAccountName, BigDecimal.ZERO);
|
||||
// Subsequent terminating (negative share) self-reward-share should be OK
|
||||
newTransactionData = AccountUtils.createRewardShare(repository, testAccountName, testAccountName, CANCEL_SHARE_PERCENT);
|
||||
newTransaction = Transaction.fromData(repository, newTransactionData);
|
||||
|
||||
// Confirm terminating reward-share is valid
|
||||
|
Reference in New Issue
Block a user