Skip to content
This repository has been archived by the owner on Jan 12, 2022. It is now read-only.

Commit

Permalink
feat: integrate store and keychains
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Werner committed Nov 27, 2021
1 parent d40e9db commit 86a1b2d
Show file tree
Hide file tree
Showing 48 changed files with 929 additions and 389 deletions.
12 changes: 12 additions & 0 deletions fixtures/chains/testnet/blockheaders.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"height": 600000,
"blockheader": "00000020a69faca490012f4371e4bd40a40167809e6fe1b03c9c5dcb7df247f2fa000000b4b8c2d4a78fb35e50a4eda0bd3a5b9b32f4d739b28a4266f24775b3b79d5b61c73275610e04021ea74d0000",
"hash":"000000de786e659950e0f27681faf1a91871d15de264d0b769cb5941c1d807c3"
},
{
"height": 610000,
"blockheader": "00000020af5155e1f8d5b60fb247ac9bb8badbdce1fa72302336c7daab53585caf0000006d495b9629989ac3699d528956b1f617fb51a199ef4277c3f45f4606aba8e461d1c78a61210b011e94ef0000",
"hash":"00000094d124cfb68d6d59ffaec9f7d63965cb894855684e23a586274b49708f"
}
]
14 changes: 14 additions & 0 deletions fixtures/chains/testnet/transactions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"blockHash": "000000b2537d9b3468e02dc6f49fb8bbb5c9d8b77895df1e76816fbad5555d7d",
"transaction": "02000000019583d226737edfad682cc1f0297f688bfca4c0046062ed3e88fa47dd47eab2b2010000006a473044022021550232cc659fa412deb2ea98956faf9ff0efea3a04029bb63022f6809105ce0220230a3f955a3ee7563f5b006342d8db940f3b29f4e312701bf51720575c7a7a8a012103f3d2b2ddfe6ffad2b8fc1708f2eafadfed36bf31a05dfa04b1fc176526475b0ffeffffff02a003d806000000001976a91464220a1c12690ec26d837b3be0a2e3588bb4b79188ac4c5bc92b250000001976a9143e89746b9aa52703ab784bc0df467b160406ffb988acb56b0900",
"height": 617398,
"isInstantLocked": true,
"isChainLocked": true
},
{
"transaction": "0200000001c7d66bb85e0069c221b44b07f49f52cc4f2e54f70e14430b94888327763a66a9010000006b483045022100da5b319f73e6adfee751f33308f5a8c1fceeab2683e15e132d79053b3118639602204262022fb85f88d9802649a289a1134b678efcf708faaeae8f101e8eab785054012102bc626898b49f31f5194de7bc68004401639a20cfa82e4c2eac9684a91fc47a57feffffff0270f2f605000000001976a91464220a1c12690ec26d837b3be0a2e3588bb4b79188ac912e250c250000001976a91415e1edb5c5d9e67d0e36f94343b3eff26bb76d1088ac266e0900",
"blockHash":"0000005c81a683007e86e75c76b4b2feca229f806702ca92953562f2ae628ce7",
"height": 618023
}
]
1 change: 1 addition & 0 deletions src/EVENTS.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
TRANSACTION: 'transaction',
BLOCKHEADER: 'blockheader',
FETCHED_ADDRESS: 'FETCHED/ADDRESS',
UPDATED_ADDRESS: 'UPDATED/ADDRESS',
ERROR_UPDATE_ADDRESS: 'ERROR/UPDATE_ADDRESS',
FETCHED_TRANSACTION: 'FETCHED/TRANSACTION',
FETCHED_UNCONFIRMED_TRANSACTION: 'FETCHED/UNCONFIRMED_TRANSACTION',
Expand Down
5 changes: 5 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import { Account } from "./types/Account/Account";
import { Wallet } from "./types/Wallet/Wallet";
import { Identities } from "./types/Identities/Identities";
import { IdentitiesStore } from "./types/IdentitiesStore/IdentitiesStore";
import { KeyChain } from "./types/KeyChain/KeyChain";
import { KeyChainStore } from "./types/KeyChainStore/KeyChainStore";
import { Storage } from "./types/Storage/Storage";
import { ChainStore } from "./types/ChainStore/ChainStore";
import CONSTANTS from "./CONSTANTS";
import EVENTS from "./EVENTS";
import utils from "./utils";
Expand All @@ -14,9 +17,11 @@ import plugins from "./plugins";
export {
Account,
Wallet,
ChainStore,
KeyChain,
KeyChainStore,
Identities,
IdentitiesStore,
EVENTS,
CONSTANTS,
utils,
Expand Down
12 changes: 10 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@
// polyfill included here. Making it work with webpack is rather tricky, so it is used as per
// documentation: https://github.com/YuzuJS/setImmediate#usage
require('setimmediate');
const Wallet = require('./types/Wallet/Wallet');
const Account = require('./types/Account/Account');
const ChainStore = require('./types/ChainStore/ChainStore');
const Identities = require('./types/Identities/Identities');
const IdentitiesStore = require('./types/IdentitiesStore/IdentitiesStore');
const KeyChain = require('./types/KeyChain/KeyChain');
const KeyChainStore = require('./types/KeyChainStore/KeyChainStore');
const Storage = require('./types/Storage/Storage');
const Wallet = require('./types/Wallet/Wallet');
const WalletStore = require('./types/WalletStore/WalletStore');
const EVENTS = require('./EVENTS');
const CONSTANTS = require('./CONSTANTS');
const utils = require('./utils');
const plugins = require('./plugins');

module.exports = {
Wallet,
Account,
ChainStore,
Identities,
IdentitiesStore,
KeyChain,
KeyChainStore,
Storage,
Wallet,
WalletStore,
EVENTS,
CONSTANTS,
utils,
Expand Down
19 changes: 11 additions & 8 deletions src/plugins/Plugins/ChainPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ class ChainPlugin extends StandardPlugin {
*/
async execBlockListener() {
const self = this;
const { network } = this.storage.store.wallets[this.walletId];
const { network } = this.storage.application;
const chainStore = this.storage.getChainStore(network);

// const { network } = this.storage.store.wallets[this.walletId];

if (!this.isSubscribedToBlocks) {
self.transport.on(EVENTS.BLOCK, async (ev) => {
// const { network } = self.storage.store.wallets[self.walletId];
const { payload: block } = ev;
this.parentEvents.emit(EVENTS.BLOCK, { type: EVENTS.BLOCK, payload: block });
// We do not announce BLOCKHEADER as this is done by Storage
await self.storage.importBlockHeader(block.header);
await chainStore.importBlockHeader(block.header);
});
self.transport.on(EVENTS.BLOCKHEIGHT_CHANGED, async (ev) => {
const { payload: blockheight } = ev;
Expand All @@ -50,7 +53,7 @@ class ChainPlugin extends StandardPlugin {
type: EVENTS.BLOCKHEIGHT_CHANGED, payload: blockheight,
});

this.storage.store.chains[network.toString()].blockHeight = blockheight;
chainStore.state.blockHeight = blockheight;
logger.debug(`ChainPlugin - setting chain blockheight ${blockheight}`);
});
await self.transport.subscribeToBlocks();
Expand All @@ -69,20 +72,20 @@ class ChainPlugin extends StandardPlugin {
return false;
}

const { network } = this.storage.application;
const chainStore = this.storage.getChainStore(network);
const { chain: { blocksCount: blocks }, network: { fee: { relay } } } = res;

const { network } = this.storage.store.wallets[this.walletId];

logger.debug('ChainPlugin - Setting up starting blockHeight', blocks);

this.storage.store.chains[network.toString()].blockHeight = blocks;
chainStore.state.blockHeight = blocks;

if (relay) {
this.storage.store.chains[network.toString()].fees.minRelay = dashToDuffs(relay);
chainStore.state.fees.minRelay = dashToDuffs(relay);
}

const bestBlock = await this.transport.getBlockHeaderByHeight(blocks);
await this.storage.importBlockHeader(bestBlock);
await chainStore.importBlockHeader(bestBlock);

return true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/Workers/IdentitySyncWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class IdentitySyncWorker extends Worker {
}

async execute() {
const indexedIds = await this.storage.getIndexedIdentityIds(this.walletId);
const walletStore = this.storage.getWalletStore(this.walletId);
const indexedIds = await walletStore.getIndexedIdentityIds();

// Add gaps to empty indices
const unusedIndices = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ class TransactionSyncStreamWorker extends Worker {
.getMessagesList()
.map((instantSendLock) => new InstantLock(Buffer.from(instantSendLock)));
}

return walletTransactions;
}

Expand All @@ -136,7 +135,7 @@ class TransactionSyncStreamWorker extends Worker {
const {
skipSynchronizationBeforeHeight,
skipSynchronization,
} = (this.storage.store.syncOptions || {});
} = (this.storage.application.syncOptions || {});

if (skipSynchronization) {
logger.debug('TransactionSyncStreamWorker - Wallet created from a new mnemonic. Sync from the best block height.');
Expand Down Expand Up @@ -239,25 +238,15 @@ class TransactionSyncStreamWorker extends Worker {
}

setLastSyncedBlockHash(hash) {
const { walletId } = this;
const accountsStore = this.storage.store.wallets[walletId].accounts;

const accountStore = ([WALLET_TYPES.HDWALLET, WALLET_TYPES.HDPUBLIC].includes(this.walletType))
? accountsStore[this.BIP44PATH.toString()]
: accountsStore[this.index.toString()];
const applicationStore = this.storage.application;

accountStore.blockHash = hash;
applicationStore.blockHash = hash;

return accountStore.blockHash;
return applicationStore.blockHash;
}

getLastSyncedBlockHash() {
const { walletId } = this;
const accountsStore = this.storage.store.wallets[walletId].accounts;

const { blockHash } = ([WALLET_TYPES.HDWALLET, WALLET_TYPES.HDPUBLIC].includes(this.walletType))
? accountsStore[this.BIP44PATH.toString()]
: accountsStore[this.index.toString()];
const { blockHash } = this.storage.application;

return blockHash;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const logger = require('../../../../logger');

function onStreamError(error, reject) {
logger.silly('TransactionSyncStreamWorker - end stream on error');
logger.silly(error.message);
reject(error);
}
module.exports = onStreamError;
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
const { WALLET_TYPES } = require('../../../../CONSTANTS');
/**
* Return last synced block height
* @return {number}
*/
module.exports = function getLastSyncedBlockHeight() {
const { walletId } = this;
const accountsStore = this.storage.store.wallets[walletId].accounts;

let { blockHeight } = ([WALLET_TYPES.HDWALLET, WALLET_TYPES.HDPUBLIC].includes(this.walletType))
? accountsStore[this.BIP44PATH.toString()]
: accountsStore[this.index.toString()];
let { blockHeight } = this.storage.application;

// Fix Genesis issue on DCore
if (blockHeight === 0) blockHeight = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ async function handleTransactionFromStream(transaction) {
// eslint-disable-next-line no-restricted-syntax
// eslint-disable-next-line no-underscore-dangle
const transactionHash = transaction.hash;

this.pendingRequest[transactionHash] = { isProcessing: true, type: 'transaction' };
// eslint-disable-next-line no-await-in-loop
const getTransactionResponse = await this.transport.getTransaction(transactionHash);
Expand Down Expand Up @@ -62,8 +61,8 @@ async function handleTransactionFromStream(transaction) {
const metadata = {
blockHash: getTransactionResponse.blockHash,
height: getTransactionResponse.height,
instantLocked: getTransactionResponse.instantLocked,
chainLocked: getTransactionResponse.chainLocked,
instantLocked: getTransactionResponse.isInstantLocked,
chainLocked: getTransactionResponse.isChainLocked,
};

delete this.pendingRequest[transactionHash];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ async function processChunks(dataChunk) {
.filterWalletTransactions(transactionsFromResponse, addresses, network);

if (walletTransactions.transactions.length) {
// Normalizing format of transaction for account.importTransactions
const walletTransactionWithoutMetadata = walletTransactions.transactions.map((tx) => [tx]);
// When a transaction exist, there is multiple things we need to do :
// 1) The transaction itself needs to be imported
const addressesGeneratedCount = await self
.importTransactions(walletTransactions.transactions);

.importTransactions(walletTransactionWithoutMetadata);
// 2) Transaction metadata need to be fetched and imported as well.
// as such event might happen in the future
// As we require height information, we fetch transaction using client
Expand Down Expand Up @@ -80,10 +81,10 @@ async function processChunks(dataChunk) {
// Wrapping `cancel` in `setImmediate` due to bug with double-free
// explained here (https://github.com/grpc/grpc-node/issues/1652)
// and here (https://github.com/nodejs/node/issues/38964)
await new Promise((resolveCancel) => setImmediate(() => {
self.stream.cancel();
resolveCancel();
}));
// await new Promise((resolveCancel) => setImmediate(() => {
// self.stream.cancel();
// resolveCancel();
// }));
}
}
}
Expand All @@ -95,7 +96,7 @@ async function processChunks(dataChunk) {
if (merkleBlockFromResponse) {
// Reverse hashes, as they're little endian in the header
const transactionsInHeader = merkleBlockFromResponse.hashes.map((hashHex) => Buffer.from(hashHex, 'hex').reverse().toString('hex'));
const transactionsInWallet = Object.keys(self.storage.getStore().transactions);
const transactionsInWallet = [...self.storage.getChainStore(self.network).state.transactions.keys()];
const isTruePositive = isAnyIntersection(transactionsInHeader, transactionsInWallet);
if (isTruePositive) {
self.importBlockHeader(merkleBlockFromResponse.header);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@ const { WALLET_TYPES } = require('../../../../CONSTANTS');
* @return {number}
*/
module.exports = function setLastSyncedBlockHeight(blockHeight) {
const { walletId } = this;
const accountsStore = this.storage.store.wallets[walletId].accounts;
const applicationStore = this.storage.application;
applicationStore.blockHeight = blockHeight;

const accountStore = ([WALLET_TYPES.HDWALLET, WALLET_TYPES.HDPUBLIC].includes(this.walletType))
? accountsStore[this.BIP44PATH.toString()]
: accountsStore[this.index.toString()];

accountStore.blockHeight = blockHeight;

return accountStore.blockHeight;
return applicationStore.blockHeight;
};
Loading

0 comments on commit 86a1b2d

Please sign in to comment.