Skip to content

Commit 66270c2

Browse files
authored
Merge pull request #756 from axone-protocol/fix/predicate-gas-consumption
Fix/predicate gas consumption
2 parents f621ceb + a1aa708 commit 66270c2

16 files changed

+182
-211
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,17 @@ Targets:
120120
chain-init Initialize the blockchain with default settings.
121121
chain-start Start the blockchain with existing configuration (see chain-init)
122122
chain-stop Stop the blockchain
123-
chain-upgrade Test the chain upgrade from the given FROM_VERSION to the given TO_VERSION
123+
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
124124
Clean:
125125
clean Remove all the files from the target folder
126126
Proto:
127127
proto Generate all resources for proto files (go, doc, etc.)
128-
proto-format Format Protobuf files
129-
proto-build Build all Protobuf files
130128
proto-gen Generate all the code from the Protobuf files
131129
Documentation:
132130
doc Generate all the documentation
133131
doc-proto Generate the documentation from the Protobuf files
134132
doc-command Generate markdown documentation for the command
133+
doc-predicate Generate markdown documentation for all the predicates (module logic)
135134
Mock:
136135
mock Generate all the mocks (for tests)
137136
Release:

docs/predicate/bech32_address_2.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ axone_addr('axone1p8u47en82gmzfm259y6z93r9qe63l25d858vqu').
162162

163163
``` yaml
164164
height: 42
165-
gas_used: 4140
165+
gas_used: 4141
166166
answer:
167167
has_more: false
168168
results:

docs/predicate/consult_1.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ hello(Who).
6565

6666
``` yaml
6767
height: 42
68-
gas_used: 4142
68+
gas_used: 4143
6969
answer:
7070
has_more: false
7171
variables: ["Who"]
@@ -129,7 +129,7 @@ response: |
129129

130130
``` yaml
131131
height: 42
132-
gas_used: 4141
132+
gas_used: 4142
133133
answer:
134134
has_more: false
135135
variables: ["X"]

docs/predicate/current_output_1.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ write_char_to_user_output(x).
6161

6262
``` yaml
6363
height: 42
64-
gas_used: 4240
64+
gas_used: 4241
6565
answer:
6666
has_more: false
6767
variables:
@@ -108,7 +108,7 @@ log_message('Hello world!').
108108

109109
``` yaml
110110
height: 42
111-
gas_used: 4274
111+
gas_used: 4276
112112
answer:
113113
has_more: false
114114
variables:
@@ -155,7 +155,7 @@ log_message('Hello world!').
155155

156156
``` yaml
157157
height: 42
158-
gas_used: 4240
158+
gas_used: 4242
159159
answer:
160160
has_more: false
161161
variables:
@@ -205,7 +205,7 @@ log_message("Hello 🧙!").
205205

206206
``` yaml
207207
height: 42
208-
gas_used: 4254
208+
gas_used: 4263
209209
answer:
210210
has_more: false
211211
variables:

docs/predicate/open_4.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ open(URI, read, _, []).
100100

101101
``` yaml
102102
height: 42
103-
gas_used: 4146
103+
gas_used: 4153
104104
answer:
105105
has_more: false
106106
variables: ["URI"]
@@ -152,7 +152,7 @@ read_resource('cosmwasm:storage:axone15ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08x
152152

153153
``` yaml
154154
height: 42
155-
gas_used: 4142
155+
gas_used: 4144
156156
answer:
157157
has_more: false
158158
variables: ["Chars"]
@@ -202,7 +202,7 @@ read_resource('cosmwasm:storage:axone15ekvz3qdter33mdnk98v8whv5qdr53yusksnfgc08x
202202

203203
``` yaml
204204
height: 42
205-
gas_used: 4142
205+
gas_used: 4144
206206
answer:
207207
has_more: false
208208
variables: ["Chars"]

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ replace (
7676
github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.9.0
7777

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

8181
// replace broken goleveldb
8282
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ github.com/aws/aws-sdk-go v1.44.122/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX
292292
github.com/aws/aws-sdk-go v1.44.224 h1:09CiaaF35nRmxrzWZ2uRq5v6Ghg/d2RiPjZnSgtt+RQ=
293293
github.com/aws/aws-sdk-go v1.44.224/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
294294
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
295-
github.com/axone-protocol/prolog v1.0.0 h1:CASA1QrPOWhYox8YUStML33rekoA/7Gnp/ldDPZqCTA=
296-
github.com/axone-protocol/prolog v1.0.0/go.mod h1:lbZPekEi6qr5WX29GgEmhZlTxUkeWeiJ8cZZRq8qjAE=
295+
github.com/axone-protocol/prolog v1.0.1-0.20240924120526-53584b2b5c0b h1:s4U2NJBSdjZHRVOQuS2USITMFZO+Y3DuQBZagP527Q4=
296+
github.com/axone-protocol/prolog v1.0.1-0.20240924120526-53584b2b5c0b/go.mod h1:lbZPekEi6qr5WX29GgEmhZlTxUkeWeiJ8cZZRq8qjAE=
297297
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
298298
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
299299
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=

x/logic/interpreter/instrument.go

Lines changed: 0 additions & 127 deletions
This file was deleted.

x/logic/interpreter/interpreter.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,23 @@ type Option func(*prolog.Interpreter) error
1717

1818
// WithPredicates configures the interpreter to register the specified predicates.
1919
// See WithPredicate for more details.
20-
func WithPredicates(ctx goctx.Context, predicates []string, hook Hook) Option {
20+
func WithPredicates(ctx goctx.Context, predicates []string) Option {
2121
return func(i *prolog.Interpreter) error {
2222
for _, predicate := range predicates {
23-
if err := WithPredicate(ctx, predicate, hook)(i); err != nil {
23+
if err := WithPredicate(ctx, predicate)(i); err != nil {
2424
return err
2525
}
2626
}
2727
return nil
2828
}
2929
}
3030

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

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

83+
// WithHooks configures the interpreter to use the specified hooks.
84+
func WithHooks(hooks ...engine.HookFunc) Option {
85+
return func(i *prolog.Interpreter) error {
86+
i.InstallHook(
87+
engine.CompositeHook(hooks...),
88+
)
89+
return nil
90+
}
91+
}
92+
8493
// New creates a new prolog.Interpreter with the specified options.
8594
func New(
8695
opts ...Option,

x/logic/interpreter/registry.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,17 @@ var RegistryNames = func() []string {
131131
return names
132132
}()
133133

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

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

153-
invariant := hook(name)
154-
155154
switch arity {
156155
case 0:
157-
i.Register0(atom, Instrument0(invariant, p.(func(*engine.VM, engine.Cont, *engine.Env) *engine.Promise)))
156+
i.Register0(atom, p.(func(*engine.VM, engine.Cont, *engine.Env) *engine.Promise))
158157
case 1:
159-
i.Register1(atom, Instrument1(invariant, p.(func(*engine.VM, engine.Term, engine.Cont, *engine.Env) *engine.Promise)))
158+
i.Register1(atom, p.(func(*engine.VM, engine.Term, engine.Cont, *engine.Env) *engine.Promise))
160159
case 2:
161-
i.Register2(atom, Instrument2(invariant, p.(func(*engine.VM, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise)))
160+
i.Register2(atom, p.(func(*engine.VM, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise))
162161
case 3:
163-
i.Register3(atom, Instrument3(invariant, p.(func(*engine.VM, engine.Term, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise)))
162+
i.Register3(atom, p.(func(*engine.VM, engine.Term, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise))
164163
case 4:
165-
i.Register4(atom, Instrument4(invariant, p.(func(*engine.VM, engine.Term, engine.Term, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise)))
164+
i.Register4(atom, p.(func(*engine.VM, engine.Term, engine.Term, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise))
166165
case 5:
167-
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)))
166+
i.Register5(atom, p.(func(*engine.VM, engine.Term, engine.Term, engine.Term, engine.Term, engine.Term, engine.Cont, *engine.Env) *engine.Promise))
168167
case 6:
169-
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)))
168+
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))
170169
case 7:
171-
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)))
170+
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))
172171
case 8:
173-
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)))
172+
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))
174173
default:
175174
panic(fmt.Sprintf("unsupported arity: %s", name))
176175
}

x/logic/keeper/features/bech32_address_2.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Feature: bech32_address/2
109109
Then the answer we get is:
110110
""" yaml
111111
height: 42
112-
gas_used: 4140
112+
gas_used: 4141
113113
answer:
114114
has_more: false
115115
results:
@@ -130,7 +130,7 @@ Feature: bech32_address/2
130130
Then the answer we get is:
131131
""" yaml
132132
height: 42
133-
gas_used: 4140
133+
gas_used: 4141
134134
answer:
135135
has_more: false
136136
results:

0 commit comments

Comments
 (0)