|
1 | 1 | package node |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "hash/maphash" |
| 6 | + "sync" |
| 7 | + |
| 8 | + "github.com/wavesplatform/gowaves/pkg/p2p/peer" |
4 | 9 | "github.com/wavesplatform/gowaves/pkg/proto" |
5 | 10 | "github.com/wavesplatform/gowaves/pkg/state" |
6 | 11 | "github.com/wavesplatform/gowaves/pkg/types" |
@@ -33,3 +38,176 @@ func maybeEnableExtendedApi(state startProvidingExtendedApi, lastBlock *proto.Bl |
33 | 38 | } |
34 | 39 | return nil |
35 | 40 | } |
| 41 | + |
| 42 | +type safeMap[K comparable, V any] struct { |
| 43 | + mu sync.Mutex |
| 44 | + m map[K]V |
| 45 | +} |
| 46 | + |
| 47 | +func newSafeMap[K comparable, V any]() *safeMap[K, V] { |
| 48 | + return &safeMap[K, V]{ |
| 49 | + m: make(map[K]V), |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func (s *safeMap[K, V]) SetIfNew(key K, value V) bool { |
| 54 | + s.mu.Lock() |
| 55 | + defer s.mu.Unlock() |
| 56 | + if _, exists := s.m[key]; exists { |
| 57 | + return false |
| 58 | + } |
| 59 | + s.m[key] = value |
| 60 | + return true |
| 61 | +} |
| 62 | + |
| 63 | +func (s *safeMap[K, V]) Delete(key K) { |
| 64 | + s.mu.Lock() |
| 65 | + defer s.mu.Unlock() |
| 66 | + delete(s.m, key) |
| 67 | +} |
| 68 | + |
| 69 | +type protoMessageWrapper struct { |
| 70 | + protoMsg peer.ProtoMessage |
| 71 | + messageID uint64 |
| 72 | +} |
| 73 | + |
| 74 | +// messageIDFilterFunc is a function type that takes a proto.Message and returns a uint64 |
| 75 | +// representing the deterministic message ID. This is used to filter messages based on |
| 76 | +// their message ID: it can be calculated from the message payload, or from the whole message itself. |
| 77 | +// |
| 78 | +// If the returned message ID is 0, the message bypasses filtering. |
| 79 | +type messageIDFilterFunc func(message proto.Message) uint64 |
| 80 | + |
| 81 | +type txMessagePayloadIDFilter struct { |
| 82 | + seed maphash.Seed |
| 83 | +} |
| 84 | + |
| 85 | +func (f txMessagePayloadIDFilter) Filter(message proto.Message) uint64 { |
| 86 | + switch msg := message.(type) { |
| 87 | + case *proto.TransactionMessage: |
| 88 | + return maphash.Bytes(f.seed, msg.Transaction) |
| 89 | + case *proto.PBTransactionMessage: |
| 90 | + return maphash.Bytes(f.seed, msg.Transaction) |
| 91 | + default: |
| 92 | + return 0 // non-transaction messages bypass filtering |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func messagesSplitter( |
| 97 | + ctx context.Context, |
| 98 | + origMessageCh <-chan peer.ProtoMessage, |
| 99 | + noFilteredChan, filteredChan chan<- protoMessageWrapper, |
| 100 | + sm *safeMap[uint64, struct{}], |
| 101 | + messageIDFilter messageIDFilterFunc, |
| 102 | +) { |
| 103 | + for { |
| 104 | + var ( |
| 105 | + msg peer.ProtoMessage |
| 106 | + messageID uint64 |
| 107 | + ) |
| 108 | + select { |
| 109 | + case <-ctx.Done(): |
| 110 | + return |
| 111 | + case msg = <-origMessageCh: |
| 112 | + messageID = messageIDFilter(msg.Message) |
| 113 | + } |
| 114 | + bypassesFiltering := messageID == 0 // if messageID is 0, it means the message no need to be filtered. |
| 115 | + needToWrite := bypassesFiltering || sm.SetIfNew(messageID, struct{}{}) |
| 116 | + if !needToWrite { |
| 117 | + continue // skip a message if message ID already exists in the map |
| 118 | + } |
| 119 | + outCh := noFilteredChan |
| 120 | + if !bypassesFiltering { |
| 121 | + outCh = filteredChan |
| 122 | + } |
| 123 | + select { |
| 124 | + case <-ctx.Done(): |
| 125 | + return |
| 126 | + case outCh <- protoMessageWrapper{msg, messageID}: |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func runMessagesMerger( |
| 132 | + ctx context.Context, wg *sync.WaitGroup, |
| 133 | + noFilteredChan, filteredChan <-chan protoMessageWrapper, |
| 134 | + sm *safeMap[uint64, struct{}], |
| 135 | +) <-chan peer.ProtoMessage { |
| 136 | + wg.Add(1) |
| 137 | + outMessageCh := make(chan peer.ProtoMessage) // intentionally unbuffered channel because of map usage |
| 138 | + go func() { |
| 139 | + defer wg.Done() |
| 140 | + for { |
| 141 | + var wMsg protoMessageWrapper |
| 142 | + select { |
| 143 | + case <-ctx.Done(): |
| 144 | + return |
| 145 | + case wMsg = <-noFilteredChan: |
| 146 | + case wMsg = <-filteredChan: |
| 147 | + } |
| 148 | + select { |
| 149 | + case <-ctx.Done(): |
| 150 | + return |
| 151 | + case outMessageCh <- wMsg.protoMsg: |
| 152 | + if wMsg.messageID != 0 { |
| 153 | + sm.Delete(wMsg.messageID) // remove message ID from the map after sending the message |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + }() |
| 158 | + return outMessageCh |
| 159 | +} |
| 160 | + |
| 161 | +type chanLenProvider[T any] <-chan T |
| 162 | + |
| 163 | +func (c chanLenProvider[T]) Len() int { return len(c) } |
| 164 | + |
| 165 | +type aggregatedLenProvider []lenProvider |
| 166 | + |
| 167 | +func (a aggregatedLenProvider) Len() int { |
| 168 | + l := 0 |
| 169 | + for _, p := range a { |
| 170 | + l += p.Len() |
| 171 | + } |
| 172 | + return l |
| 173 | +} |
| 174 | + |
| 175 | +type waiter interface { |
| 176 | + Wait() |
| 177 | +} |
| 178 | + |
| 179 | +func deduplicateProtoMessages( |
| 180 | + ctx context.Context, origMessageCh <-chan peer.ProtoMessage, |
| 181 | + messageIDFilter messageIDFilterFunc, |
| 182 | +) (<-chan peer.ProtoMessage, lenProvider, waiter) { |
| 183 | + const noFilteredChanSize = 1000 |
| 184 | + |
| 185 | + wg := &sync.WaitGroup{} |
| 186 | + sm := newSafeMap[uint64, struct{}]() |
| 187 | + |
| 188 | + noFilteredChan := make(chan protoMessageWrapper, noFilteredChanSize) |
| 189 | + filteredChan := make(chan protoMessageWrapper, max(cap(origMessageCh)-noFilteredChanSize, noFilteredChanSize)) |
| 190 | + |
| 191 | + wg.Add(1) |
| 192 | + go func() { // run messages splitter with filtering |
| 193 | + defer wg.Done() |
| 194 | + messagesSplitter(ctx, origMessageCh, noFilteredChan, filteredChan, sm, messageIDFilter) |
| 195 | + }() |
| 196 | + |
| 197 | + out := runMessagesMerger(ctx, wg, noFilteredChan, filteredChan, sm) |
| 198 | + |
| 199 | + lp := aggregatedLenProvider{ |
| 200 | + chanLenProvider[peer.ProtoMessage](origMessageCh), |
| 201 | + chanLenProvider[protoMessageWrapper](noFilteredChan), |
| 202 | + chanLenProvider[protoMessageWrapper](filteredChan), |
| 203 | + chanLenProvider[peer.ProtoMessage](out), |
| 204 | + } |
| 205 | + return out, lp, wg |
| 206 | +} |
| 207 | + |
| 208 | +func deduplicateProtoTxMessages( |
| 209 | + ctx context.Context, origMessageCh <-chan peer.ProtoMessage, |
| 210 | +) (<-chan peer.ProtoMessage, lenProvider, waiter) { |
| 211 | + payloadIDFilter := txMessagePayloadIDFilter{seed: maphash.MakeSeed()} |
| 212 | + return deduplicateProtoMessages(ctx, origMessageCh, payloadIDFilter.Filter) |
| 213 | +} |
0 commit comments