Skip to content

Commit

Permalink
feat(x/intent): index intent mentioned addresses (#112)
Browse files Browse the repository at this point in the history
* feat(shield): Validate extracts some useful Metadata traversing the parsed AST

* feat(x/intent): store the list of addresses referenced by the intent definition

* feat(x/intent): store the creator of the intent

* chore(spaceward): regen hooks

* chore: remove outdated and wrong notice
  • Loading branch information
Pitasi committed Mar 21, 2024
1 parent 1e02be6 commit e4c1290
Show file tree
Hide file tree
Showing 72 changed files with 19,809 additions and 24,846 deletions.
254 changes: 231 additions & 23 deletions api/warden/intent/intent.pulsar.go

Large diffs are not rendered by default.

20,894 changes: 7,658 additions & 13,236 deletions docs/static/openapi.yml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.8.4
github.com/yoheimuta/go-protoparser/v4 v4.9.0
golang.org/x/crypto v0.17.0
golang.org/x/tools v0.15.0
google.golang.org/genproto/googleapis/api v0.0.0-20231120223509-83a465c0220f
Expand Down Expand Up @@ -228,7 +229,6 @@ require (
github.com/tidwall/btree v1.7.0 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/vbatts/tar-split v0.11.5 // indirect
github.com/yoheimuta/go-protoparser/v4 v4.9.0 // indirect
github.com/zondax/hid v0.9.2 // indirect
github.com/zondax/ledger-go v0.14.3 // indirect
go.etcd.io/bbolt v1.3.8 // indirect
Expand Down
8 changes: 6 additions & 2 deletions proto/warden/intent/intent.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ option go_package = "github.com/warden-protocol/wardenprotocol/warden/x/intent/t

message Intent {
uint64 id = 1;
string name = 2;
string creator = 2;
string name = 3;

// The definition of the intent written in the Shield language.
string definition = 3;
string definition = 4;

// The list of addresses referenced from the intent definition.
repeated string addresses = 5;
}

49 changes: 49 additions & 0 deletions shield/internal/metadata/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package metadata

import (
"github.com/warden-protocol/wardenprotocol/shield/internal/ast"
)

// Metadata contains informations about a parsed program.
type Metadata struct {
// Identifiers is a list of all identifiers found in the program.
// Except for function names.
Identifiers []string

// FunctionIdentifiers is a list of all function names found in the program.
FunctionIdentifiers []string
}

func (m *Metadata) AddIdentifier(identifier string) {
m.Identifiers = append(m.Identifiers, identifier)
}

func (m *Metadata) AddFunction(identifier string) {
m.FunctionIdentifiers = append(m.FunctionIdentifiers, identifier)
}

// ExtractMetadata extracts metadata from an expression.
func ExtractMetadata(expr ast.Expression) Metadata {
var metadata Metadata
processNode(expr, &metadata)
return metadata
}

func processNode(node ast.Expression, metadata *Metadata) {
switch n := node.(type) {
case *ast.Identifier:
metadata.AddIdentifier(n.Value)
case *ast.InfixExpression:
processNode(n.Left, metadata)
processNode(n.Right, metadata)
case *ast.CallExpression:
metadata.AddFunction(n.Function.Value)
for _, arg := range n.Arguments {
processNode(arg, metadata)
}
case *ast.ArrayLiteral:
for _, e := range n.Elements {
processNode(e, metadata)
}
}
}
97 changes: 97 additions & 0 deletions shield/internal/metadata/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package metadata

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/warden-protocol/wardenprotocol/shield/internal/ast"
"github.com/warden-protocol/wardenprotocol/shield/internal/lexer"
"github.com/warden-protocol/wardenprotocol/shield/internal/parser"
)

func TestExtractMetadata(t *testing.T) {
tests := []struct {
code string
identifiers []string
functions []string
}{
{
code: "foo",
identifiers: []string{"foo"},
functions: nil,
},
{
code: "foo()",
identifiers: []string{},
functions: []string{"foo"},
},
{
code: "foo(bar)",
identifiers: []string{"bar"},
functions: []string{"foo"},
},
{
code: "foo(bar, baz)",
identifiers: []string{"bar", "baz"},
functions: []string{"foo"},
},
{
code: "foo(bar, baz, qux)",
identifiers: []string{"bar", "baz", "qux"},
functions: []string{"foo"},
},
{
code: "foo(bar, baz, qux) && quux",
identifiers: []string{"bar", "baz", "qux", "quux"},
functions: []string{"foo"},
},
{
code: "foo(bar, baz, qux) && quux()",
identifiers: []string{"bar", "baz", "qux"},
functions: []string{"foo", "quux"},
},
{
code: "foo(bar, baz, qux) && quux() && corge",
identifiers: []string{"bar", "baz", "qux", "corge"},
functions: []string{"foo", "quux"},
},
{
code: "foo(bar, baz, qux) && quux() && corge()",
identifiers: []string{"bar", "baz", "qux"},
functions: []string{"foo", "quux", "corge"},
},
{
code: "[foo, bar, baz]",
identifiers: []string{"foo", "bar", "baz"},
functions: nil,
},
{
code: "[foo(), bar(), baz()]",
identifiers: []string{},
functions: []string{"foo", "bar", "baz"},
},
{
code: "[1, 2, 3, foo]",
identifiers: []string{"foo"},
functions: nil,
},
}

for _, tt := range tests {
expr, metadata := testExtractMetadata(t, tt.code)
require.ElementsMatchf(t, tt.identifiers, metadata.Identifiers, "Wrong Identifiers list.\nCode:\n%s\nParsed expression:\n%+v", tt.code, expr)
require.ElementsMatchf(t, tt.functions, metadata.FunctionIdentifiers, "Wrong FunctionIdentifiers list. Code: %s", tt.code)
}
}

func testExtractMetadata(t *testing.T, code string) (ast.Expression, Metadata) {
t.Helper()
l := lexer.New(code)
p := parser.New(l)
if p.Errors() != nil {
t.Fatalf("parser errors: %v", p.Errors())
}

expr := p.Parse()
return expr, ExtractMetadata(expr)
}
17 changes: 12 additions & 5 deletions shield/shield.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/warden-protocol/wardenprotocol/shield/internal/evaluator"
"github.com/warden-protocol/wardenprotocol/shield/internal/lexer"
"github.com/warden-protocol/wardenprotocol/shield/internal/metadata"
"github.com/warden-protocol/wardenprotocol/shield/internal/parser"
"github.com/warden-protocol/wardenprotocol/shield/object"
)
Expand All @@ -24,16 +25,22 @@ func Run(input string, env Environment) (object.Object, error) {
return res, nil
}

func Validate(input string) error {
type Metadata = metadata.Metadata

// Validate is a static check for parsing the code, ensuring the syntax is correct.
// It also extracts metadata from the code while doing it.
// It returns an error if the input is empty or if there are parsing errors.
func Validate(input string) (Metadata, error) {
if input == "" {
return fmt.Errorf("empty input")
return Metadata{}, fmt.Errorf("empty input")
}

l := lexer.New(input)
p := parser.New(l)
p.Parse()
expr := p.Parse()
if len(p.Errors()) > 0 {
return fmt.Errorf("parser errors: %v", p.Errors())
return Metadata{}, fmt.Errorf("parser errors: %v", p.Errors())
}
return nil

return metadata.ExtractMetadata(expr), nil
}

0 comments on commit e4c1290

Please sign in to comment.