forked from hyperledger/fabric
/
assembler.go
194 lines (164 loc) · 5.66 KB
/
assembler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package smartbft
import (
"encoding/asn1"
"sync/atomic"
"github.com/SmartBFT-Go/consensus/pkg/types"
cb "github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/orderer/common/cluster"
"github.com/hyperledger/fabric/protoutil"
"github.com/pkg/errors"
)
//go:generate mockery -dir . -name Ledger -case underscore -output mocks
// Ledger returns the height and a block with the given number
type Ledger interface {
// Height returns the number of blocks in the ledger this channel is associated with.
Height() uint64
// Block returns a block with the given number,
// or nil if such a block doesn't exist.
Block(number uint64) *cb.Block
}
// Assembler is the proposal assembler
type Assembler struct {
RuntimeConfig *atomic.Value
Logger *flogging.FabricLogger
VerificationSeq func() uint64
}
// AssembleProposal assembles a proposal from the metadata and the request
func (a *Assembler) AssembleProposal(metadata []byte, requests [][]byte) (nextProp types.Proposal) {
rtc := a.RuntimeConfig.Load().(RuntimeConfig)
lastConfigBlockNum := rtc.LastConfigBlock.Header.Number
lastBlock := rtc.LastBlock
if len(requests) == 0 {
a.Logger.Panicf("Programming error, no requests in proposal")
}
batchedRequests := singleConfigTxOrSeveralNonConfigTx(requests, a.Logger)
block := protoutil.NewBlock(lastBlock.Header.Number+1, protoutil.BlockHeaderHash(lastBlock.Header))
block.Data = &cb.BlockData{Data: batchedRequests}
block.Header.DataHash = protoutil.BlockDataHash(block.Data)
if protoutil.IsConfigBlock(block) {
lastConfigBlockNum = block.Header.Number
}
block.Metadata.Metadata[cb.BlockMetadataIndex_LAST_CONFIG] = protoutil.MarshalOrPanic(&cb.Metadata{
Value: protoutil.MarshalOrPanic(&cb.LastConfig{Index: lastConfigBlockNum}),
})
block.Metadata.Metadata[cb.BlockMetadataIndex_SIGNATURES] = protoutil.MarshalOrPanic(&cb.Metadata{
Value: protoutil.MarshalOrPanic(&cb.OrdererBlockMetadata{
ConsenterMetadata: metadata,
LastConfig: &cb.LastConfig{
Index: lastConfigBlockNum,
},
}),
})
tuple := &ByteBufferTuple{
A: protoutil.MarshalOrPanic(block.Data),
B: protoutil.MarshalOrPanic(block.Metadata),
}
prop := types.Proposal{
Header: protoutil.BlockHeaderBytes(block.Header),
Payload: tuple.ToBytes(),
Metadata: metadata,
VerificationSequence: int64(a.VerificationSeq()),
}
return prop
}
func singleConfigTxOrSeveralNonConfigTx(requests [][]byte, logger Logger) [][]byte {
// Scan until a config transaction is found
var batchedRequests [][]byte
var i int
for i < len(requests) {
currentRequest := requests[i]
envelope, err := protoutil.UnmarshalEnvelope(currentRequest)
if err != nil {
logger.Panicf("Programming error, received bad envelope but should have validated it: %v", err)
continue
}
// If we saw a config transaction, we cannot add any more transactions to the batch.
if protoutil.IsConfigTransaction(envelope) {
break
}
// Else, it's not a config transaction, so add it to the batch.
batchedRequests = append(batchedRequests, currentRequest)
i++
}
// If we don't have any transaction in the batch, it is safe to assume we only
// saw a single transaction which is a config transaction.
if len(batchedRequests) == 0 {
batchedRequests = [][]byte{requests[0]}
}
// At this point, batchedRequests contains either a single config transaction, or a few non config transactions.
return batchedRequests
}
// LastConfigBlockFromLedgerOrPanic returns the last config block from the ledger
func LastConfigBlockFromLedgerOrPanic(ledger Ledger, logger Logger) *cb.Block {
block, err := lastConfigBlockFromLedger(ledger)
if err != nil {
logger.Panicf("Failed retrieving last config block: %v", err)
}
return block
}
func lastConfigBlockFromLedger(ledger Ledger) (*cb.Block, error) {
lastBlockSeq := ledger.Height() - 1
lastBlock := ledger.Block(lastBlockSeq)
if lastBlock == nil {
return nil, errors.Errorf("unable to retrieve block [%d]", lastBlockSeq)
}
lastConfigBlock, err := cluster.LastConfigBlock(lastBlock, ledger)
if err != nil {
return nil, err
}
return lastConfigBlock, nil
}
func PreviousConfigBlockFromLedgerOrPanic(ledger Ledger, logger Logger) *cb.Block {
block, err := previousConfigBlockFromLedger(ledger)
if err != nil {
logger.Panicf("Failed retrieving previous config block: %v", err)
}
return block
}
func previousConfigBlockFromLedger(ledger Ledger) (*cb.Block, error) {
previousBlockSeq := ledger.Height() - 2
if ledger.Height() == 1 {
previousBlockSeq = 0
}
previousBlock := ledger.Block(previousBlockSeq)
if previousBlock == nil {
return nil, errors.Errorf("unable to retrieve block [%d]", previousBlockSeq)
}
previousConfigBlock, err := cluster.LastConfigBlock(previousBlock, ledger)
if err != nil {
return nil, err
}
return previousConfigBlock, nil
}
// LastBlockFromLedgerOrPanic returns the last block from the ledger
func LastBlockFromLedgerOrPanic(ledger Ledger, logger Logger) *cb.Block {
lastBlockSeq := ledger.Height() - 1
lastBlock := ledger.Block(lastBlockSeq)
if lastBlock == nil {
logger.Panicf("Failed retrieving last block")
}
return lastBlock
}
// ByteBufferTuple is the byte slice tuple
type ByteBufferTuple struct {
A []byte
B []byte
}
// ToBytes marshals the buffer tuple to bytes
func (bbt *ByteBufferTuple) ToBytes() []byte {
bytes, err := asn1.Marshal(*bbt)
if err != nil {
panic(err)
}
return bytes
}
// FromBytes unmarshals bytes to a buffer tuple
func (bbt *ByteBufferTuple) FromBytes(bytes []byte) error {
_, err := asn1.Unmarshal(bytes, bbt)
return err
}