Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions node/assembler/oba_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hyperledger/fabric-x-orderer/common/types"
"github.com/hyperledger/fabric-x-orderer/common/utils"
"github.com/hyperledger/fabric-x-orderer/node/consensus/state"
"github.com/hyperledger/fabric-x-orderer/node/ledger"

"github.com/hyperledger/fabric-protos-go-apiv2/common"
"github.com/hyperledger/fabric/protoutil"
Expand Down Expand Up @@ -65,6 +66,12 @@ func (obac *OrderedBatchAttestationCreator) Append(batchId types.BatchID, decisi
BatchCount: batchCount,
},
}
blockMetadata, err := ledger.AssemblerBlockMetadataToBytes(batchId, &state.OrderingInformation{DecisionNum: decisionNum, BatchCount: batchCount, BatchIndex: batchIndex}, 0)
if err != nil {
panic("Failed to invoke AssemblerBlockMetadataToBytes")
}
protoutil.InitBlockMetadata(ba.OrderingInformation.CommonBlock)
ba.OrderingInformation.CommonBlock.Metadata.Metadata[common.BlockMetadataIndex_ORDERER] = blockMetadata
obac.headerHash = protoutil.BlockHeaderHash(ba.OrderingInformation.CommonBlock.Header)
obac.prevBa = ba
return ba
Expand Down
32 changes: 26 additions & 6 deletions node/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/hyperledger/fabric-x-orderer/node/consensus/badb"
"github.com/hyperledger/fabric-x-orderer/node/consensus/state"
"github.com/hyperledger/fabric-x-orderer/node/delivery"
"github.com/hyperledger/fabric-x-orderer/node/ledger"
protos "github.com/hyperledger/fabric-x-orderer/node/protos/comm"
"github.com/hyperledger/fabric/protoutil"
"github.com/pkg/errors"
Expand Down Expand Up @@ -244,6 +245,19 @@ func (c *Consensus) VerifyProposal(proposal smartbft_types.Proposal) ([]smartbft
return nil, fmt.Errorf("proposed common block header number %d in index %d isn't equal to computed number %d", hdr.AvailableCommonBlocks[i].Header.Number, i, lastBlockNumber)
}

blockMetadata, err := ledger.AssemblerBlockMetadataToBytes(ba[0], &state.OrderingInformation{DecisionNum: arma_types.DecisionNum(md.LatestSequence), BatchCount: len(attestations), BatchIndex: i}, 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calculate the TX count, or tests will break when you switch to use this metadata instead of creating it in the assembler.
In addition, right now we are not signing the metadata when we sign the block, but in fabric they do. One more reason to have the orderer metadata computed correctly here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add this in the next PR

if err != nil {
c.Logger.Panicf("Failed to invoke AssemblerBlockMetadataToBytes: %s", err)
}

if hdr.AvailableCommonBlocks[i].Metadata == nil || hdr.AvailableCommonBlocks[i].Metadata.Metadata == nil {
return nil, fmt.Errorf("proposed common block metadata in index %d is nil", i)
}

if !bytes.Equal(blockMetadata, hdr.AvailableCommonBlocks[i].Metadata.Metadata[common.BlockMetadataIndex_ORDERER]) {
return nil, fmt.Errorf("proposed common block metadata in index %d isn't equal to computed metadata", i)
}

}

for i, availableBlock := range hdr.AvailableBlocks {
Expand Down Expand Up @@ -325,7 +339,7 @@ func (c *Consensus) VerifySignature(signature smartbft_types.Signature) error {
// VerificationSequence returns the current verification sequence
// (from SmartBFT API)
func (c *Consensus) VerificationSequence() uint64 {
return 0
return 0 // TODO save current verification sequence and return it here
}

// RequestsFromProposal returns from the given proposal the included requests' info
Expand Down Expand Up @@ -448,6 +462,11 @@ func (c *Consensus) AssembleProposal(metadata []byte, requests [][]byte) smartbf
lastBlockNumber := lastCommonBlockHeader.Number
prevHash := protoutil.BlockHeaderHash(lastCommonBlockHeader)

md := &smartbftprotos.ViewMetadata{}
if err := proto.Unmarshal(metadata, md); err != nil {
panic(err)
}

c.Logger.Infof("Creating proposal with %d attestations", len(attestations))

availableBlocks := make([]state.AvailableBlock, len(attestations))
Expand All @@ -463,6 +482,12 @@ func (c *Consensus) AssembleProposal(metadata []byte, requests [][]byte) smartbf
lastBlockNumber++
availableCommonBlocks[i] = protoutil.NewBlock(lastBlockNumber, prevHash)
availableCommonBlocks[i].Header.DataHash = ba[0].Digest()
blockMetadata, err := ledger.AssemblerBlockMetadataToBytes(ba[0], &state.OrderingInformation{DecisionNum: arma_types.DecisionNum(md.LatestSequence), BatchCount: len(attestations), BatchIndex: i}, 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here - TX count

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next PR

if err != nil {
c.Logger.Panicf("Failed to invoke AssemblerBlockMetadataToBytes: %s", err)
}
protoutil.InitBlockMetadata(availableCommonBlocks[i])
availableCommonBlocks[i].Metadata.Metadata[common.BlockMetadataIndex_ORDERER] = blockMetadata
hdr.Number = lastBlockNumber
hdr.PrevHash = prevHash
prevHash = protoutil.BlockHeaderHash(availableCommonBlocks[i].Header)
Expand All @@ -473,11 +498,6 @@ func (c *Consensus) AssembleProposal(metadata []byte, requests [][]byte) smartbf
newState.AppContext = protoutil.MarshalOrPanic(availableCommonBlocks[len(attestations)-1].Header)
}

md := &smartbftprotos.ViewMetadata{}
if err := proto.Unmarshal(metadata, md); err != nil {
panic(err)
}

reqs := arma_types.BatchedRequests(requests)

return smartbft_types.Proposal{
Expand Down
17 changes: 14 additions & 3 deletions node/consensus/consensus_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func initialStateFromConfig(config *config.ConsenterNodeConfig) *state.State {
return &initState
}

func appendGenesisBlock(genesisBlock *common.Block, initState *state.State, ledger *ledger.ConsensusLedger) {
func appendGenesisBlock(genesisBlock *common.Block, initState *state.State, consensusLedger *ledger.ConsensusLedger) {
genesisBlocks := make([]state.AvailableBlock, 1)
genesisDigest := protoutil.ComputeBlockDataHash(genesisBlock.GetData())

Expand All @@ -283,18 +283,29 @@ func appendGenesisBlock(genesisBlock *common.Block, initState *state.State, ledg

initState.AppContext = protoutil.MarshalOrPanic(lastCommonBlockHeader)

availableCommonBlocks := []*common.Block{genesisBlock}

protoutil.InitBlockMetadata(availableCommonBlocks[0])

blockMetadata, err := ledger.AssemblerBlockMetadataToBytes(state.NewAvailableBatch(0, arma_types.ShardIDConsensus, 0, genesisDigest), &state.OrderingInformation{DecisionNum: 0, BatchCount: 1, BatchIndex: 0}, 1)
if err != nil {
panic("failed to invoke AssemblerBlockMetadataToBytes")
}

availableCommonBlocks[0].Metadata.Metadata[common.BlockMetadataIndex_ORDERER] = blockMetadata

genesisProposal := smartbft_types.Proposal{
Payload: protoutil.MarshalOrPanic(genesisBlock),
Header: (&state.Header{
AvailableCommonBlocks: []*common.Block{genesisBlock},
AvailableCommonBlocks: availableCommonBlocks,
AvailableBlocks: genesisBlocks,
State: initState,
Num: 0,
}).Serialize(),
Metadata: nil,
}

ledger.Append(state.DecisionToBytes(genesisProposal, nil))
consensusLedger.Append(state.DecisionToBytes(genesisProposal, nil))
}

func (c *Consensus) clientConfig() comm.ClientConfig {
Expand Down
9 changes: 8 additions & 1 deletion node/consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,9 +717,16 @@ func TestVerifyProposal(t *testing.T) {
latestBlockHeader.DataHash = baf123id1p1s1.Digest()
latestBlockHeader.PreviousHash = protoutil.BlockHeaderHash(initialAppContext)

header.AvailableBlocks = []state.AvailableBlock{{Header: &state.BlockHeader{Number: latestBlockHeader.Number, PrevHash: latestBlockHeader.PreviousHash, Digest: latestBlockHeader.DataHash}, Batch: state.NewAvailableBatch(baf123id1p1s1.Primary(), baf123id1p1s1.Shard(), baf123id1p1s1.Seq(), baf123id1p1s1.Digest())}}
ab := state.NewAvailableBatch(baf123id1p1s1.Primary(), baf123id1p1s1.Shard(), baf123id1p1s1.Seq(), baf123id1p1s1.Digest())

header.AvailableBlocks = []state.AvailableBlock{{Header: &state.BlockHeader{Number: latestBlockHeader.Number, PrevHash: latestBlockHeader.PreviousHash, Digest: latestBlockHeader.DataHash}, Batch: ab}}
header.AvailableCommonBlocks = []*common.Block{{Header: latestBlockHeader}}

protoutil.InitBlockMetadata(header.AvailableCommonBlocks[0])
blockMetadata, err := ledger.AssemblerBlockMetadataToBytes(ab, &state.OrderingInformation{DecisionNum: 0, BatchCount: 1, BatchIndex: 0}, 0)
require.Nil(t, err)
header.AvailableCommonBlocks[0].Metadata.Metadata[common.BlockMetadataIndex_ORDERER] = blockMetadata

newState := initialState
newState.AppContext = protoutil.MarshalOrPanic(latestBlockHeader)

Expand Down
19 changes: 13 additions & 6 deletions node/delivery/consensus_ba_deliver_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,26 @@ func (cr *ConsensusBAReplicator) Replicate() <-chan types.OrderedBatchAttestatio
cr.logger.Panicf("Failed extracting ordered batch attestation from decision: %s", err2)
}

cr.logger.Infof("Decision %d, with %d AvailableBlocks", block.GetHeader().GetNumber(), len(header.AvailableBlocks))
for index, ab := range header.AvailableBlocks {
cr.logger.Infof("BA index: %d; BatchID: %s; BA block header: %s; BA block signers: %+v", index, types.BatchIDToString(ab.Batch), ab.Header.String(), signersFromSigs(sigs[index]))
cr.logger.Infof("Decision %d, with %d AvailableCommonBlocks", block.GetHeader().GetNumber(), len(header.AvailableCommonBlocks))
for index, acb := range header.AvailableCommonBlocks {

primary, shard, seq, _, _, _, _, err := ledger.AssemblerBlockMetadataFromBytes(acb.Metadata.Metadata[common.BlockMetadataIndex_ORDERER])
if err != nil {
cr.logger.Panicf("Failed extracting info from metadata: %s", err)
}

acbBatch := state.NewAvailableBatch(primary, shard, seq, acb.Header.DataHash)
Comment on lines +117 to +122
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably bundle this into a method "AssemblerBatchIDFromBlock" and put it in utils...
But I suspect we will have to streamline here anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Next PR


cr.logger.Infof("BA index: %d; BatchID: %s; Common Block: %s; BA block signers: %+v", index, types.BatchIDToString(acbBatch), types.CommonBlockToString(acb), signersFromSigs(sigs[index]))

abo := &state.AvailableBatchOrdered{
AvailableBatch: ab.Batch,
AvailableBatch: acbBatch,
OrderingInformation: &state.OrderingInformation{
CommonBlock: header.AvailableCommonBlocks[index],
BlockHeader: ab.Header,
Signatures: sigs[index],
DecisionNum: header.Num,
BatchIndex: index,
BatchCount: len(header.AvailableBlocks),
BatchCount: len(header.AvailableCommonBlocks),
},
}

Expand Down
2 changes: 1 addition & 1 deletion node/delivery/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func extractHeaderAndSigsFromBlock(block *common.Block) (*state.Header, [][]smar
return stateHeader, sigs, nil
}

sigs, err := state.UnpackBlockHeaderSigs(compoundSigs, len(stateHeader.AvailableBlocks))
sigs, err := state.UnpackBlockHeaderSigs(compoundSigs, len(stateHeader.AvailableCommonBlocks))
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to extract header signatures from compound signature, block %d", block.GetHeader().GetNumber())
}
Expand Down
8 changes: 2 additions & 6 deletions node/ledger/assembler_ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (l *AssemblerLedger) Append(batch types.Batch, orderingInfo types.OrderingI
t1 := time.Now()
defer func() {
l.Logger.Infof("Appended block %d of %d requests to ledger in %v",
ordInfo.BlockHeader.Number, len(batch.Requests()), time.Since(t1))
ordInfo.CommonBlock.Header.Number, len(batch.Requests()), time.Since(t1))
}()

block := &common.Block{
Expand All @@ -147,11 +147,7 @@ func (l *AssemblerLedger) Append(batch types.Batch, orderingInfo types.OrderingI
},
}

var metadataContents [][]byte
for i := 0; i < len(common.BlockMetadataIndex_name); i++ {
metadataContents = append(metadataContents, []byte{})
}
block.Metadata = &common.BlockMetadata{Metadata: metadataContents}
protoutil.InitBlockMetadata(block)

var sigs []*common.MetadataSignature
var signers []uint64
Expand Down