Skip to content

Commit

Permalink
feat: filter-pool for blocks
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Gianelloni <[email protected]>
  • Loading branch information
wolf31o2 committed Oct 27, 2023
1 parent 2226d1f commit a6292cd
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 15 deletions.
44 changes: 43 additions & 1 deletion filter/chainsync/chainsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package chainsync

import (
"encoding/hex"
"strings"

"github.com/blinklabs-io/gouroboros/bech32"
"github.com/blinklabs-io/gouroboros/ledger"
"github.com/blinklabs-io/snek/event"
"github.com/blinklabs-io/snek/input/chainsync"
Expand All @@ -27,8 +29,9 @@ type ChainSync struct {
inputChan chan event.Event
outputChan chan event.Event
filterAddresses []string
filterPolicyIds []string
filterAssetFingerprints []string
filterPolicyIds []string
filterPoolIds []string
}

// New returns a new ChainSync object with the specified options applied
Expand All @@ -55,6 +58,45 @@ func (c *ChainSync) Start() error {
return
}
switch v := evt.Payload.(type) {
case chainsync.BlockEvent:
// Check pool filter
if len(c.filterPoolIds) > 0 {
filterMatched := false
for _, filterPoolId := range c.filterPoolIds {
isPoolBech32 := strings.HasPrefix(filterPoolId, "pool")
foundMatch := false
be := evt.Payload.(chainsync.BlockEvent)
if be.IssuerVkey == filterPoolId {
foundMatch = true
} else if isPoolBech32 {
issuerBytes, err := hex.DecodeString(be.IssuerVkey)
if err != nil {
// eat this error... nom nom nom
continue
}
// lifted from gouroboros/ledger
convData, err := bech32.ConvertBits(issuerBytes, 8, 5, true)
if err != nil {
continue
}
encoded, err := bech32.Encode("pool", convData)
if err != nil {
continue
}
if encoded == filterPoolId {
foundMatch = true
}
}
if foundMatch {
filterMatched = true
break
}
}
// Skip the event if none of the filter values matched
if !filterMatched {
continue
}
}
case chainsync.TransactionEvent:
// Check address filter
if len(c.filterAddresses) > 0 {
Expand Down
13 changes: 10 additions & 3 deletions filter/chainsync/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ func WithAddresses(addresses []string) ChainSyncOptionFunc {
}
}

// WithAssetFingerprints specifies the asset fingerprint (asset1xxx) to filter on
func WithAssetFingerprints(assetFingerprints []string) ChainSyncOptionFunc {
return func(c *ChainSync) {
c.filterAssetFingerprints = assetFingerprints[:]
}
}

// WithPolicies specfies the address to filter on
func WithPolicies(policyIds []string) ChainSyncOptionFunc {
return func(c *ChainSync) {
c.filterPolicyIds = policyIds[:]
}
}

// WithAssetFingerprints specifies the asset fingerprint (asset1xxx) to filter on
func WithAssetFingerprints(assetFingerprints []string) ChainSyncOptionFunc {
// WithPoolIds specifies the pool to filter on
func WithPoolIds(poolIds []string) ChainSyncOptionFunc {
return func(c *ChainSync) {
c.filterAssetFingerprints = assetFingerprints[:]
c.filterPoolIds = poolIds[:]
}
}
33 changes: 25 additions & 8 deletions filter/chainsync/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import (

var cmdlineOptions struct {
address string
policyId string
asset string
policyId string
poolId string
}

func init() {
Expand All @@ -42,6 +43,14 @@ func init() {
Dest: &(cmdlineOptions.address),
CustomFlag: "address",
},
{
Name: "asset",
Type: plugin.PluginOptionTypeString,
Description: "specifies the asset fingerprint (asset1xxx) to filter on",
DefaultValue: "",
Dest: &(cmdlineOptions.asset),
CustomFlag: "asset",
},
{
Name: "policy",
Type: plugin.PluginOptionTypeString,
Expand All @@ -51,12 +60,12 @@ func init() {
CustomFlag: "policy",
},
{
Name: "asset",
Name: "pool",
Type: plugin.PluginOptionTypeString,
Description: "specifies the asset fingerprint (asset1xxx) to filter on",
Description: "specifies Pool ID to filter on",
DefaultValue: "",
Dest: &(cmdlineOptions.asset),
CustomFlag: "asset",
Dest: &(cmdlineOptions.poolId),
CustomFlag: "pool",
},
},
},
Expand All @@ -73,6 +82,14 @@ func NewFromCmdlineOptions() plugin.Plugin {
),
)
}
if cmdlineOptions.asset != "" {
pluginOptions = append(
pluginOptions,
WithAssetFingerprints(
strings.Split(cmdlineOptions.asset, ","),
),
)
}
if cmdlineOptions.policyId != "" {
pluginOptions = append(
pluginOptions,
Expand All @@ -81,11 +98,11 @@ func NewFromCmdlineOptions() plugin.Plugin {
),
)
}
if cmdlineOptions.asset != "" {
if cmdlineOptions.poolId != "" {
pluginOptions = append(
pluginOptions,
WithAssetFingerprints(
strings.Split(cmdlineOptions.asset, ","),
WithPoolIds(
strings.Split(cmdlineOptions.poolId, ","),
),
)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/blinklabs-io/snek
go 1.20

require (
github.com/blinklabs-io/gouroboros v0.58.0
github.com/blinklabs-io/gouroboros v0.59.0
github.com/gen2brain/beeep v0.0.0-20230602101333-f384c29b62dd
github.com/gin-gonic/gin v1.9.1
github.com/kelseyhightower/envconfig v1.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGB
cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/blinklabs-io/gouroboros v0.58.0 h1:W1/fjntOfJ3Yn41/SjgxV4cS7XSuogCZYznygRXUj8Q=
github.com/blinklabs-io/gouroboros v0.58.0/go.mod h1:D5YJka8EyVmiXNMbRvjH23H9lNMLA4+qSlNNC/j7R0k=
github.com/blinklabs-io/gouroboros v0.59.0 h1:oNJrxg3CEmWoq1hPav9p1lkdDcnCy10AlGDfjHTf/8M=
github.com/blinklabs-io/gouroboros v0.59.0/go.mod h1:D5YJka8EyVmiXNMbRvjH23H9lNMLA4+qSlNNC/j7R0k=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE=
Expand Down

0 comments on commit a6292cd

Please sign in to comment.