Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter-processor: move reflection into generated code #1463

Merged
merged 9 commits into from
Feb 15, 2023

Conversation

winder
Copy link
Contributor

@winder winder commented Feb 13, 2023

Summary

These changes focus on updating the generated SignedTxnFunc function.

  • Renamed to LookupFieldByTag.
  • Cast return values to uint64, int64 and string.
  • Do not allow sub-tags. i.e. previously txn was a valid tag, now only leaves are allowed such as txn.fee.
  • Base64 encode "data" types like the note field, previously they were silently unsupported.
  • Disallow unsupported types like slices (i.e. foreign accounts, app arguments) and maps (i.e. eval delta values).
  • Minor refactoring outside the generate function (i.e. the String() function can no longer be called, because it happens inside the generated code).
  • Adds initial support for switching to the SDK transaction type.

Followup:

  • Because there are now only 3 types returned much of the code can be simplified.
  • Stop using pointers in generated code. It is currently required because the reflect packages widely at runtime.
  • Convert integration style tests to unit tests (i.e. there are no Searcher unit tests, they all begin with creating a processor).

Test Plan

There are extensive unit tests to cover regressions.

@winder winder added the Enhancement New feature or request label Feb 13, 2023
Comment on lines +20 to +133
"txn.votekey": true,
"txn.selkey": true,
"txn.sprfkey": true,
// no point in filtering on state proof things?
"txn.sp.c": true,
"txn.sp.S.pth": true,
"txn.sp.S.hsh": true,
"txn.sp.S.hsh.t": true,
"txn.sp.P.pth": true,
"txn.sp.P.hsh": true,
"txn.sp.P.hsh.t": true,
"txn.sp.r": true,
"txn.sp.pr": true,
"txn.spmsg.b": true,
"txn.spmsg.v": true,
// inner transactions are handled differently
"dt.itx": true,
// TODO: this can be removed if the sub fields are supported
"dt": true,
// TODO: support map types?
"dt.gd": true,
"dt.ld": true,
// TODO: support array types?
"txn.apaa": true,
"txn.apat": true,
"txn.apfa": true,
"txn.apbx": true,
"txn.apas": true,
"txn.apap": true,
"txn.apsu": true,
"dt.lg": true,
}

func noCast(t reflect.StructField) bool {
switch reflect.New(t.Type).Elem().Interface().(type) {
case uint64:
return true
case int64:
return true
case string:
return true
}
return false
}

func simpleCast(t reflect.StructField) string {
switch reflect.New(t.Type).Elem().Interface().(type) {
// unsigned
case uint:
return "uint64"
case uint8:
return "uint64"
case uint16:
return "uint64"
case uint32:
return "uint64"
// signed
case int:
return "int64"
case int8:
return "int64"
case int16:
return "int64"
case int32:
return "int64" //
// alias
// SDK types
case types.MicroAlgos:
// SDK microalgo does not need ".Raw"
return "uint64"
case types.AssetIndex:
return "uint64"
case types.AppIndex:
return "uint64"
case types.Round:
return "uint64"
case types.OnCompletion:
return "uint64"
case types.StateProofType:
return "uint64"
case types.TxType:
return "string"
// go-algorand types
case basics.AssetIndex:
return "uint64"
case basics.AppIndex:
return "uint64"
case basics.Round:
return "uint64"
case transactions.OnCompletion:
return "uint64"
case protocol.StateProofType:
return "uint64"
case protocol.TxType:
return "string"

}
return ""

}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a lot, but it really isn't. The code is setup so that you re-run the generator and address un-handled types until everything is handled.

Comment on lines +247 to +269
const templateStr = `// Code generated via go generate. DO NOT EDIT.

package {{ .PackageName }}

import (
"encoding/base64"
"fmt"

"github.com/algorand/go-algorand/data/transactions"
)

// LookupFieldByTag takes a tag and associated SignedTxnInBlock and returns the value
// referenced by the tag. An error is returned if the tag does not exist
func LookupFieldByTag(tag string, input *transactions.SignedTxnInBlock) (interface{}, error) {
switch tag {
{{ range .StructFields }} case "{{ .TagPath }}":
value := {{ ReturnValue . "input" }}
return &value, nil
{{ end }} default:
return nil, fmt.Errorf("unknown tag: %s", tag)
}
}
`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now using the text/template package instead of string building.

@codecov
Copy link

codecov bot commented Feb 13, 2023

Codecov Report

Merging #1463 (1c92b48) into develop (2805588) will decrease coverage by 0.56%.
The diff coverage is 10.00%.

@@             Coverage Diff             @@
##           develop    #1463      +/-   ##
===========================================
- Coverage    65.37%   64.82%   -0.56%     
===========================================
  Files           83       84       +1     
  Lines        11414    11462      +48     
===========================================
- Hits          7462     7430      -32     
- Misses        3381     3464      +83     
+ Partials       571      568       -3     
Impacted Files Coverage Δ
...rocessors/filterprocessor/expression/expression.go 58.57% <ø> (-10.01%) ⬇️
...plugins/processors/filterprocessor/gen/generate.go 0.00% <0.00%> (ø)
...s/processors/filterprocessor/gen/internal/utils.go 0.00% <0.00%> (ø)
...filterprocessor/fields/generated_signed_txn_map.go 17.08% <16.03%> (+5.08%) ⬆️
...gins/processors/filterprocessor/fields/searcher.go 75.67% <100.00%> (-7.02%) ⬇️
...ins/processors/filterprocessor/filter_processor.go 80.55% <100.00%> (ø)
...ilterprocessor/expression/numerical_expressions.go 21.73% <0.00%> (-28.99%) ⬇️
... and 1 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@winder winder marked this pull request as ready for review February 13, 2023 17:52
@winder winder marked this pull request as draft February 14, 2023 13:58
@winder winder marked this pull request as ready for review February 14, 2023 13:59
Copy link
Contributor

@Eric-Warehime Eric-Warehime left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would maybe like more unit tests here. Or maybe we should just be marking any gen/ files as excluded since they're sort of tested by outputting the generated code itself?

@@ -0,0 +1 @@
package main
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this do anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. I was thinking about adding tests, I'll do that in the next PR.

@winder winder merged commit 1ed87ae into algorand:develop Feb 15, 2023
@winder winder deleted the will/filter-gen-resolve-type branch February 15, 2023 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants