@@ -25,9 +25,34 @@ type (
25
25
HandlerFunc func (context.Context , p2pmsg.Message ) ([]p2pmsg.Message , error )
26
26
HandlerRegistry map [protoreflect.FullName ][]HandlerFunc
27
27
ValidatorFunc func (context.Context , p2pmsg.Message ) (pubsub.ValidationResult , error )
28
- ValidatorRegistry map [string ]pubsub.ValidatorEx
28
+ ValidatorRegistry map [string ][] pubsub.ValidatorEx
29
29
)
30
30
31
+ func (r * ValidatorRegistry ) GetCombinedValidator (topic string ) pubsub.ValidatorEx {
32
+ validate := func (ctx context.Context , sender peer.ID , message * pubsub.Message ) pubsub.ValidationResult {
33
+ ignored := false
34
+ for _ , valFunc := range (* r )[topic ] {
35
+ res := valFunc (ctx , sender , message )
36
+ switch res {
37
+ case pubsub .ValidationAccept :
38
+ continue
39
+ case pubsub .ValidationReject :
40
+ return pubsub .ValidationReject
41
+ case pubsub .ValidationIgnore :
42
+ ignored = true
43
+ default :
44
+ log .Warn ().Str ("topic" , topic ).Msg ("unknown validation result %d, treating as reject" )
45
+ return pubsub .ValidationReject
46
+ }
47
+ }
48
+ if ignored {
49
+ return pubsub .ValidationIgnore
50
+ }
51
+ return pubsub .ValidationAccept
52
+ }
53
+ return validate
54
+ }
55
+
31
56
const (
32
57
allowTraceContext = true // whether we allow the trace field to be set in the message envelope
33
58
invalidResultType = pubsub .ValidationReject
@@ -134,16 +159,6 @@ func (m *P2PMessaging) AddHandlerFunc(handlerFunc HandlerFunc, protos ...p2pmsg.
134
159
135
160
func (m * P2PMessaging ) addValidatorImpl (valFunc ValidatorFunc , messProto p2pmsg.Message ) {
136
161
topic := messProto .Topic ()
137
- _ , exists := m .validatorRegistry [topic ]
138
- if exists {
139
- // This is likely not intended and happens when different messages return the same P2PMessage.Topic().
140
- // Currently a topic is mapped 1 to 1 to a message type (instead of using an envelope for unmarshalling)
141
- // (If feature needed, allow for chaining of successively registered validator functions per topic)
142
- panic (errors .Errorf (
143
- "can't register more than one validator per topic (topic: '%s', message-type: '%s')" ,
144
- topic ,
145
- reflect .TypeOf (messProto )))
146
- }
147
162
handleError := func (err error ) {
148
163
log .Info ().Str ("topic" , topic ).Err (err ).Msg ("received invalid message)" )
149
164
}
@@ -176,8 +191,12 @@ func (m *P2PMessaging) addValidatorImpl(valFunc ValidatorFunc, messProto p2pmsg.
176
191
}
177
192
return valid
178
193
}
179
- m .validatorRegistry [topic ] = validate
180
- m .AddGossipTopic (topic )
194
+
195
+ _ , exists := m .validatorRegistry [topic ]
196
+ if ! exists {
197
+ m .AddGossipTopic (topic )
198
+ }
199
+ m .validatorRegistry [topic ] = append (m .validatorRegistry [topic ], validate )
181
200
}
182
201
183
202
// AddValidator will add a validator-function to a P2PHandler instance:
0 commit comments