mirror of
https://github.com/Qortal/qortal.git
synced 2025-07-23 04:36:50 +00:00
Self-reward-share tests and fixes.
Added Transaction.isFeeValid() to allow transaction subclasses to override and allow zero fees, etc. Added tests to cover self-reward-shares, including zero fee scenario. Set 'dilbert' test account to level 8 in test genesis block. Removed leftover mention of "previous_level" from HSQLDBAccountLevelTransactions, and AccountLevelTransactionData. (Previous level makes no sense as ACCOUNT_LEVEL transactions are genesis-block only). Fixed some incorrect uses of PrivateKeyAccount.getSharedSecret() to PrivateKeyAccount.getRewardSharePrivateKey() in tests.
This commit is contained in:
@@ -38,11 +38,11 @@ public class AccountUtils {
|
||||
byte[] reference = mintingAccount.getLastReference();
|
||||
long timestamp = repository.getTransactionRepository().fromSignature(reference).getTimestamp() + 1;
|
||||
|
||||
byte[] rewardSharePrivateKey = mintingAccount.getSharedSecret(recipientAccount.getPublicKey());
|
||||
PrivateKeyAccount rewardShareAccount = new PrivateKeyAccount(null, rewardSharePrivateKey);
|
||||
byte[] rewardSharePrivateKey = mintingAccount.getRewardSharePrivateKey(recipientAccount.getPublicKey());
|
||||
byte[] rewardSharePublicKey = PrivateKeyAccount.toPublicKey(rewardSharePrivateKey);
|
||||
|
||||
BaseTransactionData baseTransactionData = new BaseTransactionData(timestamp, txGroupId, reference, mintingAccount.getPublicKey(), fee, null);
|
||||
TransactionData transactionData = new RewardShareTransactionData(baseTransactionData, recipientAccount.getAddress(), rewardShareAccount.getPublicKey(), sharePercent);
|
||||
TransactionData transactionData = new RewardShareTransactionData(baseTransactionData, recipientAccount.getAddress(), rewardSharePublicKey, sharePercent);
|
||||
|
||||
return transactionData;
|
||||
}
|
||||
@@ -54,7 +54,7 @@ public class AccountUtils {
|
||||
TransactionUtils.signAndMint(repository, transactionData, rewardShareAccount);
|
||||
|
||||
PrivateKeyAccount recipientAccount = Common.getTestAccount(repository, recipient);
|
||||
byte[] rewardSharePrivateKey = rewardShareAccount.getSharedSecret(recipientAccount.getPublicKey());
|
||||
byte[] rewardSharePrivateKey = rewardShareAccount.getRewardSharePrivateKey(recipientAccount.getPublicKey());
|
||||
|
||||
return rewardSharePrivateKey;
|
||||
}
|
||||
|
@@ -36,7 +36,7 @@ public class TransactionUtils {
|
||||
assertEquals("Transaction invalid", ValidationResult.OK, result);
|
||||
}
|
||||
|
||||
/** Signs transaction using given account and forges a new block, using "alice" account. */
|
||||
/** Signs transaction using given account and forges a new block, using "alice" self-reward-share key. */
|
||||
public static void signAndMint(Repository repository, TransactionData transactionData, PrivateKeyAccount signingAccount) throws DataException {
|
||||
signAsUnconfirmed(repository, transactionData, signingAccount);
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package org.qora.test.forging;
|
||||
package org.qora.test.minting;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -16,6 +16,7 @@ import org.qora.repository.RepositoryManager;
|
||||
import org.qora.test.common.AccountUtils;
|
||||
import org.qora.test.common.BlockUtils;
|
||||
import org.qora.test.common.Common;
|
||||
import org.qora.test.common.TransactionUtils;
|
||||
import org.qora.transaction.Transaction;
|
||||
import org.qora.transaction.Transaction.ValidationResult;
|
||||
import org.qora.utils.Base58;
|
||||
@@ -124,4 +125,51 @@ public class RewardShareTests extends Common {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelfShare() throws DataException {
|
||||
final String testAccountName = "dilbert";
|
||||
|
||||
try (final Repository repository = RepositoryManager.getRepository()) {
|
||||
PrivateKeyAccount signingAccount = Common.getTestAccount(repository, testAccountName);
|
||||
// byte[] rewardSharePrivateKey = aliceAccount.getRewardSharePrivateKey(aliceAccount.getPublicKey());
|
||||
// PrivateKeyAccount rewardShareAccount = new PrivateKeyAccount(repository, rewardSharePrivateKey);
|
||||
|
||||
// Create self-reward-share
|
||||
TransactionData transactionData = AccountUtils.createRewardShare(repository, testAccountName, testAccountName, BigDecimal.valueOf(100L));
|
||||
Transaction transaction = Transaction.fromData(repository, transactionData);
|
||||
|
||||
// Confirm self-share is valid
|
||||
ValidationResult validationResult = transaction.isValidUnconfirmed();
|
||||
assertEquals("Initial self-share should be valid", ValidationResult.OK, validationResult);
|
||||
|
||||
// Check zero fee is valid
|
||||
transactionData.setFee(BigDecimal.ZERO);
|
||||
validationResult = transaction.isValidUnconfirmed();
|
||||
assertEquals("Zero-fee self-share should be valid", ValidationResult.OK, validationResult);
|
||||
|
||||
TransactionUtils.signAndMint(repository, transactionData, signingAccount);
|
||||
|
||||
// Subsequent non-terminating (0% share) self-reward-share should be invalid
|
||||
TransactionData newTransactionData = AccountUtils.createRewardShare(repository, testAccountName, testAccountName, BigDecimal.valueOf(99L));
|
||||
Transaction newTransaction = Transaction.fromData(repository, newTransactionData);
|
||||
|
||||
// Confirm subsequent self-reward-share is actually invalid
|
||||
validationResult = newTransaction.isValidUnconfirmed();
|
||||
assertNotSame("Subsequent self-share should be invalid", ValidationResult.OK, validationResult);
|
||||
|
||||
// Recheck with zero fee
|
||||
newTransactionData.setFee(BigDecimal.ZERO);
|
||||
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);
|
||||
newTransaction = Transaction.fromData(repository, newTransactionData);
|
||||
|
||||
// Confirm terminating reward-share is valid
|
||||
validationResult = newTransaction.isValidUnconfirmed();
|
||||
assertEquals("Subsequent zero-fee self-share should be invalid", ValidationResult.OK, validationResult);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package org.qora.test.forging;
|
||||
package org.qora.test.minting;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
@@ -37,11 +37,11 @@ public class RewardTests extends Common {
|
||||
try (final Repository repository = RepositoryManager.getRepository()) {
|
||||
Map<String, Map<Long, BigDecimal>> initialBalances = AccountUtils.getBalances(repository, Asset.QORT);
|
||||
|
||||
PrivateKeyAccount forgingAccount = Common.getTestAccount(repository, "alice");
|
||||
PrivateKeyAccount mintingAccount = Common.getTestAccount(repository, "alice");
|
||||
|
||||
BigDecimal blockReward = BlockUtils.getNextBlockReward(repository);
|
||||
|
||||
BlockMinter.mintTestingBlock(repository, forgingAccount);
|
||||
BlockMinter.mintTestingBlock(repository, mintingAccount);
|
||||
|
||||
BigDecimal expectedBalance = initialBalances.get("alice").get(Asset.QORT).add(blockReward);
|
||||
AccountUtils.assertBalance(repository, "alice", Asset.QORT, expectedBalance);
|
||||
@@ -53,7 +53,7 @@ public class RewardTests extends Common {
|
||||
try (final Repository repository = RepositoryManager.getRepository()) {
|
||||
Map<String, Map<Long, BigDecimal>> initialBalances = AccountUtils.getBalances(repository, Asset.QORT);
|
||||
|
||||
PrivateKeyAccount forgingAccount = Common.getTestAccount(repository, "alice");
|
||||
PrivateKeyAccount mintingAccount = Common.getTestAccount(repository, "alice");
|
||||
|
||||
List<RewardByHeight> rewards = BlockChain.getInstance().getBlockRewardsByHeight();
|
||||
|
||||
@@ -68,7 +68,7 @@ public class RewardTests extends Common {
|
||||
rewardInfo = rewards.get(rewardIndex);
|
||||
}
|
||||
|
||||
BlockMinter.mintTestingBlock(repository, forgingAccount);
|
||||
BlockMinter.mintTestingBlock(repository, mintingAccount);
|
||||
expectedBalance = expectedBalance.add(rewardInfo.reward);
|
||||
}
|
||||
|
||||
@@ -77,16 +77,16 @@ public class RewardTests extends Common {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProxyReward() throws DataException {
|
||||
public void testRewardSharing() throws DataException {
|
||||
final BigDecimal share = new BigDecimal("12.8");
|
||||
|
||||
try (final Repository repository = RepositoryManager.getRepository()) {
|
||||
byte[] proxyPrivateKey = AccountUtils.rewardShare(repository, "alice", "bob", share);
|
||||
PrivateKeyAccount proxyAccount = new PrivateKeyAccount(repository, proxyPrivateKey);
|
||||
byte[] rewardSharePrivateKey = AccountUtils.rewardShare(repository, "alice", "bob", share);
|
||||
PrivateKeyAccount rewardShareAccount = new PrivateKeyAccount(repository, rewardSharePrivateKey);
|
||||
|
||||
Map<String, Map<Long, BigDecimal>> initialBalances = AccountUtils.getBalances(repository, Asset.QORT);
|
||||
BigDecimal blockReward = BlockUtils.getNextBlockReward(repository);
|
||||
BlockMinter.mintTestingBlock(repository, proxyAccount);
|
||||
BlockMinter.mintTestingBlock(repository, rewardShareAccount);
|
||||
|
||||
// We're expecting reward * 12.8% to Bob, the rest to Alice
|
||||
|
@@ -52,6 +52,7 @@
|
||||
{ "type": "ISSUE_ASSET", "owner": "QixPbJUwsaHsVEofJdozU9zgVqkK6aYhrK", "assetName": "OTHER", "description": "other test asset", "data": "", "quantity": 1000000, "isDivisible": true, "fee": 0 },
|
||||
{ "type": "ISSUE_ASSET", "owner": "QgV4s3xnzLhVBEJxcYui4u4q11yhUHsd9v", "assetName": "GOLD", "description": "gold test asset", "data": "", "quantity": 1000000, "isDivisible": true, "fee": 0 },
|
||||
{ "type": "ACCOUNT_FLAGS", "target": "QgV4s3xnzLhVBEJxcYui4u4q11yhUHsd9v", "andMask": -1, "orMask": 1, "xorMask": 0 },
|
||||
{ "type": "ACCOUNT_LEVEL", "target": "Qci5m9k4rcwe4ruKrZZQKka4FzUUMut3er", "level": 8 },
|
||||
{ "type": "REWARD_SHARE", "minterPublicKey": "2tiMr5LTpaWCgbRvkPK8TFd7k63DyHJMMFFsz9uBf1ZP", "recipient": "QgV4s3xnzLhVBEJxcYui4u4q11yhUHsd9v", "rewardSharePublicKey": "7PpfnvLSG7y4HPh8hE7KoqAjLCkv7Ui6xw4mKAkbZtox", "sharePercent": 100 }
|
||||
]
|
||||
}
|
||||
|
Reference in New Issue
Block a user