From a6292cd7dcb463978da6e53365b2deaf43db7069 Mon Sep 17 00:00:00 2001 From: Chris Gianelloni Date: Thu, 26 Oct 2023 09:16:41 -0400 Subject: [PATCH] feat: filter-pool for blocks Signed-off-by: Chris Gianelloni --- filter/chainsync/chainsync.go | 44 ++++++++++++++++++++++++++++++++++- filter/chainsync/option.go | 13 ++++++++--- filter/chainsync/plugin.go | 33 +++++++++++++++++++------- go.mod | 2 +- go.sum | 4 ++-- 5 files changed, 81 insertions(+), 15 deletions(-) diff --git a/filter/chainsync/chainsync.go b/filter/chainsync/chainsync.go index c34931e..15c34ef 100644 --- a/filter/chainsync/chainsync.go +++ b/filter/chainsync/chainsync.go @@ -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" @@ -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 @@ -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 { diff --git a/filter/chainsync/option.go b/filter/chainsync/option.go index 4d5b7bf..5739e52 100644 --- a/filter/chainsync/option.go +++ b/filter/chainsync/option.go @@ -23,6 +23,13 @@ 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) { @@ -30,9 +37,9 @@ func WithPolicies(policyIds []string) ChainSyncOptionFunc { } } -// 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[:] } } diff --git a/filter/chainsync/plugin.go b/filter/chainsync/plugin.go index dd017db..88c2f60 100644 --- a/filter/chainsync/plugin.go +++ b/filter/chainsync/plugin.go @@ -22,8 +22,9 @@ import ( var cmdlineOptions struct { address string - policyId string asset string + policyId string + poolId string } func init() { @@ -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, @@ -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", }, }, }, @@ -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, @@ -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, ","), ), ) } diff --git a/go.mod b/go.mod index d16188e..e0a40fb 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 772305d..f4fc04b 100644 --- a/go.sum +++ b/go.sum @@ -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=