Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core-state): wallet repository clone instantiation #4009

Merged
merged 8 commits into from
Sep 11, 2020

Conversation

rainydio
Copy link
Contributor

@rainydio rainydio commented Sep 8, 2020

Summary

Clone of wallet repository was instantiated incorrectly:

public initialize(): void {
  for (const index of this.blockchainWalletRepository.getIndexNames()) {
    this.indexes[index] = this.blockchainWalletRepository.getIndex(index).clone();
  }
}

This error was actually made by me back in the days when I had little understanding of its internals. The issue is with the wallet index clone method:

public clone(): Contracts.State.WalletIndex {
  const walletIndex = new WalletIndex(this.indexer, this.autoIndex);

  for (const [key, value] of this.entries()) {
    walletIndex.set(key, value.clone());
  }

  return walletIndex;
}

It performs deep clone and as result cloned wallet repository will contain separate but identical wallet copies in all of its indexes:

const walletByPublicKey = walletRepositoryClone.findByPublicKey(tx.senderPublicKey);
const walletByAddress = walletRepositoryClone.findByAddress(walletByPublicKey.address);

assert(walletByPublicKey === walletByAddress); // throws

Fixed initialize by taking every wallet from "addresses" index (it's analogous to primary key) and then cloning and re-indexing them. This slows down initialization. If wallet indexes were storing address as value instead of wallet reference, then it would be possible to clone them and avoid re-indexing.


Now also throwing error when attempting to index state=blockchain wallet into it. Previously index automatically cloned wallet which meant that caller was left with wallet reference that wasn't actually indexed. Now caller is required to clone wallet himself if he has to transfer wallet between wallet repositories.

Checklist

  • Ready to be merged

@ghost ghost added Complexity: Low labels Sep 8, 2020
@codecov
Copy link

codecov bot commented Sep 8, 2020

Codecov Report

Merging #4009 into develop will increase coverage by 1.75%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #4009      +/-   ##
===========================================
+ Coverage    96.84%   98.60%   +1.75%     
===========================================
  Files          631      631              
  Lines        14588    14591       +3     
  Branches      1732     1735       +3     
===========================================
+ Hits         14128    14387     +259     
+ Misses         370      114     -256     
  Partials        90       90              
Flag Coverage Δ
#functional 6.88% <0.00%> (?)
#integration 9.95% <0.00%> (-0.01%) ⬇️
#unit 96.77% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
packages/core-state/src/wallets/wallet-index.ts 100.00% <ø> (ø)
...ckages/core-state/src/wallets/wallet-repository.ts 92.08% <ø> (ø)
.../core-state/src/wallets/wallet-repository-clone.ts 100.00% <100.00%> (ø)
...re-test-framework/src/utils/transaction-factory.ts 98.86% <0.00%> (+2.84%) ⬆️
packages/core-database/src/utils/transform.ts 100.00% <0.00%> (+25.00%) ⬆️
...core-database/src/repositories/round-repository.ts 87.50% <0.00%> (+50.00%) ⬆️
...-framework/src/matchers/functional/vote-balance.ts 90.00% <0.00%> (+70.00%) ⬆️
...atabase/src/repositories/transaction-repository.ts 100.00% <0.00%> (+78.37%) ⬆️
...core-database/src/repositories/block-repository.ts 89.04% <0.00%> (+80.82%) ⬆️
...e-test-framework/src/matchers/functional/entity.ts 93.54% <0.00%> (+87.09%) ⬆️
... and 24 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a75f016...f66ed96. Read the comment docs.

@rainydio rainydio marked this pull request as ready for review September 8, 2020 16:11
@air1one air1one merged commit 2fb47e6 into develop Sep 11, 2020
@ghost ghost deleted the fix/core-state/wallet-repository-clone-instantiation branch September 11, 2020 14:55
@ghost ghost removed the Status: Needs Review label Sep 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants