Skip to content

Commit

Permalink
Local endpoint refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwinGuayacan committed Dec 20, 2022
1 parent 9902a38 commit 737a6eb
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 265 deletions.
16 changes: 2 additions & 14 deletions lib/chainweb/pact.ex
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
defmodule Kadena.Chainweb.Pact do
@moduledoc """
<<<<<<< HEAD
Exposes functions to interact with the Pact API endpoints.
"""

alias Kadena.Chainweb.Pact.Send
alias Kadena.Chainweb.Pact.{Local, Send}

@default_network_opts [network_id: :testnet04, chain_id: 0]

defdelegate send(cmds, network_opts \\ @default_network_opts), to: Send, as: :process
=======
Specifies the API for processing HTTP requests.
"""
alias Kadena.Chainweb.Pact.Local
defdelegate local(request, network \\ :testnet04, chain_id \\ "0"), to: Local, as: :process

# defdelegate send(request, network \\ :test, chain_id \\ "0"), to: Send, as: :process
# defdelegate poll(request, network \\ :test, chain_id \\ "0"), to: Poll, as: :process
# defdelegate listen(request, network \\ :test, chain_id \\ "0"), to: Listen, as: :process
# defdelegate spv(request, network \\ :test, chain_id \\ "0"), to: SPV, as: :process
>>>>>>> 40795f4 (Add local endpoitn and PACT API)
defdelegate local(cmds, network_opts \\ @default_network_opts), to: Local, as: :process
end
46 changes: 0 additions & 46 deletions lib/chainweb/pact/endpoints.ex

This file was deleted.

31 changes: 15 additions & 16 deletions lib/chainweb/pact/local.ex
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
defmodule Kadena.Chainweb.Pact.Local do
@moduledoc """
Send endpoint implementation
Local endpoint implementation
"""

alias Kadena.Chainweb.Request
alias Kadena.Chainweb.Pact.{Endpoints, JSONPayload, LocalResponse}
alias Kadena.Types.{Command, LocalRequestBody}
alias Kadena.Chainweb.Pact.{LocalRequestBody, LocalResponse, Spec}
alias Kadena.Types.Command

@type cmd :: Command.t()
@type json_request_body :: String.t()
@behaviour Endpoints
@behaviour Spec

@endpoint "local"
@headers [{"Content-Type", "application/json"}]

@type cmd :: Command.t()
@type json :: String.t()

@impl true
def process(%Command{} = cmd, network, chain_id) do
request_body = get_json_request_body(cmd)
def process(%Command{} = cmd, network_id: network_id, chain_id: chain_id) do
request_body = json_request_body(cmd)
headers = [{"Content-Type", "application/json"}]

:post
|> Request.new(pact: [endpoint: @endpoint])
|> Request.set_chain_id(chain_id)
|> Request.set_network(network)
|> Request.set_network(network_id)
|> Request.add_body(request_body)
|> Request.add_headers(@headers)
|> Request.add_headers(headers)
|> Request.perform()
|> Request.results(as: LocalResponse)
end

def process(_cmds, _network, _chain_id), do: {:error, [command: :invalid_command]}

@spec get_json_request_body(cmd :: cmd()) :: json_request_body()
defp get_json_request_body(cmd) do
@spec json_request_body(cmd :: cmd()) :: json()
defp json_request_body(cmd) do
cmd
|> LocalRequestBody.new()
|> JSONPayload.parse()
|> LocalRequestBody.to_json!()
end
end
16 changes: 4 additions & 12 deletions lib/chainweb/pact/local_request_body.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Kadena.Chainweb.Pact.LocalRequestBody do

@behaviour Kadena.Chainweb.Pact.Type

@type command :: String.t()
@type command :: Command.t()
@type hash :: PactTransactionHash.t()
@type sigs :: SignaturesList.t()
@type cmd :: String.t()
Expand All @@ -19,11 +19,8 @@ defmodule Kadena.Chainweb.Pact.LocalRequestBody do
defstruct [:hash, :sigs, :cmd]

@impl true
def new(args) do
args
|> Command.new()
|> build_local_request_body()
end
def new(%Command{} = cmd), do: build_local_request_body(cmd)
def new(_cmd), do: {:error, [arg: :not_a_command]}

@impl true
def to_json!(%__MODULE__{hash: hash, sigs: sigs, cmd: cmd}) do
Expand All @@ -33,17 +30,12 @@ defmodule Kadena.Chainweb.Pact.LocalRequestBody do
end
end

@spec build_local_request_body(command :: command() | error()) :: t() | error()
@spec build_local_request_body(command :: command()) :: t()
defp build_local_request_body(%Command{} = command) do
attrs = Map.from_struct(command)
struct(%__MODULE__{}, attrs)
end

defp build_local_request_body({:error, [command: :not_a_list]}),
do: {:error, [local_request_body: :not_a_list]}

defp build_local_request_body({:error, reason}), do: {:error, reason}

@spec to_signature_list(signatures :: sigs()) :: {:ok, raw_sigs()}
defp to_signature_list(%SignaturesList{signatures: list}) do
sigs = Enum.map(list, fn sig -> Map.from_struct(sig) end)
Expand Down
2 changes: 1 addition & 1 deletion lib/chainweb/pact/spec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule Kadena.Chainweb.Pact.Spec do
alias Kadena.Chainweb.Error
alias Kadena.Types.Command

@type data :: list(Command.t())
@type data :: list(Command.t()) | Command.t()
@type error :: {:error, Error.t()}
@type chain_id :: 0..19 | String.t()
@type network_opts :: [network_id: atom(), chain_id: chain_id()]
Expand Down
54 changes: 0 additions & 54 deletions lib/types/local_request_body.ex

This file was deleted.

40 changes: 4 additions & 36 deletions test/chainweb/pact/local_request_body_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@ defmodule Kadena.Chainweb.Pact.LocalRequestBodyTest do

alias Kadena.Chainweb.Pact.LocalRequestBody

<<<<<<< HEAD:test/chainweb/pact/local_request_body_test.exs
alias Kadena.Types.{PactTransactionHash, SignaturesList}
=======
alias Kadena.Types.{
Command,
LocalRequestBody,
PactTransactionHash,
Signature,
SignaturesList
}
>>>>>>> 4e12645 (Add local endpoitn and PACT API):test/types/local_request_body_test.exs

setup do
cmd =
Expand Down Expand Up @@ -46,7 +41,9 @@ defmodule Kadena.Chainweb.Pact.LocalRequestBodyTest do
command: command,
cmd: cmd,
hash: hash,
sigs: sigs
sigs: sigs,
json_result:
"{\"cmd\":\"{\\\"meta\\\":{\\\"chainId\\\":\\\"0\\\",\\\"creationTime\\\":1667249173,\\\"gasLimit\\\":1000,\\\"gasPrice\\\":1.0e-6,\\\"sender\\\":\\\"k:554754f48b16df24b552f6832dda090642ed9658559fef9f3ee1bb4637ea7c94\\\",\\\"ttl\\\":28800},\\\"networkId\\\":\\\"testnet04\\\",\\\"nonce\\\":\\\"2023-06-13 17:45:18.211131 UTC\\\",\\\"payload\\\":{\\\"exec\\\":{\\\"code\\\":\\\"(+ 5 6)\\\",\\\"data\\\":{}}},\\\"signers\\\":[{\\\"addr\\\":\\\"85bef77ea3570387cac57da34938f246c7460dc533a67823f065823e327b2afd\\\",\\\"clist\\\":[{\\\"args\\\":[\\\"85bef77ea3570387cac57da34938f246c7460dc533a67823f065823e327b2afd\\\"],\\\"name\\\":\\\"coin.GAS\\\"}],\\\"pubKey\\\":\\\"85bef77ea3570387cac57da34938f246c7460dc533a67823f065823e327b2afd\\\",\\\"scheme\\\":\\\"ED25519\\\"}]}\",\"hash\":\"-1npoTU2Mi71pKE_oteJiJuHuXTXxoObJm8zzVRK2pk\",\"sigs\":[{\"sig\":\"8b234b83570359e52188cceb301036a2a7b255171e856fd550cac687a946f18fbfc0e769fd8393dda44d6d04c31b531eaf35efb3b78b5e40fd857a743133030d\"}]}"
}
end

Expand All @@ -65,43 +62,14 @@ defmodule Kadena.Chainweb.Pact.LocalRequestBodyTest do
end

describe "JSONPayload.parse/1" do
setup do
command = %Command{
cmd:
"{\"meta\":{\"chainId\":\"0\",\"creationTime\":1667249173,\"gasLimit\":1000,\"gasPrice\":1.0e-6,\"sender\":\"k:554754f48b16df24b552f6832dda090642ed9658559fef9f3ee1bb4637ea7c94\",\"ttl\":28800},\"networkId\":\"testnet04\",\"nonce\":\"2023-06-13 17:45:18.211131 UTC\",\"payload\":{\"exec\":{\"code\":\"(+ 5 6)\",\"data\":{}}},\"signers\":[{\"addr\":\"85bef77ea3570387cac57da34938f246c7460dc533a67823f065823e327b2afd\",\"clist\":[{\"args\":[\"85bef77ea3570387cac57da34938f246c7460dc533a67823f065823e327b2afd\"],\"name\":\"coin.GAS\"}],\"pubKey\":\"85bef77ea3570387cac57da34938f246c7460dc533a67823f065823e327b2afd\",\"scheme\":\"ED25519\"}]}",
hash: %PactTransactionHash{
hash: "-1npoTU2Mi71pKE_oteJiJuHuXTXxoObJm8zzVRK2pk"
},
sigs: %SignaturesList{
signatures: [
%Signature{
sig:
"8b234b83570359e52188cceb301036a2a7b255171e856fd550cac687a946f18fbfc0e769fd8393dda44d6d04c31b531eaf35efb3b78b5e40fd857a743133030d"
}
]
}
}

%{
command: command,
json_result:
"{\"cmd\":\"{\\\"meta\\\":{\\\"chainId\\\":\\\"0\\\",\\\"creationTime\\\":1667249173,\\\"gasLimit\\\":1000,\\\"gasPrice\\\":1.0e-6,\\\"sender\\\":\\\"k:554754f48b16df24b552f6832dda090642ed9658559fef9f3ee1bb4637ea7c94\\\",\\\"ttl\\\":28800},\\\"networkId\\\":\\\"testnet04\\\",\\\"nonce\\\":\\\"2023-06-13 17:45:18.211131 UTC\\\",\\\"payload\\\":{\\\"exec\\\":{\\\"code\\\":\\\"(+ 5 6)\\\",\\\"data\\\":{}}},\\\"signers\\\":[{\\\"addr\\\":\\\"85bef77ea3570387cac57da34938f246c7460dc533a67823f065823e327b2afd\\\",\\\"clist\\\":[{\\\"args\\\":[\\\"85bef77ea3570387cac57da34938f246c7460dc533a67823f065823e327b2afd\\\"],\\\"name\\\":\\\"coin.GAS\\\"}],\\\"pubKey\\\":\\\"85bef77ea3570387cac57da34938f246c7460dc533a67823f065823e327b2afd\\\",\\\"scheme\\\":\\\"ED25519\\\"}]}\",\"hash\":\"-1npoTU2Mi71pKE_oteJiJuHuXTXxoObJm8zzVRK2pk\",\"sigs\":[{\"sig\":\"8b234b83570359e52188cceb301036a2a7b255171e856fd550cac687a946f18fbfc0e769fd8393dda44d6d04c31b531eaf35efb3b78b5e40fd857a743133030d\"}]}"
}
end

test "with a valid LocalRequestBody", %{
command: command,
json_result: json_result
} do
<<<<<<< HEAD:test/chainweb/pact/local_request_body_test.exs
^json_result =
[hash: hash, sigs: sigs, cmd: cmd]
command
|> LocalRequestBody.new()
|> LocalRequestBody.to_json!()
=======
local_request_body = LocalRequestBody.new(command)
^json_result = JSONPayload.parse(local_request_body)
>>>>>>> 4e12645 (Add local endpoitn and PACT API):test/types/local_request_body_test.exs
end
end
end
Loading

0 comments on commit 737a6eb

Please sign in to comment.