-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BFT Block Puller: updatable connection source (#4571)
Change-Id: Id0b80df892595cf2b9d7b7b50bfa9069f0eb6905 Signed-off-by: Yoav Tock <tock@il.ibm.com>
- Loading branch information
Showing
22 changed files
with
806 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package deliverclient | ||
|
||
import ( | ||
"github.com/hyperledger/fabric-protos-go/common" | ||
"github.com/hyperledger/fabric/common/configtx" | ||
"github.com/hyperledger/fabric/protoutil" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var ErrNotAConfig = errors.New("not a config block") | ||
|
||
// ConfigFromBlock returns a ConfigEnvelope if exists, or a *ErrNotAConfig error. | ||
// It may also return some other error in case parsing failed. | ||
func ConfigFromBlock(block *common.Block) (*common.ConfigEnvelope, error) { | ||
if block == nil || block.Data == nil || len(block.Data.Data) == 0 { | ||
return nil, errors.New("empty block") | ||
} | ||
txn := block.Data.Data[0] | ||
env, err := protoutil.GetEnvelopeFromBlock(txn) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
payload, err := protoutil.UnmarshalPayload(env.Payload) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
if block.Header.Number == 0 { | ||
configEnvelope, err := configtx.UnmarshalConfigEnvelope(payload.Data) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "invalid config envelope") | ||
} | ||
return configEnvelope, nil | ||
} | ||
if payload.Header == nil { | ||
return nil, errors.New("nil header in payload") | ||
} | ||
chdr, err := protoutil.UnmarshalChannelHeader(payload.Header.ChannelHeader) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
if common.HeaderType(chdr.Type) != common.HeaderType_CONFIG { | ||
return nil, ErrNotAConfig | ||
} | ||
configEnvelope, err := configtx.UnmarshalConfigEnvelope(payload.Data) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "invalid config envelope") | ||
} | ||
return configEnvelope, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package deliverclient_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hyperledger/fabric-protos-go/common" | ||
"github.com/hyperledger/fabric/common/deliverclient" | ||
"github.com/hyperledger/fabric/protoutil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConfigFromBlockBadInput(t *testing.T) { | ||
for _, testCase := range []struct { | ||
name string | ||
block *common.Block | ||
expectedError string | ||
}{ | ||
{ | ||
name: "nil block", | ||
expectedError: "empty block", | ||
block: nil, | ||
}, | ||
{ | ||
name: "nil block data", | ||
expectedError: "empty block", | ||
block: &common.Block{}, | ||
}, | ||
{ | ||
name: "no data in block", | ||
expectedError: "empty block", | ||
block: &common.Block{Data: &common.BlockData{}}, | ||
}, | ||
{ | ||
name: "invalid payload", | ||
expectedError: "error unmarshalling Envelope", | ||
block: &common.Block{Data: &common.BlockData{Data: [][]byte{{1, 2, 3}}}}, | ||
}, | ||
{ | ||
name: "bad genesis block", | ||
expectedError: "invalid config envelope", | ||
block: &common.Block{ | ||
Header: &common.BlockHeader{}, Data: &common.BlockData{Data: [][]byte{protoutil.MarshalOrPanic(&common.Envelope{ | ||
Payload: protoutil.MarshalOrPanic(&common.Payload{ | ||
Data: []byte{1, 2, 3}, | ||
}), | ||
})}}, | ||
}, | ||
}, | ||
{ | ||
name: "invalid envelope in block", | ||
expectedError: "error unmarshalling Envelope", | ||
block: &common.Block{Data: &common.BlockData{Data: [][]byte{{1, 2, 3}}}}, | ||
}, | ||
{ | ||
name: "invalid payload in block envelope", | ||
expectedError: "error unmarshalling Payload", | ||
block: &common.Block{Data: &common.BlockData{Data: [][]byte{protoutil.MarshalOrPanic(&common.Envelope{ | ||
Payload: []byte{1, 2, 3}, | ||
})}}}, | ||
}, | ||
{ | ||
name: "invalid channel header", | ||
expectedError: "error unmarshalling ChannelHeader", | ||
block: &common.Block{ | ||
Header: &common.BlockHeader{Number: 1}, | ||
Data: &common.BlockData{Data: [][]byte{protoutil.MarshalOrPanic(&common.Envelope{ | ||
Payload: protoutil.MarshalOrPanic(&common.Payload{ | ||
Header: &common.Header{ | ||
ChannelHeader: []byte{1, 2, 3}, | ||
}, | ||
}), | ||
})}}, | ||
}, | ||
}, | ||
{ | ||
name: "invalid config block", | ||
expectedError: "invalid config envelope", | ||
block: &common.Block{ | ||
Header: &common.BlockHeader{}, | ||
Data: &common.BlockData{Data: [][]byte{protoutil.MarshalOrPanic(&common.Envelope{ | ||
Payload: protoutil.MarshalOrPanic(&common.Payload{ | ||
Data: []byte{1, 2, 3}, | ||
Header: &common.Header{ | ||
ChannelHeader: protoutil.MarshalOrPanic(&common.ChannelHeader{ | ||
Type: int32(common.HeaderType_CONFIG), | ||
}), | ||
}, | ||
}), | ||
})}}, | ||
}, | ||
}, | ||
} { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
conf, err := deliverclient.ConfigFromBlock(testCase.block) | ||
require.Nil(t, conf) | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), testCase.expectedError) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.