Skip to content

Commit

Permalink
Define the Tx monadic trait (#75)
Browse files Browse the repository at this point in the history
* use mtl

* update to mtl 0.1.0

* update mtl to v0.1.1 and stdlib to v0.10.0

* wip

* bug

* add trait Tx

* export Tx module

* use Tx instead of Reader
  • Loading branch information
janmasrovira authored Jan 20, 2025
1 parent 4f4b17e commit 59e2a36
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Applib.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ module Applib;

import Applib.Helpers open public;
import Applib.Identities open public;

import Applib.Authorization as Authorization public;
import Applib.Trait.Tx open public;
13 changes: 13 additions & 0 deletions Applib/Data/StandardInputs.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Applib.Data.StandardInputs;

import Stdlib.Prelude open;
import Anoma.State open;
import Anoma open;
import Applib.Identities open;

--- Standard inputs for Anoma transaction functions
type StandardInputs :=
mkStandardInputs@{
caller : Identity;
currentRoot : CommitmentTree.Root;
};
27 changes: 14 additions & 13 deletions Applib/Helpers.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ module Applib.Helpers;
import Stdlib.Prelude open;
import Stdlib.Debug.Fail open using {failwith};

import Applib.Data.StandardInputs open public;
import Applib.Identities open;
import Stdlib.Data.Set as Set open using {Set; empty; insert; union; isMember};
import Applib.Trait.Tx open public;
import Stdlib.Data.Map as Map open using {Map};
import Anoma open;
import Anoma.State open;
import Anoma.Builtin.ByteArray open;
import Anoma.Builtin.System open;
import BaseLayer.ResourceMachine as BaseLayer;
import Mtl open hiding {Identity};

findResourceByNullifier
(nf : Nat) (privateInputs : BaseLayer.Witness) : Maybe Resource :=
Expand Down Expand Up @@ -47,14 +50,6 @@ isNullifierPresent
(nullifier : Nullifier) (nullifierSet : Set Nullifier) : Bool :=
Set.isMember nullifier nullifierSet;

--- Standard inputs for transaction functions.
--- NOTE: In the future, this will include more settings such as the information flow control flag.
type StandardInputs :=
mkStandardInputs@{
caller : Identity;
currentRoot : CommitmentTree.Root;
};

mkActionHelper
(consumed created : List Resource) {app-data : Nat := 0} : BaseLayer.Action :=
let
Expand Down Expand Up @@ -113,13 +108,19 @@ emptyTx : BaseLayer.Transaction :=
--- @param created The created resources.
--- @return The transaction object.
prepareStandardTransaction
(standardInputs : StandardInputs)
{M : Type -> Type}
{{Tx M}}
{{Monad M}}
(consumed created : List Resource)
{app-data : Nat := 0}
: BaseLayer.Transaction :=
mkTransactionHelper@{
roots := [StandardInputs.currentRoot standardInputs];
actions := [mkActionHelper consumed created {app-data}];
: M BaseLayer.Transaction :=
do {
si <- getStandardInputs;
pure
mkTransactionHelper@{
roots := [StandardInputs.currentRoot si];
actions := [mkActionHelper consumed created {app-data}];
};
};

composeAll (txs : Set BaseLayer.Transaction) : BaseLayer.Transaction :=
Expand Down
23 changes: 23 additions & 0 deletions Applib/Trait/Random.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Applib.Trait.Random;

import Stdlib.Prelude open;
import Anoma.Builtin.System as Builtin;
import Anoma open;
import Mtl open;

mkPrng (seed : Nat) : PRNG := Builtin.pseudoRandomNumberGeneratorInit seed;

--- An environment capable of generating random ;ByteArray;s
trait
type Random (M : Type -> Type) :=
mkRandom@{
--- Returns a random ;ByteArray; of the given size
genRandomBytes (numBytes : Nat) : M ByteArray;
};

instance
StateT-Random {M : Type -> Type} {{Monad M}} : Random (StateT PRNG M) :=
mkRandom@{
genRandomBytes (n : Nat) : StateT PRNG M ByteArray :=
mkStateT (pseudoRandomNumberGeneratorNextBytes n >> pure);
};
60 changes: 60 additions & 0 deletions Applib/Trait/Tx.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module Applib.Trait.Tx;

import Stdlib.Prelude open;
import Anoma open;
import Applib.Data.StandardInputs open;
import BaseLayer.ResourceMachine open;
import Applib.Trait.Random open;
import Mtl open;

--- An environment used to create Anoma transactions
trait
type Tx (M : Type -> Type) :=
mkTx@{
--- Generates a random ;Nonce;
genRandomNonce : M Nonce;
getStandardInputs : M StandardInputs;
};

open Tx public;

module Private;
--- Concrete implementation of the ;Tx; trait
StandardTx (A : Type) : Type :=
StateT PRNG (ReaderT StandardInputs Mtl.Identity) A;

instance
StandardTx-MonadReaderI
: Reader
StandardInputs
(StateT PRNG (ReaderT StandardInputs Mtl.Identity)) :=
mkReader@{
ask : StandardTx StandardInputs := lift Reader.ask;
local
{A}
(f : StandardInputs -> StandardInputs)
: StandardTx A -> StandardTx A
| (mkStateT st) := mkStateT (st >> Reader.local f);
};

instance
StandardTx-Tx : Tx (StateT PRNG (ReaderT StandardInputs Mtl.Identity)) :=
mkTx@{
getStandardInputs : StandardTx StandardInputs := ask;
genRandomNonce : StandardTx Nonce :=
do {
bytes <- Random.genRandomBytes 32;
pure (Nonce.from32SizedByteArray bytes);
};
};

runStandardTx' {A} (seed : Nat) (si : StandardInputs) : StandardTx A -> A :=
evalState (mkPrng seed) >> runReader si >> Mtl.run;
end;

open Private using {StandardTx} public;

--- Run the transaction builder
runTx
(seed : Nat) (si : StandardInputs) : StandardTx Transaction -> Transaction :=
evalState (mkPrng seed) >> runReader si >> Mtl.run;
6 changes: 5 additions & 1 deletion Package.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ package : Package :=
defaultPackage@{
name := "anoma-applib";
version := mkVersion 0 6 0;
dependencies := [github "anoma" "juvix-stdlib" "v0.9.0"];
dependencies :=
[
github "anoma" "juvix-stdlib" "v0.10.0";
github "anoma" "juvix-mtl" "v0.1.1";
];
};
14 changes: 12 additions & 2 deletions juvix.lock.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@
# Do not edit this file manually.

version: 2
checksum: e2a4860cbb63c726e1c85e7734bb7dc653905f2be37229bd4fe4b78d90082a22
checksum: 88261d9c3949726f96fa44113236bd6077672f1a01902736df1da0267d43abcd
dependencies:
- git:
name: anoma_juvix-stdlib
ref: 01ff19f1135048be3402e094f2fc89406a44a995
ref: c5d4fcd87b26608f628e6b116587f1ec227e1bf0
url: https://github.com/anoma/juvix-stdlib
dependencies: []
- git:
name: anoma_juvix-mtl
ref: bda071b2477ff15c0292bee3dd31bc2c7d254f44
url: https://github.com/anoma/juvix-mtl
dependencies:
- git:
name: anoma_juvix-stdlib
ref: c5d4fcd87b26608f628e6b116587f1ec227e1bf0
url: https://github.com/anoma/juvix-stdlib
dependencies: []

0 comments on commit 59e2a36

Please sign in to comment.