Skip to content

Commit

Permalink
Simpliy unit test for transaction chain integrity
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmanzanera committed Oct 17, 2023
1 parent 094995a commit 91a28dc
Showing 1 changed file with 112 additions and 123 deletions.
235 changes: 112 additions & 123 deletions lib/archethic/transaction_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -939,81 +939,80 @@ defmodule Archethic.TransactionChain do
With only one transaction
iex> [
...> %Transaction{
...> address: <<0, 0, 109, 140, 2, 60, 50, 109, 201, 126, 206, 164, 10, 86, 225, 58, 136, 241, 118, 74, 3, 215, 6, 106, 165, 24, 51, 192, 212, 58, 143, 33, 68, 2>>,
...> type: :transfer,
...> data: %TransactionData{},
...> previous_public_key:
...> <<0, 0, 221, 228, 196, 111, 16, 222, 0, 119, 32, 150, 228, 25, 206, 79, 37, 213, 8, 130, 22,
...> 212, 99, 55, 72, 11, 248, 250, 11, 140, 137, 167, 118, 253>>,
...> previous_signature:
...> <<232, 186, 237, 220, 71, 212, 177, 17, 156, 167, 145, 125, 92, 70, 213, 120, 216, 215,
...> 255, 158, 104, 117, 162, 18, 142, 75, 73, 205, 71, 7, 141, 90, 178, 239, 212, 227, 167,
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>,
...> origin_signature:
...> <<232, 186, 237, 220, 71, 212, 177, 17, 156, 167, 145, 125, 92, 70, 213, 120, 216, 215,
...> 255, 158, 104, 117, 162, 18, 142, 75, 73, 205, 71, 7, 141, 90, 178, 239, 212, 227, 167,
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>
...> }
...> ]
...> |> TransactionChain.proof_of_integrity()
# Hash of the transaction
<<0, 40, 125, 254, 9, 35, 184, 104, 179, 207, 31, 73, 219, 127, 73, 218, 195,
166, 44, 129, 115, 82, 170, 36, 101, 112, 100, 56, 248, 71, 191, 119, 88>>
iex> tx = %Transaction{
...> address: <<0, 0, 109, 140, 2, 60, 50, 109, 201, 126, 206, 164, 10, 86, 225, 58, 136, 241, 118, 74, 3, 215, 6, 106, 165, 24, 51, 192, 212, 58, 143, 33, 68, 2>>,
...> type: :transfer,
...> data: %TransactionData{},
...> previous_public_key:
...> <<0, 0, 221, 228, 196, 111, 16, 222, 0, 119, 32, 150, 228, 25, 206, 79, 37, 213, 8, 130, 22,
...> 212, 99, 55, 72, 11, 248, 250, 11, 140, 137, 167, 118, 253>>,
...> previous_signature:
...> <<232, 186, 237, 220, 71, 212, 177, 17, 156, 167, 145, 125, 92, 70, 213, 120, 216, 215,
...> 255, 158, 104, 117, 162, 18, 142, 75, 73, 205, 71, 7, 141, 90, 178, 239, 212, 227, 167,
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>,
...> origin_signature:
...> <<232, 186, 237, 220, 71, 212, 177, 17, 156, 167, 145, 125, 92, 70, 213, 120, 216, 215,
...> 255, 158, 104, 117, 162, 18, 142, 75, 73, 205, 71, 7, 141, 90, 178, 239, 212, 227, 167,
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>
...> }
iex> TransactionChain.proof_of_integrity([tx])
tx
|> Transaction.to_pending()
|> Transaction.serialize()
|> Crypto.hash()
With multiple transactions
iex> [
...> %Transaction{
...> address:
...> <<0, 0, 61, 7, 130, 64, 140, 226, 192, 8, 238, 88, 226, 106, 137, 45, 69, 113, 239,
...> 240, 45, 55, 225, 169, 170, 121, 238, 136, 192, 161, 252, 33, 71, 3>>,
...> type: :transfer,
...> data: %TransactionData{},
...> previous_public_key:
...> <<0, 0, 96, 233, 188, 240, 217, 251, 22, 2, 210, 59, 170, 25, 33, 61, 124, 135,
...> 138, 65, 189, 207, 253, 84, 254, 193, 42, 130, 170, 159, 34, 72, 52, 162>>,
...> previous_signature:
...> <<232, 186, 237, 220, 71, 212, 177, 17, 156, 167, 145, 125, 92, 70, 213, 120, 216, 215,
...> 255, 158, 104, 117, 162, 18, 142, 75, 73, 205, 71, 7, 141, 90, 178, 239, 212, 227, 167,
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>,
...> origin_signature:
...> <<232, 186, 237, 220, 71, 212, 177, 17, 156, 167, 145, 125, 92, 70, 213, 120, 216, 215,
...> 255, 158, 104, 117, 162, 18, 142, 75, 73, 205, 71, 7, 141, 90, 178, 239, 212, 227, 167,
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>
...> },
...> %Transaction{
...> address: <<0, 0, 109, 140, 2, 60, 50, 109, 201, 126, 206, 164, 10, 86, 225, 58, 136, 241, 118, 74, 3, 215, 6, 106, 165, 24, 51, 192, 212, 58, 143, 33, 68, 2>>,
...> type: :transfer,
...> data: %TransactionData{},
...> previous_public_key:
...> <<0, 0, 221, 228, 196, 111, 16, 222, 0, 119, 32, 150, 228, 25, 206, 79, 37, 213, 8, 130, 22,
...> 212, 99, 55, 72, 11, 248, 250, 11, 140, 137, 167, 118, 253>>,
...> previous_signature:
...> <<232, 186, 237, 220, 71, 212, 177, 17, 156, 167, 145, 125, 92, 70, 213, 120, 216, 215,
...> 255, 158, 104, 117, 162, 18, 142, 75, 73, 205, 71, 7, 141, 90, 178, 239, 212, 227, 167,
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>,
...> origin_signature:
...> <<232, 186, 237, 220, 71, 212, 177, 17, 156, 167, 145, 125, 92, 70, 213, 120, 216, 215,
...> 255, 158, 104, 117, 162, 18, 142, 75, 73, 205, 71, 7, 141, 90, 178, 239, 212, 227, 167,
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>,
...> validation_stamp: %ValidationStamp{
...> proof_of_integrity: <<0, 40, 125, 254, 9, 35, 184, 104, 179, 207, 31, 73, 219, 127, 73, 218, 195,
...> 166, 44, 129, 115, 82, 170, 36, 101, 112, 100, 56, 248, 71, 191, 119, 88>>
...> }
...> }
...> ]
...> |> TransactionChain.proof_of_integrity()
# Hash of the transaction + previous proof of integrity
<<0, 221, 242, 236, 164, 169, 60, 219, 181, 79, 26, 59, 12, 151, 246, 44, 75,
116, 163, 163, 100, 229, 11, 145, 166, 136, 232, 201, 101, 127, 227, 28, 107>>
iex> tx2 = %Transaction{
...> address:
...> <<0, 0, 61, 7, 130, 64, 140, 226, 192, 8, 238, 88, 226, 106, 137, 45, 69, 113, 239,
...> 240, 45, 55, 225, 169, 170, 121, 238, 136, 192, 161, 252, 33, 71, 3>>,
...> type: :transfer,
...> data: %TransactionData{},
...> previous_public_key:
...> <<0, 0, 96, 233, 188, 240, 217, 251, 22, 2, 210, 59, 170, 25, 33, 61, 124, 135,
...> 138, 65, 189, 207, 253, 84, 254, 193, 42, 130, 170, 159, 34, 72, 52, 162>>,
...> previous_signature:
...> <<232, 186, 237, 220, 71, 212, 177, 17, 156, 167, 145, 125, 92, 70, 213, 120, 216, 215,
...> 255, 158, 104, 117, 162, 18, 142, 75, 73, 205, 71, 7, 141, 90, 178, 239, 212, 227, 167,
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>,
...> origin_signature:
...> <<232, 186, 237, 220, 71, 212, 177, 17, 156, 167, 145, 125, 92, 70, 213, 120, 216, 215,
...> 255, 158, 104, 117, 162, 18, 142, 75, 73, 205, 71, 7, 141, 90, 178, 239, 212, 227, 167,
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>
...> }
iex> tx1 = %Transaction{
...> address: <<0, 0, 109, 140, 2, 60, 50, 109, 201, 126, 206, 164, 10, 86, 225, 58, 136, 241, 118, 74, 3, 215, 6, 106, 165, 24, 51, 192, 212, 58, 143, 33, 68, 2>>,
...> type: :transfer,
...> data: %TransactionData{},
...> previous_public_key:
...> <<0, 0, 221, 228, 196, 111, 16, 222, 0, 119, 32, 150, 228, 25, 206, 79, 37, 213, 8, 130, 22,
...> 212, 99, 55, 72, 11, 248, 250, 11, 140, 137, 167, 118, 253>>,
...> previous_signature:
...> <<232, 186, 237, 220, 71, 212, 177, 17, 156, 167, 145, 125, 92, 70, 213, 120, 216, 215,
...> 255, 158, 104, 117, 162, 18, 142, 75, 73, 205, 71, 7, 141, 90, 178, 239, 212, 227, 167,
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>,
...> origin_signature:
...> <<232, 186, 237, 220, 71, 212, 177, 17, 156, 167, 145, 125, 92, 70, 213, 120, 216, 215,
...> 255, 158, 104, 117, 162, 18, 142, 75, 73, 205, 71, 7, 141, 90, 178, 239, 212, 227, 167,
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>,
...> validation_stamp: %ValidationStamp{
...> proof_of_integrity: <<0, 25, 23, 234, 157, 43, 41, 235, 17, 96, 140, 19, 255, 72, 190, 165, 57, 199,
...> 167, 167, 195, 8, 59, 230, 229, 246, 12, 191, 68, 203, 99, 11, 176>>
...> }
...> }
iex> TransactionChain.proof_of_integrity([tx2, tx1])
[
TransactionChain.proof_of_integrity([tx2]),
tx1.validation_stamp.proof_of_integrity
]
|> Crypto.hash()
"""
@spec proof_of_integrity(nonempty_list(Transaction.t())) :: binary()
def proof_of_integrity([
Expand All @@ -1038,57 +1037,47 @@ defmodule Archethic.TransactionChain do
## Examples
iex> [
...> %Transaction{
...> address:
...> <<0, 0, 61, 7, 130, 64, 140, 226, 192, 8, 238, 88, 226, 106, 137, 45, 69, 113, 239,
...> 240, 45, 55, 225, 169, 170, 121, 238, 136, 192, 161, 252, 33, 71, 3>>,
...> type: :transfer,
...> data: %TransactionData{},
...> previous_public_key:
...> <<0, 0, 96, 233, 188, 240, 217, 251, 22, 2, 210, 59, 170, 25, 33, 61, 124, 135,
...> 138, 65, 189, 207, 253, 84, 254, 193, 42, 130, 170, 159, 34, 72, 52, 162>>,
...> previous_signature:
...> <<232, 186, 237, 220, 71, 212, 177, 17, 156, 167, 145, 125, 92, 70, 213, 120, 216, 215,
...> 255, 158, 104, 117, 162, 18, 142, 75, 73, 205, 71, 7, 141, 90, 178, 239, 212, 227, 167,
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>,
...> origin_signature:
...> <<232, 186, 237, 220, 71, 212, 177, 17, 156, 167, 145, 125, 92, 70, 213, 120, 216, 215,
...> 255, 158, 104, 117, 162, 18, 142, 75, 73, 205, 71, 7, 141, 90, 178, 239, 212, 227, 167,
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>,
...> validation_stamp: %ValidationStamp{
...> timestamp: ~U[2020-03-30 12:06:30.000Z],
...> proof_of_integrity: <<0, 221, 242, 236, 164, 169, 60, 219, 181, 79, 26, 59, 12, 151, 246, 44, 75,
...> 116, 163, 163, 100, 229, 11, 145, 166, 136, 232, 201, 101, 127, 227, 28, 107>>
...> }
...> },
...> %Transaction{
...> address: <<0, 0, 109, 140, 2, 60, 50, 109, 201, 126, 206, 164, 10, 86, 225, 58, 136, 241, 118, 74, 3, 215, 6, 106, 165, 24, 51, 192, 212, 58, 143, 33, 68, 2>>,
...> type: :transfer,
...> data: %TransactionData{},
...> previous_public_key:
...> <<0, 0, 221, 228, 196, 111, 16, 222, 0, 119, 32, 150, 228, 25, 206, 79, 37, 213, 8, 130, 22,
...> 212, 99, 55, 72, 11, 248, 250, 11, 140, 137, 167, 118, 253>>,
...> previous_signature:
...> <<232, 186, 237, 220, 71, 212, 177, 17, 156, 167, 145, 125, 92, 70, 213, 120, 216, 215,
...> 255, 158, 104, 117, 162, 18, 142, 75, 73, 205, 71, 7, 141, 90, 178, 239, 212, 227, 167,
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>,
...> origin_signature:
...> <<232, 186, 237, 220, 71, 212, 177, 17, 156, 167, 145, 125, 92, 70, 213, 120, 216, 215,
...> 255, 158, 104, 117, 162, 18, 142, 75, 73, 205, 71, 7, 141, 90, 178, 239, 212, 227, 167,
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>,
...> validation_stamp: %ValidationStamp{
...> timestamp: ~U[2020-03-30 10:06:30.000Z],
...> proof_of_integrity: <<0, 40, 125, 254, 9, 35, 184, 104, 179, 207, 31, 73, 219, 127, 73, 218, 195,
...> 166, 44, 129, 115, 82, 170, 36, 101, 112, 100, 56, 248, 71, 191, 119, 88>>
...> }
...> }
...> ]
...> |> TransactionChain.valid?()
iex> tx2 = %Transaction{
...> address:
...> <<0, 0, 61, 7, 130, 64, 140, 226, 192, 8, 238, 88, 226, 106, 137, 45, 69, 113, 239,
...> 240, 45, 55, 225, 169, 170, 121, 238, 136, 192, 161, 252, 33, 71, 3>>,
...> type: :transfer,
...> data: %TransactionData{},
...> previous_public_key:
...> <<0, 0, 96, 233, 188, 240, 217, 251, 22, 2, 210, 59, 170, 25, 33, 61, 124, 135,
...> 138, 65, 189, 207, 253, 84, 254, 193, 42, 130, 170, 159, 34, 72, 52, 162>>,
...> previous_signature:
...> <<232, 186, 237, 220, 71, 212, 177, 17, 156, 167, 145, 125, 92, 70, 213, 120, 216, 215,
...> 255, 158, 104, 117, 162, 18, 142, 75, 73, 205, 71, 7, 141, 90, 178, 239, 212, 227, 167,
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>,
...> origin_signature:
...> <<232, 186, 237, 220, 71, 212, 177, 17, 156, 167, 145, 125, 92, 70, 213, 120, 216, 215,
...> 255, 158, 104, 117, 162, 18, 142, 75, 73, 205, 71, 7, 141, 90, 178, 239, 212, 227, 167,
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>
...> }
iex> tx1 = %Transaction{
...> address: <<0, 0, 109, 140, 2, 60, 50, 109, 201, 126, 206, 164, 10, 86, 225, 58, 136, 241, 118, 74, 3, 215, 6, 106, 165, 24, 51, 192, 212, 58, 143, 33, 68, 2>>,
...> type: :transfer,
...> data: %TransactionData{},
...> previous_public_key:
...> <<0, 0, 221, 228, 196, 111, 16, 222, 0, 119, 32, 150, 228, 25, 206, 79, 37, 213, 8, 130, 22,
...> 212, 99, 55, 72, 11, 248, 250, 11, 140, 137, 167, 118, 253>>,
...> previous_signature:
...> <<232, 186, 237, 220, 71, 212, 177, 17, 156, 167, 145, 125, 92, 70, 213, 120, 216, 215,
...> 255, 158, 104, 117, 162, 18, 142, 75, 73, 205, 71, 7, 141, 90, 178, 239, 212, 227, 167,
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>,
...> origin_signature:
...> <<232, 186, 237, 220, 71, 212, 177, 17, 156, 167, 145, 125, 92, 70, 213, 120, 216, 215,
...> 255, 158, 104, 117, 162, 18, 142, 75, 73, 205, 71, 7, 141, 90, 178, 239, 212, 227, 167,
...> 161, 155, 143, 43, 50, 6, 7, 97, 130, 134, 174, 7, 235, 183, 88, 165, 197, 25, 219, 84,
...> 232, 135, 42, 112, 58, 181, 13>>
...> }
iex> tx1 = %{ tx1 | validation_stamp: %ValidationStamp{ proof_of_integrity: TransactionChain.proof_of_integrity([tx1]), timestamp: ~U[2022-09-10 10:00:00Z] }}
iex> tx2 = %{ tx2 | validation_stamp: %ValidationStamp{ proof_of_integrity: TransactionChain.proof_of_integrity([tx2, tx1]), timestamp: ~U[2022-12-10 10:00:00Z] }}
iex> TransactionChain.valid?([tx2, tx1])
true
"""
Expand Down

0 comments on commit 91a28dc

Please sign in to comment.