From 70b0bc063af66239e3f225cbb2da935d681dc86f Mon Sep 17 00:00:00 2001 From: ccamel Date: Wed, 4 Jan 2023 22:43:17 +0100 Subject: [PATCH] feat(logic): add block_height/1 predicate --- x/logic/predicate/block.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 x/logic/predicate/block.go diff --git a/x/logic/predicate/block.go b/x/logic/predicate/block.go new file mode 100644 index 00000000..d7876d18 --- /dev/null +++ b/x/logic/predicate/block.go @@ -0,0 +1,25 @@ +package predicate + +import ( + "context" + + "github.com/ichiban/prolog/engine" +) + +// BlockHeight is higher order function that given a context returns the following predicate: +// +// block_height(?Height) +// +// where Height represents the current chain height at the time of the query. +// The predicate is non-deterministic, producing a different height each time it is called. +func BlockHeight(ctx context.Context) engine.Predicate1 { + return func(vm *engine.VM, height engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise { + sdkContext, err := UnwrapSDKContext(ctx) + if err != nil { + return engine.Error(err) + } + + return engine.Unify(vm, height, engine.Integer(sdkContext.BlockHeight()), cont, env) + } +} +