2018-06-13 15:48:28 +00:00
|
|
|
package test;
|
|
|
|
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
2018-07-03 10:40:24 +00:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2018-06-15 16:16:44 +00:00
|
|
|
import java.math.BigDecimal;
|
2018-06-21 11:38:45 +00:00
|
|
|
import java.util.ArrayList;
|
2018-06-29 09:29:18 +00:00
|
|
|
import java.util.Arrays;
|
2018-06-21 11:38:45 +00:00
|
|
|
import java.util.List;
|
2018-06-13 15:48:28 +00:00
|
|
|
|
2018-06-21 11:38:45 +00:00
|
|
|
import org.junit.After;
|
2018-06-13 15:48:28 +00:00
|
|
|
import org.junit.Test;
|
|
|
|
|
2018-06-15 16:16:44 +00:00
|
|
|
import com.google.common.hash.HashCode;
|
|
|
|
|
|
|
|
import data.account.AccountBalanceData;
|
|
|
|
import data.account.AccountData;
|
2018-06-13 15:48:28 +00:00
|
|
|
import data.block.BlockData;
|
2018-07-01 15:35:45 +00:00
|
|
|
import data.naming.NameData;
|
2018-07-03 10:40:24 +00:00
|
|
|
import data.transaction.BuyNameTransactionData;
|
2018-07-02 17:59:11 +00:00
|
|
|
import data.transaction.CancelSellNameTransactionData;
|
2018-06-21 11:38:45 +00:00
|
|
|
import data.transaction.CreatePollTransactionData;
|
2018-07-03 10:40:24 +00:00
|
|
|
import data.transaction.MessageTransactionData;
|
2018-06-15 16:16:44 +00:00
|
|
|
import data.transaction.PaymentTransactionData;
|
2018-07-01 15:35:45 +00:00
|
|
|
import data.transaction.RegisterNameTransactionData;
|
2018-07-02 17:09:36 +00:00
|
|
|
import data.transaction.SellNameTransactionData;
|
2018-07-01 15:35:45 +00:00
|
|
|
import data.transaction.UpdateNameTransactionData;
|
2018-06-29 09:29:18 +00:00
|
|
|
import data.transaction.VoteOnPollTransactionData;
|
2018-06-21 11:38:45 +00:00
|
|
|
import data.voting.PollData;
|
|
|
|
import data.voting.PollOptionData;
|
2018-06-29 10:05:15 +00:00
|
|
|
import data.voting.VoteOnPollData;
|
2018-06-15 16:16:44 +00:00
|
|
|
import qora.account.Account;
|
|
|
|
import qora.account.PrivateKeyAccount;
|
|
|
|
import qora.account.PublicKeyAccount;
|
|
|
|
import qora.assets.Asset;
|
2018-06-13 15:48:28 +00:00
|
|
|
import qora.block.Block;
|
2018-06-15 16:16:44 +00:00
|
|
|
import qora.block.BlockChain;
|
2018-07-03 10:40:24 +00:00
|
|
|
import qora.transaction.BuyNameTransaction;
|
2018-07-02 17:59:11 +00:00
|
|
|
import qora.transaction.CancelSellNameTransaction;
|
2018-06-21 11:38:45 +00:00
|
|
|
import qora.transaction.CreatePollTransaction;
|
2018-07-03 10:40:24 +00:00
|
|
|
import qora.transaction.MessageTransaction;
|
2018-06-15 16:16:44 +00:00
|
|
|
import qora.transaction.PaymentTransaction;
|
2018-07-01 15:35:45 +00:00
|
|
|
import qora.transaction.RegisterNameTransaction;
|
2018-07-02 17:09:36 +00:00
|
|
|
import qora.transaction.SellNameTransaction;
|
2018-06-13 15:48:28 +00:00
|
|
|
import qora.transaction.Transaction;
|
2018-06-15 16:16:44 +00:00
|
|
|
import qora.transaction.Transaction.ValidationResult;
|
2018-07-01 15:35:45 +00:00
|
|
|
import qora.transaction.UpdateNameTransaction;
|
2018-06-29 09:29:18 +00:00
|
|
|
import qora.transaction.VoteOnPollTransaction;
|
2018-06-15 16:16:44 +00:00
|
|
|
import repository.AccountRepository;
|
2018-06-13 15:48:28 +00:00
|
|
|
import repository.DataException;
|
|
|
|
import repository.Repository;
|
2018-06-15 16:16:44 +00:00
|
|
|
import repository.RepositoryFactory;
|
2018-06-13 15:48:28 +00:00
|
|
|
import repository.RepositoryManager;
|
2018-06-15 16:16:44 +00:00
|
|
|
import repository.hsqldb.HSQLDBRepositoryFactory;
|
2018-06-29 09:29:18 +00:00
|
|
|
import settings.Settings;
|
2018-06-13 15:48:28 +00:00
|
|
|
|
2018-06-15 16:16:44 +00:00
|
|
|
// Don't extend Common as we want to use an in-memory database
|
|
|
|
public class TransactionTests {
|
2018-06-13 15:48:28 +00:00
|
|
|
|
2018-06-15 16:16:44 +00:00
|
|
|
private static final String connectionUrl = "jdbc:hsqldb:mem:db/test;create=true;close_result=true;sql.strict_exec=true;sql.enforce_names=true;sql.syntax_mys=true";
|
2018-06-13 15:48:28 +00:00
|
|
|
|
2018-06-15 16:16:44 +00:00
|
|
|
private static final byte[] generatorSeed = HashCode.fromString("0123456789abcdeffedcba98765432100123456789abcdeffedcba9876543210").asBytes();
|
|
|
|
private static final byte[] senderSeed = HashCode.fromString("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef").asBytes();
|
|
|
|
private static final byte[] recipientSeed = HashCode.fromString("fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210").asBytes();
|
2018-06-13 15:48:28 +00:00
|
|
|
|
2018-06-21 11:38:45 +00:00
|
|
|
private static final BigDecimal generatorBalance = BigDecimal.valueOf(1_000_000_000L);
|
|
|
|
private static final BigDecimal senderBalance = BigDecimal.valueOf(1_000_000L);
|
|
|
|
|
|
|
|
private Repository repository;
|
|
|
|
private AccountRepository accountRepository;
|
2018-07-01 15:35:45 +00:00
|
|
|
private BlockData parentBlockData;
|
2018-06-21 11:38:45 +00:00
|
|
|
private PrivateKeyAccount sender;
|
|
|
|
private PrivateKeyAccount generator;
|
|
|
|
private byte[] reference;
|
|
|
|
|
2018-06-29 09:29:18 +00:00
|
|
|
public void createTestAccounts(Long genesisTimestamp) throws DataException {
|
2018-06-15 16:16:44 +00:00
|
|
|
RepositoryFactory repositoryFactory = new HSQLDBRepositoryFactory(connectionUrl);
|
|
|
|
RepositoryManager.setRepositoryFactory(repositoryFactory);
|
2018-06-13 15:48:28 +00:00
|
|
|
|
2018-06-13 16:46:51 +00:00
|
|
|
try (final Repository repository = RepositoryManager.getRepository()) {
|
2018-06-15 16:16:44 +00:00
|
|
|
assertEquals("Blockchain should be empty for this test", 0, repository.getBlockRepository().getBlockchainHeight());
|
|
|
|
}
|
2018-06-13 15:48:28 +00:00
|
|
|
|
2018-06-29 09:29:18 +00:00
|
|
|
// [Un]set genesis timestamp as required by test
|
|
|
|
if (genesisTimestamp != null)
|
|
|
|
Settings.getInstance().setGenesisTimestamp(genesisTimestamp);
|
|
|
|
else
|
|
|
|
Settings.getInstance().unsetGenesisTimestamp();
|
|
|
|
|
2018-06-15 16:16:44 +00:00
|
|
|
// This needs to be called outside of acquiring our own repository or it will deadlock
|
|
|
|
BlockChain.validate();
|
2018-06-13 15:48:28 +00:00
|
|
|
|
2018-06-21 11:38:45 +00:00
|
|
|
// Grab repository for further use, including during test itself
|
|
|
|
repository = RepositoryManager.getRepository();
|
|
|
|
|
|
|
|
// Grab genesis block
|
2018-07-01 15:35:45 +00:00
|
|
|
parentBlockData = repository.getBlockRepository().fromHeight(1);
|
2018-06-21 11:38:45 +00:00
|
|
|
|
|
|
|
accountRepository = repository.getAccountRepository();
|
|
|
|
|
|
|
|
// Create test generator account
|
|
|
|
generator = new PrivateKeyAccount(repository, generatorSeed);
|
|
|
|
accountRepository.save(new AccountData(generator.getAddress(), generatorSeed));
|
|
|
|
accountRepository.save(new AccountBalanceData(generator.getAddress(), Asset.QORA, generatorBalance));
|
|
|
|
|
|
|
|
// Create test sender account
|
|
|
|
sender = new PrivateKeyAccount(repository, senderSeed);
|
|
|
|
|
|
|
|
// Mock account
|
|
|
|
reference = senderSeed;
|
|
|
|
accountRepository.save(new AccountData(sender.getAddress(), reference));
|
|
|
|
|
|
|
|
// Mock balance
|
|
|
|
accountRepository.save(new AccountBalanceData(sender.getAddress(), Asset.QORA, senderBalance));
|
|
|
|
|
|
|
|
repository.saveChanges();
|
|
|
|
}
|
|
|
|
|
|
|
|
@After
|
|
|
|
public void closeRepository() throws DataException {
|
|
|
|
RepositoryManager.closeRepositoryFactory();
|
|
|
|
}
|
|
|
|
|
2018-07-03 10:40:24 +00:00
|
|
|
private Transaction createPayment(PrivateKeyAccount sender, String recipient) throws DataException {
|
|
|
|
// Make a new payment transaction
|
|
|
|
BigDecimal amount = BigDecimal.valueOf(1_000L);
|
|
|
|
BigDecimal fee = BigDecimal.ONE;
|
|
|
|
long timestamp = parentBlockData.getTimestamp() + 1_000;
|
|
|
|
PaymentTransactionData paymentTransactionData = new PaymentTransactionData(sender.getPublicKey(), recipient, amount, fee, timestamp, reference);
|
|
|
|
|
|
|
|
Transaction paymentTransaction = new PaymentTransaction(repository, paymentTransactionData);
|
|
|
|
paymentTransaction.calcSignature(sender);
|
|
|
|
|
|
|
|
return paymentTransaction;
|
|
|
|
}
|
|
|
|
|
2018-06-21 11:38:45 +00:00
|
|
|
@Test
|
|
|
|
public void testPaymentTransaction() throws DataException {
|
2018-06-29 09:29:18 +00:00
|
|
|
createTestAccounts(null);
|
|
|
|
|
2018-06-21 11:38:45 +00:00
|
|
|
// Make a new payment transaction
|
|
|
|
Account recipient = new PublicKeyAccount(repository, recipientSeed);
|
|
|
|
BigDecimal amount = BigDecimal.valueOf(1_000L);
|
|
|
|
BigDecimal fee = BigDecimal.ONE;
|
2018-07-01 15:35:45 +00:00
|
|
|
long timestamp = parentBlockData.getTimestamp() + 1_000;
|
2018-06-21 11:38:45 +00:00
|
|
|
PaymentTransactionData paymentTransactionData = new PaymentTransactionData(sender.getPublicKey(), recipient.getAddress(), amount, fee, timestamp,
|
|
|
|
reference);
|
|
|
|
|
|
|
|
Transaction paymentTransaction = new PaymentTransaction(repository, paymentTransactionData);
|
|
|
|
paymentTransaction.calcSignature(sender);
|
|
|
|
assertTrue(paymentTransaction.isSignatureValid());
|
|
|
|
assertEquals(ValidationResult.OK, paymentTransaction.isValid());
|
|
|
|
|
|
|
|
// Forge new block with payment transaction
|
2018-07-01 15:35:45 +00:00
|
|
|
Block block = new Block(repository, parentBlockData, generator, null, null);
|
2018-06-21 11:38:45 +00:00
|
|
|
block.addTransaction(paymentTransactionData);
|
|
|
|
block.sign();
|
|
|
|
|
|
|
|
assertTrue("Block signatures invalid", block.isSignatureValid());
|
|
|
|
assertEquals("Block is invalid", Block.ValidationResult.OK, block.isValid());
|
|
|
|
|
|
|
|
block.process();
|
|
|
|
repository.saveChanges();
|
|
|
|
|
|
|
|
// Check sender's balance
|
|
|
|
BigDecimal expectedBalance = senderBalance.subtract(amount).subtract(fee);
|
|
|
|
BigDecimal actualBalance = accountRepository.getBalance(sender.getAddress(), Asset.QORA).getBalance();
|
|
|
|
assertTrue("Sender's new balance incorrect", expectedBalance.compareTo(actualBalance) == 0);
|
|
|
|
|
|
|
|
// Fee should be in generator's balance
|
|
|
|
expectedBalance = generatorBalance.add(fee);
|
|
|
|
actualBalance = accountRepository.getBalance(generator.getAddress(), Asset.QORA).getBalance();
|
|
|
|
assertTrue("Generator's new balance incorrect", expectedBalance.compareTo(actualBalance) == 0);
|
|
|
|
|
|
|
|
// Amount should be in recipient's balance
|
|
|
|
expectedBalance = amount;
|
|
|
|
actualBalance = accountRepository.getBalance(recipient.getAddress(), Asset.QORA).getBalance();
|
|
|
|
assertTrue("Recipient's new balance incorrect", expectedBalance.compareTo(actualBalance) == 0);
|
|
|
|
}
|
|
|
|
|
2018-07-01 15:35:45 +00:00
|
|
|
@Test
|
|
|
|
public void testRegisterNameTransaction() throws DataException {
|
|
|
|
createTestAccounts(null);
|
|
|
|
|
|
|
|
// Make a new register name transaction
|
|
|
|
String name = "test name";
|
|
|
|
String data = "{\"key\":\"value\"}";
|
|
|
|
|
|
|
|
BigDecimal fee = BigDecimal.ONE;
|
|
|
|
long timestamp = parentBlockData.getTimestamp() + 1_000;
|
|
|
|
RegisterNameTransactionData registerNameTransactionData = new RegisterNameTransactionData(sender.getPublicKey(), sender.getAddress(), name, data, fee,
|
|
|
|
timestamp, reference);
|
|
|
|
|
|
|
|
Transaction registerNameTransaction = new RegisterNameTransaction(repository, registerNameTransactionData);
|
|
|
|
registerNameTransaction.calcSignature(sender);
|
|
|
|
assertTrue(registerNameTransaction.isSignatureValid());
|
|
|
|
assertEquals(ValidationResult.OK, registerNameTransaction.isValid());
|
|
|
|
|
|
|
|
// Forge new block with transaction
|
|
|
|
Block block = new Block(repository, parentBlockData, generator, null, null);
|
|
|
|
block.addTransaction(registerNameTransactionData);
|
|
|
|
block.sign();
|
|
|
|
|
|
|
|
assertTrue("Block signatures invalid", block.isSignatureValid());
|
|
|
|
assertEquals("Block is invalid", Block.ValidationResult.OK, block.isValid());
|
|
|
|
|
|
|
|
block.process();
|
|
|
|
repository.saveChanges();
|
|
|
|
|
|
|
|
// Check sender's balance
|
|
|
|
BigDecimal expectedBalance = senderBalance.subtract(fee);
|
|
|
|
BigDecimal actualBalance = accountRepository.getBalance(sender.getAddress(), Asset.QORA).getBalance();
|
|
|
|
assertTrue("Sender's new balance incorrect", expectedBalance.compareTo(actualBalance) == 0);
|
|
|
|
|
|
|
|
// Fee should be in generator's balance
|
|
|
|
expectedBalance = generatorBalance.add(fee);
|
|
|
|
actualBalance = accountRepository.getBalance(generator.getAddress(), Asset.QORA).getBalance();
|
|
|
|
assertTrue("Generator's new balance incorrect", expectedBalance.compareTo(actualBalance) == 0);
|
|
|
|
|
|
|
|
// Check name was registered
|
|
|
|
NameData actualNameData = this.repository.getNameRepository().fromName(name);
|
|
|
|
assertNotNull(actualNameData);
|
|
|
|
|
|
|
|
// Check sender's reference
|
|
|
|
assertTrue("Sender's new reference incorrect", Arrays.equals(registerNameTransactionData.getSignature(), sender.getLastReference()));
|
|
|
|
|
|
|
|
// Update variables for use by other tests
|
|
|
|
reference = sender.getLastReference();
|
|
|
|
parentBlockData = block.getBlockData();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2018-07-02 17:09:36 +00:00
|
|
|
public void testUpdateNameTransaction() throws DataException {
|
2018-07-01 15:35:45 +00:00
|
|
|
// Register name using another test
|
|
|
|
testRegisterNameTransaction();
|
|
|
|
|
|
|
|
String name = "test name";
|
|
|
|
NameData originalNameData = this.repository.getNameRepository().fromName(name);
|
|
|
|
|
|
|
|
// Update name's owner and data
|
|
|
|
Account newOwner = new PublicKeyAccount(repository, recipientSeed);
|
|
|
|
String newData = "{\"newKey\":\"newValue\"}";
|
|
|
|
byte[] nameReference = reference;
|
|
|
|
|
|
|
|
BigDecimal fee = BigDecimal.ONE;
|
2018-07-02 17:59:11 +00:00
|
|
|
long timestamp = parentBlockData.getTimestamp() + 1_000;
|
2018-07-01 15:35:45 +00:00
|
|
|
UpdateNameTransactionData updateNameTransactionData = new UpdateNameTransactionData(sender.getPublicKey(), newOwner.getAddress(), name, newData,
|
|
|
|
nameReference, fee, timestamp, reference);
|
|
|
|
|
|
|
|
Transaction updateNameTransaction = new UpdateNameTransaction(repository, updateNameTransactionData);
|
|
|
|
updateNameTransaction.calcSignature(sender);
|
|
|
|
assertTrue(updateNameTransaction.isSignatureValid());
|
|
|
|
assertEquals(ValidationResult.OK, updateNameTransaction.isValid());
|
|
|
|
|
|
|
|
// Forge new block with transaction
|
|
|
|
Block block = new Block(repository, parentBlockData, generator, null, null);
|
|
|
|
block.addTransaction(updateNameTransactionData);
|
|
|
|
block.sign();
|
|
|
|
|
|
|
|
assertTrue("Block signatures invalid", block.isSignatureValid());
|
|
|
|
assertEquals("Block is invalid", Block.ValidationResult.OK, block.isValid());
|
|
|
|
|
|
|
|
block.process();
|
|
|
|
repository.saveChanges();
|
|
|
|
|
|
|
|
// Check name was updated
|
|
|
|
NameData actualNameData = this.repository.getNameRepository().fromName(name);
|
|
|
|
assertEquals(newOwner.getAddress(), actualNameData.getOwner());
|
|
|
|
assertEquals(newData, actualNameData.getData());
|
|
|
|
|
|
|
|
// Now orphan block
|
|
|
|
block.orphan();
|
|
|
|
repository.saveChanges();
|
|
|
|
|
|
|
|
// Check name has been reverted correctly
|
|
|
|
actualNameData = this.repository.getNameRepository().fromName(name);
|
|
|
|
assertEquals(originalNameData.getOwner(), actualNameData.getOwner());
|
|
|
|
assertEquals(originalNameData.getData(), actualNameData.getData());
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:09:36 +00:00
|
|
|
@Test
|
|
|
|
public void testSellNameTransaction() throws DataException {
|
|
|
|
// Register name using another test
|
|
|
|
testRegisterNameTransaction();
|
|
|
|
|
|
|
|
String name = "test name";
|
|
|
|
|
|
|
|
// Sale price
|
|
|
|
BigDecimal amount = BigDecimal.valueOf(1234L).setScale(8);
|
|
|
|
|
|
|
|
BigDecimal fee = BigDecimal.ONE;
|
2018-07-02 17:59:11 +00:00
|
|
|
long timestamp = parentBlockData.getTimestamp() + 1_000;
|
2018-07-02 17:09:36 +00:00
|
|
|
SellNameTransactionData sellNameTransactionData = new SellNameTransactionData(sender.getPublicKey(), name, amount, fee, timestamp, reference);
|
|
|
|
|
|
|
|
Transaction sellNameTransaction = new SellNameTransaction(repository, sellNameTransactionData);
|
|
|
|
sellNameTransaction.calcSignature(sender);
|
|
|
|
assertTrue(sellNameTransaction.isSignatureValid());
|
|
|
|
assertEquals(ValidationResult.OK, sellNameTransaction.isValid());
|
|
|
|
|
|
|
|
// Forge new block with transaction
|
|
|
|
Block block = new Block(repository, parentBlockData, generator, null, null);
|
|
|
|
block.addTransaction(sellNameTransactionData);
|
|
|
|
block.sign();
|
|
|
|
|
|
|
|
assertTrue("Block signatures invalid", block.isSignatureValid());
|
|
|
|
assertEquals("Block is invalid", Block.ValidationResult.OK, block.isValid());
|
|
|
|
|
|
|
|
block.process();
|
|
|
|
repository.saveChanges();
|
|
|
|
|
|
|
|
// Check name was updated
|
|
|
|
NameData actualNameData = this.repository.getNameRepository().fromName(name);
|
|
|
|
assertTrue(actualNameData.getIsForSale());
|
|
|
|
assertEquals(amount, actualNameData.getSalePrice());
|
|
|
|
|
|
|
|
// Now orphan block
|
|
|
|
block.orphan();
|
|
|
|
repository.saveChanges();
|
|
|
|
|
|
|
|
// Check name has been reverted correctly
|
|
|
|
actualNameData = this.repository.getNameRepository().fromName(name);
|
|
|
|
assertFalse(actualNameData.getIsForSale());
|
|
|
|
assertNull(actualNameData.getSalePrice());
|
2018-07-02 17:59:11 +00:00
|
|
|
|
|
|
|
// Re-process block for use by other tests
|
|
|
|
block.process();
|
|
|
|
repository.saveChanges();
|
|
|
|
|
|
|
|
// Update variables for use by other tests
|
|
|
|
reference = sender.getLastReference();
|
|
|
|
parentBlockData = block.getBlockData();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testCancelSellNameTransaction() throws DataException {
|
|
|
|
// Register and sell name using another test
|
|
|
|
testSellNameTransaction();
|
|
|
|
|
|
|
|
String name = "test name";
|
|
|
|
NameData originalNameData = this.repository.getNameRepository().fromName(name);
|
|
|
|
|
|
|
|
BigDecimal fee = BigDecimal.ONE;
|
|
|
|
long timestamp = parentBlockData.getTimestamp() + 1_000;
|
|
|
|
CancelSellNameTransactionData cancelSellNameTransactionData = new CancelSellNameTransactionData(sender.getPublicKey(), name, fee, timestamp, reference);
|
|
|
|
|
|
|
|
Transaction cancelSellNameTransaction = new CancelSellNameTransaction(repository, cancelSellNameTransactionData);
|
|
|
|
cancelSellNameTransaction.calcSignature(sender);
|
|
|
|
assertTrue(cancelSellNameTransaction.isSignatureValid());
|
|
|
|
assertEquals(ValidationResult.OK, cancelSellNameTransaction.isValid());
|
|
|
|
|
|
|
|
// Forge new block with transaction
|
|
|
|
Block block = new Block(repository, parentBlockData, generator, null, null);
|
|
|
|
block.addTransaction(cancelSellNameTransactionData);
|
|
|
|
block.sign();
|
|
|
|
|
|
|
|
assertTrue("Block signatures invalid", block.isSignatureValid());
|
|
|
|
assertEquals("Block is invalid", Block.ValidationResult.OK, block.isValid());
|
|
|
|
|
|
|
|
block.process();
|
|
|
|
repository.saveChanges();
|
|
|
|
|
|
|
|
// Check name was updated
|
|
|
|
NameData actualNameData = this.repository.getNameRepository().fromName(name);
|
|
|
|
assertFalse(actualNameData.getIsForSale());
|
|
|
|
assertEquals(originalNameData.getSalePrice(), actualNameData.getSalePrice());
|
|
|
|
|
|
|
|
// Now orphan block
|
|
|
|
block.orphan();
|
|
|
|
repository.saveChanges();
|
|
|
|
|
|
|
|
// Check name has been reverted correctly
|
|
|
|
actualNameData = this.repository.getNameRepository().fromName(name);
|
|
|
|
assertTrue(actualNameData.getIsForSale());
|
|
|
|
assertEquals(originalNameData.getSalePrice(), actualNameData.getSalePrice());
|
|
|
|
|
|
|
|
// Update variables for use by other tests
|
|
|
|
reference = sender.getLastReference();
|
|
|
|
parentBlockData = block.getBlockData();
|
2018-07-02 17:09:36 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 10:40:24 +00:00
|
|
|
@Test
|
|
|
|
public void testBuyNameTransaction() throws DataException {
|
|
|
|
// Register and sell name using another test
|
|
|
|
testSellNameTransaction();
|
|
|
|
|
|
|
|
String name = "test name";
|
|
|
|
NameData originalNameData = this.repository.getNameRepository().fromName(name);
|
|
|
|
String seller = originalNameData.getOwner();
|
|
|
|
|
|
|
|
// Buyer
|
|
|
|
PrivateKeyAccount buyer = new PrivateKeyAccount(repository, recipientSeed);
|
|
|
|
byte[] nameReference = reference;
|
|
|
|
|
|
|
|
// Send buyer some funds so they have a reference
|
|
|
|
Transaction somePaymentTransaction = createPayment(sender, buyer.getAddress());
|
|
|
|
byte[] buyersReference = somePaymentTransaction.getTransactionData().getSignature();
|
|
|
|
|
|
|
|
// Forge new block with transaction
|
|
|
|
Block block = new Block(repository, parentBlockData, generator, null, null);
|
|
|
|
block.addTransaction(somePaymentTransaction.getTransactionData());
|
|
|
|
block.sign();
|
|
|
|
|
|
|
|
block.process();
|
|
|
|
repository.saveChanges();
|
|
|
|
parentBlockData = block.getBlockData();
|
|
|
|
|
|
|
|
BigDecimal fee = BigDecimal.ONE;
|
|
|
|
long timestamp = parentBlockData.getTimestamp() + 1_000;
|
|
|
|
BuyNameTransactionData buyNameTransactionData = new BuyNameTransactionData(buyer.getPublicKey(), name, originalNameData.getSalePrice(), seller,
|
|
|
|
nameReference, fee, timestamp, buyersReference);
|
|
|
|
|
|
|
|
Transaction buyNameTransaction = new BuyNameTransaction(repository, buyNameTransactionData);
|
|
|
|
buyNameTransaction.calcSignature(buyer);
|
|
|
|
assertTrue(buyNameTransaction.isSignatureValid());
|
|
|
|
assertEquals(ValidationResult.OK, buyNameTransaction.isValid());
|
|
|
|
|
|
|
|
// Forge new block with transaction
|
|
|
|
block = new Block(repository, parentBlockData, generator, null, null);
|
|
|
|
block.addTransaction(buyNameTransactionData);
|
|
|
|
block.sign();
|
|
|
|
|
|
|
|
assertTrue("Block signatures invalid", block.isSignatureValid());
|
|
|
|
assertEquals("Block is invalid", Block.ValidationResult.OK, block.isValid());
|
|
|
|
|
|
|
|
block.process();
|
|
|
|
repository.saveChanges();
|
|
|
|
|
|
|
|
// Check name was updated
|
|
|
|
NameData actualNameData = this.repository.getNameRepository().fromName(name);
|
|
|
|
assertFalse(actualNameData.getIsForSale());
|
|
|
|
assertEquals(originalNameData.getSalePrice(), actualNameData.getSalePrice());
|
|
|
|
assertEquals(buyer.getAddress(), actualNameData.getOwner());
|
|
|
|
|
|
|
|
// Now orphan block
|
|
|
|
block.orphan();
|
|
|
|
repository.saveChanges();
|
|
|
|
|
|
|
|
// Check name has been reverted correctly
|
|
|
|
actualNameData = this.repository.getNameRepository().fromName(name);
|
|
|
|
assertTrue(actualNameData.getIsForSale());
|
|
|
|
assertEquals(originalNameData.getSalePrice(), actualNameData.getSalePrice());
|
|
|
|
assertEquals(originalNameData.getOwner(), actualNameData.getOwner());
|
|
|
|
}
|
|
|
|
|
2018-06-21 11:38:45 +00:00
|
|
|
@Test
|
|
|
|
public void testCreatePollTransaction() throws DataException {
|
2018-06-29 09:29:18 +00:00
|
|
|
// This test requires GenesisBlock's timestamp is set to something after BlockChain.VOTING_RELEASE_TIMESTAMP
|
|
|
|
createTestAccounts(BlockChain.VOTING_RELEASE_TIMESTAMP + 1_000L);
|
2018-06-21 11:38:45 +00:00
|
|
|
|
|
|
|
// Make a new create poll transaction
|
|
|
|
String pollName = "test poll";
|
|
|
|
String description = "test poll description";
|
|
|
|
|
|
|
|
List<PollOptionData> pollOptions = new ArrayList<PollOptionData>();
|
|
|
|
pollOptions.add(new PollOptionData("abort"));
|
|
|
|
pollOptions.add(new PollOptionData("retry"));
|
|
|
|
pollOptions.add(new PollOptionData("fail"));
|
|
|
|
|
|
|
|
Account recipient = new PublicKeyAccount(repository, recipientSeed);
|
|
|
|
BigDecimal fee = BigDecimal.ONE;
|
2018-07-01 15:35:45 +00:00
|
|
|
long timestamp = parentBlockData.getTimestamp() + 1_000;
|
2018-06-21 11:38:45 +00:00
|
|
|
CreatePollTransactionData createPollTransactionData = new CreatePollTransactionData(sender.getPublicKey(), recipient.getAddress(), pollName,
|
|
|
|
description, pollOptions, fee, timestamp, reference);
|
|
|
|
|
|
|
|
Transaction createPollTransaction = new CreatePollTransaction(repository, createPollTransactionData);
|
|
|
|
createPollTransaction.calcSignature(sender);
|
|
|
|
assertTrue(createPollTransaction.isSignatureValid());
|
|
|
|
assertEquals(ValidationResult.OK, createPollTransaction.isValid());
|
|
|
|
|
2018-06-29 09:29:18 +00:00
|
|
|
// Forge new block with transaction
|
2018-07-01 15:35:45 +00:00
|
|
|
Block block = new Block(repository, parentBlockData, generator, null, null);
|
2018-06-21 11:38:45 +00:00
|
|
|
block.addTransaction(createPollTransactionData);
|
|
|
|
block.sign();
|
|
|
|
|
|
|
|
assertTrue("Block signatures invalid", block.isSignatureValid());
|
|
|
|
assertEquals("Block is invalid", Block.ValidationResult.OK, block.isValid());
|
|
|
|
|
|
|
|
block.process();
|
|
|
|
repository.saveChanges();
|
|
|
|
|
|
|
|
// Check sender's balance
|
|
|
|
BigDecimal expectedBalance = senderBalance.subtract(fee);
|
|
|
|
BigDecimal actualBalance = accountRepository.getBalance(sender.getAddress(), Asset.QORA).getBalance();
|
|
|
|
assertTrue("Sender's new balance incorrect", expectedBalance.compareTo(actualBalance) == 0);
|
|
|
|
|
|
|
|
// Fee should be in generator's balance
|
|
|
|
expectedBalance = generatorBalance.add(fee);
|
|
|
|
actualBalance = accountRepository.getBalance(generator.getAddress(), Asset.QORA).getBalance();
|
|
|
|
assertTrue("Generator's new balance incorrect", expectedBalance.compareTo(actualBalance) == 0);
|
|
|
|
|
|
|
|
// Check poll was created
|
|
|
|
PollData actualPollData = this.repository.getVotingRepository().fromPollName(pollName);
|
|
|
|
assertNotNull(actualPollData);
|
2018-06-29 09:29:18 +00:00
|
|
|
|
|
|
|
// Check sender's reference
|
|
|
|
assertTrue("Sender's new reference incorrect", Arrays.equals(createPollTransactionData.getSignature(), sender.getLastReference()));
|
|
|
|
|
2018-07-01 15:35:45 +00:00
|
|
|
// Update variables for use by other tests
|
2018-06-29 09:29:18 +00:00
|
|
|
reference = sender.getLastReference();
|
2018-07-01 15:35:45 +00:00
|
|
|
parentBlockData = block.getBlockData();
|
2018-06-29 09:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testVoteOnPollTransaction() throws DataException {
|
|
|
|
// Create poll using another test
|
|
|
|
testCreatePollTransaction();
|
|
|
|
|
|
|
|
// Try all options, plus invalid optionIndex (note use of <= for this)
|
|
|
|
String pollName = "test poll";
|
|
|
|
int pollOptionsSize = 3;
|
|
|
|
BigDecimal fee = BigDecimal.ONE;
|
2018-07-01 15:35:45 +00:00
|
|
|
long timestamp = parentBlockData.getTimestamp() + 1_000;
|
2018-06-29 09:29:18 +00:00
|
|
|
|
|
|
|
for (int optionIndex = 0; optionIndex <= pollOptionsSize; ++optionIndex) {
|
|
|
|
// Make a vote-on-poll transaction
|
|
|
|
VoteOnPollTransactionData voteOnPollTransactionData = new VoteOnPollTransactionData(sender.getPublicKey(), pollName, optionIndex, fee, timestamp,
|
|
|
|
reference);
|
|
|
|
|
|
|
|
Transaction voteOnPollTransaction = new VoteOnPollTransaction(repository, voteOnPollTransactionData);
|
|
|
|
voteOnPollTransaction.calcSignature(sender);
|
|
|
|
assertTrue(voteOnPollTransaction.isSignatureValid());
|
|
|
|
|
|
|
|
if (optionIndex == pollOptionsSize) {
|
|
|
|
assertEquals(ValidationResult.POLL_OPTION_DOES_NOT_EXIST, voteOnPollTransaction.isValid());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assertEquals(ValidationResult.OK, voteOnPollTransaction.isValid());
|
|
|
|
|
|
|
|
// Forge new block with transaction
|
2018-07-01 15:35:45 +00:00
|
|
|
Block block = new Block(repository, parentBlockData, generator, null, null);
|
2018-06-29 09:29:18 +00:00
|
|
|
block.addTransaction(voteOnPollTransactionData);
|
|
|
|
block.sign();
|
|
|
|
|
|
|
|
assertTrue("Block signatures invalid", block.isSignatureValid());
|
|
|
|
assertEquals("Block is invalid", Block.ValidationResult.OK, block.isValid());
|
|
|
|
|
|
|
|
block.process();
|
|
|
|
repository.saveChanges();
|
|
|
|
|
2018-06-29 10:05:15 +00:00
|
|
|
// Check vote was registered properly
|
|
|
|
VoteOnPollData actualVoteOnPollData = repository.getVotingRepository().getVote(pollName, sender.getPublicKey());
|
|
|
|
assertNotNull(actualVoteOnPollData);
|
|
|
|
assertEquals(optionIndex, actualVoteOnPollData.getOptionIndex());
|
|
|
|
|
2018-06-29 09:29:18 +00:00
|
|
|
// update variables for next round
|
2018-07-01 15:35:45 +00:00
|
|
|
parentBlockData = block.getBlockData();
|
2018-06-29 09:29:18 +00:00
|
|
|
timestamp += 1_000;
|
|
|
|
reference = voteOnPollTransaction.getTransactionData().getSignature();
|
|
|
|
}
|
2018-06-29 10:05:15 +00:00
|
|
|
|
|
|
|
// Check poll's votes
|
|
|
|
List<VoteOnPollData> votes = repository.getVotingRepository().getVotes(pollName);
|
|
|
|
assertNotNull(votes);
|
|
|
|
|
|
|
|
assertEquals("Only one vote expected", 1, votes.size());
|
|
|
|
|
|
|
|
assertEquals("Wrong vote option index", pollOptionsSize - 1, votes.get(0).getOptionIndex());
|
|
|
|
assertTrue("Wrong voter public key", Arrays.equals(sender.getPublicKey(), votes.get(0).getVoterPublicKey()));
|
|
|
|
|
|
|
|
// Orphan last block
|
|
|
|
BlockData lastBlockData = repository.getBlockRepository().getLastBlock();
|
|
|
|
Block lastBlock = new Block(repository, lastBlockData);
|
|
|
|
lastBlock.orphan();
|
|
|
|
repository.saveChanges();
|
|
|
|
|
|
|
|
// Recheck poll's votes
|
|
|
|
votes = repository.getVotingRepository().getVotes(pollName);
|
|
|
|
assertNotNull(votes);
|
|
|
|
|
|
|
|
assertEquals("Only one vote expected", 1, votes.size());
|
|
|
|
|
|
|
|
assertEquals("Wrong vote option index", pollOptionsSize - 1 - 1, votes.get(0).getOptionIndex());
|
|
|
|
assertTrue("Wrong voter public key", Arrays.equals(sender.getPublicKey(), votes.get(0).getVoterPublicKey()));
|
2018-06-13 15:48:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 10:40:24 +00:00
|
|
|
@Test
|
|
|
|
public void testIssueAssetTransaction() throws DataException {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testTransferAssetTransaction() throws DataException {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testCreateAssetOrderTransaction() throws DataException {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testCancelAssetOrderTransaction() throws DataException {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testMultiPaymentTransaction() throws DataException {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testMessageTransaction() throws DataException, UnsupportedEncodingException {
|
|
|
|
createTestAccounts(null);
|
|
|
|
|
|
|
|
// Make a new message transaction
|
|
|
|
Account recipient = new PublicKeyAccount(repository, recipientSeed);
|
|
|
|
BigDecimal amount = BigDecimal.valueOf(1_000L);
|
|
|
|
BigDecimal fee = BigDecimal.ONE;
|
|
|
|
long timestamp = parentBlockData.getTimestamp() + 1_000;
|
|
|
|
int version = Transaction.getVersionByTimestamp(timestamp);
|
|
|
|
byte[] data = "test".getBytes("UTF-8");
|
|
|
|
boolean isText = true;
|
|
|
|
boolean isEncrypted = false;
|
|
|
|
|
|
|
|
MessageTransactionData messageTransactionData = new MessageTransactionData(version, sender.getPublicKey(), recipient.getAddress(), Asset.QORA, amount,
|
|
|
|
data, isText, isEncrypted, fee, timestamp, reference);
|
|
|
|
|
|
|
|
Transaction messageTransaction = new MessageTransaction(repository, messageTransactionData);
|
|
|
|
messageTransaction.calcSignature(sender);
|
|
|
|
assertTrue(messageTransaction.isSignatureValid());
|
|
|
|
assertEquals(ValidationResult.OK, messageTransaction.isValid());
|
|
|
|
|
|
|
|
// Forge new block with message transaction
|
|
|
|
Block block = new Block(repository, parentBlockData, generator, null, null);
|
|
|
|
block.addTransaction(messageTransactionData);
|
|
|
|
block.sign();
|
|
|
|
|
|
|
|
assertTrue("Block signatures invalid", block.isSignatureValid());
|
|
|
|
assertEquals("Block is invalid", Block.ValidationResult.OK, block.isValid());
|
|
|
|
|
|
|
|
block.process();
|
|
|
|
repository.saveChanges();
|
|
|
|
|
|
|
|
// Check sender's balance
|
|
|
|
BigDecimal expectedBalance = senderBalance.subtract(amount).subtract(fee);
|
|
|
|
BigDecimal actualBalance = accountRepository.getBalance(sender.getAddress(), Asset.QORA).getBalance();
|
|
|
|
assertTrue("Sender's new balance incorrect", expectedBalance.compareTo(actualBalance) == 0);
|
|
|
|
|
|
|
|
// Fee should be in generator's balance
|
|
|
|
expectedBalance = generatorBalance.add(fee);
|
|
|
|
actualBalance = accountRepository.getBalance(generator.getAddress(), Asset.QORA).getBalance();
|
|
|
|
assertTrue("Generator's new balance incorrect", expectedBalance.compareTo(actualBalance) == 0);
|
|
|
|
|
|
|
|
// Amount should be in recipient's balance
|
|
|
|
expectedBalance = amount;
|
|
|
|
actualBalance = accountRepository.getBalance(recipient.getAddress(), Asset.QORA).getBalance();
|
|
|
|
assertTrue("Recipient's new balance incorrect", expectedBalance.compareTo(actualBalance) == 0);
|
|
|
|
}
|
|
|
|
|
2018-06-13 15:48:28 +00:00
|
|
|
}
|