Skip to content

Commit

Permalink
Merge pull request #756 from axone-protocol/fix/predicate-gas-consump…
Browse files Browse the repository at this point in the history
…tion

Fix/predicate gas consumption
  • Loading branch information
ccamel authored Sep 26, 2024
2 parents f621ceb + a1aa708 commit 66270c2
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 211 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,17 @@ Targets:
chain-init Initialize the blockchain with default settings.
chain-start Start the blockchain with existing configuration (see chain-init)
chain-stop Stop the blockchain
chain-upgrade Test the chain upgrade from the given FROM_VERSION to the given TO_VERSION
chain-upgrade Test the chain upgrade from the given FROM_VERSION to the given TO_VERSION. You can pass also the proposal json file on PROPOSAL var
Clean:
clean Remove all the files from the target folder
Proto:
proto Generate all resources for proto files (go, doc, etc.)
proto-format Format Protobuf files
proto-build Build all Protobuf files
proto-gen Generate all the code from the Protobuf files
Documentation:
doc Generate all the documentation
doc-proto Generate the documentation from the Protobuf files
doc-command Generate markdown documentation for the command
doc-predicate Generate markdown documentation for all the predicates (module logic)
Mock:
mock Generate all the mocks (for tests)
Release:
Expand Down
2 changes: 1 addition & 1 deletion docs/predicate/bech32_address_2.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ axone_addr('axone1p8u47en82gmzfm259y6z93r9qe63l25d858vqu').

``` yaml
height: 42
gas_used: 4140
gas_used: 4141
answer:
has_more: false
results:
Expand Down
4 changes: 2 additions & 2 deletions docs/predicate/consult_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ hello(Who).

``` yaml
height: 42
gas_used: 4142
gas_used: 4143
answer:
has_more: false
variables: ["Who"]
Expand Down Expand Up @@ -129,7 +129,7 @@ response: |

``` yaml
height: 42
gas_used: 4141
gas_used: 4142
answer:
has_more: false
variables: ["X"]
Expand Down
8 changes: 4 additions & 4 deletions docs/predicate/current_output_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ write_char_to_user_output(x).

``` yaml
height: 42
gas_used: 4240
gas_used: 4241
answer:
has_more: false
variables:
Expand Down Expand Up @@ -108,7 +108,7 @@ log_message('Hello world!').

``` yaml
height: 42
gas_used: 4274
gas_used: 4276
answer:
has_more: false
variables:
Expand Down Expand Up @@ -155,7 +155,7 @@ log_message('Hello world!').

``` yaml
height: 42
gas_used: 4240
gas_used: 4242
answer:
has_more: false
variables:
Expand Down Expand Up @@ -205,7 +205,7 @@ log_message("Hello 🧙!").

``` yaml
height: 42
gas_used: 4254
gas_used: 4263
answer:
has_more: false
variables:
Expand Down
6 changes: 3 additions & 3 deletions docs/predicate/open_4.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ open(URI, read, _, []).

``` yaml
height: 42
gas_used: 4146
gas_used: 4153
answer:
has_more: false
variables: ["URI"]
Expand Down Expand Up @@ -152,7 +152,7 @@ read_resource('cosmwasm:storage:axone15ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08x

``` yaml
height: 42
gas_used: 4142
gas_used: 4144
answer:
has_more: false
variables: ["Chars"]
Expand Down Expand Up @@ -202,7 +202,7 @@ read_resource('cosmwasm:storage:axone15ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08x

``` yaml
height: 42
gas_used: 4142
gas_used: 4144
answer:
has_more: false
variables: ["Chars"]
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ replace (
github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.9.0

// Use cutom fork of prolog interpreter
github.com/ichiban/prolog => github.com/axone-protocol/prolog v1.0.0
github.com/ichiban/prolog => github.com/axone-protocol/prolog v1.0.1-0.20240924120526-53584b2b5c0b

// replace broken goleveldb
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ github.com/aws/aws-sdk-go v1.44.122/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX
github.com/aws/aws-sdk-go v1.44.224 h1:09CiaaF35nRmxrzWZ2uRq5v6Ghg/d2RiPjZnSgtt+RQ=
github.com/aws/aws-sdk-go v1.44.224/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
github.com/axone-protocol/prolog v1.0.0 h1:CASA1QrPOWhYox8YUStML33rekoA/7Gnp/ldDPZqCTA=
github.com/axone-protocol/prolog v1.0.0/go.mod h1:lbZPekEi6qr5WX29GgEmhZlTxUkeWeiJ8cZZRq8qjAE=
github.com/axone-protocol/prolog v1.0.1-0.20240924120526-53584b2b5c0b h1:s4U2NJBSdjZHRVOQuS2USITMFZO+Y3DuQBZagP527Q4=
github.com/axone-protocol/prolog v1.0.1-0.20240924120526-53584b2b5c0b/go.mod h1:lbZPekEi6qr5WX29GgEmhZlTxUkeWeiJ8cZZRq8qjAE=
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
Expand Down
127 changes: 0 additions & 127 deletions x/logic/interpreter/instrument.go

This file was deleted.

23 changes: 16 additions & 7 deletions x/logic/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,23 @@ type Option func(*prolog.Interpreter) error

// WithPredicates configures the interpreter to register the specified predicates.
// See WithPredicate for more details.
func WithPredicates(ctx goctx.Context, predicates []string, hook Hook) Option {
func WithPredicates(ctx goctx.Context, predicates []string) Option {
return func(i *prolog.Interpreter) error {
for _, predicate := range predicates {
if err := WithPredicate(ctx, predicate, hook)(i); err != nil {
if err := WithPredicate(ctx, predicate)(i); err != nil {
return err
}
}
return nil
}
}

// WithPredicate configures the interpreter to register the specified predicate with the specified hook.
// The hook is a function that is called before the predicate is executed and can be used to check some conditions,
// like the gas consumption or the permission to execute the predicate.
// WithPredicate configures the interpreter to register the specified predicate.
//
// The predicates names must be present in the registry, otherwise the function will return an error.
func WithPredicate(_ goctx.Context, predicate string, hook Hook) Option {
func WithPredicate(_ goctx.Context, predicate string) Option {
return func(i *prolog.Interpreter) error {
if err := Register(i, predicate, hook); err != nil {
if err := Register(i, predicate); err != nil {
return fmt.Errorf("error registering predicate '%s': %w", predicate, err)
}
return nil
Expand Down Expand Up @@ -70,6 +68,7 @@ func WithFS(fs fs.FS) Option {
}
}

// WithMaxVariables configures the interpreter to use the specified maximum number of variables.
func WithMaxVariables(maxVariables *math.Uint) Option {
return func(i *prolog.Interpreter) error {
if maxVariables != nil {
Expand All @@ -81,6 +80,16 @@ func WithMaxVariables(maxVariables *math.Uint) Option {
}
}

// WithHooks configures the interpreter to use the specified hooks.
func WithHooks(hooks ...engine.HookFunc) Option {
return func(i *prolog.Interpreter) error {
i.InstallHook(
engine.CompositeHook(hooks...),
)
return nil
}
}

// New creates a new prolog.Interpreter with the specified options.
func New(
opts ...Option,
Expand Down
35 changes: 17 additions & 18 deletions x/logic/interpreter/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,17 @@ var RegistryNames = func() []string {
return names
}()

type Hook = func(functor string) func(env *engine.Env) error
// IsRegistered returns true if the predicate with the given name is registered in the interpreter.
// Registered predicates are built-in predicates that are available in the interpreter.
func IsRegistered(name string) bool {
_, ok := registry.Get(name)
return ok
}

// Register registers a well-known predicate in the interpreter with support for consumption measurement.
// name is the name of the predicate in the form of "atom/arity".
// cost is the cost of executing the predicate.
// meter is the gas meter object that is called when the predicate is called and which allows to count the cost of
// executing the predicate(ctx).
// Register registers a well-known predicate in the interpreter.
//
//nolint:lll
func Register(i *prolog.Interpreter, name string, hook Hook) error {
func Register(i *prolog.Interpreter, name string) error {
if p, ok := registry.Get(name); ok {
parts := strings.Split(name, "/")
if len(parts) == 2 {
Expand All @@ -150,27 +151,25 @@ func Register(i *prolog.Interpreter, name string, hook Hook) error {
return err
}

invariant := hook(name)

switch arity {
case 0:
i.Register0(atom, Instrument0(invariant, p.(func(*engine.VM, engine.Cont, *engine.Env) *engine.Promise)))
i.Register0(atom, p.(func(*engine.VM, engine.Cont, *engine.Env) *engine.Promise))
case 1:
i.Register1(atom, Instrument1(invariant, p.(func(*engine.VM, engine.Term, engine.Cont, *engine.Env) *engine.Promise)))
i.Register1(atom, p.(func(*engine.VM, engine.Term, engine.Cont, *engine.Env) *engine.Promise))
case 2:
i.Register2(atom, Instrument2(invariant, p.(func(*engine.VM, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise)))
i.Register2(atom, p.(func(*engine.VM, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise))
case 3:
i.Register3(atom, Instrument3(invariant, p.(func(*engine.VM, engine.Term, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise)))
i.Register3(atom, p.(func(*engine.VM, engine.Term, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise))
case 4:
i.Register4(atom, Instrument4(invariant, p.(func(*engine.VM, engine.Term, engine.Term, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise)))
i.Register4(atom, p.(func(*engine.VM, engine.Term, engine.Term, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise))
case 5:
i.Register5(atom, Instrument5(invariant, p.(func(*engine.VM, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise)))
i.Register5(atom, p.(func(*engine.VM, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise))
case 6:
i.Register6(atom, Instrument6(invariant, p.(func(*engine.VM, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise)))
i.Register6(atom, p.(func(*engine.VM, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise))
case 7:
i.Register7(atom, Instrument7(invariant, p.(func(*engine.VM, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise)))
i.Register7(atom, p.(func(*engine.VM, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise))
case 8:
i.Register8(atom, Instrument8(invariant, p.(func(*engine.VM, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise)))
i.Register8(atom, p.(func(*engine.VM, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise))
default:
panic(fmt.Sprintf("unsupported arity: %s", name))
}
Expand Down
4 changes: 2 additions & 2 deletions x/logic/keeper/features/bech32_address_2.feature
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Feature: bech32_address/2
Then the answer we get is:
""" yaml
height: 42
gas_used: 4140
gas_used: 4141
answer:
has_more: false
results:
Expand All @@ -130,7 +130,7 @@ Feature: bech32_address/2
Then the answer we get is:
""" yaml
height: 42
gas_used: 4140
gas_used: 4141
answer:
has_more: false
results:
Expand Down
Loading

0 comments on commit 66270c2

Please sign in to comment.