Skip to content

Commit

Permalink
change evaluate RequestId for update config tx (hyperledger#4585)
Browse files Browse the repository at this point in the history
Signed-off-by: Fedor Partanskiy <[email protected]>
  • Loading branch information
pfi79 authored Dec 28, 2023
1 parent 4201eb5 commit fc588b5
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
5 changes: 3 additions & 2 deletions orderer/consensus/smartbft/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,15 @@ func NewChain(
metricsWalBFT *wal.Metrics,
bccsp bccsp.BCCSP,
) (*BFTChain, error) {
logger := flogging.MustGetLogger("orderer.consensus.smartbft.chain").With(zap.String("channel", support.ChannelID()))

requestInspector := &RequestInspector{
ValidateIdentityStructure: func(_ *msp.SerializedIdentity) error {
return nil
},
Logger: logger,
}

logger := flogging.MustGetLogger("orderer.consensus.smartbft.chain").With(zap.String("channel", support.ChannelID()))

c := &BFTChain{
RuntimeConfig: &atomic.Value{},
Channel: support.ChannelID(),
Expand Down
51 changes: 51 additions & 0 deletions orderer/consensus/smartbft/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ type request struct {
// RequestInspector inspects incomming requests and validates serialized identity
type RequestInspector struct {
ValidateIdentityStructure func(identity *msp.SerializedIdentity) error
Logger *flogging.FabricLogger
}

func (ri *RequestInspector) requestIDFromSigHeader(sigHdr *cb.SignatureHeader) (types.RequestInfo, error) {
Expand All @@ -277,24 +278,74 @@ func (ri *RequestInspector) requestIDFromSigHeader(sigHdr *cb.SignatureHeader) (
}, nil
}

func (ri *RequestInspector) requestIDFromEnvelope(envelope *cb.Envelope) (types.RequestInfo, error) {
data, err := proto.Marshal(envelope)
if err != nil {
return types.RequestInfo{}, err
}

req, err := ri.unwrapReqFromEnvelop(envelope)
if err != nil {
return types.RequestInfo{}, err
}

txID := sha256.Sum256(data)
clientID := sha256.Sum256(req.sigHdr.Creator)
return types.RequestInfo{
ID: hex.EncodeToString(txID[:]),
ClientID: hex.EncodeToString(clientID[:]),
}, nil
}

// RequestID unwraps the request info from the raw request
func (ri *RequestInspector) RequestID(rawReq []byte) types.RequestInfo {
req, err := ri.unwrapReq(rawReq)
if err != nil {
return types.RequestInfo{}
}

if req.chHdr.Type == int32(cb.HeaderType_CONFIG) {
configEnvelope := &cb.ConfigEnvelope{}
_, err = protoutil.UnmarshalEnvelopeOfType(req.envelope, cb.HeaderType_CONFIG, configEnvelope)
if err != nil {
ri.Logger.Errorf("can't get config envelope: %s", err.Error())
return types.RequestInfo{}
}

reqInfo, err := ri.requestIDFromEnvelope(configEnvelope.LastUpdate)
if err != nil {
ri.Logger.Errorf("can't get request ID: %s", err.Error())
return types.RequestInfo{}
}

return reqInfo
}

reqInfo, err := ri.requestIDFromSigHeader(req.sigHdr)
if err != nil {
return types.RequestInfo{}
}
return reqInfo
}

func (ri *RequestInspector) isEmpty(req types.RequestInfo) bool {
if len(req.ID) == 0 && len(req.ClientID) == 0 {
return true
}

return false
}

func (ri *RequestInspector) unwrapReq(req []byte) (*request, error) {
envelope, err := protoutil.UnmarshalEnvelope(req)
if err != nil {
return nil, err
}

return ri.unwrapReqFromEnvelop(envelope)
}

func (ri *RequestInspector) unwrapReqFromEnvelop(envelope *cb.Envelope) (*request, error) {
payload := &cb.Payload{}
if err := proto.Unmarshal(envelope.Payload, payload); err != nil {
return nil, errors.Wrap(err, "failed unmarshaling payload")
Expand Down
9 changes: 8 additions & 1 deletion orderer/consensus/smartbft/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,18 @@ func (v *Verifier) verifyRequest(rawRequest []byte, noConfigAllowed bool) (types
}

if req.chHdr.Type == int32(cb.HeaderType_CONFIG) {
err := v.ConfigValidator.ValidateConfig(req.envelope)
err = v.ConfigValidator.ValidateConfig(req.envelope)
if err != nil {
v.Logger.Errorf("Error verifying config update: %v", err)
return types.RequestInfo{}, err
}

reqID := v.ReqInspector.RequestID(rawRequest)
if v.ReqInspector.isEmpty(reqID) {
return types.RequestInfo{}, errors.Errorf("request id is empty")
}

return reqID, nil
}

return v.ReqInspector.requestIDFromSigHeader(req.sigHdr)
Expand Down
2 changes: 2 additions & 0 deletions orderer/consensus/smartbft/verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func TestVerifyConsenterSig(t *testing.T) {
ValidateIdentityStructure: func(_ *msp.SerializedIdentity) error {
return nil
},
Logger: logger,
}

cv := &mocks.ConsenterVerifier{}
Expand Down Expand Up @@ -415,6 +416,7 @@ func TestVerifyProposal(t *testing.T) {
ValidateIdentityStructure: func(_ *msp.SerializedIdentity) error {
return nil
},
Logger: logger,
}

lastHash := hex.EncodeToString(protoutil.BlockHeaderHash(lastBlock.Header))
Expand Down

0 comments on commit fc588b5

Please sign in to comment.