Skip to content

Commit

Permalink
refactor(logic): reorganise prolog related files into dedicated pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Jan 6, 2024
1 parent 6644a9c commit 5df389e
Show file tree
Hide file tree
Showing 25 changed files with 938 additions and 891 deletions.
5 changes: 3 additions & 2 deletions x/logic/keeper/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/okp4/okp4d/x/logic/interpreter"
"github.com/okp4/okp4d/x/logic/interpreter/bootstrap"
"github.com/okp4/okp4d/x/logic/meter"
prolog2 "github.com/okp4/okp4d/x/logic/prolog"
"github.com/okp4/okp4d/x/logic/types"
"github.com/okp4/okp4d/x/logic/util"
)
Expand Down Expand Up @@ -126,7 +127,7 @@ func (k Keeper) newInterpreter(ctx goctx.Context) (*prolog.Interpreter, *util.Bo
lo.Map(
lo.Filter(
interpreter.RegistryNames,
util.Indexed(util.WhitelistBlacklistMatches(whitelistPredicates, blacklistPredicates, util.PredicateMatches))),
util.Indexed(util.WhitelistBlacklistMatches(whitelistPredicates, blacklistPredicates, prolog2.PredicateMatches))),
toPredicate(
nonNilNorZeroOrDefaultUint64(gasPolicy.DefaultPredicateCost, defaultPredicateCost),
gasPolicy.GetPredicateCosts())),
Expand Down Expand Up @@ -165,7 +166,7 @@ func (k Keeper) newInterpreter(ctx goctx.Context) (*prolog.Interpreter, *util.Bo
func toPredicate(defaultCost uint64, predicateCosts []types.PredicateCost) func(string, int) lo.Tuple2[string, uint64] {
return func(predicate string, _ int) lo.Tuple2[string, uint64] {
for _, c := range predicateCosts {
if util.PredicateMatches(predicate)(c.Predicate) {
if prolog2.PredicateMatches(predicate)(c.Predicate) {
return lo.T2(predicate, nonNilNorZeroOrDefaultUint64(c.Cost, defaultCost))
}
}
Expand Down
11 changes: 5 additions & 6 deletions x/logic/predicate/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"fmt"

"github.com/ichiban/prolog/engine"
"github.com/okp4/okp4d/x/logic/prolog"

bech322 "github.com/cosmos/cosmos-sdk/types/bech32"

"github.com/okp4/okp4d/x/logic/util"
)

// Bech32Address is a predicate that convert a [bech32] encoded string into [base64] bytes and give the address prefix,
Expand Down Expand Up @@ -45,7 +44,7 @@ func Bech32Address(vm *engine.VM, address, bech32 engine.Term, cont engine.Cont,
if err != nil {
return engine.Error(fmt.Errorf("bech32_address/2: failed to decode Bech32: %w", err))
}
pair := AtomPair.Apply(util.StringToTerm(h), util.BytesToCodepointListTermWithDefault(a))
pair := prolog.AtomPair.Apply(prolog.StringToTerm(h), prolog.BytesToCodepointListTermWithDefault(a))
return engine.Unify(vm, address, pair, cont, env)
default:
return engine.Error(fmt.Errorf("bech32_address/2: invalid Bech32 type: %T, should be Atom or Variable", b))
Expand All @@ -57,21 +56,21 @@ func Bech32Address(vm *engine.VM, address, bech32 engine.Term, cont engine.Cont,
if err != nil {
return engine.Error(fmt.Errorf("bech32_address/2: %w", err))
}
return engine.Unify(vm, bech32, util.StringToTerm(bech32Decoded), cont, env)
return engine.Unify(vm, bech32, prolog.StringToTerm(bech32Decoded), cont, env)
default:
return engine.Error(fmt.Errorf("bech32_address/2: invalid address type: %T, should be Compound (Hrp, Address)", addressPair))
}
})
}

func addressPairToBech32(addressPair engine.Compound, env *engine.Env) (string, error) {
if addressPair.Functor() != AtomPair || addressPair.Arity() != 2 {
if addressPair.Functor() != prolog.AtomPair || addressPair.Arity() != 2 {
return "", fmt.Errorf("address should be a Pair '-(Hrp, Address)'")
}

switch a := env.Resolve(addressPair.Arg(1)).(type) {
case engine.Compound:
data, err := util.StringTermToBytes(a, "", env)
data, err := prolog.StringTermToBytes(a, "", env)
if err != nil {
return "", fmt.Errorf("failed to convert term to bytes list: %w", err)
}
Expand Down
73 changes: 0 additions & 73 deletions x/logic/predicate/atom.go

This file was deleted.

5 changes: 3 additions & 2 deletions x/logic/predicate/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/ichiban/prolog/engine"
"github.com/okp4/okp4d/x/logic/prolog"

sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -162,8 +163,8 @@ func fetchBalances(
func(ctx context.Context) *engine.Promise {
return engine.Unify(
vm,
util.Tuple(engine.NewAtom(address), CoinsToTerm(coins)),
util.Tuple(account, balances),
prolog.Tuple(engine.NewAtom(address), CoinsToTerm(coins)),
prolog.Tuple(account, balances),
cont,
env,
)
Expand Down
29 changes: 15 additions & 14 deletions x/logic/predicate/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/ichiban/prolog/engine"
"github.com/okp4/okp4d/x/logic/prolog"

"github.com/okp4/okp4d/x/logic/util"
)
Expand Down Expand Up @@ -57,7 +58,7 @@ func CryptoDataHash(
algorithmOpt := engine.NewAtom("algorithm")

return engine.Delay(func(ctx context.Context) *engine.Promise {
algorithmAtom, err := util.GetOptionAsAtomWithDefault(algorithmOpt, options, engine.NewAtom("sha256"), env)
algorithmAtom, err := prolog.GetOptionAsAtomWithDefault(algorithmOpt, options, engine.NewAtom("sha256"), env)
if err != nil {
return engine.Error(fmt.Errorf("%s: %w", functor, err))
}
Expand All @@ -68,7 +69,7 @@ func CryptoDataHash(
algorithmAtom.String(),
util.HashAlgNames()))
}
decodedData, err := termToBytes(data, options, AtomUtf8, env)
decodedData, err := termToBytes(data, options, prolog.AtomUtf8, env)
if err != nil {
return engine.Error(fmt.Errorf("%s: failed to decode data: %w", functor, err))
}
Expand All @@ -78,7 +79,7 @@ func CryptoDataHash(
return engine.Error(fmt.Errorf("%s: failed to hash data: %w", functor, err))
}

return engine.Unify(vm, hash, util.BytesToCodepointListTermWithDefault(result), cont, env)
return engine.Unify(vm, hash, prolog.BytesToCodepointListTermWithDefault(result), cont, env)
})
}

Expand Down Expand Up @@ -167,11 +168,11 @@ func xVerify(functor string, key, data, sig, options engine.Term, defaultAlgo ut
) *engine.Promise {
typeOpt := engine.NewAtom("type")
return engine.Delay(func(ctx context.Context) *engine.Promise {
typeTerm, err := util.GetOptionWithDefault(typeOpt, options, engine.NewAtom(defaultAlgo.String()), env)
typeTerm, err := prolog.GetOptionWithDefault(typeOpt, options, engine.NewAtom(defaultAlgo.String()), env)
if err != nil {
return engine.Error(fmt.Errorf("%s: %w", functor, err))
}
typeAtom, err := util.AssertAtom(env, typeTerm)
typeAtom, err := prolog.AssertAtom(env, typeTerm)
if err != nil {
return engine.Error(fmt.Errorf("%s: %w", functor, err))
}
Expand All @@ -183,17 +184,17 @@ func xVerify(functor string, key, data, sig, options engine.Term, defaultAlgo ut
strings.Join(util.Map(algos, func(a util.KeyAlg) string { return a.String() }), ", ")))
}

decodedKey, err := termToBytes(key, AtomEncoding.Apply(AtomOctet), AtomHex, env)
decodedKey, err := termToBytes(key, prolog.AtomEncoding.Apply(prolog.AtomOctet), prolog.AtomHex, env)
if err != nil {
return engine.Error(fmt.Errorf("%s: failed to decode public key: %w", functor, err))
}

decodedData, err := termToBytes(data, options, AtomHex, env)
decodedData, err := termToBytes(data, options, prolog.AtomHex, env)
if err != nil {
return engine.Error(fmt.Errorf("%s: failed to decode data: %w", functor, err))
}

decodedSignature, err := termToBytes(sig, AtomEncoding.Apply(AtomOctet), AtomHex, env)
decodedSignature, err := termToBytes(sig, prolog.AtomEncoding.Apply(prolog.AtomOctet), prolog.AtomHex, env)
if err != nil {
return engine.Error(fmt.Errorf("%s: failed to decode signature: %w", functor, err))
}
Expand All @@ -212,20 +213,20 @@ func xVerify(functor string, key, data, sig, options engine.Term, defaultAlgo ut
}

func termToBytes(term, options, defaultEncoding engine.Term, env *engine.Env) ([]byte, error) {
encodingTerm, err := util.GetOptionWithDefault(AtomEncoding, options, defaultEncoding, env)
encodingTerm, err := prolog.GetOptionWithDefault(prolog.AtomEncoding, options, defaultEncoding, env)
if err != nil {
return nil, err
}
encodingAtom, err := util.AssertAtom(env, encodingTerm)
encodingAtom, err := prolog.AssertAtom(env, encodingTerm)
if err != nil {
return nil, err
}

switch encodingAtom {
case AtomHex:
return util.TermHexToBytes(term, env)
case AtomOctet, AtomUtf8:
return util.StringTermToBytes(term, "", env)
case prolog.AtomHex:
return prolog.TermHexToBytes(term, env)
case prolog.AtomOctet, prolog.AtomUtf8:
return prolog.StringTermToBytes(term, "", env)
default:
return nil, fmt.Errorf("invalid encoding: %s. Possible values: hex, octet", encodingAtom.String())
}
Expand Down
7 changes: 3 additions & 4 deletions x/logic/predicate/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (

"github.com/ichiban/prolog/engine"
godid "github.com/nuts-foundation/go-did/did"

"github.com/okp4/okp4d/x/logic/util"
"github.com/okp4/okp4d/x/logic/prolog"
)

// AtomDID is a term which represents a DID as a compound term `did(Method, ID, Path, Query, Fragment)`.
Expand Down Expand Up @@ -118,7 +117,7 @@ func processSegment(segments engine.Compound, segmentNumber uint8, fn func(segme
if _, ok := term.(engine.Variable); ok {
return nil
}
segment, err := util.AssertAtom(env, segments.Arg(int(segmentNumber)))
segment, err := prolog.AssertAtom(env, segments.Arg(int(segmentNumber)))
if err != nil {
return fmt.Errorf("failed to resolve atom at segment %d: %w", segmentNumber, err)
}
Expand All @@ -140,7 +139,7 @@ func didToTerms(did *godid.DID) ([]engine.Term, error) {
if err != nil {
return nil, err
}
terms = append(terms, util.StringToTerm(r))
terms = append(terms, prolog.StringToTerm(r))
}

return terms, nil
Expand Down
9 changes: 4 additions & 5 deletions x/logic/predicate/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
"fmt"

"github.com/ichiban/prolog/engine"

"github.com/okp4/okp4d/x/logic/util"
"github.com/okp4/okp4d/x/logic/prolog"
)

// HexBytes is a predicate that unifies hexadecimal encoded bytes to a list of bytes.
Expand Down Expand Up @@ -46,14 +45,14 @@ func HexBytes(vm *engine.VM, hexa, bts engine.Term, cont engine.Cont, env *engin
if result == nil {
return engine.Error(fmt.Errorf("hex_bytes/2: nil hexadecimal conversion in input"))
}
return engine.Unify(vm, bts, util.BytesToCodepointListTermWithDefault(result), cont, env)
return engine.Unify(vm, bts, prolog.BytesToCodepointListTermWithDefault(result), cont, env)
case engine.Compound:
src, err := util.StringTermToBytes(b, "", env)
src, err := prolog.StringTermToBytes(b, "", env)
if err != nil {
return engine.Error(fmt.Errorf("hex_bytes/2: %w", err))
}
dst := hex.EncodeToString(src)
return engine.Unify(vm, hexa, util.StringToTerm(dst), cont, env)
return engine.Unify(vm, hexa, prolog.StringToTerm(dst), cont, env)
default:
return engine.Error(fmt.Errorf("hex_bytes/2: invalid hex type: %T, should be Variable or List", b))
}
Expand Down
Loading

0 comments on commit 5df389e

Please sign in to comment.