Skip to content

Commit

Permalink
add transaction weight calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
Boog900 committed Jan 10, 2024
1 parent 7aa4e46 commit af2e80c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
54 changes: 51 additions & 3 deletions src/consensus_rules/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ This chapter does not include miner, coinbase, transactions as they are handled

## Index

1. [Rules That Apply To All Versions](./transactions.md#rules-that-apply-to-all-versions)
2. [Rules Specific to Version 1](./transactions/ring_signatures.md)
1. [Miscellaneous Rules](#miscellaneous-rules)
2. [Input Rules](./transactions/inputs.md)
3. [Output Rules](./transactions/outputs.md)
4. [Unlock Time](./transactions/unlock_time.md)
5. [Ring Signatures](./transactions/ring_signatures.md)
6. [RingCT](./transactions/ring_ct.md)

## Rules That Apply To All Versions
## Miscellaneous Rules

### Version

Expand All @@ -27,6 +31,36 @@ The size of the `transaction blob` must not be bigger than 1 million bytes[^tx-s

From v8 the transactions _weight_ must not be bigger than half of the [block penalty free zone](./blocks/weights.md#penalty-free-zone) minus 600[^tx-weight_limit].

#### Calculating Transaction Weight

For all transactions that don't use bulletproofs or bulletproofs+ the weight is just the length of the transaction blob.[^weight-pre-bp]

For bulletproofs(+) transactions we add a "clawback" onto the transaction.

To calculate the "clawback" we fist define a `bpBase` which is the size of a 2 output proof, normalized to 1 proof by dividing by 2[^bp-base]:

for bulletproofs: \\(fields = 9\\)

for bulletproofs+: \\(fields = 6\\)

\\(bpBase = \frac{(32 * (fields + 7 * 2))}{2}\\)

Next we calculate the size of the bulletproofs(+) field by first getting the first power of 2 above or equal to the number of outputs: `firstPower2AboveNumbOuts`.

If `firstPower2AboveNumbOuts` is <= 2 then the \\(clawback = 0\\)[^fp2-less-than-2].

Next define the number of L and R elements[^lr-elements]: \\(nlr = firstPower2AboveNumbOuts + 6\\)

now the size of the bulletproofs(+) field is[^bp+-size]:

\\(bpSize = 32 * (fields + 2 * nlr)\\)

now the `clawback` is[^clawback]:

\\( clawback = \frac{(bpBase * firstPower2AboveNumbOuts - bpSize) * 4}{ 5} \\)

To get the transaction weight now you just get the length of the transaction blob and add this `clawback`[^bp-tx-weight].

---

[^tx-v0]: <https://github.com/monero-project/monero/blob/eac1b86bb2818ac552457380c9dd421fb8935e5b/src/cryptonote_core/tx_pool.cpp#L152>
Expand All @@ -39,3 +73,17 @@ From v8 the transactions _weight_ must not be bigger than half of the [block pen
and <https://github.com/monero-project/monero/blob/eac1b86bb2818ac552457380c9dd421fb8935e5b/src/cryptonote_basic/cryptonote_basic_impl.cpp#L78>

[^tx-weight_limit]: <https://github.com/monero-project/monero/blob/eac1b86bb2818ac552457380c9dd421fb8935e5b/src/cryptonote_core/tx_pool.cpp#L117> && <https://github.com/monero-project/monero/blob/eac1b86bb2818ac552457380c9dd421fb8935e5b/src/cryptonote_core/tx_pool.cpp#L221>

[^weight-pre-bp]: <https://github.com/monero-project/monero/blob/ac02af92867590ca80b2779a7bbeafa99ff94dcb/src/cryptonote_basic/cryptonote_format_utils.cpp#L447-L453>

[^bp-base]: <https://github.com/monero-project/monero/blob/ac02af92867590ca80b2779a7bbeafa99ff94dcb/src/cryptonote_basic/cryptonote_format_utils.cpp#L110C40-L110C40>

[^fp2-less-than-2]: <https://github.com/monero-project/monero/blob/ac02af92867590ca80b2779a7bbeafa99ff94dcb/src/cryptonote_basic/cryptonote_format_utils.cpp#L112>

[^lr-elements]: <https://github.com/monero-project/monero/blob/ac02af92867590ca80b2779a7bbeafa99ff94dcb/src/cryptonote_basic/cryptonote_format_utils.cpp#L117>

[^bp+-size]: <https://github.com/monero-project/monero/blob/ac02af92867590ca80b2779a7bbeafa99ff94dcb/src/cryptonote_basic/cryptonote_format_utils.cpp#L118>

[^clawback]: <https://github.com/monero-project/monero/blob/ac02af92867590ca80b2779a7bbeafa99ff94dcb/src/cryptonote_basic/cryptonote_format_utils.cpp#L122>

[^bp-tx-weight]: <https://github.com/monero-project/monero/blob/ac02af92867590ca80b2779a7bbeafa99ff94dcb/src/cryptonote_basic/cryptonote_format_utils.cpp#L457>
4 changes: 2 additions & 2 deletions src/consensus_rules/transactions/ring_ct.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ were not being checked for new fork rules they are:

### OutPKs Valid Points

All outPKs must be valid points[^outPKs-valid-points].
All outPKs must be conically encoded points[^outPKs-valid-points].

## Simple Types Rules

Expand All @@ -56,7 +56,7 @@ These rules apply to all RCT "simple" types, which are all except type "FULL".

This rule applies to the pseudo-outs, from type 3 (Bulletproof) the pseudo-outs field moved to the prunable RCT section from the non-prunable section.

The pseudo-outs must all be valid points[^pseudo-outs-valid-points].
The pseudo-outs must all be conically encoded points[^pseudo-outs-valid-points].

### Pseudo-outs OutPKs Balance

Expand Down

0 comments on commit af2e80c

Please sign in to comment.