Skip to content

Commit

Permalink
fix serialization issue
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmanzanera committed Dec 18, 2024
1 parent 92dd4fb commit 637d900
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 26 deletions.
14 changes: 12 additions & 2 deletions lib/archethic/contracts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -400,16 +400,26 @@ defmodule Archethic.Contracts do
{:error, raise_to_failure(err, stacktrace)}
end

defp cast_trigger_result({:error, reason}, _, _) do
defp cast_trigger_result({:error, reason}, _, _) when is_binary(reason) do
{:error,
%Failure{
error: "invalid execution",
error: :execution_raise,
user_friendly_error: reason,
stacktrace: [],
logs: []
}}
end

defp cast_trigger_result({:error, %{"message" => message}}, _, _) do
{:error,
%Failure{
error: :contract_throw,
user_friendly_error: message,
stacktrace: [],
logs: []
}}
end

# No output transaction, no state update
defp cast_valid_trigger_result({:ok, nil, next_state, logs}, previous_state, _, encoded_state)
when next_state == previous_state do
Expand Down
43 changes: 26 additions & 17 deletions lib/archethic/transaction_chain/transaction/data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,17 @@ defmodule Archethic.TransactionChain.TransactionData do
end

smart_contract_binary =
if tx_version >= 4 do
if contract != nil do
<<byte_size(contract.bytecode)::32, contract.bytecode::binary,
TypedEncoding.serialize(contract.manifest, mode)::bitstring>>
else
<<0::32>>
end
else
if tx_version <= 3 do
<<byte_size(code)::32, code::binary>>
else
case contract do
nil ->
<<byte_size(code)::32, code::binary, 0::32>>

%{bytecode: bytecode, manifest: manifest} ->
<<0::32, byte_size(bytecode)::32, bytecode::binary,
TypedEncoding.serialize(manifest, mode)::bitstring>>
end
end

<<smart_contract_binary::bitstring, byte_size(content)::32, content::binary,
Expand Down Expand Up @@ -160,23 +162,30 @@ defmodule Archethic.TransactionChain.TransactionData do
{%{tx_data | code: code}, rest}
end

def deserialize(<<0::32, rest::bitstring>>, tx_version, serialization_mode),
do: do_deserialize(rest, tx_version, serialization_mode)

def deserialize(
<<bytecode_size::32, bytecode::binary-size(bytecode_size), rest::bitstring>>,
<<code_size::32, code::binary-size(code_size), bytecode_size::32,
bytecode::binary-size(bytecode_size), rest::bitstring>>,
tx_version,
serialization_mode
) do
{manifest, rest} = TypedEncoding.deserialize(rest, serialization_mode)
{contract, rest} =
if bytecode_size > 0 do
{manifest, rest} = TypedEncoding.deserialize(rest, serialization_mode)

{
%{bytecode: bytecode, manifest: manifest},
rest
}
else
{nil, rest}
end

{tx_data, rest} = do_deserialize(rest, tx_version, serialization_mode)

{%{
tx_data
| contract: %{
bytecode: bytecode,
manifest: manifest
}
| contract: contract,
code: decompress_code(code)
}, rest}
end

Expand Down
13 changes: 8 additions & 5 deletions test/archethic/transaction_chain/transaction_data_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,13 @@ defmodule Archethic.TransactionChain.TransactionDataTest do

defp gen_contract() do
gen all(
bytecode <- StreamData.binary(min_length: 1),
functions <- StreamData.list_of(gen_contract_manifest_function()),
bytecode <- StreamData.binary(min_length: 1, max_length: 2_000),
functions <- StreamData.list_of(gen_contract_manifest_function(), max_length: 5),
state <-
StreamData.map_of(
StreamData.string(:alphanumeric),
StreamData.one_of([StreamData.constant("u32"), StreamData.constant("string")])
StreamData.one_of([StreamData.constant("u32"), StreamData.constant("string")]),
max_length: 5
)
) do
%{
Expand All @@ -201,12 +202,14 @@ defmodule Archethic.TransactionChain.TransactionDataTest do
input <-
StreamData.map_of(
StreamData.string(:alphanumeric),
StreamData.one_of([StreamData.constant("u32"), StreamData.constant("string")])
StreamData.one_of([StreamData.constant("u32"), StreamData.constant("string")]),
max_length: 3
),
output <-
StreamData.map_of(
StreamData.string(:alphanumeric),
StreamData.one_of([StreamData.constant("u32"), StreamData.constant("string")])
StreamData.one_of([StreamData.constant("u32"), StreamData.constant("string")]),
length: 1
),
type <-
StreamData.one_of([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ defmodule ArchethicWeb.API.JsonRPC.Methods.EstimateTransactionFeeTest do

assert {:ok,
%{
"fee" => 5_000_080,
"fee" => 5_000_100,
"rates" => %{
"eur" => 0.2,
"usd" => 0.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ defmodule ArchethicWeb.API.REST.TransactionControllerTest do
})

assert %{
"fee" => 6_500_289,
"fee" => 6_500_309,
"rates" => %{
"eur" => 0.2,
"usd" => 0.2
Expand Down

0 comments on commit 637d900

Please sign in to comment.