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

Add previous transaction address in GraphQl transaction schema #806

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/archethic/transaction_chain/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ defmodule Archethic.TransactionChain.Transaction do
type: Atom.to_string(tx.type),
data: TransactionData.to_map(tx.data),
previous_public_key: tx.previous_public_key,
previous_address: previous_address(tx),
previous_signature: tx.previous_signature,
origin_signature: tx.origin_signature,
validation_stamp: ValidationStamp.to_map(tx.validation_stamp),
Expand Down
1 change: 1 addition & 0 deletions lib/archethic_web/graphql_schema/transaction_type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ defmodule ArchethicWeb.GraphQLSchema.TransactionType do
field(:data, :data)
field(:previous_public_key, :public_key)
field(:previous_signature, :hex)
field(:previous_address, :address)
field(:origin_signature, :hex)
field(:validation_stamp, :validation_stamp)
field(:cross_validation_stamps, list_of(:cross_validation_stamp))
Expand Down
44 changes: 39 additions & 5 deletions test/archethic_web/graphql_schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,38 @@ defmodule ArchethicWeb.GraphQLSchemaTest do
assert %{"errors" => [%{"message" => "transaction_not_exists"}]} = json_response(conn, 200)
end

test "should the transaction with the requested fields", %{conn: conn} do
test "should return the transaction with the requested fields", %{conn: conn} do
addr = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>> |> Base.encode16()
prev_public_key = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>

MockClient
|> stub(:send_message, fn _, %GetTransaction{}, _ ->
{:ok, %Transaction{address: addr, type: :transfer, data: %TransactionData{}}}
{:ok,
%Transaction{
address: addr,
previous_public_key: prev_public_key,
type: :transfer,
data: %TransactionData{}
}}
end)

conn =
post(conn, "/api", %{
"query" => "query { transaction(address: \"#{addr}\") { address } }"
"query" => "query { transaction(address: \"#{addr}\") { address, previousAddress } }"
})

assert %{"data" => %{"transaction" => %{"address" => address}}} = json_response(conn, 200)
assert %{
"data" => %{
"transaction" => %{
"address" => address,
"previousAddress" => previous_address
}
}
} = json_response(conn, 200)

assert addr == Base.decode16!(address, case: :mixed)

assert Base.decode16!(previous_address) == Crypto.derive_address(prev_public_key)
end
end

Expand All @@ -114,7 +131,12 @@ defmodule ArchethicWeb.GraphQLSchemaTest do
{:ok, %LastTransactionAddress{address: last_address}}

_, %GetTransaction{address: ^last_address}, _ ->
{:ok, %Transaction{address: last_address, type: :transfer}}
{:ok,
%Transaction{
previous_public_key: first_addr,
address: last_address,
type: :transfer
}}
end)

conn =
Expand Down Expand Up @@ -225,16 +247,20 @@ defmodule ArchethicWeb.GraphQLSchemaTest do
|> stub(:list_transactions, fn _ ->
addr1 = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>
addr2 = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>
prev_addr1 = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>
prev_addr2 = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>

[
%Transaction{
address: addr1,
type: :transfer,
previous_public_key: prev_addr1,
data: %TransactionData{}
},
%Transaction{
address: addr2,
type: :transfer,
previous_public_key: prev_addr2,
data: %TransactionData{}
}
]
Expand All @@ -254,9 +280,11 @@ defmodule ArchethicWeb.GraphQLSchemaTest do
|> stub(:list_transactions, fn _ ->
Enum.map(1..20, fn _ ->
addr = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>
prev_addr = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>

%Transaction{
address: addr,
previous_public_key: prev_addr,
type: :transfer,
data: %TransactionData{}
}
Expand All @@ -281,11 +309,13 @@ defmodule ArchethicWeb.GraphQLSchemaTest do
transactions = [
%Transaction{
address: first,
previous_public_key: last,
type: :transfer,
data: %TransactionData{}
},
%Transaction{
address: last,
previous_public_key: last,
type: :hosting,
data: %TransactionData{}
}
Expand Down Expand Up @@ -328,10 +358,12 @@ defmodule ArchethicWeb.GraphQLSchemaTest do
transactions =
Enum.map(1..20, fn _ ->
addr = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>
prev_addr = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>

%Transaction{
address: addr,
type: :transfer,
previous_public_key: prev_addr,
data: %TransactionData{}
}
end)
Expand Down Expand Up @@ -369,10 +401,12 @@ defmodule ArchethicWeb.GraphQLSchemaTest do
transactions =
Enum.map(1..20, fn _ ->
addr = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>
prev_addr = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>

%Transaction{
address: addr,
type: :transfer,
previous_public_key: prev_addr,
data: %TransactionData{}
}
end)
Expand Down