-
Notifications
You must be signed in to change notification settings - Fork 8.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BFT Block Puller: updatable connection source #4571
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't we have any util package in our entire codebase we can move this function to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It got promoted from
orderer/common/cluster/util.go
tocommon/deliverclient/util.go
, where I plan to put all the stuff that is common to both orderer and peer in the context of the block puller. Can't put it in packagecommon/util
norprotoutil
because of import cycles. I am open to suggestions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose the import cycle is because of
github.com/hyperledger/fabric/common/configtx
? Then can't you move it there?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However if you think there are additional things to be placed here, I guess we can leave it as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In terms of import cycles it can be placed in
common/configtx
, but I think it is not a good place to put code that parses blocks, as that package deals only with individual txs. I prefer to leave it where it is for the time being. I think that during the refactoring that is about to happen when we overhaul the orderer, several additional things will get moved into that util.go file.