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

Transaction Consensus Rules #165

Merged
merged 1 commit into from
Jul 10, 2024

Conversation

jaoleal
Copy link
Contributor

@jaoleal jaoleal commented May 22, 2024

See #162 for context.
After a reading at crates/floresta-chain/, based on commenting and codebase...
Floresta, what concern the validation of Transactions, has

  • In block transaction structure validation. ( Verifies if has, at least, one transaction per block, if has coinbase, etc...)
  • Script validation.
  • PrevOut validation. (If has a valid PrevOut).

So, based on Core validation.cpp and @Davidson-Souza comment on #162

The lacking Rules are:

Values validation.

- Output value is equal or less than total input (Implemented, just needs tests)

- none value is negative

- Any value is not greater than the number in satoshis of the maximum possible bitcoins. (MAX_MONEY = 21000000 * 100 000 000)

- Double Input Spend. (The case that has more than one equal input, mostly referring to the same utxo)

- Inputs are spending UTXO.

Size validation

- Size, without witness, in vbytes has to be less than MAX_BLOCK_WEIGHT(?)

  • no more than 80000 sigops
    What is a sigop
    Their values
  • ScriptPubKey Size has to be >2 and <520 bytes
  • if coinbase, scriptSig has to be >2 and <100 bytes
  • Absolute and Relative Locktimes defined by sequence.

This list can and will change, lacking rules that got implemented will change to Rule
Draft will be ready after all Rules are properly tested.

So, for block validation we do have three points of validation
partial_chain.rs with PartialChainStateInner::process_block() function that delegates the block validation itself for PartialChainStateInner::validate_block(), which verifies:

  • merkle_root
  • witness_commitment
  • prev_block
  • transactions (with the validations that was described earlier)

The second point being the ChainState<PersistedState>::validate_block() that has the same functionality as the other validate_block() function just described above, and BlockchainInterface trait that has a unimplemented validate_block() function.

Since we have a Chainstate<PersistedState>::validate_header() we can consider that the known rules are implemented but not tested.

@jaoleal
Copy link
Contributor Author

jaoleal commented May 23, 2024

I think thats it for transactions

@Davidson-Souza
Copy link
Collaborator

I think you need to impose some constraints on the scriptSig sizes too. IIRC only taproot can have unlimited script sizes

@jaoleal jaoleal changed the title Transactions Consensus Rules Consensus Rules Jun 13, 2024
@jaoleal
Copy link
Contributor Author

jaoleal commented Jun 19, 2024

I`m including the nixify branch so i can already work with flakes

@jaoleal jaoleal force-pushed the ConsensusRules branch 3 times, most recently from 5fdc7a4 to f81f455 Compare June 22, 2024 17:45
Copy link
Contributor Author

@jaoleal jaoleal left a comment

Choose a reason for hiding this comment

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

@Davidson-Souza what do you think ?

crates/floresta-chain/src/pruned_utreexo/consensus.rs Outdated Show resolved Hide resolved
crates/floresta-chain/src/pruned_utreexo/consensus.rs Outdated Show resolved Hide resolved
crates/floresta-chain/src/pruned_utreexo/error.rs Outdated Show resolved Hide resolved
Copy link
Contributor Author

@jaoleal jaoleal left a comment

Choose a reason for hiding this comment

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

After refactoring to functions, the code improve and we can now follow to tests on transactions validations.

crates/floresta-chain/src/pruned_utreexo/consensus.rs Outdated Show resolved Hide resolved
crates/floresta-chain/src/pruned_utreexo/consensus.rs Outdated Show resolved Hide resolved
crates/floresta-chain/src/pruned_utreexo/consensus.rs Outdated Show resolved Hide resolved
@jaoleal
Copy link
Contributor Author

jaoleal commented Jul 2, 2024

Yeah, but the design of the code will not help in this case. Itll only fail, we will not know why and what triggered that. Not enough info, not good.

Done some unitary that cover the errors that the new functions has to output.
This should be it for transactions.
For sake of organization Ill rename the draft so we can merge it well.

A personally talk with @Davidson-Souza we established that the objective for block validation code is just some rewriting and implementation to the rest of the code. The first comment explains the actual state of block validation in Florestad.

So doing it in a separate PR right after this one merged.

Tagging @Davidson-Souza for a final review.

@jaoleal jaoleal changed the title Consensus Rules Transaction Consensus Rules Jul 2, 2024
@jaoleal
Copy link
Contributor Author

jaoleal commented Jul 2, 2024

Strange... Clippy is asking things out of the scope of this PR
maybe we consider a Issue to fix this

@Davidson-Souza
Copy link
Collaborator

Davidson-Souza commented Jul 2, 2024

Strange... Clippy is asking things out of the scope of this PR

The ones I'm seeing are due of an assert_eq! with unit type. Replace it with a assert!(something().is_ok())

@Davidson-Souza
Copy link
Collaborator

Also, could you please rebase and squash this?

@jaoleal jaoleal force-pushed the ConsensusRules branch 4 times, most recently from e6e56fe to 43dec4a Compare July 2, 2024 19:43
@jaoleal
Copy link
Contributor Author

jaoleal commented Jul 2, 2024

Done, Squashed and lint satisfied

@jaoleal jaoleal marked this pull request as ready for review July 2, 2024 19:51
@jaoleal
Copy link
Contributor Author

jaoleal commented Jul 2, 2024

Just two things that I need to expose.

About the bip34 rust-bitcoin implementation.
It automatically validates while calling is_coinbase().
checks bip34, the txid and retrieve the actual boolean about it.
so, no need to reimplement that.

And the constraint of having only 1 coinbase.
we do not deal with the case where we do not have any coinbase because itll automatically fails when calculating the subsidy.
And will fail when have more because of the simplest n == 0 before entering the check. An extra coinbase will fail when treated as a regular transaction.

@jaoleal
Copy link
Contributor Author

jaoleal commented Jul 9, 2024

Removed Sequence and time validation so we can implement it on #187 since we need some refactoring

@Davidson-Souza
Copy link
Collaborator

I've tried to run on the latest commit on mainnet, and I got an error:

[2024-07-09 14:23:46 ERROR floresta_wire::p2p_wire::sync_node] Invalid block Header { block_hash: 0x00000000000000000001711fd54f6864cb693f1f5f87f6a9007bf7a6c5dd30f7, version: Version(642318336), prev_blockhash: 0x00000000000000000002447c802760582b80022f937d754aece888ec9e96f212, merkle_root: 0x7a2b90440f925f9e32f6c6ddda47206adc0b29f7bb8fc5295de0ef95ab7d6bf5, time: 1719078530, bits: CompactTarget(386096421), nonce: 2054740483 } received by peer 0 reason: BlockValidation(InvalidTx("Some input's Scriptsig has more than 520 bytes on"))

But this block is part of the main chain

@jaoleal
Copy link
Contributor Author

jaoleal commented Jul 9, 2024

Hmmm, that's interesting... i need to make better error logs

Copy link
Contributor Author

@jaoleal jaoleal left a comment

Choose a reason for hiding this comment

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

Did not change anything of features or rules, but now we can know which transaction we are invalidating

@jaoleal jaoleal force-pushed the ConsensusRules branch 3 times, most recently from 9972fc8 to 9e0c144 Compare July 10, 2024 14:19
@Davidson-Souza Davidson-Souza merged commit f94aa6e into vinteumorg:master Jul 10, 2024
5 checks passed
@jaoleal jaoleal mentioned this pull request Jul 15, 2024
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.

3 participants