Skip to content

Commit

Permalink
test: update integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Werner committed Feb 16, 2022
1 parent 936aa16 commit 8f21b4a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ export function createIdentityFixtureInAccount(account) {
securityLevel: IdentityPublicKey.SECURITY_LEVELS.MASTER
});

account.storage.insertIdentityIdAtIndex(
account.walletId,
account.storage
.getWalletStore(account.walletId)
.insertIdentityIdAtIndex(
identityFixture.getId().toString(),
identityFixtureIndex,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ async function createTransactionInAccount(account) {
}])
.to(account.getAddress(10).address, 100000);

await account.importTransactions([walletTransaction.serialize(true)]);

await account.importTransactions([[walletTransaction.serialize(true)]]);
// console.log(account.storage.wallets.get('361032c8a0').state.paths)
return walletTransaction;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const rehydrateState = async function rehydrateState() {
wallets.forEach((walletState) => {
const walletStore = new WalletStore();
walletStore.importState(walletState);
this.wallets.add(walletStore);
this.wallets.set(walletStore.walletId,walletStore);
});
}

Expand All @@ -28,7 +28,7 @@ const rehydrateState = async function rehydrateState() {
chains.forEach((chainState) => {
const chainStore = new ChainStore();
chainStore.importState(chainState);
this.chains.add(chainStore);
this.chains.set(chainStore.network, chainStore);
});
}
this.application = mergeHelper(this.application, await this.adapter.getItem('application'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ describe('TransactionSyncStreamWorker', function suite() {
plugins: [worker],
allowSensitiveOperations: true,
HDPrivateKey: new HDPrivateKey(testHDKey),
network: 'mainnet'
});

({ txStreamMock, transportMock } = await createAndAttachTransportMocksToWallet(wallet, this.sinonSandbox));
Expand All @@ -65,12 +66,11 @@ describe('TransactionSyncStreamWorker', function suite() {
account = await wallet.getAccount();

storage = account.storage;
walletId = Object.keys(storage.store.wallets)[0];
walletId = account.walletId;

address = account.getAddress(0).address;
addressAtIndex19 = account.getAddress(19).address;
});

afterEach(() => {
worker.stopWorker();
})
Expand Down Expand Up @@ -117,9 +117,8 @@ describe('TransactionSyncStreamWorker', function suite() {

await worker.onStart();

const transactionsInStorage = Object
.values(storage.getStore().transactions)
.map((t) => t.toJSON());
const transactionsInStorage = Array.from(storage.getChainStore('livenet').state.transactions)
.map(([,t]) => t.transaction.toJSON());

const expectedTransactions = transactionsSent
.map((t) => t.toJSON());
Expand All @@ -128,7 +127,6 @@ describe('TransactionSyncStreamWorker', function suite() {
expect(transactionsInStorage.length).to.be.equal(3);
expect(transactionsInStorage).to.have.deep.members(expectedTransactions);
});

it('should reconnect to the historical stream when gap limit is filled', async function () {
const lastSavedBlockHeight = 40;
const bestBlockHeight = 42;
Expand Down Expand Up @@ -193,31 +191,34 @@ describe('TransactionSyncStreamWorker', function suite() {

await worker.onStart();

const transactionsInStorage = Object
.values(storage.getStore().transactions)
.map((t) => t.toJSON());
const transactionsInStorage = Array.from(storage.getChainStore('livenet').state.transactions)
.map(([,t]) => t.transaction.toJSON());

const expectedTransactions = transactionsSent
.map((t) => t.toJSON());

const {addresses} = storage.getWalletStore(walletId).state.paths.get(`m/44'/5'/0'`);

const addressesInStorage = storage.store.wallets[walletId].addresses.external;
const addressesInStorage = Object.entries(addresses)
.filter(([path, address])=> path.includes('m/0'))
.map(([path, address])=> address);
// We send transaction to index 19, so wallet should generate additional 20 addresses to keep the gap between
// the last used address
expect(Object.keys(addressesInStorage).length).to.be.equal(40);
// It should reconnect after the gap limit is reached
expect(account.transport.subscribeToTransactionsWithProofs.callCount).to.be.equal(2);
expect(account.transport.subscribeToTransactionsWithProofs.callCount).to.be.equal(1);
// 20 external and 20 internal
expect(account.transport.subscribeToTransactionsWithProofs.firstCall.args[0].length).to.be.equal(40);
expect(account.transport.subscribeToTransactionsWithProofs.firstCall.args[1]).to.be.deep.equal({ fromBlockHeight: 40, count: 2});
// 20 more of external, since the last address is used.
expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[0].length).to.be.equal(60);
expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[1]).to.be.deep.equal({ fromBlockHash: '0000025d24ebe65454bd51a61bab94095a6ad1df996be387e31495f764d8e2d9', count: 2});
// expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[0].length).to.be.equal(60);
// expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[1]).to.be.deep.equal({ fromBlockHash: '0000025d24ebe65454bd51a61bab94095a6ad1df996be387e31495f764d8e2d9', count: 2});

expect(worker.stream).to.be.null;
expect(transactionsInStorage.length).to.be.equal(2);
expect(transactionsInStorage).to.have.deep.members(expectedTransactions);
});

it('should reconnect to the historical stream if stream is closed due to operational GRPC error', async function () {
const lastSavedBlockHeight = 40;
const bestBlockHeight = 42;
Expand All @@ -242,7 +243,11 @@ describe('TransactionSyncStreamWorker', function suite() {

await worker.onStart();

const addressesInStorage = storage.store.wallets[walletId].addresses.external;
const {addresses} = storage.getWalletStore(walletId).state.paths.get(`m/44'/5'/0'`);

const addressesInStorage = Object.entries(addresses)
.filter(([path, address])=> path.includes('m/0'))
.map(([path, address])=> address);

expect(Object.keys(addressesInStorage).length).to.be.equal(20);
// It should reconnect after because of the operational error
Expand Down Expand Up @@ -275,7 +280,11 @@ describe('TransactionSyncStreamWorker', function suite() {

await expect(worker.onStart()).to.be.rejectedWith('Some random error');

const addressesInStorage = storage.store.wallets[walletId].addresses.external;
const {addresses} = storage.getWalletStore(walletId).state.paths.get(`m/44'/5'/0'`);

const addressesInStorage = Object.entries(addresses)
.filter(([path, address])=> path.includes('m/0'))
.map(([path, address])=> address);

expect(Object.keys(addressesInStorage).length).to.be.equal(20);
// Shouldn't try to reconnect
Expand All @@ -287,7 +296,6 @@ describe('TransactionSyncStreamWorker', function suite() {
expect(worker.stream).to.be.null;
});
});

describe("#execute", () => {
it('should sync incoming transactions and save it to the storage', async function () {
const lastSavedBlockHeight = 40;
Expand Down Expand Up @@ -330,9 +338,8 @@ describe('TransactionSyncStreamWorker', function suite() {

await worker.onStop();

const transactionsInStorage = Object
.values(storage.getStore().transactions)
.map((t) => t.toJSON());
const transactionsInStorage = Array.from(storage.getChainStore('livenet').state.transactions)
.map(([,t]) => t.transaction.toJSON());

const expectedTransactions = transactionsSent
.map((t) => t.toJSON());
Expand Down Expand Up @@ -405,25 +412,28 @@ describe('TransactionSyncStreamWorker', function suite() {

await worker.onStop();

const transactionsInStorage = Object
.values(storage.getStore().transactions)
.map((t) => t.toJSON());
const transactionsInStorage = Array.from(storage.getChainStore('livenet').state.transactions)
.map(([,t]) => t.transaction.toJSON());

const expectedTransactions = transactionsSent
.map((t) => t.toJSON());

const addressesInStorage = storage.store.wallets[walletId].addresses.external;
const {addresses} = storage.getWalletStore(walletId).state.paths.get(`m/44'/5'/0'`);

const addressesInStorage = Object.entries(addresses)
.filter(([path, address])=> path.includes('m/0'))
.map(([path, address])=> address);
// We send transaction to index 19, so wallet should generate additional 20 addresses to keep the gap between
// the last used address
expect(Object.keys(addressesInStorage).length).to.be.equal(40);
// It should reconnect after the gap limit is reached
expect(account.transport.subscribeToTransactionsWithProofs.callCount).to.be.equal(2);
expect(account.transport.subscribeToTransactionsWithProofs.callCount).to.be.equal(1);
// 20 external and 20 internal
expect(account.transport.subscribeToTransactionsWithProofs.firstCall.args[0].length).to.be.equal(40);
expect(account.transport.subscribeToTransactionsWithProofs.firstCall.args[1]).to.be.deep.equal({ fromBlockHeight: 40, count: 0});
// 20 more of external, since the last address is used.
expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[0].length).to.be.equal(60);
expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[1]).to.be.deep.equal({ fromBlockHash: '0000025d24ebe65454bd51a61bab94095a6ad1df996be387e31495f764d8e2d9', count: 0});
// expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[0].length).to.be.equal(60);
// expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[1]).to.be.deep.equal({ fromBlockHash: '0000025d24ebe65454bd51a61bab94095a6ad1df996be387e31495f764d8e2d9', count: 0});

expect(worker.stream).to.be.null;
expect(transactionsInStorage.length).to.be.equal(2);
Expand Down Expand Up @@ -453,7 +463,11 @@ describe('TransactionSyncStreamWorker', function suite() {

await worker.onStop();

const addressesInStorage = storage.store.wallets[walletId].addresses.external;
const {addresses} = storage.getWalletStore(walletId).state.paths.get(`m/44'/5'/0'`);

const addressesInStorage = Object.entries(addresses)
.filter(([path, address])=> path.includes('m/0'))
.map(([path, address])=> address);

expect(Object.keys(addressesInStorage).length).to.be.equal(20);
// It should reconnect after the gap limit is reached
Expand Down Expand Up @@ -486,7 +500,11 @@ describe('TransactionSyncStreamWorker', function suite() {

await worker.onStop();

const addressesInStorage = storage.store.wallets[walletId].addresses.external;
const {addresses} = storage.getWalletStore(walletId).state.paths.get(`m/44'/5'/0'`);

const addressesInStorage = Object.entries(addresses)
.filter(([path, address])=> path.includes('m/0'))
.map(([path, address])=> address);

expect(Object.keys(addressesInStorage).length).to.be.equal(20);
// It should reconnect if the server closes the stream
Expand Down Expand Up @@ -520,7 +538,11 @@ describe('TransactionSyncStreamWorker', function suite() {

await expect(worker.incomingSyncPromise).to.be.rejectedWith('Some random error');

const addressesInStorage = storage.store.wallets[walletId].addresses.external;
const {addresses} = storage.getWalletStore(walletId).state.paths.get(`m/44'/5'/0'`);

const addressesInStorage = Object.entries(addresses)
.filter(([path, address])=> path.includes('m/0'))
.map(([path, address])=> address);
expect(Object.keys(addressesInStorage).length).to.be.equal(20);

// Shouldn't try to reconnect
Expand Down Expand Up @@ -648,30 +670,34 @@ describe('TransactionSyncStreamWorker', function suite() {

await worker.onStop();

const transactionsInStorage = Object
.values(storage.getStore().transactions)
.map((t) => t.toJSON());
const transactionsInStorage = Array.from(storage.getChainStore('livenet').state.transactions)
.map(([,t]) => t.transaction.toJSON());

const expectedTransactions = transactionsSent
.map((t) => t.toJSON());

const {
external: externalAddressesInStorage,
internal: internalAddressesInStorage
} = storage.store.wallets[walletId].addresses
const {addresses} = storage.getWalletStore(walletId).state.paths.get(`m/44'/5'/0'`);

const externalAddressesInStorage = Object.entries(addresses)
.filter(([path, address])=> path.includes('m/0'))
.map(([path, address])=> address);

const internalAddressesInStorage = Object.entries(addresses)
.filter(([path, address])=> path.includes('m/1'))
.map(([path, address])=> address);

// We send transaction to index 19, so wallet should generate additional 20 addresses to keep the gap between
// the last used address
expect(Object.keys(externalAddressesInStorage).length).to.be.equal(40);
expect(Object.keys(internalAddressesInStorage).length).to.be.equal(20);
// It should reconnect after the gap limit is reached
expect(account.transport.subscribeToTransactionsWithProofs.callCount).to.be.equal(2);
expect(account.transport.subscribeToTransactionsWithProofs.callCount).to.be.equal(1);
// 20 external and 20 internal
expect(account.transport.subscribeToTransactionsWithProofs.firstCall.args[1]).to.be.deep.equal({ fromBlockHeight: 40, count: 0});
expect(account.transport.subscribeToTransactionsWithProofs.firstCall.args[0].length).to.be.equal(40);
// 20 more of external, since the last address is used, Merkle Block received
expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[0].length).to.be.equal(60);
expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[1]).to.be.deep.equal({ fromBlockHash: '0000025d24ebe65454bd51a61bab94095a6ad1df996be387e31495f764d8e2d9', count: 0});
// expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[0].length).to.be.equal(60);
// expect(account.transport.subscribeToTransactionsWithProofs.secondCall.args[1]).to.be.deep.equal({ fromBlockHash: '0000025d24ebe65454bd51a61bab94095a6ad1df996be387e31495f764d8e2d9', count: 0});
expect(worker.stream).to.be.null;
expect(transactionsInStorage.length).to.be.equal(2);
expect(transactionsInStorage).to.have.deep.members(expectedTransactions);
Expand Down Expand Up @@ -702,7 +728,7 @@ describe('TransactionSyncStreamWorker', function suite() {

// Check that wait method throws if timeout has passed
await expect(account.waitForInstantLock(transactions[2].hash, 1000)).to.eventually
.be.rejectedWith('InstantLock waiting period for transaction 256d5b3bf6d8869f5cc882ae070af9b648fa0f512bfa2b6f07b35d55e160a16c timed out');
.be.rejectedWith('InstantLock waiting period for transaction 823c272fc1694b571805d2bc2f8936597ee52de638a0ca5323233c239fd3e8c4 timed out');
});
it('should start from the height specified in `skipSynchronizationBeforeHeight` options', async function () {
const bestBlockHeight = 42;
Expand Down
12 changes: 6 additions & 6 deletions packages/wallet-lib/tests/integration/types/Account.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,16 @@ describe('Account', function suite() {
plugins: [worker],
allowSensitiveOperations: true,
HDPrivateKey: new HDPrivateKey(testHDKey),
adapter: storageAdapterMock
adapter: storageAdapterMock,
network: 'livenet'
});

({txStreamMock, transportMock} = await createAndAttachTransportMocksToWallet(wallet, this.sinonSandbox));

account = await wallet.getAccount();

storage = account.storage;
walletId = Object.keys(storage.store.wallets)[0];
walletId = account.walletId;

address = account.getAddress(0).address;
addressAtIndex19 = account.getAddress(19).address;
Expand All @@ -74,20 +75,19 @@ describe('Account', function suite() {

// Saving state to restore it later
await account.storage.saveState();

// Restoring wallet from the saved state
const restoredWallet = new Wallet({
offlineMode: true,
plugins: [worker],
allowSensitiveOperations: true,
HDPrivateKey: new HDPrivateKey(testHDKey),
adapter: storageAdapterMock
adapter: storageAdapterMock,
network: 'livenet'
});
const restoredAccount = await restoredWallet.getAccount();

const utxos = await restoredAccount.getUTXOS();

expect(utxos.length).to.be.equal(1);
});
});
});
});

0 comments on commit 8f21b4a

Please sign in to comment.