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

refactor: strengthen a nonce check in performGenericWalletChecks() #2949

Merged
merged 11 commits into from
Sep 24, 2019

Conversation

vasild
Copy link
Contributor

@vasild vasild commented Sep 20, 2019

refactor: strengthen a nonce check in performGenericWalletChecks()

In TransactionHandler.performGenericWalletChecks() we would only check
if the transaction's nonce is greater than the sender wallet nonce. The
check should be not just greater, but exactly sender wallet nonce plus
one.

Also, move multiplicated code into two methods and use those methods.

In TransactionHandler.performGenericWalletChecks() we would only check
if the transaction's nonce is greater than the sender wallet nonce. The
check should be not just greater, but exactly sender wallet nonce plus
one.

Also, move multiplicated code into two methods and use those methods.
@vasild vasild force-pushed the strengthen-nonce-check branch from f2bf499 to 7052f9a Compare September 20, 2019 11:25
@spkjp
Copy link
Contributor

spkjp commented Sep 20, 2019

LGTM. Just need to sort out the failing tests.

@codecov
Copy link

codecov bot commented Sep 23, 2019

Codecov Report

Merging #2949 into develop will increase coverage by 0.09%.
The diff coverage is 85.71%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #2949      +/-   ##
===========================================
+ Coverage    68.31%   68.41%   +0.09%     
===========================================
  Files          405      405              
  Lines         9826     9818       -8     
  Branches       464      462       -2     
===========================================
+ Hits          6713     6717       +4     
+ Misses        3077     3065      -12     
  Partials        36       36
Impacted Files Coverage Δ
...ages/core-transactions/src/handlers/htlc-refund.ts 80.51% <100%> (+2.31%) ⬆️
...kages/core-transactions/src/handlers/htlc-claim.ts 81.39% <100%> (+2.32%) ⬆️
packages/core-transaction-pool/src/connection.ts 92.22% <100%> (ø) ⬆️
packages/core-transaction-pool/src/processor.ts 81.33% <100%> (+2.38%) ⬆️
...ages/core-transactions/src/handlers/transaction.ts 68.33% <100%> (+4.39%) ⬆️
packages/core-state/src/wallets/wallet.ts 85.71% <50%> (-1.52%) ⬇️
packages/crypto/src/validation/keywords.ts 90.74% <0%> (-0.79%) ⬇️
packages/crypto/src/transactions/types/schemas.ts 100% <0%> (ø) ⬆️
packages/core-p2p/src/peer-communicator.ts 61.45% <0%> (+1.04%) ⬆️
... and 4 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 ebee960...26f980a. Read the comment docs.

@vasild
Copy link
Contributor Author

vasild commented Sep 23, 2019

This change revealed a flaw in the code: if we receive more than one transaction for addition in the pool, in Processor.validate() / filterAndTransformTransactions() we would call throwIfCannotBeApplied() on each one, without applying any of them:
https://github.com/ArkEcosystem/core/blob/7bfba2710/packages/core-transaction-pool/src/processor.ts#L124
this, obviously, will not work wrt nonce - receiving two transactions from the same sender, we have to apply the first one so that the nonce is incremented before testing if the second can be applied.

There are a few solutions:

  • Remove the throwIfCannotBeApplied() call from filterAndTransformTransactions(). It looks unnecessary because later throwIfCannotBeApplied() is called again from the pool's Connection.addTransaction().

  • If the call to throwIfCannotBeApplied() from filterAndTransformTransactions() is absolutely unnecessary, then a bigger refactor is due, changing https://github.com/ArkEcosystem/core/blob/7bfba2710/packages/core-transaction-pool/src/processor.ts#L28 to process transactions one by one: calling filterAndTransformTransactions(), removeForgedTransactions() and addTransactionsToPool() for one transaction before proceeding with the next.

  • Workaround the flaw from the test, processing the transactions one by one:

             // reset processor, if not the 1st transaction will still be in this.accept and mess up
             processor = transactionPool.makeProcessor();

-            await processor.validate([votes[0].data, delegateRegs[0].data, signatures[0].data]);
+            await processor.validate([votes[0].data]);
+            processor = transactionPool.makeProcessor();
+            await processor.validate([delegateRegs[0].data]);
+            processor = transactionPool.makeProcessor();
+            await processor.validate([signatures[0].data]);

Later throwIfCannotBeApplied() is called from the pool's
Connection.addTransaction().
…hen-nonce-check

* ArkEcosystem/core/develop:
  ci: force exit tests if there are hanging promises
  ci: use different test coverage directories for each type (#2956)
@spkjp
Copy link
Contributor

spkjp commented Sep 23, 2019

We could also apply the transactions on a temporary wallet manager, but given that we already call apply (on the pool wallet manager) at the end of the processor, removing the check completely should also be fine.

@codecov-io
Copy link

codecov-io commented Sep 24, 2019

Codecov Report

❗ No coverage uploaded for pull request base (develop@1e37cdd). Click here to learn what that means.
The diff coverage is 85.71%.

Impacted file tree graph

@@            Coverage Diff             @@
##             develop    #2949   +/-   ##
==========================================
  Coverage           ?   68.39%           
==========================================
  Files              ?      405           
  Lines              ?     9827           
  Branches           ?      465           
==========================================
  Hits               ?     6721           
  Misses             ?     3070           
  Partials           ?       36
Impacted Files Coverage Δ
...ages/core-transactions/src/handlers/htlc-refund.ts 80.51% <100%> (ø)
...kages/core-transactions/src/handlers/htlc-claim.ts 81.39% <100%> (ø)
packages/core-transaction-pool/src/connection.ts 92.22% <100%> (ø)
packages/core-transaction-pool/src/processor.ts 81.33% <100%> (ø)
...ages/core-transactions/src/handlers/transaction.ts 68.33% <100%> (ø)
packages/core-state/src/wallets/wallet.ts 85.71% <50%> (ø)

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 1e37cdd...e5bc0b9. Read the comment docs.

vasild and others added 3 commits September 24, 2019 10:54
…hen-nonce-check

* ArkEcosystem/core/develop:
  test(e2e): No need to run as root the chmod command when modifying own's files (#2958)
…hen-nonce-check

* ArkEcosystem/core/develop:
  chore: drop node 11 support (#2965)
@faustbrian faustbrian merged commit d7dbbec into develop Sep 24, 2019
@ghost ghost deleted the strengthen-nonce-check branch September 24, 2019 12:07
vasild added a commit that referenced this pull request Sep 24, 2019
…move-throwIfCannotBeApplied

* ArkEcosystem/core/develop:
  refactor: strengthen a nonce check in performGenericWalletChecks() (#2949)
  ci: setup github action workflow for e2e tests (#2964)
vasild added a commit that referenced this pull request Sep 24, 2019
…l-range-selection

* ArkEcosystem/core/develop:
  refactor: strengthen a nonce check in performGenericWalletChecks() (#2949)
  ci: setup github action workflow for e2e tests (#2964)
vasild added a commit that referenced this pull request Sep 27, 2019
…ts-nonce

* ArkEcosystem/core/develop:
  refactor(core-transaction-pool): don't accept expired v1 transactions (#2948)
  fix(core-snapshots): remove bogus skipRoundRows (#2973)
  feat(core-api): endpoints for locks/businesses/bridgechains (#2940)
  fix(core-blockchain): round deletion during rollback (#2970)
  fix: range selection in pool's getTransactions() (#2952)
  feat: expose `isValidPeer` via ajv format rule (#2960)
  chore: remove trailing whitespace (#2971)
  fix(core-transactions): update wallet nonce when applying v1 transaction (#2959)
  fix(core-blockchain): do not reset `noBlockCounter` when `downloadBlocks` succeeds (#2968)
  refactor(pool): remove unnecessary check from validateTransactions() (#2951)
  ci: temporarily disable pull_request.synchronize event (#2966)
  refactor: strengthen a nonce check in performGenericWalletChecks() (#2949)
  ci: setup github action workflow for e2e tests (#2964)
  chore: drop node 11 support (#2965)
  test(e2e): No need to run as root the chmod command when modifying own's files (#2958)
  refactor(crypto): change maximum recipients of multipayment via milestone (#2961)
  ci: split functional job into 1 job per type (#2963)
  refactor(core-p2p): make peer reply errors less verbose (#2962)
  fix(core-state): index recipient wallets during bootstrap (#2947)
  Tell git to ignore vim's temporary files (#2957)
  ci: force exit tests if there are hanging promises
  ci: use different test coverage directories for each type (#2956)
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.

4 participants