-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(x/intent): index intent mentioned addresses (#112)
* 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
Showing
72 changed files
with
19,809 additions
and
24,846 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.