@@ -16,7 +16,9 @@ import (
1616 "math"
1717 "slices"
1818
19+ "github.com/hyperledger/fabric-protos-go-apiv2/common"
1920 "github.com/hyperledger/fabric-x-orderer/common/types"
21+ "github.com/hyperledger/fabric/protoutil"
2022)
2123
2224type Rule func (* State , types.Logger , ... ControlEvent )
@@ -302,16 +304,47 @@ func (c *Complaint) String() string {
302304 return fmt .Sprintf ("Complaint: Signer: %d; Shard: %d; Term %d; Reason: %s" , c .Signer , c .Shard , c .Term , c .Reason )
303305}
304306
307+ type ConfigRequest struct {
308+ Envelope * common.Envelope
309+ }
310+
311+ func (c * ConfigRequest ) Bytes () []byte {
312+ bytes , err := protoutil .Marshal (c .Envelope )
313+ if err != nil {
314+ panic (fmt .Sprintf ("failed to marshal envelope: %v" , err ))
315+ }
316+
317+ return bytes
318+ }
319+
320+ func (c * ConfigRequest ) FromBytes (bytes []byte ) error {
321+ envelope , err := protoutil .UnmarshalEnvelope (bytes )
322+ if err != nil {
323+ return fmt .Errorf ("failed to unmarshal envelope: %v" , err )
324+ }
325+
326+ c .Envelope = envelope
327+ return nil
328+ }
329+
330+ func (c * ConfigRequest ) String () string {
331+ // TODO: add more info to this string
332+ return "Config Request"
333+ }
334+
305335type ControlEvent struct {
306- BAF types.BatchAttestationFragment
307- Complaint * Complaint
336+ BAF types.BatchAttestationFragment
337+ Complaint * Complaint
338+ ConfigRequest * ConfigRequest
308339}
309340
310341func (ce * ControlEvent ) String () string {
311342 if ce .Complaint != nil {
312343 return ce .Complaint .String ()
313344 } else if ce .BAF != nil {
314345 return ce .BAF .String ()
346+ } else if ce .ConfigRequest != nil {
347+ return ce .ConfigRequest .String ()
315348 }
316349 return "empty control event"
317350}
@@ -330,6 +363,10 @@ func (ce *ControlEvent) ID() string {
330363 binary .BigEndian .PutUint16 (payloadToHash [18 :], uint16 (ce .BAF .Primary ()))
331364 binary .BigEndian .PutUint16 (payloadToHash [20 :], uint16 (ce .BAF .Shard ()))
332365 copy (payloadToHash [22 :], ce .BAF .Digest ())
366+ } else if ce .ConfigRequest != nil {
367+ // TODO: maybe use a different ID for ConfigRequest
368+ ce .ConfigRequest .Envelope .Signature = nil
369+ payloadToHash = ce .ConfigRequest .Bytes ()
333370 } else {
334371 return string ("" )
335372 }
@@ -344,6 +381,7 @@ func (ce *ControlEvent) SignerID() string {
344381 signerID = fmt .Sprintf ("%d" , ce .Complaint .Signer )
345382 } else if ce .BAF != nil {
346383 signerID = fmt .Sprintf ("%d" , ce .BAF .Signer ())
384+ // TODO: add ConfigRequest SignerID
347385 } else {
348386 return string ("" )
349387 }
@@ -363,6 +401,11 @@ func (ce *ControlEvent) Bytes() []byte {
363401 bytes = make ([]byte , len (rawComplaint )+ 1 )
364402 bytes [0 ] = 2
365403 copy (bytes [1 :], rawComplaint )
404+ case ce .ConfigRequest != nil :
405+ rawConfig := ce .ConfigRequest .Bytes ()
406+ bytes = make ([]byte , len (rawConfig )+ 1 )
407+ bytes [0 ] = 3
408+ copy (bytes [1 :], rawConfig )
366409 default :
367410 panic ("empty control event" )
368411 }
@@ -379,12 +422,15 @@ func (ce *ControlEvent) FromBytes(bytes []byte, fragmentFromBytes func([]byte) (
379422 case 2 :
380423 ce .Complaint = & Complaint {}
381424 return ce .Complaint .FromBytes (bytes [1 :])
425+ case 3 :
426+ ce .ConfigRequest = & ConfigRequest {}
427+ return ce .ConfigRequest .FromBytes (bytes [1 :])
382428 }
383429
384430 return fmt .Errorf ("unknown prefix (%d)" , bytes [0 ])
385431}
386432
387- func (s * State ) Process (l types.Logger , ces ... ControlEvent ) (* State , []types.BatchAttestationFragment ) {
433+ func (s * State ) Process (l types.Logger , ces ... ControlEvent ) (* State , []types.BatchAttestationFragment , * ConfigRequest ) {
388434 s2 := s .Clone ()
389435
390436 for _ , rule := range Rules {
@@ -393,8 +439,8 @@ func (s *State) Process(l types.Logger, ces ...ControlEvent) (*State, []types.Ba
393439
394440 // After applying rules, extract all batch attestations for which enough fragments have been collected.
395441 extracted := ExtractBatchAttestationsFromPending (s2 , l )
396-
397- return s2 , extracted
442+ configRequests := ExtractConfigRequest ( ces )
443+ return s2 , extracted , configRequests
398444}
399445
400446func (s * State ) Clone () * State {
@@ -658,3 +704,13 @@ func shardExists(shard types.ShardID, shardTerms []ShardTerm) (int, bool) {
658704 }
659705 return - 1 , false
660706}
707+
708+ func ExtractConfigRequest (ces []ControlEvent ) * ConfigRequest {
709+ // TODO: decide how to handle multiple config requests
710+ for _ , ce := range ces {
711+ if ce .ConfigRequest != nil {
712+ return ce .ConfigRequest
713+ }
714+ }
715+ return nil
716+ }
0 commit comments