From 5e9e6907e2fec383e26d95fa89d7d017e726e36a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Uster?= Date: Mon, 19 Jun 2023 15:55:56 +0000 Subject: [PATCH 1/2] Add get_transaction_fee to playbook --- lib/archethic/utils/regression/playbook.ex | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/lib/archethic/utils/regression/playbook.ex b/lib/archethic/utils/regression/playbook.ex index a944285e5..ce414fd02 100644 --- a/lib/archethic/utils/regression/playbook.ex +++ b/lib/archethic/utils/regression/playbook.ex @@ -135,6 +135,55 @@ defmodule Archethic.Utils.Regression.Playbook do end end + def get_transaction_fee( + transaction_seed, + tx_type, + transaction_data = %TransactionData{}, + host, + port, + curve \\ Crypto.default_curve(), + proto \\ :http + ) do + chain_length = get_chain_size(transaction_seed, curve, host, port) + + {previous_public_key, previous_private_key} = + Crypto.derive_keypair(transaction_seed, chain_length, curve) + + {next_public_key, _} = Crypto.derive_keypair(transaction_seed, chain_length + 1, curve) + + genesis_origin_private_key = get_origin_private_key(host, port, proto) + + tx = + %Transaction{ + address: Crypto.derive_address(next_public_key), + type: tx_type, + data: transaction_data, + previous_public_key: previous_public_key + } + |> Transaction.previous_sign_transaction_with_key(previous_private_key) + |> Transaction.origin_sign_transaction(genesis_origin_private_key) + + true = + Crypto.verify?( + tx.previous_signature, + Transaction.extract_for_previous_signature(tx) |> Transaction.serialize(), + tx.previous_public_key + ) + + case WebClient.with_connection( + host, + port, + &WebClient.json(&1, "/api/transaction_fee", tx_to_json(tx)), + proto + ) do + {:ok, _transaction_fee} = transaction_fee -> + transaction_fee + + _ -> + :error + end + end + defp get_origin_private_key(host, port, proto) do body = %{ "origin_public_key" => Base.encode16(@genesis_origin_public_key) From 40ec1f974a2c08ac66f7ceb77329f34ba8f5b381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Uster?= Date: Tue, 20 Jun 2023 11:30:52 +0000 Subject: [PATCH 2/2] properly return the error --- lib/archethic/utils/regression/playbook.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/archethic/utils/regression/playbook.ex b/lib/archethic/utils/regression/playbook.ex index ce414fd02..4bc328895 100644 --- a/lib/archethic/utils/regression/playbook.ex +++ b/lib/archethic/utils/regression/playbook.ex @@ -144,7 +144,7 @@ defmodule Archethic.Utils.Regression.Playbook do curve \\ Crypto.default_curve(), proto \\ :http ) do - chain_length = get_chain_size(transaction_seed, curve, host, port) + chain_length = get_chain_size(transaction_seed, curve, host, port, proto) {previous_public_key, previous_private_key} = Crypto.derive_keypair(transaction_seed, chain_length, curve) @@ -179,8 +179,8 @@ defmodule Archethic.Utils.Regression.Playbook do {:ok, _transaction_fee} = transaction_fee -> transaction_fee - _ -> - :error + error -> + error end end