Skip to content

Commit

Permalink
feat: filtering tx by pool (#209)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Gianelloni <[email protected]>
  • Loading branch information
wolf31o2 authored May 16, 2024
1 parent 76a690d commit 084b3be
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
54 changes: 51 additions & 3 deletions filter/chainsync/chainsync.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Blink Labs Software
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -18,11 +18,12 @@ import (
"encoding/hex"
"strings"

"github.com/blinklabs-io/gouroboros/bech32"
"github.com/blinklabs-io/gouroboros/ledger"

"github.com/blinklabs-io/adder/event"
"github.com/blinklabs-io/adder/input/chainsync"
"github.com/blinklabs-io/adder/plugin"
"github.com/blinklabs-io/gouroboros/bech32"
"github.com/blinklabs-io/gouroboros/ledger"
)

type ChainSync struct {
Expand Down Expand Up @@ -193,6 +194,53 @@ func (c *ChainSync) Start() error {
continue
}
}
// Check pool filter
if len(c.filterPoolIds) > 0 {
filterMatched := false
for _, filterPoolId := range c.filterPoolIds {
if filterMatched {
break
}
isPoolBech32 := strings.HasPrefix(filterPoolId, "pool")
foundMatch := false
for _, certificate := range v.Certificates {
switch certificate.(type) {
case *ledger.StakeDelegationCertificate:
cert := &ledger.StakeDelegationCertificate{}
b := &ledger.Blake2b224{}
copy(b[:], cert.PoolKeyHash[:])
if b.String() == filterPoolId {
foundMatch = true
} else if isPoolBech32 {
// lifted from gouroboros/ledger
convData, err := bech32.ConvertBits(certificate.Cbor(), 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
}
}
}
if foundMatch {
filterMatched = true
break
}
}
// Skip the event if none of the filter values matched
if !filterMatched {
continue
}
}
}
c.outputChan <- evt
}
Expand Down
14 changes: 8 additions & 6 deletions input/chainsync/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type TransactionEvent struct {
TransactionCbor byteSliceJsonHex `json:"transactionCbor,omitempty"`
Inputs []ledger.TransactionInput `json:"inputs"`
Outputs []ledger.TransactionOutput `json:"outputs"`
Certificates []ledger.Certificate `json:"certificates"`
Metadata *cbor.Value `json:"metadata,omitempty"`
Fee uint64 `json:"fee"`
TTL uint64 `json:"ttl,omitempty"`
Expand Down Expand Up @@ -60,12 +61,13 @@ func NewTransactionEvent(
includeCbor bool,
) TransactionEvent {
evt := TransactionEvent{
Transaction: tx,
BlockHash: block.Hash(),
Inputs: tx.Inputs(),
Outputs: tx.Outputs(),
Fee: tx.Fee(),
TTL: tx.TTL(),
Transaction: tx,
BlockHash: block.Hash(),
Inputs: tx.Inputs(),
Outputs: tx.Outputs(),
Certificates: tx.Certificates(),
Fee: tx.Fee(),
TTL: tx.TTL(),
}
if includeCbor {
evt.TransactionCbor = tx.Cbor()
Expand Down

0 comments on commit 084b3be

Please sign in to comment.