Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Verify transaction against its block during import #10954

Merged
merged 4 commits into from
Aug 15, 2019
Merged

Conversation

grbIzl
Copy link
Collaborator

@grbIzl grbIzl commented Aug 7, 2019

Currently transaction's basic verification is not performed during its import from the pool (transaction's verify_basic method is not called).
As a result, incorrect transaction (for example, with incorrect chain id) can be added into the block and mined. Such blocks won't be accepted by other nodes (this transaction's verification will be called for the new block on other nodes). But incorrect block will stay on originator's node.

Solution: perform engine's verify_transaction_basic before adding transaction to the block during import (this will call transaction's verify_basic).

Drawback: this additional verification will increase import's time.

Closes #10411

@grbIzl grbIzl added M4-core ⛓ Core client code / Rust. A0-pleasereview 🤓 Pull request needs code review. labels Aug 7, 2019
@dvdplm
Copy link
Collaborator

dvdplm commented Aug 7, 2019

Have you measured the increased import time with this change?

@dvdplm dvdplm requested a review from tomusdrw August 7, 2019 17:05
Copy link
Collaborator

@tomusdrw tomusdrw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think basic verification should rather be performed before verify_signed.
Also I would like to avoid using engine directly and rather have client.verify_for_pending_block(&transaction, &open_block.header) to keep the logic contained.

@grbIzl
Copy link
Collaborator Author

grbIzl commented Aug 8, 2019

Have you measured the increased import time with this change?

With the fix:
Total pushed 1713 transactions in 354 ms
Avg transaction's import time: 0.206 ms

Without fix:
Total pushed 1636 transactions in 320 ms
Avg transaction's import time: 0.196 ms

Only the time of the transactions' import to the block was measured.

@grbIzl grbIzl requested a review from tomusdrw August 8, 2019 12:10
@dvdplm
Copy link
Collaborator

dvdplm commented Aug 8, 2019

With the fix:
Total pushed 1713 transactions in 354 ms
Avg transaction's import time: 0.206 ms

Without fix:
Total pushed 1636 transactions in 320 ms
Avg transaction's import time: 0.196 ms

Are they different transactions? If yes, can you measure with the same txs?

@grbIzl
Copy link
Collaborator Author

grbIzl commented Aug 8, 2019

Are they different transactions? If yes, can you measure with the same txs?

No, it was actually the same tx sent > 1000 times in the loop.

curl --data '{"jsonrpc":"2.0","method":"personal_sendTransaction","params":[{"from":"0x004ec07d2329997267Ec62b4166639513386F32E","to":"0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e","value":"0xde0b6b3a7640000"}, "user"],"id":0}' -H "Content-Type: application/json" -X POST localhost:8540

Copy link
Collaborator

@tomusdrw tomusdrw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but I think we can simplify this even further.


/// Verifies transaction against its block (before its import into this block)
pub fn verify_for_pending_block(&self, transaction: &UnverifiedTransaction, header: &Header) -> Result<(), transaction::Error> {
self.engine.machine().verify_transaction_basic(transaction, header)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not call verify_transaction here as well (and verify against header not best_block_header)? So that we can actually simplify the miners code and perform only one check.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't understand your suggestion. Which verify_transaction method do you mean? verify_transaction from machine doesn't call required verification methods. verify_transaction from pool client doesn't check against custom header, only for best block's

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sorry. I meant:

	/// Verifies transaction against provided pending block header.
	/// 
	/// The check is performed before the transaction is imported into this block.
	/// We also check any conditions that rely on chain status.
	/// If you want to verify against chain's best block use `verify_signed`.
	pub fn verify_for_pending_block(&self, transaction: &SignedTransaction, header: &Header) -> Result<(), transaction::Error> {
		self.engine.machine().verify_transaction_basic(transaction, header)?;
		self.engine.machine().verify_transaction(transaction, header, self.chain)?;
        Ok(())
}

So that client.verify_for_pending_block is the only method we call in the miner code:

let result = client.verify_for_pending_block(&transaction, &open_block.header)
				.map_err(|e| e.into())

If verify_signed is then not used anywhere after this change we should remove it.

My point is to have one single "verify" for the mining code exposed by PoolClient abstraction, so that you don't need to know details about basic/signed verification and the order of checks performed (they can stay encapsulated in PoolClient).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Got it.


self.verify_signed(&tx)?;

self.verify_for_pending_block(&tx, &self.best_block_header)?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, unfortunately we can't change the order here. basic needs to be performed before verify_unordered, otherwise we are risking a DoS attack vector.

Maybe just use engine.machine() methods here directly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

@ordian ordian added this to the 2.7 milestone Aug 13, 2019
@ordian ordian added B0-patch-stable 🕷 Pull request should also be back-ported to the stable branch. B1-patch-beta 🕷🕷 labels Aug 13, 2019
@ordian ordian merged commit fd75491 into master Aug 15, 2019
@ordian ordian deleted the CheckChainId branch August 15, 2019 14:10
@ordian ordian added A8-looksgood 🦄 Pull request is reviewed well. and removed A0-pleasereview 🤓 Pull request needs code review. labels Aug 15, 2019
ordian added a commit that referenced this pull request Aug 15, 2019
* master:
  Verify transaction against its block during import (#10954)
  [evmbin] fix compilation (#10976)
  Update to latest trie version. (#10972)
  [blooms-db] Fix benchmarks (#10974)
  Fix ethcore/benches build. (#10964)
  tx-pool: accept local tx with higher gas price when pool full (#10901)
  Disable unsyncable expanse chain (#10926)
dvdplm added a commit that referenced this pull request Aug 15, 2019
* master:
  Extract the Engine trait (#10958)
  Better error message for rpc gas price errors (#10931)
  [.gitlab.yml] cargo check ethcore benches (#10965)
  Verify transaction against its block during import (#10954)
  [evmbin] fix compilation (#10976)
  Update to latest trie version. (#10972)
  [blooms-db] Fix benchmarks (#10974)
  Fix ethcore/benches build. (#10964)
  tx-pool: accept local tx with higher gas price when pool full (#10901)
  Disable unsyncable expanse chain (#10926)
s3krit pushed a commit that referenced this pull request Sep 11, 2019
* Verify transaction against its block during import

* Client method for transaction verification added

* Verification methods united

* Verification sequence for transaction verifier returned
s3krit pushed a commit that referenced this pull request Sep 11, 2019
* Verify transaction against its block during import

* Client method for transaction verification added

* Verification methods united

* Verification sequence for transaction verifier returned
This was referenced Sep 12, 2019
s3krit added a commit that referenced this pull request Sep 12, 2019
* add more tx tests (#11038)
* Fix parallel transactions race-condition (#10995)
* Add blake2_f precompile (#11017)
* [trace] introduce trace failed to Ext (#11019)
* Edit publish-onchain.sh to use https (#11016)
* Fix deadlock in network-devp2p (#11013)
* EIP 1108: Reduce alt_bn128 precompile gas costs (#11008)
* xDai chain support and nodes list update (#10989)
* EIP 2028: transaction gas lowered from 68 to 16 (#10987)
* EIP-1344 Add CHAINID op-code (#10983)
* manual publish jobs for releases, no changes for nightlies (#10977)
* [blooms-db] Fix benchmarks (#10974)
* Verify transaction against its block during import (#10954)
* Better error message for rpc gas price errors (#10931)
* Fix fork choice (#10837)
* Fix compilation on recent nightlies (#10991)
s3krit added a commit that referenced this pull request Sep 12, 2019
* add more tx tests (#11038)
* Fix parallel transactions race-condition (#10995)
* Add blake2_f precompile (#11017)
* [trace] introduce trace failed to Ext (#11019)
* Edit publish-onchain.sh to use https (#11016)
* Fix deadlock in network-devp2p (#11013)
* EIP 1108: Reduce alt_bn128 precompile gas costs (#11008)
* xDai chain support and nodes list update (#10989)
* EIP 2028: transaction gas lowered from 68 to 16 (#10987)
* EIP-1344 Add CHAINID op-code (#10983)
* manual publish jobs for releases, no changes for nightlies (#10977)
* [blooms-db] Fix benchmarks (#10974)
* Verify transaction against its block during import (#10954)
* Better error message for rpc gas price errors (#10931)
* tx-pool: accept local tx with higher gas price when pool full (#10901)
* Fix fork choice (#10837)
* Cleanup unused vm dependencies (#10787)
* Fix compilation on recent nightlies (#10991)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A8-looksgood 🦄 Pull request is reviewed well. B0-patch-stable 🕷 Pull request should also be back-ported to the stable branch. M4-core ⛓ Core client code / Rust.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

POA: chain id is not verified during transition submit
4 participants