Skip to content

Commit

Permalink
fix(core-transaction-pool): wallet-manager fallback to database walle…
Browse files Browse the repository at this point in the history
…t manager findByIndex() when no "local" match (#3256)

* fix: wallet-manager findByIndex
try find index with db wallet manager if no match "locally"

* test: wallet-manager findByIndex
  • Loading branch information
air1one authored Nov 15, 2019
1 parent 8f028f8 commit 9c9e7b3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Blockchain, Container, Database } from "@arkecosystem/core-interfaces";
import { Blockchain, Container, Database, State } from "@arkecosystem/core-interfaces";
import { Wallets } from "@arkecosystem/core-state";
import { Handlers } from "@arkecosystem/core-transactions";
import { Blocks, Identities, Utils } from "@arkecosystem/crypto";
import { generateMnemonic } from "bip39";
Expand Down Expand Up @@ -222,3 +223,34 @@ describe("Apply transactions and block rewards to wallets on new block", () => {
expect(+delegateWallet.balance).toBe(+forgingDelegate.balance + reward + totalFee); // balance increased by reward + fee
});
});

describe("findByIndex", () => {
describe("when transaction pool wallet manager does not find a wallet by index", () => {
it("should get the wallet from database wallet manager if it exists", async () => {
const publicKey = "02664fe58caa4a960ed74169a5968a5f69587ba50b75087d268f5788af3a5bf56d";
const address = Identities.Address.fromPublicKey(publicKey);
const lockId = "cd08f14f049ea0ff0661635929ed267275e71509561c78d72a55a0bbccc48c30";
const walletWithHtlcLock = Object.assign(new Wallets.Wallet(address), {
attributes: {
htlc: {
locks: {
[lockId]: {
amount: Utils.BigNumber.make(10),
recipientId: address,
secretHash: lockId,
expiration: {
type: 1,
value: 100,
},
},
},
},
},
});
container.resolvePlugin<Database.IDatabaseService>("database").walletManager.reindex(walletWithHtlcLock);

const poolWallet = poolWalletManager.findByIndex(State.WalletIndexes.Locks, lockId);
expect(poolWallet).toEqual(walletWithHtlcLock);
});
});
});
17 changes: 17 additions & 0 deletions packages/core-transaction-pool/src/wallet-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ export class WalletManager extends Wallets.WalletManager {
return this.findByIndex(State.WalletIndexes.Addresses, address);
}

public findByIndex(index: string | string[], key: string): State.IWallet | undefined {
const wallet = super.findByIndex(index, key);

if (wallet) {
return wallet;
}

const dbWallet = this.databaseService.walletManager.findByIndex(index, key);
if (dbWallet) {
const cloneWallet = clonedeep(dbWallet);
this.reindex(cloneWallet);
return cloneWallet;
}

return undefined;
}

public forget(publicKey: string): void {
this.forgetByPublicKey(publicKey);
this.forgetByAddress(Identities.Address.fromPublicKey(publicKey));
Expand Down

0 comments on commit 9c9e7b3

Please sign in to comment.