|
| 1 | +// Copyright 2023 Blink Labs, LLC. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package chainsync |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/blinklabs-io/gouroboros/ledger" |
| 19 | + "github.com/blinklabs-io/snek/event" |
| 20 | + "github.com/blinklabs-io/snek/input/chainsync" |
| 21 | +) |
| 22 | + |
| 23 | +type ChainSync struct { |
| 24 | + errorChan chan error |
| 25 | + inputChan chan event.Event |
| 26 | + outputChan chan event.Event |
| 27 | + filterAddress string |
| 28 | + filterPolicyId string |
| 29 | + filterAssetFingerprint string |
| 30 | +} |
| 31 | + |
| 32 | +// New returns a new ChainSync object with the specified options applied |
| 33 | +func New(options ...ChainSyncOptionFunc) *ChainSync { |
| 34 | + c := &ChainSync{ |
| 35 | + errorChan: make(chan error), |
| 36 | + inputChan: make(chan event.Event, 10), |
| 37 | + outputChan: make(chan event.Event, 10), |
| 38 | + } |
| 39 | + for _, option := range options { |
| 40 | + option(c) |
| 41 | + } |
| 42 | + return c |
| 43 | +} |
| 44 | + |
| 45 | +// Start the chain sync filter |
| 46 | +func (c *ChainSync) Start() error { |
| 47 | + go func() { |
| 48 | + // TODO: pre-process filter params to be more useful for direct comparison |
| 49 | + for { |
| 50 | + evt, ok := <-c.inputChan |
| 51 | + // Channel has been closed, which means we're shutting down |
| 52 | + if !ok { |
| 53 | + return |
| 54 | + } |
| 55 | + switch v := evt.Payload.(type) { |
| 56 | + case chainsync.TransactionEvent: |
| 57 | + // Check address filter |
| 58 | + if c.filterAddress != "" { |
| 59 | + foundMatch := false |
| 60 | + // TODO: extract and compare stake addresses when this is done |
| 61 | + // https://github.com/blinklabs-io/gouroboros/issues/302 |
| 62 | + for _, output := range v.Outputs { |
| 63 | + if output.Address().String() == c.filterAddress { |
| 64 | + foundMatch = true |
| 65 | + break |
| 66 | + } |
| 67 | + } |
| 68 | + if !foundMatch { |
| 69 | + continue |
| 70 | + } |
| 71 | + } |
| 72 | + // Check policy ID filter |
| 73 | + if c.filterPolicyId != "" { |
| 74 | + foundMatch := false |
| 75 | + for _, output := range v.Outputs { |
| 76 | + if output.Assets() != nil { |
| 77 | + for _, policyId := range output.Assets().Policies() { |
| 78 | + if policyId.String() == c.filterPolicyId { |
| 79 | + foundMatch = true |
| 80 | + break |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + if foundMatch { |
| 85 | + break |
| 86 | + } |
| 87 | + } |
| 88 | + if !foundMatch { |
| 89 | + continue |
| 90 | + } |
| 91 | + } |
| 92 | + // Check asset fingerprint filter |
| 93 | + if c.filterAssetFingerprint != "" { |
| 94 | + foundMatch := false |
| 95 | + for _, output := range v.Outputs { |
| 96 | + if output.Assets() != nil { |
| 97 | + for _, policyId := range output.Assets().Policies() { |
| 98 | + for _, assetName := range output.Assets().Assets(policyId) { |
| 99 | + assetFp := ledger.NewAssetFingerprint(policyId.Bytes(), assetName) |
| 100 | + if assetFp.String() == c.filterAssetFingerprint { |
| 101 | + foundMatch = true |
| 102 | + } |
| 103 | + } |
| 104 | + if foundMatch { |
| 105 | + break |
| 106 | + } |
| 107 | + } |
| 108 | + if foundMatch { |
| 109 | + break |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + if !foundMatch { |
| 114 | + continue |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + c.outputChan <- evt |
| 119 | + } |
| 120 | + }() |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +// Stop the chain sync filter |
| 125 | +func (c *ChainSync) Stop() error { |
| 126 | + close(c.inputChan) |
| 127 | + close(c.outputChan) |
| 128 | + close(c.errorChan) |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +// ErrorChan returns the filter error channel |
| 133 | +func (c *ChainSync) ErrorChan() chan error { |
| 134 | + return c.errorChan |
| 135 | +} |
| 136 | + |
| 137 | +// InputChan returns the input event channel |
| 138 | +func (c *ChainSync) InputChan() chan<- event.Event { |
| 139 | + return c.inputChan |
| 140 | +} |
| 141 | + |
| 142 | +// OutputChan returns the output event channel |
| 143 | +func (c *ChainSync) OutputChan() <-chan event.Event { |
| 144 | + return c.outputChan |
| 145 | +} |
0 commit comments