Skip to content

Commit 084b3be

Browse files
authored
feat: filtering tx by pool (#209)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent 76a690d commit 084b3be

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

filter/chainsync/chainsync.go

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 Blink Labs Software
1+
// Copyright 2024 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -18,11 +18,12 @@ import (
1818
"encoding/hex"
1919
"strings"
2020

21+
"github.com/blinklabs-io/gouroboros/bech32"
22+
"github.com/blinklabs-io/gouroboros/ledger"
23+
2124
"github.com/blinklabs-io/adder/event"
2225
"github.com/blinklabs-io/adder/input/chainsync"
2326
"github.com/blinklabs-io/adder/plugin"
24-
"github.com/blinklabs-io/gouroboros/bech32"
25-
"github.com/blinklabs-io/gouroboros/ledger"
2627
)
2728

2829
type ChainSync struct {
@@ -193,6 +194,53 @@ func (c *ChainSync) Start() error {
193194
continue
194195
}
195196
}
197+
// Check pool filter
198+
if len(c.filterPoolIds) > 0 {
199+
filterMatched := false
200+
for _, filterPoolId := range c.filterPoolIds {
201+
if filterMatched {
202+
break
203+
}
204+
isPoolBech32 := strings.HasPrefix(filterPoolId, "pool")
205+
foundMatch := false
206+
for _, certificate := range v.Certificates {
207+
switch certificate.(type) {
208+
case *ledger.StakeDelegationCertificate:
209+
cert := &ledger.StakeDelegationCertificate{}
210+
b := &ledger.Blake2b224{}
211+
copy(b[:], cert.PoolKeyHash[:])
212+
if b.String() == filterPoolId {
213+
foundMatch = true
214+
} else if isPoolBech32 {
215+
// lifted from gouroboros/ledger
216+
convData, err := bech32.ConvertBits(certificate.Cbor(), 8, 5, true)
217+
if err != nil {
218+
continue
219+
}
220+
encoded, err := bech32.Encode("pool", convData)
221+
if err != nil {
222+
continue
223+
}
224+
if encoded == filterPoolId {
225+
foundMatch = true
226+
}
227+
}
228+
if foundMatch {
229+
filterMatched = true
230+
break
231+
}
232+
}
233+
}
234+
if foundMatch {
235+
filterMatched = true
236+
break
237+
}
238+
}
239+
// Skip the event if none of the filter values matched
240+
if !filterMatched {
241+
continue
242+
}
243+
}
196244
}
197245
c.outputChan <- evt
198246
}

input/chainsync/tx.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type TransactionEvent struct {
3333
TransactionCbor byteSliceJsonHex `json:"transactionCbor,omitempty"`
3434
Inputs []ledger.TransactionInput `json:"inputs"`
3535
Outputs []ledger.TransactionOutput `json:"outputs"`
36+
Certificates []ledger.Certificate `json:"certificates"`
3637
Metadata *cbor.Value `json:"metadata,omitempty"`
3738
Fee uint64 `json:"fee"`
3839
TTL uint64 `json:"ttl,omitempty"`
@@ -60,12 +61,13 @@ func NewTransactionEvent(
6061
includeCbor bool,
6162
) TransactionEvent {
6263
evt := TransactionEvent{
63-
Transaction: tx,
64-
BlockHash: block.Hash(),
65-
Inputs: tx.Inputs(),
66-
Outputs: tx.Outputs(),
67-
Fee: tx.Fee(),
68-
TTL: tx.TTL(),
64+
Transaction: tx,
65+
BlockHash: block.Hash(),
66+
Inputs: tx.Inputs(),
67+
Outputs: tx.Outputs(),
68+
Certificates: tx.Certificates(),
69+
Fee: tx.Fee(),
70+
TTL: tx.TTL(),
6971
}
7072
if includeCbor {
7173
evt.TransactionCbor = tx.Cbor()

0 commit comments

Comments
 (0)