Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,42 @@
import com.exonum.binding.common.hash.HashCode;
import com.exonum.binding.test.RequiresNativeLibrary;
import com.exonum.binding.testkit.TestKit;
import com.exonum.binding.testkit.TestKitExtension;
import com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

@RequiresNativeLibrary
class CryptocurrencySchemaIntegrationTest {

@RegisterExtension
TestKitExtension testKitExtension = new TestKitExtension(
TestKit.builder()
.withService(CryptocurrencyServiceModule.class));

private static final PublicKey WALLET_OWNER_KEY =
PredefinedOwnerKeys.FIRST_OWNER_KEY_PAIR.getPublicKey();

@Test
void getStateHashes() {
try (TestKit testKit = TestKit.forService(CryptocurrencyServiceModule.class)) {
testKit.withSnapshot((view) -> {
CryptocurrencySchema schema = new CryptocurrencySchema(view);

HashCode walletsMerkleRoot = schema.wallets().getRootHash();
ImmutableList<HashCode> expectedHashes = ImmutableList.of(walletsMerkleRoot);

assertThat(schema.getStateHashes()).isEqualTo(expectedHashes);
return null;
});
}
void getStateHashes(TestKit testKit) {
testKit.withSnapshot((view) -> {
CryptocurrencySchema schema = new CryptocurrencySchema(view);

HashCode walletsMerkleRoot = schema.wallets().getRootHash();
ImmutableList<HashCode> expectedHashes = ImmutableList.of(walletsMerkleRoot);

assertThat(schema.getStateHashes()).isEqualTo(expectedHashes);
return null;
});
}

@Test
void walletHistoryNoRecords() {
try (TestKit testKit = TestKit.forService(CryptocurrencyServiceModule.class)) {
testKit.withSnapshot((view) -> {
CryptocurrencySchema schema = new CryptocurrencySchema(view);

assertThat(schema.transactionsHistory(WALLET_OWNER_KEY)).isEmpty();
return null;
});
}
void walletHistoryNoRecords(TestKit testKit) {
testKit.withSnapshot((view) -> {
CryptocurrencySchema schema = new CryptocurrencySchema(view);

assertThat(schema.transactionsHistory(WALLET_OWNER_KEY)).isEmpty();
return null;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,20 @@
import com.exonum.binding.storage.indices.MapIndex;
import com.exonum.binding.test.RequiresNativeLibrary;
import com.exonum.binding.testkit.TestKit;
import com.exonum.binding.testkit.TestKitExtension;
import com.exonum.binding.transaction.RawTransaction;
import java.util.Optional;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

class CreateWalletTxTest {

@RegisterExtension
TestKitExtension testKitExtension = new TestKitExtension(
TestKit.builder()
.withService(CryptocurrencyServiceModule.class));

private static final KeyPair OWNER_KEY_PAIR = PredefinedOwnerKeys.FIRST_OWNER_KEY_PAIR;

@Test
Expand All @@ -67,51 +74,47 @@ void constructorRejectsNegativeBalance() {

@Test
@RequiresNativeLibrary
void executeCreateWalletTx() {
try (TestKit testKit = TestKit.forService(CryptocurrencyServiceModule.class)) {
TransactionMessage transactionMessage =
newCreateWalletTransaction(DEFAULT_INITIAL_BALANCE, OWNER_KEY_PAIR);
testKit.createBlockWithTransactions(transactionMessage);

testKit.withSnapshot((view) -> {
// Check that entries have been added
CryptocurrencySchema schema = new CryptocurrencySchema(view);
MapIndex<PublicKey, Wallet> wallets = schema.wallets();

PublicKey emulatedNodePublicKey = OWNER_KEY_PAIR.getPublicKey();
assertThat(wallets.containsKey(emulatedNodePublicKey)).isTrue();
assertThat(wallets.get(emulatedNodePublicKey).getBalance())
.isEqualTo(DEFAULT_INITIAL_BALANCE);
return null;
});
}
void executeCreateWalletTx(TestKit testKit) {
TransactionMessage transactionMessage =
newCreateWalletTransaction(DEFAULT_INITIAL_BALANCE, OWNER_KEY_PAIR);
testKit.createBlockWithTransactions(transactionMessage);

testKit.withSnapshot((view) -> {
// Check that entries have been added
CryptocurrencySchema schema = new CryptocurrencySchema(view);
MapIndex<PublicKey, Wallet> wallets = schema.wallets();

PublicKey emulatedNodePublicKey = OWNER_KEY_PAIR.getPublicKey();
assertThat(wallets.containsKey(emulatedNodePublicKey)).isTrue();
assertThat(wallets.get(emulatedNodePublicKey).getBalance())
.isEqualTo(DEFAULT_INITIAL_BALANCE);
return null;
});
}

@Test
@RequiresNativeLibrary
void executeAlreadyExistingWalletTx() {
try (TestKit testKit = TestKit.forService(CryptocurrencyServiceModule.class)) {
// Create a new wallet
TransactionMessage transactionMessage =
newCreateWalletTransaction(DEFAULT_INITIAL_BALANCE, OWNER_KEY_PAIR);
testKit.createBlockWithTransactions(transactionMessage);

// Attempt to execute a transaction with the same owner public key.
// Use different balance so that it is not rejected as a duplicate
TransactionMessage transactionMessage2 =
newCreateWalletTransaction(DEFAULT_INITIAL_BALANCE * 2, OWNER_KEY_PAIR);
testKit.createBlockWithTransactions(transactionMessage2);

// Check that the second tx has failed
testKit.withSnapshot((view) -> {
Blockchain blockchain = Blockchain.newInstance(view);
Optional<TransactionResult> txResult = blockchain.getTxResult(transactionMessage2.hash());
TransactionResult expectedTransactionResult =
TransactionResult.error(WALLET_ALREADY_EXISTS.errorCode, null);
assertThat(txResult).hasValue(expectedTransactionResult);
return null;
});
}
void executeAlreadyExistingWalletTx(TestKit testKit) {
// Create a new wallet
TransactionMessage transactionMessage =
newCreateWalletTransaction(DEFAULT_INITIAL_BALANCE, OWNER_KEY_PAIR);
testKit.createBlockWithTransactions(transactionMessage);

// Attempt to execute a transaction with the same owner public key.
// Use different balance so that it is not rejected as a duplicate
TransactionMessage transactionMessage2 =
newCreateWalletTransaction(DEFAULT_INITIAL_BALANCE * 2, OWNER_KEY_PAIR);
testKit.createBlockWithTransactions(transactionMessage2);

// Check that the second tx has failed
testKit.withSnapshot((view) -> {
Blockchain blockchain = Blockchain.newInstance(view);
Optional<TransactionResult> txResult = blockchain.getTxResult(transactionMessage2.hash());
TransactionResult expectedTransactionResult =
TransactionResult.error(WALLET_ALREADY_EXISTS.errorCode, null);
assertThat(txResult).hasValue(expectedTransactionResult);
return null;
});
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,60 +32,65 @@
import com.exonum.binding.storage.indices.ProofMapIndexProxy;
import com.exonum.binding.test.RequiresNativeLibrary;
import com.exonum.binding.testkit.TestKit;
import com.exonum.binding.testkit.TestKitExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

@RequiresNativeLibrary
class TransferTxHistoryTest {

@RegisterExtension
TestKitExtension testKitExtension = new TestKitExtension(
TestKit.builder()
.withService(CryptocurrencyServiceModule.class));

private static final KeyPair ACCOUNT_1 = PredefinedOwnerKeys.FIRST_OWNER_KEY_PAIR;
private static final KeyPair ACCOUNT_2 = PredefinedOwnerKeys.SECOND_OWNER_KEY_PAIR;

@Test
@RequiresNativeLibrary
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same is here. The class and tests are annotated.

void transfersHistoryBetweenTwoAccountsTest() {
try (TestKit testKit = TestKit.forService(CryptocurrencyServiceModule.class)) {
// Create source and target wallets with the same initial balance
long initialBalance = 100L;
TransactionMessage createFromWalletTx1 =
newCreateWalletTransaction(initialBalance, ACCOUNT_1);
TransactionMessage createFromWalletTx2 =
newCreateWalletTransaction(initialBalance, ACCOUNT_2);
testKit.createBlockWithTransactions(createFromWalletTx1, createFromWalletTx2);
void transfersHistoryBetweenTwoAccountsTest(TestKit testKit) {
// Create source and target wallets with the same initial balance
long initialBalance = 100L;
TransactionMessage createFromWalletTx1 =
newCreateWalletTransaction(initialBalance, ACCOUNT_1);
TransactionMessage createFromWalletTx2 =
newCreateWalletTransaction(initialBalance, ACCOUNT_2);
testKit.createBlockWithTransactions(createFromWalletTx1, createFromWalletTx2);

// Create and execute 1st transaction
long seed1 = 1L;
long transferSum1 = 40L;
TransactionMessage transferTx1 = newTransferTransaction(
seed1, ACCOUNT_1, ACCOUNT_2.getPublicKey(), transferSum1);
testKit.createBlockWithTransactions(transferTx1);
// Create and execute 1st transaction
long seed1 = 1L;
long transferSum1 = 40L;
TransactionMessage transferTx1 = newTransferTransaction(
seed1, ACCOUNT_1, ACCOUNT_2.getPublicKey(), transferSum1);
testKit.createBlockWithTransactions(transferTx1);

// Create and execute 2nd transaction
long seed2 = 2L;
long transferSum2 = 10L;
TransactionMessage transferTx2 = newTransferTransaction(
seed2, ACCOUNT_2, ACCOUNT_1.getPublicKey(), transferSum2);
testKit.createBlockWithTransactions(transferTx2);
// Create and execute 2nd transaction
long seed2 = 2L;
long transferSum2 = 10L;
TransactionMessage transferTx2 = newTransferTransaction(
seed2, ACCOUNT_2, ACCOUNT_1.getPublicKey(), transferSum2);
testKit.createBlockWithTransactions(transferTx2);

testKit.withSnapshot((view) -> {
// Check that wallets have correct balances
CryptocurrencySchema schema = new CryptocurrencySchema(view);
ProofMapIndexProxy<PublicKey, Wallet> wallets = schema.wallets();
long expectedBalance1 = initialBalance - transferSum1 + transferSum2;
assertThat(wallets.get(ACCOUNT_1.getPublicKey()).getBalance())
.isEqualTo(expectedBalance1);
long expectedBalance2 = initialBalance + transferSum1 - transferSum2;
assertThat(wallets.get(ACCOUNT_2.getPublicKey()).getBalance())
.isEqualTo(expectedBalance2);
testKit.withSnapshot((view) -> {
// Check that wallets have correct balances
CryptocurrencySchema schema = new CryptocurrencySchema(view);
ProofMapIndexProxy<PublicKey, Wallet> wallets = schema.wallets();
long expectedBalance1 = initialBalance - transferSum1 + transferSum2;
assertThat(wallets.get(ACCOUNT_1.getPublicKey()).getBalance())
.isEqualTo(expectedBalance1);
long expectedBalance2 = initialBalance + transferSum1 - transferSum2;
assertThat(wallets.get(ACCOUNT_2.getPublicKey()).getBalance())
.isEqualTo(expectedBalance2);

// Check history
HashCode messageHash1 = transferTx1.hash();
HashCode messageHash2 = transferTx2.hash();
assertThat(schema.transactionsHistory(ACCOUNT_1.getPublicKey()))
.containsExactly(messageHash1, messageHash2);
assertThat(schema.transactionsHistory(ACCOUNT_2.getPublicKey()))
.containsExactly(messageHash1, messageHash2);
return null;
});
}
// Check history
HashCode messageHash1 = transferTx1.hash();
HashCode messageHash2 = transferTx2.hash();
assertThat(schema.transactionsHistory(ACCOUNT_1.getPublicKey()))
.containsExactly(messageHash1, messageHash2);
assertThat(schema.transactionsHistory(ACCOUNT_2.getPublicKey()))
.containsExactly(messageHash1, messageHash2);
return null;
});
}
}
Loading