forked from Qortal/qortal
Updated and added some naming tests.
This commit is contained in:
parent
33ac1fed2a
commit
06a2c380bd
@ -7,15 +7,13 @@ import java.util.List;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.qortal.account.PrivateKeyAccount;
|
import org.qortal.account.PrivateKeyAccount;
|
||||||
import org.qortal.data.transaction.RegisterNameTransactionData;
|
import org.qortal.asset.Asset;
|
||||||
import org.qortal.data.transaction.TransactionData;
|
import org.qortal.controller.BlockMinter;
|
||||||
import org.qortal.data.transaction.UpdateNameTransactionData;
|
import org.qortal.data.transaction.*;
|
||||||
import org.qortal.repository.DataException;
|
import org.qortal.repository.DataException;
|
||||||
import org.qortal.repository.Repository;
|
import org.qortal.repository.Repository;
|
||||||
import org.qortal.repository.RepositoryManager;
|
import org.qortal.repository.RepositoryManager;
|
||||||
import org.qortal.test.common.BlockUtils;
|
import org.qortal.test.common.*;
|
||||||
import org.qortal.test.common.Common;
|
|
||||||
import org.qortal.test.common.TransactionUtils;
|
|
||||||
import org.qortal.test.common.transaction.TestTransaction;
|
import org.qortal.test.common.transaction.TestTransaction;
|
||||||
import org.qortal.transaction.Transaction;
|
import org.qortal.transaction.Transaction;
|
||||||
import org.qortal.transaction.Transaction.ValidationResult;
|
import org.qortal.transaction.Transaction.ValidationResult;
|
||||||
@ -68,6 +66,30 @@ public class MiscTests extends Common {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test trying to register same name twice (with different creator)
|
||||||
|
@Test
|
||||||
|
public void testDuplicateRegisterNameWithDifferentCreator() throws DataException {
|
||||||
|
try (final Repository repository = RepositoryManager.getRepository()) {
|
||||||
|
// Register-name
|
||||||
|
PrivateKeyAccount alice = Common.getTestAccount(repository, "alice");
|
||||||
|
String name = "test-name";
|
||||||
|
String data = "{}";
|
||||||
|
|
||||||
|
RegisterNameTransactionData transactionData = new RegisterNameTransactionData(TestTransaction.generateBase(alice), name, data);
|
||||||
|
TransactionUtils.signAndMint(repository, transactionData, alice);
|
||||||
|
|
||||||
|
// duplicate (this time registered by Bob)
|
||||||
|
PrivateKeyAccount bob = Common.getTestAccount(repository, "bob");
|
||||||
|
String duplicateName = "TEST-nÁme";
|
||||||
|
transactionData = new RegisterNameTransactionData(TestTransaction.generateBase(bob), duplicateName, data);
|
||||||
|
Transaction transaction = Transaction.fromData(repository, transactionData);
|
||||||
|
transaction.sign(alice);
|
||||||
|
|
||||||
|
ValidationResult result = transaction.importAsUnconfirmed();
|
||||||
|
assertTrue("Transaction should be invalid", ValidationResult.OK != result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// test register then trying to update another name to existing name
|
// test register then trying to update another name to existing name
|
||||||
@Test
|
@Test
|
||||||
public void testUpdateToExistingName() throws DataException {
|
public void testUpdateToExistingName() throws DataException {
|
||||||
@ -166,7 +188,55 @@ public class MiscTests extends Common {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// test registering and then orphaning multiple times (to simulate several re-orgs)
|
@Test
|
||||||
|
public void testOrphanAndReregisterName() throws DataException {
|
||||||
|
try (final Repository repository = RepositoryManager.getRepository()) {
|
||||||
|
|
||||||
|
PrivateKeyAccount alice = Common.getTestAccount(repository, "alice");
|
||||||
|
String name = "test-name";
|
||||||
|
String data = "{\"age\":30}";
|
||||||
|
|
||||||
|
// Ensure the name doesn't exist
|
||||||
|
assertNull(repository.getNameRepository().fromName(name));
|
||||||
|
|
||||||
|
// Register the name
|
||||||
|
RegisterNameTransactionData transactionData = new RegisterNameTransactionData(TestTransaction.generateBase(alice), name, data);
|
||||||
|
TransactionUtils.signAndMint(repository, transactionData, alice);
|
||||||
|
|
||||||
|
// Ensure the name exists and the data is correct
|
||||||
|
assertEquals(data, repository.getNameRepository().fromName(name).getData());
|
||||||
|
|
||||||
|
// Orphan the latest block
|
||||||
|
BlockUtils.orphanBlocks(repository, 1);
|
||||||
|
|
||||||
|
// Ensure the name doesn't exist once again
|
||||||
|
assertNull(repository.getNameRepository().fromName(name));
|
||||||
|
|
||||||
|
// Now check there is an unconfirmed transaction
|
||||||
|
assertEquals(1, repository.getTransactionRepository().getUnconfirmedTransactions().size());
|
||||||
|
|
||||||
|
// Re-mint the block, including the original transaction
|
||||||
|
BlockMinter.mintTestingBlock(repository, Common.getTestAccount(repository, "alice-reward-share"));
|
||||||
|
|
||||||
|
// There should no longer be an unconfirmed transaction
|
||||||
|
assertEquals(0, repository.getTransactionRepository().getUnconfirmedTransactions().size());
|
||||||
|
|
||||||
|
// Orphan the latest block
|
||||||
|
BlockUtils.orphanBlocks(repository, 1);
|
||||||
|
|
||||||
|
// There should now be an unconfirmed transaction again
|
||||||
|
assertEquals(1, repository.getTransactionRepository().getUnconfirmedTransactions().size());
|
||||||
|
|
||||||
|
// Re-mint the block, including the original transaction
|
||||||
|
BlockMinter.mintTestingBlock(repository, Common.getTestAccount(repository, "alice-reward-share"));
|
||||||
|
|
||||||
|
// Ensure there are no unconfirmed transactions
|
||||||
|
assertEquals(0, repository.getTransactionRepository().getUnconfirmedTransactions().size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// test registering and then orphaning multiple times, with a different versions of the transaction each time
|
||||||
|
// we can sometimes end up with more than one version of a transaction, if it is signed and submitted twice
|
||||||
@Test
|
@Test
|
||||||
public void testMultipleRegisterNameAndOrphan() throws DataException {
|
public void testMultipleRegisterNameAndOrphan() throws DataException {
|
||||||
try (final Repository repository = RepositoryManager.getRepository()) {
|
try (final Repository repository = RepositoryManager.getRepository()) {
|
||||||
@ -175,7 +245,7 @@ public class MiscTests extends Common {
|
|||||||
String name = "test-name";
|
String name = "test-name";
|
||||||
String data = "{\"age\":30}";
|
String data = "{\"age\":30}";
|
||||||
|
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 1; i <= 10; i++) {
|
||||||
|
|
||||||
// Ensure the name doesn't exist
|
// Ensure the name doesn't exist
|
||||||
assertNull(repository.getNameRepository().fromName(name));
|
assertNull(repository.getNameRepository().fromName(name));
|
||||||
@ -187,9 +257,16 @@ public class MiscTests extends Common {
|
|||||||
// Ensure the name exists and the data is correct
|
// Ensure the name exists and the data is correct
|
||||||
assertEquals(data, repository.getNameRepository().fromName(name).getData());
|
assertEquals(data, repository.getNameRepository().fromName(name).getData());
|
||||||
|
|
||||||
|
// The number of unconfirmed transactions should equal the number of cycles minus 1 (because one is in a block)
|
||||||
|
// If more than one made it into a block, this test would fail
|
||||||
|
assertEquals(i-1, repository.getTransactionRepository().getUnconfirmedTransactions().size());
|
||||||
|
|
||||||
// Orphan the latest block
|
// Orphan the latest block
|
||||||
BlockUtils.orphanBlocks(repository, 1);
|
BlockUtils.orphanBlocks(repository, 1);
|
||||||
|
|
||||||
|
// The number of unconfirmed transactions should equal the number of cycles
|
||||||
|
assertEquals(i, repository.getTransactionRepository().getUnconfirmedTransactions().size());
|
||||||
|
|
||||||
// Ensure the name doesn't exist once again
|
// Ensure the name doesn't exist once again
|
||||||
assertNull(repository.getNameRepository().fromName(name));
|
assertNull(repository.getNameRepository().fromName(name));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user