Skip to content

Commit

Permalink
refactor(core-state): use getters and setter on wallet (#4375)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastijankuzner authored May 4, 2021
1 parent e699b4d commit 9724ff4
Show file tree
Hide file tree
Showing 96 changed files with 1,092 additions and 722 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class DelegateWalletMock {
this.voteBalance = voteBalance;
}

public getPublicKey(): string {
return this.publicKey;
}

public getAttribute(key: string) {
switch (key) {
case "delegate.round":
Expand Down
8 changes: 4 additions & 4 deletions __tests__/functional/transaction-forging/__support__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ export const tearDown = async (): Promise<void> => {
delete walletAttributes.delegate;
}
return {
publicKey: wallet.publicKey,
balance: wallet.balance,
nonce: wallet.nonce,
publicKey: wallet.getPublicKey(),
balance: wallet.getBalance(),
nonce: wallet.getNonce(),
attributes: walletAttributes,
};
};
const sortWallets = (a: Contracts.State.Wallet, b: Contracts.State.Wallet) =>
a.publicKey!.localeCompare(b.publicKey!);
a.getPublicKey()!.localeCompare(b.getPublicKey()!);

const allByPublicKey = walletRepository
.allByPublicKey()
Expand Down
4 changes: 2 additions & 2 deletions __tests__/integration/core-api/__support__/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ export const calculateRanks = async () => {
.comparedTo(a.getAttribute<Utils.BigNumber>("delegate.voteBalance")),
);

AppUtils.sortBy(delegateWallets, (wallet) => wallet.publicKey).forEach((delegate, i) => {
const wallet = walletRepository.findByPublicKey(delegate.publicKey!);
AppUtils.sortBy(delegateWallets, (wallet) => wallet.getPublicKey()).forEach((delegate, i) => {
const wallet = walletRepository.findByPublicKey(delegate.getPublicKey()!);
wallet.setAttribute("delegate.rank", i + 1);

walletRepository.index(wallet);
Expand Down
2 changes: 1 addition & 1 deletion __tests__/integration/core-api/controllers/locks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ beforeAll(() => {

walletLocks[transaction.id] = {
amount: Utils.BigNumber.make(n * 10),
recipientId: wallet.address,
recipientId: wallet.getAddress(),
secretHash: transaction.id,
expiration:
j % 2 === 0
Expand Down
4 changes: 2 additions & 2 deletions __tests__/integration/core-api/handlers/locks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe("API 2.0 - Locks", () => {

for (let i = 1; i < 7; i++) {
const wallet = walletRepository.findByAddress(Identities.Address.fromPassphrase(`${i}`));
wallet.publicKey = Identities.PublicKey.fromPassphrase(`${i}`);
wallet.setPublicKey(Identities.PublicKey.fromPassphrase(`${i}`));

const transactions = Managers.configManager.get("genesisBlock").transactions.slice(i * 10, i * 10 + i + 1);

Expand All @@ -44,7 +44,7 @@ describe("API 2.0 - Locks", () => {

locks[transaction.id] = {
amount: Utils.BigNumber.make(10 * (j + 1)),
recipientId: wallet.address,
recipientId: wallet.getAddress(),
secretHash: transaction.id,
expiration: {
type: j % 2 === 0 ? 1 : 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ describe("DelegateSearchService.getDelegate", () => {
const wallet1 = walletRepository.findByPublicKey(Identities.PublicKey.fromPassphrase("secret1"));
wallet1.setAttribute("delegate", delegateAttribute1);
walletRepository.index(wallet1);
const delegateResource1 = delegateSearchService.getDelegate(wallet1.address);
const delegateResource1 = delegateSearchService.getDelegate(wallet1.getAddress())!;

expect(delegateResource1.address).toBe(wallet1.address);
expect(delegateResource1.address).toBe(wallet1.getAddress());
});

it("should not get non-delegate wallet by address", () => {
const wallet1 = walletRepository.findByPublicKey(Identities.PublicKey.fromPassphrase("secret1"));
const delegateResource1 = delegateSearchService.getDelegate(wallet1.address);
const delegateResource1 = delegateSearchService.getDelegate(wallet1.getAddress());

expect(delegateResource1).toBe(undefined);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LockSearchService, Identifiers as ApiIdentifiers } from "@arkecosystem/core-api/src";
import { Identifiers as ApiIdentifiers, LockSearchService } from "@arkecosystem/core-api/src";
import { Container, Contracts } from "@arkecosystem/core-kernel";
import { Identities, Utils, Enums } from "@arkecosystem/crypto";
import { Enums, Identities, Utils } from "@arkecosystem/crypto";

import { setUp } from "./__support__/setup";

Expand Down Expand Up @@ -127,7 +127,7 @@ describe("LockSearchService.getWalletLocksPage", () => {
const locksPage = lockSearchService.getWalletLocksPage(
{ offset: 0, limit: 100 },
[{ property: "amount", direction: "asc" }],
wallet1.address,
wallet1.getAddress(),
);

expect(locksPage.totalCount).toBe(2);
Expand All @@ -144,7 +144,7 @@ describe("LockSearchService.getWalletLocksPage", () => {
const locksPage = lockSearchService.getWalletLocksPage(
{ offset: 0, limit: 100 },
[{ property: "amount", direction: "asc" }],
wallet1.address,
wallet1.getAddress(),
{ amount: "1000" },
);

Expand All @@ -165,7 +165,7 @@ describe("LockSearchService.getWalletLocksPage", () => {
const locksPage = lockSearchService.getWalletLocksPage(
{ offset: 0, limit: 100 },
[{ property: "amount", direction: "asc" }],
wallet1.address,
wallet1.getAddress(),
);

expect(locksPage.totalCount).toBe(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WalletSearchService, Identifiers as ApiIdentifiers } from "@arkecosystem/core-api/src";
import { Identifiers as ApiIdentifiers, WalletSearchService } from "@arkecosystem/core-api/src";
import { Container, Contracts } from "@arkecosystem/core-kernel";
import { Identities, Utils } from "@arkecosystem/crypto";

Expand All @@ -22,9 +22,9 @@ beforeEach(async () => {
describe("WalletSearchService.getWallet", () => {
it("should get wallet by address", async () => {
const wallet = walletRepository.findByPublicKey(Identities.PublicKey.fromPassphrase("secret"));
const walletResource = walletSearchService.getWallet(wallet.address);
const walletResource = walletSearchService.getWallet(wallet.getAddress())!;

expect(walletResource.address).toEqual(wallet.address);
expect(walletResource.address).toEqual(wallet.getAddress());
});

it("should not get wallet by address", async () => {
Expand All @@ -36,9 +36,9 @@ describe("WalletSearchService.getWallet", () => {

it("should get wallet by publicKey", async () => {
const wallet = walletRepository.findByPublicKey(Identities.PublicKey.fromPassphrase("secret"));
const walletResource = walletSearchService.getWallet(Identities.PublicKey.fromPassphrase("secret"));
const walletResource = walletSearchService.getWallet(Identities.PublicKey.fromPassphrase("secret"))!;

expect(walletResource.address).toBe(wallet.address);
expect(walletResource.address).toBe(wallet.getAddress());
});

it("should not get wallet by publicKey", async () => {
Expand All @@ -52,9 +52,9 @@ describe("WalletSearchService.getWallet", () => {
const wallet = walletRepository.findByPublicKey(Identities.PublicKey.fromPassphrase("secret"));
wallet.setAttribute<string>("delegate.username", "test_username");
walletRepository.index(wallet);
const walletResource = walletSearchService.getWallet("test_username");
const walletResource = walletSearchService.getWallet("test_username")!;

expect(walletResource.address).toBe(wallet.address);
expect(walletResource.address).toBe(wallet.getAddress());
});

it("should not get wallet by username", async () => {
Expand All @@ -75,18 +75,18 @@ describe("WalletSearchService.getWalletsPage", () => {
const wallet4 = walletRepository.findByPublicKey(Identities.PublicKey.fromPassphrase("secret4"));
const wallet5 = walletRepository.findByPublicKey(Identities.PublicKey.fromPassphrase("secret5"));

wallet1.balance = Utils.BigNumber.make("100");
wallet2.balance = Utils.BigNumber.make("200");
wallet3.balance = Utils.BigNumber.make("300");
wallet4.balance = Utils.BigNumber.make("40");
wallet5.balance = Utils.BigNumber.make("50");
wallet1.setBalance(Utils.BigNumber.make("100"));
wallet2.setBalance(Utils.BigNumber.make("200"));
wallet3.setBalance(Utils.BigNumber.make("300"));
wallet4.setBalance(Utils.BigNumber.make("40"));
wallet5.setBalance(Utils.BigNumber.make("50"));

const page = walletSearchService.getWalletsPage({ limit: 3, offset: 0 }, []);

expect(page.results.length).toBe(3);
expect(page.results[0].address).toBe(wallet3.address);
expect(page.results[1].address).toBe(wallet2.address);
expect(page.results[2].address).toBe(wallet1.address);
expect(page.results[0].address).toBe(wallet3.getAddress());
expect(page.results[1].address).toBe(wallet2.getAddress());
expect(page.results[2].address).toBe(wallet1.getAddress());
expect(page.totalCount).toBe(5);
});

Expand All @@ -97,17 +97,17 @@ describe("WalletSearchService.getWalletsPage", () => {
const wallet4 = walletRepository.findByPublicKey(Identities.PublicKey.fromPassphrase("secret4"));
const wallet5 = walletRepository.findByPublicKey(Identities.PublicKey.fromPassphrase("secret5"));

wallet1.balance = Utils.BigNumber.make("100");
wallet2.balance = Utils.BigNumber.make("200");
wallet3.balance = Utils.BigNumber.make("300");
wallet4.balance = Utils.BigNumber.make("40");
wallet5.balance = Utils.BigNumber.make("50");
wallet1.setBalance(Utils.BigNumber.make("100"));
wallet2.setBalance(Utils.BigNumber.make("200"));
wallet3.setBalance(Utils.BigNumber.make("300"));
wallet4.setBalance(Utils.BigNumber.make("40"));
wallet5.setBalance(Utils.BigNumber.make("50"));

const page = walletSearchService.getWalletsPage({ limit: 3, offset: 3 }, []);

expect(page.results.length).toBe(2);
expect(page.results[0].address).toBe(wallet5.address);
expect(page.results[1].address).toBe(wallet4.address);
expect(page.results[0].address).toBe(wallet5.getAddress());
expect(page.results[1].address).toBe(wallet4.getAddress());
expect(page.totalCount).toBe(5);
});

Expand All @@ -118,18 +118,18 @@ describe("WalletSearchService.getWalletsPage", () => {
const wallet4 = walletRepository.findByPublicKey(Identities.PublicKey.fromPassphrase("secret4"));
const wallet5 = walletRepository.findByPublicKey(Identities.PublicKey.fromPassphrase("secret5"));

wallet1.balance = Utils.BigNumber.make("100");
wallet2.balance = Utils.BigNumber.make("200");
wallet3.balance = Utils.BigNumber.make("300");
wallet4.balance = Utils.BigNumber.make("40");
wallet5.balance = Utils.BigNumber.make("50");
wallet1.setBalance(Utils.BigNumber.make("100"));
wallet2.setBalance(Utils.BigNumber.make("200"));
wallet3.setBalance(Utils.BigNumber.make("300"));
wallet4.setBalance(Utils.BigNumber.make("40"));
wallet5.setBalance(Utils.BigNumber.make("50"));

const page = walletSearchService.getWalletsPage({ limit: 5, offset: 0 }, [], { balance: { from: "100" } });

expect(page.results.length).toBe(3);
expect(page.results[0].address).toBe(wallet3.address);
expect(page.results[1].address).toBe(wallet2.address);
expect(page.results[2].address).toBe(wallet1.address);
expect(page.results[0].address).toBe(wallet3.getAddress());
expect(page.results[1].address).toBe(wallet2.getAddress());
expect(page.results[2].address).toBe(wallet1.getAddress());
expect(page.totalCount).toBe(3);
});
});
Expand All @@ -142,18 +142,18 @@ describe("WalletSearchService.getActiveWalletsPage", () => {
const wallet4 = walletRepository.findByAddress(Identities.Address.fromPassphrase("secret4"));
const wallet5 = walletRepository.findByAddress(Identities.Address.fromPassphrase("secret5"));

wallet1.balance = Utils.BigNumber.make("100");
wallet2.balance = Utils.BigNumber.make("200");
wallet3.balance = Utils.BigNumber.make("300");
wallet4.balance = Utils.BigNumber.make("400");
wallet5.balance = Utils.BigNumber.make("500");
wallet1.setBalance(Utils.BigNumber.make("100"));
wallet2.setBalance(Utils.BigNumber.make("200"));
wallet3.setBalance(Utils.BigNumber.make("300"));
wallet4.setBalance(Utils.BigNumber.make("400"));
wallet5.setBalance(Utils.BigNumber.make("500"));

const page = walletSearchService.getActiveWalletsPage({ offset: 0, limit: 100 }, [
{ property: "balance", direction: "asc" },
]);

expect(page.totalCount).toBe(2);
expect(page.results[0].address).toBe(wallet1.address);
expect(page.results[1].address).toBe(wallet2.address);
expect(page.results[0].address).toBe(wallet1.getAddress());
expect(page.results[1].address).toBe(wallet2.getAddress());
});
});
6 changes: 3 additions & 3 deletions __tests__/integration/core-state/__support__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const getExpectedVoteBalances = (
const publicKey = wallet.getAttribute("vote");
const voteBalance = wallet
.getAttribute("htlc.lockedBalance", Utils.BigNumber.ZERO)
.plus(wallet.balance)
.plus(wallet.getBalance())
.plus(expectedVoteBalances[publicKey] ?? Utils.BigNumber.ZERO);

if (voteBalance.isZero()) {
Expand All @@ -34,8 +34,8 @@ export const getActualVoteBalances = (
if (wallet.hasAttribute("delegate.voteBalance")) {
const voteBalance = wallet.getAttribute("delegate.voteBalance");
if (!voteBalance.isZero()) {
AppUtils.assert.defined<string>(wallet.publicKey);
actualVoteBalances[wallet.publicKey] = voteBalance;
AppUtils.assert.defined<string>(wallet.getPublicKey());
actualVoteBalances[wallet.getPublicKey()!] = voteBalance;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions __tests__/unit/core-api/__support__/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export const buildSenderWallet = (app: Application, passphrase: string | null =
Identities.Address.fromPassphrase(passphrase ? passphrase : passphrases[0]),
);

wallet.publicKey = Identities.PublicKey.fromPassphrase(passphrase ? passphrase : passphrases[0]);
wallet.balance = Utils.BigNumber.make(7527654310);
wallet.setPublicKey(Identities.PublicKey.fromPassphrase(passphrase ? passphrase : passphrases[0]));
wallet.setBalance(Utils.BigNumber.make(7527654310));

return wallet;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,15 @@ let mockBlockchainService;

beforeEach(() => {
mockWallet = {
address: "ANRNMPjQjJGVsVbyeqwShcxKTidYJ2S1Hm",
publicKey: "021770413ad01c60b94e1d3ed44c00e0145fe7897e40f5f6265e220f4e65cf427f",

getAddress: () => {
return "ANRNMPjQjJGVsVbyeqwShcxKTidYJ2S1Hm";
},
getPublicKey: () => {
return "021770413ad01c60b94e1d3ed44c00e0145fe7897e40f5f6265e220f4e65cf427f";
},
hasAttribute: () => {
return true;
},

getAttribute: () => {
return "genesis_13";
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ beforeEach(() => {
mockWalletRepository = {
findByPublicKey: () => {
return {
address: "D8791H5uhZcg1tJmpryVUFanvLth52AmrJ",
getAddress: jest.fn().mockReturnValue("D8791H5uhZcg1tJmpryVUFanvLth52AmrJ"),
};
},
};
Expand Down
10 changes: 5 additions & 5 deletions __tests__/unit/core-api/services/delegate-search-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe("DelegateSearchService", () => {
describe("getDelegate", () => {
it("should return delegate by wallet address", () => {
const delegate = new Wallets.Wallet("ANBkoGqWeTSiaEVgVzSKZd3jS7UWzv9PSo", attributeMap);
delegate.publicKey = "03287bfebba4c7881a0509717e71b34b63f31e40021c321f89ae04f84be6d6ac37";
delegate.setPublicKey("03287bfebba4c7881a0509717e71b34b63f31e40021c321f89ae04f84be6d6ac37");

delegate.setAttribute("delegate", {
username: "delegate_username",
Expand All @@ -67,7 +67,7 @@ describe("DelegateSearchService", () => {

it("should return delegate by wallet address with produced blocks", () => {
const delegate = new Wallets.Wallet("ANBkoGqWeTSiaEVgVzSKZd3jS7UWzv9PSo", attributeMap);
delegate.publicKey = "03287bfebba4c7881a0509717e71b34b63f31e40021c321f89ae04f84be6d6ac37";
delegate.setPublicKey("03287bfebba4c7881a0509717e71b34b63f31e40021c321f89ae04f84be6d6ac37");

delegate.setAttribute("delegate", {
username: "delegate_username",
Expand All @@ -92,7 +92,7 @@ describe("DelegateSearchService", () => {

it("should return undefined if walled is not delegate", () => {
const delegate = new Wallets.Wallet("ANBkoGqWeTSiaEVgVzSKZd3jS7UWzv9PSo", attributeMap);
delegate.publicKey = "03287bfebba4c7881a0509717e71b34b63f31e40021c321f89ae04f84be6d6ac37";
delegate.setPublicKey("03287bfebba4c7881a0509717e71b34b63f31e40021c321f89ae04f84be6d6ac37");

walletRepository.findByAddress = jest.fn().mockReturnValue(delegate);

Expand All @@ -103,7 +103,7 @@ describe("DelegateSearchService", () => {
describe("getDelegatesPage", () => {
it("should return results with delegate", () => {
const delegate = new Wallets.Wallet("ANBkoGqWeTSiaEVgVzSKZd3jS7UWzv9PSo", attributeMap);
delegate.publicKey = "03287bfebba4c7881a0509717e71b34b63f31e40021c321f89ae04f84be6d6ac37";
delegate.setPublicKey("03287bfebba4c7881a0509717e71b34b63f31e40021c321f89ae04f84be6d6ac37");

delegate.setAttribute("delegate", {
username: "delegate_username",
Expand Down Expand Up @@ -134,7 +134,7 @@ describe("DelegateSearchService", () => {

it("should return empty array if all tested criterias are false", () => {
const delegate = new Wallets.Wallet("ANBkoGqWeTSiaEVgVzSKZd3jS7UWzv9PSo", attributeMap);
delegate.publicKey = "03287bfebba4c7881a0509717e71b34b63f31e40021c321f89ae04f84be6d6ac37";
delegate.setPublicKey("03287bfebba4c7881a0509717e71b34b63f31e40021c321f89ae04f84be6d6ac37");

delegate.setAttribute("delegate", {
username: "delegate_username",
Expand Down
Loading

0 comments on commit 9724ff4

Please sign in to comment.