module Anoma;

import Anoma.Resource open public;
import Anoma.BuiltinResources open public;
import Anoma.Balance open public;
import Anoma.PartialTx open public;
import Anoma.Kind open public;

import Anoma.Prelude open;
import Stdlib.Trait.Partial open;

import Data.ByteString open public;

import Data.Map as Map;
open Map using {Map};
open Resource;
open PartialTx;

validPartialTx {{Partial}} (m : Map LogicHash LogicFunction) (ptx : PartialTx)
  : Bool :=
  let
    getLogicFn (kind : ResourceKind) (res : Resource) : PartialTx -> Bool :=
      let
        hash : LogicHash := Resource.logicHash res;
      in case Map.lookup hash m of {
           | just f := f kind
           | _ :=
             fail
               ("Could not find function associated with hash: "
                 ++str Show.show hash)
         };
  in all λ {r := getLogicFn created r ptx} (createdResources ptx)
    && all λ {r := getLogicFn consumed r ptx} (consumedResources ptx);

mkBalance (r : Resource) : Balance := balance [kind r, quantity r];

balanceDelta (tx : PartialTx) : Balance :=
  subBalance
    (sumBalances (map mkBalance (consumedResources tx)))
    (sumBalances (map mkBalance (createdResources tx)));

checkTransaction {{Partial}} (m : Map LogicHash LogicFunction) (ptxs : List
  PartialTx) : Bool :=
  let
    allValid : Bool := all (validPartialTx m) ptxs;
    sumDeltasIsZero : Bool :=
      balanceIsZero (sumBalances (map balanceDelta ptxs));
  in allValid && sumDeltasIsZero;
Last modified on 2023-12-07 10:36 UTC