Skip to content

Commit

Permalink
use Tx trait
Browse files Browse the repository at this point in the history
  • Loading branch information
janmasrovira committed Jan 17, 2025
1 parent c1c6b6d commit 9710059
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 120 deletions.
14 changes: 5 additions & 9 deletions Counter/Simple/Increment.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ module Counter.Simple.Increment;
import Stdlib.Prelude open;
import Anoma open;
import Anoma.State.CommitmentTree open;
import Applib.Helpers open;
import Applib.Identities open;
import Applib open;
import Extra.Encoded open;

import Counter.Simple.Resource open;
import BaseLayer.TransactionRequest open;
import BaseLayer.ResourceMachine open;
import Extra.Tx open;

--- Increments the counter value by 1.
--- @param currentCounter The current counter to increment.
Expand All @@ -36,12 +34,10 @@ increment
};

in
pure
prepareStandardTransaction@{
standardInputs;
consumed := [currentCounter];
created := [newCounter];
};
prepareStandardTransaction@{
consumed := [currentCounter];
created := [newCounter];
};
};

main
Expand Down
29 changes: 12 additions & 17 deletions Counter/Simple/Initialize.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ import Stdlib.Data.Set as Set open using {Set};
import Stdlib.Data.Map as Map;
import Anoma open;
import Anoma.State.CommitmentTree open;
import Applib.Helpers open;
import Applib.Identities open;
import Extra.Encoded open;

import Counter.Simple.Resource open;
import BaseLayer.TransactionRequest open;
import BaseLayer.ResourceMachine open;
import Extra.Tx open;
import Applib open;

--- Initializes a counter that can be incremented by everyone.
--- @param standardInputs The transaction function standard inputs.
Expand All @@ -37,19 +34,17 @@ initialize
ephemeral := false;
};
in
pure
prepareStandardTransaction@{
standardInputs;
consumed :=
[
mkCounter@{
logic;
nonce := nonce1;
ephemeral := true;
};
];
created := [initialCounter];
};
prepareStandardTransaction@{
consumed :=
[
mkCounter@{
logic;
nonce := nonce1;
ephemeral := true;
};
];
created := [initialCounter];
};
};

std : StandardInputs :=
Expand Down
6 changes: 5 additions & 1 deletion Counter/Simple/Resource.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import Applib.Helpers open;
--- @param ephemeral Whether the resource is ephemeral or not.
--- @return The constructed counter resource.
mkCounter
(logic : Logic) (nonce : Nonce) {count : Nat := 0} {ephemeral : Bool := false} : Resource :=
(logic : Logic)
(nonce : Nonce)
{count : Nat := 0}
{ephemeral : Bool := false}
: Resource :=
mkResource@{
logic;
label := "SimpleCounter" |> anomaEncode;
Expand Down
55 changes: 30 additions & 25 deletions Counter/Unique/Increment.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import BaseLayer.TransactionRequest open;
import Applib.Helpers open using {mkActionHelper};
import Extra open;
import Counter.Unique.Resource open;
import Applib.Helpers open;
import Applib.Identities open;
import Applib open;
import Anoma.State.CommitmentTree open using {mkRoot};
import Stdlib.Debug open;

Expand All @@ -19,30 +18,36 @@ increment
(logic : Logic)
(latestRoot : Nat)
: Transaction :=
if
| not (hasCounterKind currentCounter logic) :=
failwith "The input resource has the wrong kind"
| else :=
let
(nonce, _) := mkPrng randSeed |> randomNonce;
newCounter :=
mkCounter@{
logic;
nonce;
uniqueLabel := currentCounter |> Resource.label |> anomaDecode;
count := Resource.value currentCounter + 1;
ephemeral := false;
let
standardInputs :=
mkStandardInputs@{
caller := Universal.identity;
currentRoot := mkRoot latestRoot;
};
in runTx
randSeed
standardInputs
if
| not (hasCounterKind currentCounter logic) :=
failwith "The input resource has the wrong kind"
| else :=
do {
nonce <- genRandomNonce;
let
newCounter :=
mkCounter@{
logic;
nonce;
uniqueLabel := currentCounter |> Resource.label |> anomaDecode;
count := Resource.value currentCounter + 1;
ephemeral := false;
};
in
prepareStandardTransaction@{
consumed := [currentCounter];
created := [newCounter];
};
standardInputs :=
mkStandardInputs@{
caller := Universal.identity;
currentRoot := mkRoot latestRoot;
};
in prepareStandardTransaction@{
standardInputs;
consumed := [currentCounter];
created := [newCounter];
};
};

main
(randSeed : Nat)
Expand Down
12 changes: 5 additions & 7 deletions Counter/Unique/Initialize.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Applib.Helpers open;
import Applib.Identities open;
import Anoma.State.CommitmentTree open using {mkRoot};
import Stdlib.Debug open;
import Extra.Tx open;
import Applib open;

initialize
(randSeed : Nat)
Expand Down Expand Up @@ -51,12 +51,10 @@ initialize
};
ephConsumable := consumable@Resource{ephemeral := true};
in
pure
prepareStandardTransaction@{
standardInputs;
consumed := [consumable; ephCounter];
created := [ephConsumable; newCounter];
};
prepareStandardTransaction@{
consumed := [consumable; ephCounter];
created := [ephConsumable; newCounter];
};
};

main
Expand Down
4 changes: 3 additions & 1 deletion Counter/Unique/Logic.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ counter-correct? (publicInputs : Instance) (privateInputs : Witness) : Bool :=
case Witness.consumed privateInputs, Witness.created privateInputs of {
| [consumable; ephCounter], [ephConsumable; newCounter] :=
-- This is called for consumable and ephCounter
initialize-correct? ephCounter publicInputs
initialize-correct?
ephCounter
publicInputs
| [prevCounter], [nextCounter] :=
increment-correct? prevCounter privateInputs
| _ := false
Expand Down
36 changes: 20 additions & 16 deletions Counter/Unique/Resource.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,25 @@ mkCounter
};

counterKind (consumable : Resource) (logic : Logic) : Kind :=
let dummyCounter := mkCounter@{
logic;
uniqueLabel := mkUniqueCounterLabel consumable;
nonce := Nonce.fromNat 0;
count := 0;
ephemeral := true}
in
kind dummyCounter;
let
dummyCounter :=
mkCounter@{
logic;
uniqueLabel := mkUniqueCounterLabel consumable;
nonce := Nonce.fromNat 0;
count := 0;
ephemeral := true;
};
in kind dummyCounter;

hasCounterKind (resource : Resource) (logic : Logic) : Bool :=
let dummyCounter := mkCounter@{
logic;
uniqueLabel := Resource.label resource |> anomaDecode;
nonce := Nonce.fromNat 0;
count := 0;
ephemeral := true}
in
kind resource == kind dummyCounter;
let
dummyCounter :=
mkCounter@{
logic;
uniqueLabel := Resource.label resource |> anomaDecode;
nonce := Nonce.fromNat 0;
count := 0;
ephemeral := true;
};
in kind resource == kind dummyCounter;
32 changes: 0 additions & 32 deletions Extra/Tx.juvix

This file was deleted.

1 change: 0 additions & 1 deletion HelloWorld/Interface/Transaction.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Anoma.State.CommitmentTree open;
import BaseLayer.ResourceMachine open;
import HelloWorld.Resource open;
import BaseLayer.TransactionRequest open;
import Extra.Tx open;

initialize (randSeed : Nat) (standardInputs : StandardInputs) : Transaction :=
runTx
Expand Down
9 changes: 4 additions & 5 deletions Package.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ package : Package :=
main := just "Main.juvix";
dependencies :=
[
github "anoma" "juvix-stdlib" "v0.9.0";
-- path "../anoma-applib";
github
github "anoma" "juvix-stdlib" "v0.10.0";
github
"anoma"
"anoma-applib"
"a20db447bc80f8fc42cb389143623f3535a3ad07";
github "anoma" "juvix-mtl" "v0.0.1";
"c42ecb87a410c78429e1e71ca0a6285d76fcdc60";
github "anoma" "juvix-mtl" "v0.1.1";
];
};
22 changes: 16 additions & 6 deletions juvix.lock.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,40 @@
# Do not edit this file manually.

version: 2
checksum: 4231d216679af51fc6bfd9139ea8ba69f4a6ea3d0dc9ec1b81f17d7707f5c589
checksum: eb12070e869d68995c27edf095c116a7097b45abae3467f12a56a37ae1068894
dependencies:
- git:
name: anoma_juvix-stdlib
ref: 01ff19f1135048be3402e094f2fc89406a44a995
ref: c5d4fcd87b26608f628e6b116587f1ec227e1bf0
url: https://github.com/anoma/juvix-stdlib
dependencies: []
- git:
name: anoma_anoma-applib
ref: a20db447bc80f8fc42cb389143623f3535a3ad07
ref: c42ecb87a410c78429e1e71ca0a6285d76fcdc60
url: https://github.com/anoma/anoma-applib
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: []
- git:
name: anoma_juvix-mtl
ref: e715a8408df216bc5d4bfe04f8f8eaed93d780ce
ref: bda071b2477ff15c0292bee3dd31bc2c7d254f44
url: https://github.com/anoma/juvix-mtl
dependencies:
- git:
name: anoma_juvix-stdlib
ref: 01ff19f1135048be3402e094f2fc89406a44a995
ref: c5d4fcd87b26608f628e6b116587f1ec227e1bf0
url: https://github.com/anoma/juvix-stdlib
dependencies: []

0 comments on commit 9710059

Please sign in to comment.