Skip to content

Commit a6292cd

Browse files
committed
feat: filter-pool for blocks
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent 2226d1f commit a6292cd

File tree

5 files changed

+81
-15
lines changed

5 files changed

+81
-15
lines changed

filter/chainsync/chainsync.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
package chainsync
1616

1717
import (
18+
"encoding/hex"
1819
"strings"
1920

21+
"github.com/blinklabs-io/gouroboros/bech32"
2022
"github.com/blinklabs-io/gouroboros/ledger"
2123
"github.com/blinklabs-io/snek/event"
2224
"github.com/blinklabs-io/snek/input/chainsync"
@@ -27,8 +29,9 @@ type ChainSync struct {
2729
inputChan chan event.Event
2830
outputChan chan event.Event
2931
filterAddresses []string
30-
filterPolicyIds []string
3132
filterAssetFingerprints []string
33+
filterPolicyIds []string
34+
filterPoolIds []string
3235
}
3336

3437
// New returns a new ChainSync object with the specified options applied
@@ -55,6 +58,45 @@ func (c *ChainSync) Start() error {
5558
return
5659
}
5760
switch v := evt.Payload.(type) {
61+
case chainsync.BlockEvent:
62+
// Check pool filter
63+
if len(c.filterPoolIds) > 0 {
64+
filterMatched := false
65+
for _, filterPoolId := range c.filterPoolIds {
66+
isPoolBech32 := strings.HasPrefix(filterPoolId, "pool")
67+
foundMatch := false
68+
be := evt.Payload.(chainsync.BlockEvent)
69+
if be.IssuerVkey == filterPoolId {
70+
foundMatch = true
71+
} else if isPoolBech32 {
72+
issuerBytes, err := hex.DecodeString(be.IssuerVkey)
73+
if err != nil {
74+
// eat this error... nom nom nom
75+
continue
76+
}
77+
// lifted from gouroboros/ledger
78+
convData, err := bech32.ConvertBits(issuerBytes, 8, 5, true)
79+
if err != nil {
80+
continue
81+
}
82+
encoded, err := bech32.Encode("pool", convData)
83+
if err != nil {
84+
continue
85+
}
86+
if encoded == filterPoolId {
87+
foundMatch = true
88+
}
89+
}
90+
if foundMatch {
91+
filterMatched = true
92+
break
93+
}
94+
}
95+
// Skip the event if none of the filter values matched
96+
if !filterMatched {
97+
continue
98+
}
99+
}
58100
case chainsync.TransactionEvent:
59101
// Check address filter
60102
if len(c.filterAddresses) > 0 {

filter/chainsync/option.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,23 @@ func WithAddresses(addresses []string) ChainSyncOptionFunc {
2323
}
2424
}
2525

26+
// WithAssetFingerprints specifies the asset fingerprint (asset1xxx) to filter on
27+
func WithAssetFingerprints(assetFingerprints []string) ChainSyncOptionFunc {
28+
return func(c *ChainSync) {
29+
c.filterAssetFingerprints = assetFingerprints[:]
30+
}
31+
}
32+
2633
// WithPolicies specfies the address to filter on
2734
func WithPolicies(policyIds []string) ChainSyncOptionFunc {
2835
return func(c *ChainSync) {
2936
c.filterPolicyIds = policyIds[:]
3037
}
3138
}
3239

33-
// WithAssetFingerprints specifies the asset fingerprint (asset1xxx) to filter on
34-
func WithAssetFingerprints(assetFingerprints []string) ChainSyncOptionFunc {
40+
// WithPoolIds specifies the pool to filter on
41+
func WithPoolIds(poolIds []string) ChainSyncOptionFunc {
3542
return func(c *ChainSync) {
36-
c.filterAssetFingerprints = assetFingerprints[:]
43+
c.filterPoolIds = poolIds[:]
3744
}
3845
}

filter/chainsync/plugin.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ import (
2222

2323
var cmdlineOptions struct {
2424
address string
25-
policyId string
2625
asset string
26+
policyId string
27+
poolId string
2728
}
2829

2930
func init() {
@@ -42,6 +43,14 @@ func init() {
4243
Dest: &(cmdlineOptions.address),
4344
CustomFlag: "address",
4445
},
46+
{
47+
Name: "asset",
48+
Type: plugin.PluginOptionTypeString,
49+
Description: "specifies the asset fingerprint (asset1xxx) to filter on",
50+
DefaultValue: "",
51+
Dest: &(cmdlineOptions.asset),
52+
CustomFlag: "asset",
53+
},
4554
{
4655
Name: "policy",
4756
Type: plugin.PluginOptionTypeString,
@@ -51,12 +60,12 @@ func init() {
5160
CustomFlag: "policy",
5261
},
5362
{
54-
Name: "asset",
63+
Name: "pool",
5564
Type: plugin.PluginOptionTypeString,
56-
Description: "specifies the asset fingerprint (asset1xxx) to filter on",
65+
Description: "specifies Pool ID to filter on",
5766
DefaultValue: "",
58-
Dest: &(cmdlineOptions.asset),
59-
CustomFlag: "asset",
67+
Dest: &(cmdlineOptions.poolId),
68+
CustomFlag: "pool",
6069
},
6170
},
6271
},
@@ -73,6 +82,14 @@ func NewFromCmdlineOptions() plugin.Plugin {
7382
),
7483
)
7584
}
85+
if cmdlineOptions.asset != "" {
86+
pluginOptions = append(
87+
pluginOptions,
88+
WithAssetFingerprints(
89+
strings.Split(cmdlineOptions.asset, ","),
90+
),
91+
)
92+
}
7693
if cmdlineOptions.policyId != "" {
7794
pluginOptions = append(
7895
pluginOptions,
@@ -81,11 +98,11 @@ func NewFromCmdlineOptions() plugin.Plugin {
8198
),
8299
)
83100
}
84-
if cmdlineOptions.asset != "" {
101+
if cmdlineOptions.poolId != "" {
85102
pluginOptions = append(
86103
pluginOptions,
87-
WithAssetFingerprints(
88-
strings.Split(cmdlineOptions.asset, ","),
104+
WithPoolIds(
105+
strings.Split(cmdlineOptions.poolId, ","),
89106
),
90107
)
91108
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/blinklabs-io/snek
33
go 1.20
44

55
require (
6-
github.com/blinklabs-io/gouroboros v0.58.0
6+
github.com/blinklabs-io/gouroboros v0.59.0
77
github.com/gen2brain/beeep v0.0.0-20230602101333-f384c29b62dd
88
github.com/gin-gonic/gin v1.9.1
99
github.com/kelseyhightower/envconfig v1.4.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGB
44
cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA=
55
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
66
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
7-
github.com/blinklabs-io/gouroboros v0.58.0 h1:W1/fjntOfJ3Yn41/SjgxV4cS7XSuogCZYznygRXUj8Q=
8-
github.com/blinklabs-io/gouroboros v0.58.0/go.mod h1:D5YJka8EyVmiXNMbRvjH23H9lNMLA4+qSlNNC/j7R0k=
7+
github.com/blinklabs-io/gouroboros v0.59.0 h1:oNJrxg3CEmWoq1hPav9p1lkdDcnCy10AlGDfjHTf/8M=
8+
github.com/blinklabs-io/gouroboros v0.59.0/go.mod h1:D5YJka8EyVmiXNMbRvjH23H9lNMLA4+qSlNNC/j7R0k=
99
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
1010
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
1111
github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE=

0 commit comments

Comments
 (0)