-
Notifications
You must be signed in to change notification settings - Fork 0
/
assembler.go
189 lines (159 loc) · 5.18 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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package smartbft
import (
"encoding/asn1"
"sync"
"github.com/SmartBFT-Go/consensus/pkg/types"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/orderer/common/cluster"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"
"github.com/pkg/errors"
)
//go:generate mockery -dir . -name Ledger -case underscore -output mocks
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) *common.Block
}
type Assembler struct {
Logger *flogging.FabricLogger
VerificationSeq func() uint64
sync.RWMutex
LastConfigBlockNum uint64
LastBlock *common.Block
}
func (a *Assembler) AssembleProposal(metadata []byte, requests [][]byte) (nextProp types.Proposal) {
a.RLock()
lastConfigBlockNum := a.LastConfigBlockNum
lastBlock := a.LastBlock
a.RUnlock()
if len(requests) == 0 {
a.Logger.Panicf("Programming error, no requests in proposal")
}
batchedRequests := singleConfigTxOrSeveralNonConfigTx(requests, a.Logger)
block := common.NewBlock(lastBlock.Header.Number+1, lastBlock.Header.Hash())
block.Data = &common.BlockData{Data: batchedRequests}
block.Header.DataHash = block.Data.Hash()
if isConfigBlock(block) {
lastConfigBlockNum = block.Header.Number
}
block.Metadata.Metadata[common.BlockMetadataIndex_LAST_CONFIG] = utils.MarshalOrPanic(&common.Metadata{
Value: utils.MarshalOrPanic(&common.LastConfig{Index: lastConfigBlockNum}),
})
block.Metadata.Metadata[common.BlockMetadataIndex_SIGNATURES] = utils.MarshalOrPanic(&common.Metadata{
Value: utils.MarshalOrPanic(&common.OrdererBlockMetadata{
ConsenterMetadata: metadata,
LastConfig: &common.LastConfig{
Index: lastConfigBlockNum,
},
}),
})
tuple := &ByteBufferTuple{
A: utils.MarshalOrPanic(block.Data),
B: utils.MarshalOrPanic(block.Metadata),
}
prop := types.Proposal{
Header: block.Header.Bytes(),
Payload: tuple.ToBytes(),
Metadata: metadata,
VerificationSequence: int64(a.VerificationSeq()),
}
return prop
}
func singleConfigTxOrSeveralNonConfigTx(requests [][]byte, logger PanicLogger) [][]byte {
// Scan until a config transaction is found
var batchedRequests [][]byte
var i int
for i < len(requests) {
currentRequest := requests[i]
envelope, err := utils.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 utils.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
}
func LastConfigBlockFromLedgerOrPanic(ledger Ledger, logger PanicLogger) *common.Block {
block, err := lastConfigBlockFromLedger(ledger)
if err != nil {
logger.Panicf("Failed retrieving last config block: %v", err)
}
return block
}
func lastConfigBlockFromLedger(ledger Ledger) (*common.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 PanicLogger) *common.Block {
block, err := previousConfigBlockFromLedger(ledger)
if err != nil {
logger.Panicf("Failed retrieving previous config block: %v", err)
}
return block
}
func previousConfigBlockFromLedger(ledger Ledger) (*common.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
}
func LastBlockFromLedgerOrPanic(ledger Ledger, logger PanicLogger) *common.Block {
lastBlockSeq := ledger.Height() - 1
lastBlock := ledger.Block(lastBlockSeq)
if lastBlock == nil {
logger.Panicf("Failed retrieving last block")
}
return lastBlock
}
type ByteBufferTuple struct {
A []byte
B []byte
}
func (bbt *ByteBufferTuple) ToBytes() []byte {
bytes, err := asn1.Marshal(*bbt)
if err != nil {
panic(err)
}
return bytes
}
func (bbt *ByteBufferTuple) FromBytes(bytes []byte) error {
_, err := asn1.Unmarshal(bytes, bbt)
return err
}