Skip to content

Commit

Permalink
refactor(core-state): merge index and reindex into one function insid…
Browse files Browse the repository at this point in the history
…e WalletRepository (#3563)
  • Loading branch information
bertiespell authored Mar 2, 2020
1 parent 64403cd commit af2d8dc
Show file tree
Hide file tree
Showing 37 changed files with 149 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe("Transaction Forging - Delegate Resignation", () => {
await expect(transactionsResign.id).not.toBeForged();

for (const delegate of takenDelegates) {
walletRepository.reindex(delegate);
walletRepository.index(delegate);
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/integration/core-api/__support__/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,6 @@ export const calculateRanks = async () => {
const wallet = walletRepository.findByPublicKey(delegate.publicKey!);
wallet.setAttribute("delegate.rank", i + 1);

walletRepository.reindex(wallet);
walletRepository.index(wallet);
});
};
30 changes: 15 additions & 15 deletions __tests__/integration/core-api/handlers/delegates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ beforeEach(() => {
wallet.setAttribute("delegate.producedBlocks", 75);
wallet.setAttribute("delegate.voteBalance", AppUtils.BigNumber.make(delegate.voteBalance));

walletRepository.reindex(wallet);
walletRepository.index(wallet);
});

const createWallet = (address: string): Contracts.State.Wallet =>
Expand Down Expand Up @@ -89,7 +89,7 @@ describe("API 2.0 - Delegates", () => {
const delegate = reverseDelegates[i];
delegate.setAttribute("delegate.voteBalance", AppUtils.BigNumber.make(i * 1e8));

walletRepository.reindex(delegate);
walletRepository.index(delegate);
}

const response = await api.request("GET", "delegates", { orderBy: "votes:asc" });
Expand All @@ -104,7 +104,7 @@ describe("API 2.0 - Delegates", () => {
const delegate = originalDelegates[i];
delegate.setAttribute("delegate.voteBalance", AppUtils.BigNumber.ZERO);

walletRepository.reindex(delegate);
walletRepository.index(delegate);
}
});

Expand All @@ -116,7 +116,7 @@ describe("API 2.0 - Delegates", () => {
);
const wallet: Contracts.State.Wallet = walletRepository.findByUsername("genesis_1");
wallet.setAttribute("delegate.voteBalance", AppUtils.BigNumber.make(12500000000000000));
walletRepository.reindex(wallet);
walletRepository.index(wallet);

const response = await api.request("GET", "delegates", { orderBy: "votes:desc" });
expect(response).toBeSuccessfulResponse();
Expand Down Expand Up @@ -274,17 +274,17 @@ describe("API 2.0 - Delegates", () => {
for (let i = 0; i < delegates.length; i++) {
const delegate = delegates[i];
delegate.setAttribute("delegate.voteBalance", AppUtils.BigNumber.ZERO);
walletRepository.reindex(delegate);
walletRepository.index(delegate);
}

// Give 2 delegates a vote weight
const delegate1 = walletRepository.findByUsername("genesis_1");
delegate1.setAttribute("delegate.voteBalance", AppUtils.BigNumber.make(10000000 * 1e8));
walletRepository.reindex(delegate1);
walletRepository.index(delegate1);

const delegate2 = walletRepository.findByUsername("genesis_2");
delegate2.setAttribute("delegate.voteBalance", AppUtils.BigNumber.make(10000000 * 1e8));
walletRepository.reindex(delegate2);
walletRepository.index(delegate2);

const response = await api.request("POST", "delegates/search", {
approval: {
Expand All @@ -305,7 +305,7 @@ describe("API 2.0 - Delegates", () => {
// Make sure all vote balances are at 0
for (const delegate of walletRepository.allByUsername()) {
delegate.setAttribute("delegate.voteBalance", AppUtils.BigNumber.ZERO);
walletRepository.reindex(delegate);
walletRepository.index(delegate);
}
});

Expand All @@ -321,17 +321,17 @@ describe("API 2.0 - Delegates", () => {
for (let i = 0; i < delegates.length; i++) {
const delegate = delegates[i];
delegate.setAttribute("delegate.voteBalance", AppUtils.BigNumber.ZERO);
walletRepository.reindex(delegate);
walletRepository.index(delegate);
}

// Give 2 delegates a vote weight
const delegate1 = walletRepository.findByUsername("genesis_1");
delegate1.setAttribute("delegate.voteBalance", AppUtils.BigNumber.make(10000000 * 1e8));
walletRepository.reindex(delegate1);
walletRepository.index(delegate1);

const delegate2 = walletRepository.findByUsername("genesis_2");
delegate2.setAttribute("delegate.voteBalance", AppUtils.BigNumber.make(5000000 * 1e8));
walletRepository.reindex(delegate2);
walletRepository.index(delegate2);

const response = await api.request("POST", "delegates/search", {
approval: {
Expand Down Expand Up @@ -492,13 +492,13 @@ describe("API 2.0 - Delegates", () => {
for (let i = 0; i < delegates.length; i++) {
const delegate = delegates[i];
delegate.setAttribute("delegate.producedBlocks", 0);
walletRepository.reindex(delegate);
walletRepository.index(delegate);
}

// Give 2 delegates a vote weight
const delegate1 = walletRepository.findByUsername("genesis_1");
delegate1.setAttribute("delegate.producedBlocks", delegate.producedBlocks);
walletRepository.reindex(delegate1);
walletRepository.index(delegate1);

const response = await api.request("POST", "delegates/search", {
producedBlocks: {
Expand Down Expand Up @@ -548,13 +548,13 @@ describe("API 2.0 - Delegates", () => {
for (let i = 0; i < delegates.length; i++) {
const delegate = delegates[i];
delegate.setAttribute("delegate.voteBalance", 0);
walletRepository.reindex(delegate);
walletRepository.index(delegate);
}

// Give 2 delegates a vote weight
const delegate1 = walletRepository.findByUsername("genesis_1");
delegate1.setAttribute("delegate.voteBalance", AppUtils.BigNumber.make(delegate.voteBalance));
walletRepository.reindex(delegate1);
walletRepository.index(delegate1);

const response = await api.request("POST", "delegates/search", {
voteBalance: {
Expand Down
6 changes: 3 additions & 3 deletions __tests__/integration/core-api/handlers/locks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe("API 2.0 - Locks", () => {

wallet.setAttribute("htlc.locks", locks);

walletRepository.reindex(wallet);
walletRepository.index(wallet);
}
});

Expand Down Expand Up @@ -201,7 +201,7 @@ describe("API 2.0 - Locks", () => {

it("should POST a search for locks with the exact vendorField", async () => {
const wallet = createWallet("secret", { vendorField: "HTLC" });
walletRepository.reindex(wallet);
walletRepository.index(wallet);

const response = await api.request("POST", "locks/search", {
vendorField: "HTLC",
Expand All @@ -219,7 +219,7 @@ describe("API 2.0 - Locks", () => {

it("should POST a search for locks within the timestamp range", async () => {
const wallet = createWallet("secret", { timestamp: 5000 });
walletRepository.reindex(wallet);
walletRepository.index(wallet);

const response = await api.request("POST", "locks/search", {
timestamp: {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/integration/core-api/handlers/wallets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,13 @@ describe("API 2.0 - Wallets", () => {
for (let i = 0; i < delegates.length; i++) {
const delegate = delegates[i];
delegate.setAttribute("delegate.voteBalance", Utils.BigNumber.ZERO);
walletRepository.reindex(delegate);
walletRepository.index(delegate);
}

// Give 2 delegates a vote weight
const delegate1 = walletRepository.findByUsername("genesis_1");
delegate1.setAttribute("delegate.voteBalance", Utils.BigNumber.make(balance));
walletRepository.reindex(delegate1);
walletRepository.index(delegate1);

const response = await api.request("POST", "wallets/search", {
address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ beforeEach(() => {
multiSignatureWallet = buildMultiSignatureWallet();
recipientWallet = buildRecipientWallet(factoryBuilder);

walletRepository.reindex(senderWallet);
walletRepository.reindex(secondSignatureWallet);
walletRepository.reindex(multiSignatureWallet);
walletRepository.reindex(recipientWallet);
walletRepository.index(senderWallet);
walletRepository.index(secondSignatureWallet);
walletRepository.index(multiSignatureWallet);
walletRepository.index(recipientWallet);
});

describe("MultiSignatureRegistrationTransaction", () => {
Expand All @@ -92,7 +92,7 @@ describe("MultiSignatureRegistrationTransaction", () => {

recipientWallet = new Wallets.Wallet(Identities.Address.fromMultiSignatureAsset(multiSignatureAsset), new Services.Attributes.AttributeMap(getWalletAttributeSet()));

walletRepository.reindex(recipientWallet);
walletRepository.index(recipientWallet);

multiSignatureTransaction = BuilderFactory.multiSignature()
.version(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ beforeEach(() => {
multiSignatureWallet = buildMultiSignatureWallet();
recipientWallet = buildRecipientWallet(factoryBuilder);

walletRepository.reindex(senderWallet);
walletRepository.reindex(secondSignatureWallet);
walletRepository.reindex(multiSignatureWallet);
walletRepository.reindex(recipientWallet);
walletRepository.index(senderWallet);
walletRepository.index(secondSignatureWallet);
walletRepository.index(multiSignatureWallet);
walletRepository.index(recipientWallet);
});

describe("DelegateRegistrationTransaction", () => {
Expand Down Expand Up @@ -199,7 +199,7 @@ describe("DelegateRegistrationTransaction", () => {

delegateWallet.setAttribute("delegate", { username: "dummy" });

walletRepository.reindex(delegateWallet);
walletRepository.index(delegateWallet);

await expect(handler.throwIfCannotBeApplied(delegateRegistrationTransaction, senderWallet, walletRepository)).rejects.toThrow(
WalletUsernameAlreadyRegisteredError,
Expand Down Expand Up @@ -237,7 +237,7 @@ describe("DelegateRegistrationTransaction", () => {

anotherWallet.balance = Utils.BigNumber.make(7527654310);

walletRepository.reindex(anotherWallet);
walletRepository.index(anotherWallet);

let anotherDelegateRegistrationTransaction = BuilderFactory.delegateRegistration()
.usernameAsset("dummy")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ beforeEach(() => {
multiSignatureWallet = buildMultiSignatureWallet();
recipientWallet = buildRecipientWallet(factoryBuilder);

walletRepository.reindex(senderWallet);
walletRepository.reindex(secondSignatureWallet);
walletRepository.reindex(multiSignatureWallet);
walletRepository.reindex(recipientWallet);
walletRepository.index(senderWallet);
walletRepository.index(secondSignatureWallet);
walletRepository.index(multiSignatureWallet);
walletRepository.index(recipientWallet);
});

describe("DelegateResignationTransaction", () => {
Expand Down Expand Up @@ -97,7 +97,7 @@ describe("DelegateResignationTransaction", () => {

delegateWallet.setAttribute("delegate", { username: "username" + i } );

walletRepository.reindex(delegateWallet);
walletRepository.index(delegateWallet);
allDelegates.push(delegateWallet);
}

Expand All @@ -111,7 +111,7 @@ describe("DelegateResignationTransaction", () => {

delegateWallet.balance = Utils.BigNumber.make(66 * 1e8);
delegateWallet.setAttribute("delegate", {username: "dummy"});
walletRepository.reindex(delegateWallet);
walletRepository.index(delegateWallet);

delegateResignationTransaction = BuilderFactory.delegateResignation()
.nonce("1")
Expand All @@ -134,7 +134,7 @@ describe("DelegateResignationTransaction", () => {
it("should resolve - simulate genesis wallet", async () => {
allDelegates[0].forgetAttribute("delegate");

walletRepository.reindex(allDelegates[0]);
walletRepository.index(allDelegates[0]);

setMockTransaction(delegateResignationTransaction);
await expect(handler.bootstrap()).toResolve();
Expand All @@ -160,7 +160,7 @@ describe("DelegateResignationTransaction", () => {

it("should not throw if wallet is a delegate - second sign", async () => {
secondSignatureWallet.setAttribute("delegate", {username: "dummy"});
walletRepository.reindex(secondSignatureWallet);
walletRepository.index(secondSignatureWallet);
await expect(handler.throwIfCannotBeApplied(secondSignatureDelegateResignationTransaction, secondSignatureWallet, walletRepository)).toResolve();
});

Expand All @@ -174,7 +174,7 @@ describe("DelegateResignationTransaction", () => {
.make();

anotherDelegate.setAttribute("delegate", {username: "another"});
walletRepository.reindex(anotherDelegate);
walletRepository.index(anotherDelegate);

await expect(handler.throwIfCannotBeApplied(delegateResignationTransaction, delegateWallet, walletRepository)).toResolve();
});
Expand Down
8 changes: 4 additions & 4 deletions __tests__/unit/core-transactions/handlers/two/general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ beforeEach(() => {
multiSignatureWallet = buildMultiSignatureWallet();
recipientWallet = buildRecipientWallet(factoryBuilder);

walletRepository.reindex(senderWallet);
walletRepository.reindex(secondSignatureWallet);
walletRepository.reindex(multiSignatureWallet);
walletRepository.reindex(recipientWallet);
walletRepository.index(senderWallet);
walletRepository.index(secondSignatureWallet);
walletRepository.index(multiSignatureWallet);
walletRepository.index(recipientWallet);
});

describe("General Tests", () => {
Expand Down
20 changes: 10 additions & 10 deletions __tests__/unit/core-transactions/handlers/two/htlc-claim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ beforeEach(() => {
multiSignatureWallet = buildMultiSignatureWallet();
recipientWallet = buildRecipientWallet(factoryBuilder);

walletRepository.reindex(senderWallet);
walletRepository.reindex(secondSignatureWallet);
walletRepository.reindex(multiSignatureWallet);
walletRepository.reindex(recipientWallet);
walletRepository.index(senderWallet);
walletRepository.index(secondSignatureWallet);
walletRepository.index(multiSignatureWallet);
walletRepository.index(recipientWallet);
});

describe("Htlc claim", () => {
Expand Down Expand Up @@ -116,8 +116,8 @@ describe("Htlc claim", () => {
})
.make();

walletRepository.reindex(lockWallet);
walletRepository.reindex(claimWallet);
walletRepository.index(lockWallet);
walletRepository.index(claimWallet);

let expiration = {
type: expirationType,
Expand Down Expand Up @@ -145,7 +145,7 @@ describe("Htlc claim", () => {
},
});

walletRepository.reindex(lockWallet);
walletRepository.index(lockWallet);

htlcClaimTransaction = BuilderFactory.htlcClaim()
.htlcClaimAsset({
Expand Down Expand Up @@ -238,7 +238,7 @@ describe("Htlc claim", () => {
})
.make();

walletRepository.reindex(dummyWallet);
walletRepository.index(dummyWallet);

htlcClaimTransaction = BuilderFactory.htlcClaim()
.htlcClaimAsset({
Expand Down Expand Up @@ -282,7 +282,7 @@ describe("Htlc claim", () => {
},
});

walletRepository.reindex(lockWallet);
walletRepository.index(lockWallet);

htlcClaimTransaction = BuilderFactory.htlcClaim()
.htlcClaimAsset({
Expand Down Expand Up @@ -390,7 +390,7 @@ describe("Htlc claim", () => {
})
.make();

walletRepository.reindex(dummyWallet);
walletRepository.index(dummyWallet);

htlcClaimTransaction = BuilderFactory.htlcClaim()
.htlcClaimAsset({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ beforeEach(() => {
multiSignatureWallet = buildMultiSignatureWallet();
recipientWallet = buildRecipientWallet(factoryBuilder);

walletRepository.reindex(senderWallet);
walletRepository.reindex(secondSignatureWallet);
walletRepository.reindex(multiSignatureWallet);
walletRepository.reindex(recipientWallet);
walletRepository.index(senderWallet);
walletRepository.index(secondSignatureWallet);
walletRepository.index(multiSignatureWallet);
walletRepository.index(recipientWallet);
});

describe("Htlc lock", () => {
Expand Down
Loading

0 comments on commit af2d8dc

Please sign in to comment.