anoma / green

https://anoma.github.io/anoma/
MIT License
7 stars 0 forks source link

Transaction.from_noun list type mismatch #524

Closed paulcadman closed 3 months ago

paulcadman commented 5 months ago

I created https://github.com/anoma/anoma/pull/500 to fix issues with type mistmatch between Nock encoded lists and their Elixir counterparts. There's some confusion about the fix so I'm raising this issue to highlight the supposed bug.

In the REPL, let's parse a noun with 0 (i.e Nock encoded []) in the proofs and delta fields of a transaction:

iex(paul@Pauls-MacBook-Pro)8> "[0 0 0 0 0 0 0]" |> Noun.Format.parse_always |> Transaction.from_noun
** (Protocol.UndefinedError) protocol Enumerable not implemented for 0 of type Integer. This protocol is implemented for the following type(s): Date.Range, File.Stream, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, Jason.OrderedObject, Kino.Control, Kino.Input, Kino.JS.Live, List, Map, MapSet, Range, Stream, Table.Mapper, Table.Zipper
    (elixir 1.16.2) lib/enum.ex:1: Enumerable.impl_for!/1
    (elixir 1.16.2) lib/enum.ex:166: Enumerable.reduce/3
    (elixir 1.16.2) lib/enum.ex:4396: Enum.map/2
    (anoma 0.15.0) lib/anoma/resource/transaction.ex:56: Anoma.Resource.Transaction.from_noun/1
    iex:8: (file)

The error is due to the fact that we're treating the 0 as a list in the from_noun code:

https://github.com/anoma/anoma/blob/base/lib/anoma/resource/transaction.ex#L55-L58

You can fix it by setting the proofs and transaction fields to Elixir lists:

iex(paul@Pauls-MacBook-Pro)8> [0, 0, 0, [], [], 0 | 0] |> Transaction.from_noun
%Anoma.Resource.Transaction{
  preference: nil,
  extra: 0,
  delta: %{},
  proofs: [],
  nullifiers: 0,
  commitments: 0,
  roots: 0
}

This was the reason why I used the list_nock_to_erlang to convert the noun proof field into an elixir list before it's mapped over.

mariari commented 4 months ago

@juped thoughts