This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
block_deliver.go
97 lines (83 loc) · 3.48 KB
/
block_deliver.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
/*
Copyright Xilinx Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package fmprotocol
import (
"github.com/golang/protobuf/proto"
cb "github.com/hyperledger/fabric-protos-go/common"
)
// SendBlockHeader prepares packet data based on block header and transaction information
// then send to target addres
func SendBlockHeader(addr string, data []byte, pos int, length int, transactionLen []int) {
annotation := generateBlockAnnotation(pos, length, transactionLen, data)
payload := data[pos : pos+length]
bcmSend(addr, 0x1, annotationListToBytes(annotation), len(annotation), payload)
}
// SendTransaction prepares packet data based on transaction and send to target addres
func SendTransaction(addr string, data []byte, pos int, length int) {
annotation := generateTransactionAnnotation(pos, length, data)
payload := adjustDataBasedOnLocator(data, pos, length, annotation)
bcmSend(addr, 0x2, annotationListToBytes(annotation), getAnnotationActiveSize(annotation), payload)
}
// SendBlockMeta prepares packet data based on block metadata and send to target addres
func SendBlockMeta(addr string, data []byte, pos int, length int) {
annotation := generateBlockMetaAnnotation(pos, length, data)
payload := adjustDataBasedOnLocator(data, pos, length, annotation)
bcmSend(addr, 0x3, annotationListToBytes(annotation), len(annotation), payload)
}
// SendBlock sends a block to target hardware peer via blockchain machine protocol
func SendBlock(addr string, block *cb.Block) {
data, err := proto.Marshal(block)
if err != nil {
logger.Errorf("BCM error: block serilization failed: %v", err)
return
}
// block format: block_header, transaction_1, ..., transaction_N,
// block_metadata
pos := 0
// block header: fleld == 1, length ~=70
field, length, pos := protoGetFieldLength(data, pos)
pos += length
// block data start(envelope): field == 2
field, length, pos = protoGetFieldLength(data, pos)
BlockHeaderPos := 0
BlockHeaderLen := pos - BlockHeaderPos
BlockMetaPos := pos + length
// read transactions
var TransactionPosList []int = nil
var TransactionLengthList []int = nil
for pos < BlockMetaPos {
TransactionPosList = append(TransactionPosList, pos)
field, length, pos = protoGetFieldLength(data, pos)
// field == 1, new transaction, else error ...
if field != 1 {
if len(TransactionPosList) > 0 {
TransactionPosList = TransactionPosList[:len(TransactionPosList)-1]
}
} else {
// ERROR!!!
}
pos += length
TransactionLengthList = append(TransactionLengthList,
pos-TransactionPosList[len(TransactionPosList)-1])
}
// read metadata
field, length, pos = protoGetFieldLength(data, pos)
BlockMetaLen := pos + length - BlockMetaPos
// send data
logger.Debugf("Send block [%d] header", block.Header.Number)
SendBlockHeader(addr, data, BlockHeaderPos, BlockHeaderLen, TransactionLengthList)
for i := 0; i < len(TransactionPosList); i++ {
logger.Debugf("Send block [%d] tx", block.Header.Number)
SendTransaction(addr, data, TransactionPosList[i], TransactionLengthList[i])
}
logger.Debugf("Send block [%d] metadata", block.Header.Number)
SendBlockMeta(addr, data, BlockMetaPos, BlockMetaLen)
}
// sendCertificateCacheUpdate sends certifcate cache update message to target hardware peer
// via blockchain machine protocol
func sendCertificateCacheUpdate(addr string, id int, name string, ca []byte) {
payload, annotations := generateCertificateUpdateAnnotation(id, name, ca)
bcmSend(addr, 0x0, annotationListToBytes(annotations), len(annotations), payload)
}