Add API call to list blocks with given generator. +more tests +pad genesis public key

This commit is contained in:
catbref
2019-04-17 18:11:16 +01:00
parent d1c547f24a
commit 93230e9704
7 changed files with 143 additions and 2 deletions

View File

@@ -4,11 +4,48 @@ import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import org.qora.account.PrivateKeyAccount;
import org.qora.data.transaction.PaymentTransactionData;
import org.qora.data.transaction.ProxyForgingTransactionData;
import org.qora.data.transaction.TransactionData;
import org.qora.group.Group;
import org.qora.repository.DataException;
import org.qora.repository.Repository;
public class AccountUtils {
public static final int txGroupId = Group.NO_GROUP;
public static final BigDecimal fee = BigDecimal.ONE.setScale(8);
public static void pay(Repository repository, String sender, String recipient, BigDecimal amount) throws DataException {
PrivateKeyAccount sendingAccount = Common.getTestAccount(repository, sender);
PrivateKeyAccount recipientAccount = Common.getTestAccount(repository, recipient);
byte[] reference = sendingAccount.getLastReference();
long timestamp = repository.getTransactionRepository().fromSignature(reference).getTimestamp() + 1000;
TransactionData transactionData = new PaymentTransactionData(timestamp, txGroupId, reference, sendingAccount.getPublicKey(), recipientAccount.getAddress(), amount, fee);
TransactionUtils.signAndForge(repository, transactionData, sendingAccount);
}
public static byte[] proxyForging(Repository repository, String forger, String recipient, BigDecimal share) throws DataException {
PrivateKeyAccount forgingAccount = Common.getTestAccount(repository, forger);
PrivateKeyAccount recipientAccount = Common.getTestAccount(repository, recipient);
byte[] reference = forgingAccount.getLastReference();
long timestamp = repository.getTransactionRepository().fromSignature(reference).getTimestamp() + 1000;
byte[] proxyPrivateKey = forgingAccount.getSharedSecret(recipientAccount.getPublicKey());
PrivateKeyAccount proxyAccount = new PrivateKeyAccount(null, proxyPrivateKey);
TransactionData transactionData = new ProxyForgingTransactionData(timestamp, txGroupId, reference, forgingAccount.getPublicKey(), recipientAccount.getAddress(), proxyAccount.getPublicKey(), share, fee);
TransactionUtils.signAndForge(repository, transactionData, forgingAccount);
return proxyPrivateKey;
}
public static Map<String, Map<Long, BigDecimal>> getBalances(Repository repository, long... assetIds) throws DataException {
Map<String, Map<Long, BigDecimal>> balances = new HashMap<>();

View File

@@ -1,6 +1,7 @@
package org.qora.test.forging;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;
@@ -74,4 +75,30 @@ public class RewardTests extends Common {
}
}
}
@Test
public void testProxyReward() throws DataException {
final BigDecimal share = new BigDecimal("12.8");
try (final Repository repository = RepositoryManager.getRepository()) {
// Bob needs to make a transaction so his public key is known
AccountUtils.pay(repository, "bob", "chloe", new BigDecimal("1.4444").setScale(8));
byte[] proxyPrivateKey = AccountUtils.proxyForging(repository, "alice", "bob", share);
PrivateKeyAccount proxyAccount = new PrivateKeyAccount(repository, proxyPrivateKey);
Map<String, Map<Long, BigDecimal>> initialBalances = AccountUtils.getBalances(repository, Asset.QORA);
BlockGenerator.generateTestingBlock(repository, proxyAccount);
// We're expected reward * 12.8% to Bob, the rest to Alice
// (first reward is good for first 10 blocks)
BigDecimal firstReward = BlockChain.getInstance().getBlockRewardsByHeight().get(0).reward;
BigDecimal bobShare = firstReward.multiply(share.movePointLeft(2)).setScale(8, RoundingMode.DOWN);
AccountUtils.assertBalance(repository, "bob", Asset.QORA, initialBalances.get("bob").get(Asset.QORA).add(bobShare));
BigDecimal aliceShare = firstReward.subtract(bobShare);
AccountUtils.assertBalance(repository, "alice", Asset.QORA, initialBalances.get("alice").get(Asset.QORA).add(aliceShare));
}
}
}