From 06a2c380bd574384f061287c63d7ea52b4f03d93 Mon Sep 17 00:00:00 2001 From: CalDescent Date: Fri, 17 Sep 2021 09:34:10 +0100 Subject: [PATCH] Updated and added some naming tests. --- .../org/qortal/test/naming/MiscTests.java | 93 +++++++++++++++++-- 1 file changed, 85 insertions(+), 8 deletions(-) diff --git a/src/test/java/org/qortal/test/naming/MiscTests.java b/src/test/java/org/qortal/test/naming/MiscTests.java index 46c8b688..71bf6b0a 100644 --- a/src/test/java/org/qortal/test/naming/MiscTests.java +++ b/src/test/java/org/qortal/test/naming/MiscTests.java @@ -7,15 +7,13 @@ import java.util.List; import org.junit.Before; import org.junit.Test; import org.qortal.account.PrivateKeyAccount; -import org.qortal.data.transaction.RegisterNameTransactionData; -import org.qortal.data.transaction.TransactionData; -import org.qortal.data.transaction.UpdateNameTransactionData; +import org.qortal.asset.Asset; +import org.qortal.controller.BlockMinter; +import org.qortal.data.transaction.*; import org.qortal.repository.DataException; import org.qortal.repository.Repository; import org.qortal.repository.RepositoryManager; -import org.qortal.test.common.BlockUtils; -import org.qortal.test.common.Common; -import org.qortal.test.common.TransactionUtils; +import org.qortal.test.common.*; import org.qortal.test.common.transaction.TestTransaction; import org.qortal.transaction.Transaction; 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 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 public void testMultipleRegisterNameAndOrphan() throws DataException { try (final Repository repository = RepositoryManager.getRepository()) { @@ -175,7 +245,7 @@ public class MiscTests extends Common { String name = "test-name"; String data = "{\"age\":30}"; - for (int i = 0; i < 10; i++) { + for (int i = 1; i <= 10; i++) { // Ensure the name doesn't exist assertNull(repository.getNameRepository().fromName(name)); @@ -187,9 +257,16 @@ public class MiscTests extends Common { // Ensure the name exists and the data is correct 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 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 assertNull(repository.getNameRepository().fromName(name)); }