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

nock typespecs #59

Merged
merged 21 commits into from
Dec 19, 2023
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
3 changes: 2 additions & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
line_length: 78
]
79 changes: 79 additions & 0 deletions hoon/anoma.hoon
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
:: Simple demo Nock standard library.
!.
=~ %909
~% %k.909 ~ ~
:: layer 0: version stub (+3)
|%
++ anoma +
--
:: layer 1: basic arithmetic (+1)
~% %one + ~
|%
++ dec :: +342
~/ %dec
|= a=@
?< =(0 a)
=| b=@
|- ^- @
?: =(a +(b)) b
$(b +(b))
++ add :: +20
~/ %add
|= [a=@ b=@]
^- @
?: =(0 a) b
$(a (dec a), b +(b))
++ sub :: +47
~/ %sub
|= [a=@ b=@]
^- @
?: =(0 b) a
$(a (dec a), b (dec b))
++ lth :: +343
~/ %lth
|= [a=@ b=@]
^- ?
?& !=(a b)
|-
?| =(0 a)
?& !=(0 b)
$(a (dec a), b (dec b))
== == ==
++ lte :: +84
~/ %lte
|= [a=@ b=@]
^- ?
|(=(a b) (lth a b))
++ gth :: +43
~/ %gth
|= [a=@ b=@]
^- ?
!(lte a b)
++ gte :: +22
~/ %gte
|= [a=@ b=@]
^- ?
!(lth a b)
++ mul :: +4
~/ %mul
|: [a=`@`1 b=`@`1]
=| c=@
|- ^- @
?: =(0 a) c
$(a (dec a), c (add b c))
++ div :: +170
~/ %div
|: [a=`@`1 b=`@`1]
?< =(0 b)
=| c=@
|- ^- @
?: (lth a b) c
$(a (sub a b), c +(c))
++ mod :: +46
~/ %mod
|: [a=`@`1 b=`@`1]
^- @
?< =(0 b)
(sub a (mul b (div a b)))
--
==
22 changes: 17 additions & 5 deletions lib/anoma/block.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ defmodule Anoma.Block do
"""
@spec create(
Base.t(),
Serializer.public_key() | {Serializer.public_key(), Serializer.private_key()},
Serializer.public_key()
| {Serializer.public_key(), Serializer.private_key()},
non_neg_integer()
) :: t()

Expand All @@ -71,7 +72,8 @@ defmodule Anoma.Block do
end

# create data which can easily be signed
@spec signable(Base.t(), Serializer.public_key(), non_neg_integer()) :: binary()
@spec signable(Base.t(), Serializer.public_key(), non_neg_integer()) ::
binary()
defp signable(block, pub_key, round) do
Serializer.serialize({Base.digest(block), round, pub_key})
end
Expand All @@ -86,11 +88,18 @@ defmodule Anoma.Block do

@spec encode(t()) :: tuple()
def encode(block) do
{__MODULE__, block.id, block.block, block.round, block.pub_key, block.signature}
{__MODULE__, block.id, block.block, block.round, block.pub_key,
block.signature}
end

def decode({__MODULE__, id, block, round, pub_key, sig}) do
%Block{id: id, block: block, round: round, pub_key: pub_key, signature: sig}
%Block{
id: id,
block: block,
round: round,
pub_key: pub_key,
signature: sig
}
end

@doc """
Expand Down Expand Up @@ -123,7 +132,10 @@ defmodule Anoma.Block do
def create_table(table_key, rocks?) do
resp =
if rocks? do
:mnesia.create_table(table_key, attributes: attributes(), rocksdb_copies: [node()])
:mnesia.create_table(table_key,
attributes: attributes(),
rocksdb_copies: [node()]
)
else
:mnesia.create_table(table_key, attributes: attributes())
end
Expand Down
9 changes: 8 additions & 1 deletion lib/anoma/logic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ defmodule Anoma.Logic do
# These are the known ops, not the full set.
# these are what will be compiled

@type compiled_op() :: Lt.t() | Branch.t() | Neg.t() | Mul.t() | Add.t() | Inc.t() | number()
@type compiled_op() ::
Lt.t()
| Branch.t()
| Neg.t()
| Mul.t()
| Add.t()
| Inc.t()
| number()

@typedoc """

Expand Down
12 changes: 9 additions & 3 deletions lib/anoma/node/executor/communicator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,20 @@ defmodule Anoma.Node.Executor.Communicator do
end

def start_link(arg) do
GenServer.start_link(__MODULE__, arg, Utility.name(arg, &Utility.com_name/1))
GenServer.start_link(
__MODULE__,
arg,
Utility.name(arg, &Utility.com_name/1)
)
end

############################################################
# Public RPC API #
############################################################

# Transactions
@spec new_transactions(pid(), Enumerable.t(PartialTx.t())) :: Primary.response()
@spec new_transactions(pid(), Enumerable.t(PartialTx.t())) ::
Primary.response()
def new_transactions(communicator, transactions) do
GenServer.call(communicator, {:transactions, transactions})
end
Expand All @@ -56,7 +61,8 @@ defmodule Anoma.Node.Executor.Communicator do
############################################################

# make this more interesting later
@spec broadcast_transactions(t(), Enumerable.t(PartialTx.t())) :: Primary.response()
@spec broadcast_transactions(t(), Enumerable.t(PartialTx.t())) ::
Primary.response()
defp broadcast_transactions(agent, trans) do
Primary.new_transactions(agent.primary, trans)
end
Expand Down
3 changes: 2 additions & 1 deletion lib/anoma/node/executor/primary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ defmodule Anoma.Node.Executor.Primary do
GenServer.call(primary, :dump_state)
end

@spec new_transactions(GenServer.server(), Enumerable.t(PartialTx.t())) :: response()
@spec new_transactions(GenServer.server(), Enumerable.t(PartialTx.t())) ::
response()
def new_transactions(primary, transactions) do
GenServer.call(primary, {:transactions, transactions})
end
Expand Down
11 changes: 9 additions & 2 deletions lib/anoma/node/intent/communicator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ defmodule Anoma.Node.Intent.Communicator do
end

def start_link(arg) do
GenServer.start_link(__MODULE__, arg, Utility.name(arg, &Utility.com_name/1))
GenServer.start_link(
__MODULE__,
arg,
Utility.name(arg, &Utility.com_name/1)
)
end

############################################################
Expand Down Expand Up @@ -98,7 +102,10 @@ defmodule Anoma.Node.Intent.Communicator do

@spec broadcast_intent(t(), Intent.t()) :: :ok
defp broadcast_intent(com, intent) do
Utility.broadcast(MapSet.put(com.subscribers, com.pool), {:new_intent, intent})
Utility.broadcast(
MapSet.put(com.subscribers, com.pool),
{:new_intent, intent}
)
end

@spec broadcast_intents(t(), GenServer.server()) :: :ok
Expand Down
14 changes: 11 additions & 3 deletions lib/anoma/partialtx.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@

@spec add_output(t(), Resource.t()) :: t()
def add_output(partial, output) do
%PartialTx{partial | outputs: update_resource_set(partial.outputs, output)}
%PartialTx{
partial
| outputs: update_resource_set(partial.outputs, output)
}
end

@spec balanced(t()) :: boolean()
def balanced(%PartialTx{inputs: inputs, outputs: outputs}) do
inputs
|> Map.merge(outputs, fn _k, i, o -> add_quantities(i) - add_quantities(o) end)
|> Map.merge(outputs, fn _k, i, o ->
add_quantities(i) - add_quantities(o)
end)
|> Enum.all?(fn {_k, v} -> v == 0 end)
end

Expand Down Expand Up @@ -90,7 +95,7 @@
# Helpers
######################################################################

@doc """

Check warning on line 98 in lib/anoma/partialtx.ex

View workflow job for this annotation

GitHub Actions / Tests, dialyzer, format (topic)

defp update_resource_set/2 is private, @doc attribute is always discarded for private functions/macros/types

Check warning on line 98 in lib/anoma/partialtx.ex

View workflow job for this annotation

GitHub Actions / Tests, dialyzer, format (topic)

defp update_resource_set/2 is private, @doc attribute is always discarded for private functions/macros/types

Check warning on line 98 in lib/anoma/partialtx.ex

View workflow job for this annotation

GitHub Actions / Tests, dialyzer, format (next)

defp update_resource_set/2 is private, @doc attribute is always discarded for private functions/macros/types

Check warning on line 98 in lib/anoma/partialtx.ex

View workflow job for this annotation

GitHub Actions / Tests, dialyzer, format (next)

defp update_resource_set/2 is private, @doc attribute is always discarded for private functions/macros/types
I update the resource set with the new resource


Expand Down Expand Up @@ -118,8 +123,11 @@
end

@spec sub_quantities(Resource.t(), Resource.t()) :: Resource.t()
defp sub_quantities(resource_1, resource_2) do

Check warning on line 126 in lib/anoma/partialtx.ex

View workflow job for this annotation

GitHub Actions / Tests, dialyzer, format (topic)

function sub_quantities/2 is unused

Check warning on line 126 in lib/anoma/partialtx.ex

View workflow job for this annotation

GitHub Actions / Tests, dialyzer, format (topic)

function sub_quantities/2 is unused

Check warning on line 126 in lib/anoma/partialtx.ex

View workflow job for this annotation

GitHub Actions / Tests, dialyzer, format (next)

function sub_quantities/2 is unused

Check warning on line 126 in lib/anoma/partialtx.ex

View workflow job for this annotation

GitHub Actions / Tests, dialyzer, format (next)

function sub_quantities/2 is unused
%Resource{resource_1 | quantity: resource_1.quantity - resource_2.quantity}
%Resource{
resource_1
| quantity: resource_1.quantity - resource_2.quantity
}
end
end

Expand Down
Loading
Loading