@@ -17,31 +17,34 @@ limitations under the License.
17
17
package static
18
18
19
19
import (
20
- "math/rand"
20
+ "fmt"
21
+ "time"
21
22
23
+ "github.com/hyperledger/fabric/core/crypto/primitives"
22
24
"github.com/hyperledger/fabric/orderer/common/bootstrap"
23
25
"github.com/hyperledger/fabric/orderer/common/cauthdsl"
24
26
"github.com/hyperledger/fabric/orderer/common/configtx"
25
27
cb "github.com/hyperledger/fabric/protos/common"
26
28
27
29
"github.com/golang/protobuf/proto"
30
+ "github.com/golang/protobuf/ptypes/timestamp"
28
31
)
29
32
30
33
type bootstrapper struct {
31
34
chainID []byte
32
35
}
33
36
34
- // New returns a new static bootstrap helper
37
+ // New returns a new static bootstrap helper.
35
38
func New () bootstrap.Helper {
36
- b := make ([]byte , 16 )
37
- rand .Read (b )
38
-
39
- return & bootstrapper {
40
- chainID : b ,
39
+ chainID , err := primitives .GetRandomBytes (16 )
40
+ if err != nil {
41
+ panic (fmt .Errorf ("Cannot generate random chain ID: %s" , err ))
41
42
}
43
+ return & bootstrapper {chainID }
42
44
}
43
45
44
- // errorlessMarshal prevents poluting this code with many panics, if the genesis block cannot be created, the system cannot start so panic is correct
46
+ // errorlessMarshal prevents poluting this code with panics
47
+ // If the genesis block cannot be created, the system cannot start so panic is correct
45
48
func errorlessMarshal (thing proto.Message ) []byte {
46
49
data , err := proto .Marshal (thing )
47
50
if err != nil {
@@ -50,19 +53,63 @@ func errorlessMarshal(thing proto.Message) []byte {
50
53
return data
51
54
}
52
55
53
- func (b * bootstrapper ) makeSignedConfigurationItem (id string , ctype cb.ConfigurationItem_ConfigurationType , data []byte , modificationPolicyID string ) * cb.SignedConfigurationItem {
54
- configurationBytes := errorlessMarshal (& cb.ConfigurationItem {
55
- Header : & cb.ChainHeader {
56
- ChainID : b .chainID ,
56
+ func makeChainHeader (headerType cb.HeaderType , version int32 , chainID []byte ) * cb.ChainHeader {
57
+ return & cb.ChainHeader {
58
+ Type : int32 (headerType ),
59
+ Version : version ,
60
+ Timestamp : & timestamp.Timestamp {
61
+ Seconds : time .Now ().Unix (),
62
+ Nanos : 0 ,
57
63
},
64
+ ChainID : chainID ,
65
+ }
66
+ }
67
+
68
+ func makeSignatureHeader (serializedCreatorCertChain []byte , nonce []byte , epoch uint64 ) * cb.SignatureHeader {
69
+ return & cb.SignatureHeader {
70
+ Creator : serializedCreatorCertChain ,
71
+ Nonce : nonce ,
72
+ Epoch : epoch ,
73
+ }
74
+ }
75
+
76
+ func (b * bootstrapper ) makeSignedConfigurationItem (configurationItemType cb.ConfigurationItem_ConfigurationType , modificationPolicyID string , key string , value []byte ) * cb.SignedConfigurationItem {
77
+ marshaledConfigurationItem := errorlessMarshal (& cb.ConfigurationItem {
78
+ Header : makeChainHeader (cb .HeaderType_CONFIGURATION_ITEM , 1 , b .chainID ),
79
+ Type : configurationItemType ,
58
80
LastModified : 0 ,
59
- Type : ctype ,
60
81
ModificationPolicy : modificationPolicyID ,
61
- Key : id ,
62
- Value : data ,
82
+ Key : key ,
83
+ Value : value ,
63
84
})
85
+
64
86
return & cb.SignedConfigurationItem {
65
- ConfigurationItem : configurationBytes ,
87
+ ConfigurationItem : marshaledConfigurationItem ,
88
+ Signatures : nil ,
89
+ }
90
+ }
91
+
92
+ func (b * bootstrapper ) makeConfigurationEnvelope (items ... * cb.SignedConfigurationItem ) * cb.ConfigurationEnvelope {
93
+ return & cb.ConfigurationEnvelope {
94
+ Items : items ,
95
+ }
96
+ }
97
+
98
+ func (b * bootstrapper ) makeEnvelope (configurationEnvelope * cb.ConfigurationEnvelope ) * cb.Envelope {
99
+ nonce , err := primitives .GetRandomNonce ()
100
+ if err != nil {
101
+ panic (fmt .Errorf ("Cannot generate random nonce: %s" , err ))
102
+ }
103
+ marshaledPayload := errorlessMarshal (& cb.Payload {
104
+ Header : & cb.Header {
105
+ ChainHeader : makeChainHeader (cb .HeaderType_CONFIGURATION_TRANSACTION , 1 , b .chainID ),
106
+ SignatureHeader : makeSignatureHeader (nil , nonce , 0 ),
107
+ },
108
+ Data : errorlessMarshal (configurationEnvelope ),
109
+ })
110
+ return & cb.Envelope {
111
+ Payload : marshaledPayload ,
112
+ Signature : nil ,
66
113
}
67
114
}
68
115
@@ -77,27 +124,20 @@ func sigPolicyToPolicy(sigPolicy *cb.SignaturePolicyEnvelope) []byte {
77
124
78
125
// GenesisBlock returns the genesis block to be used for bootstrapping
79
126
func (b * bootstrapper ) GenesisBlock () (* cb.Block , error ) {
80
-
81
127
// Lock down the default modification policy to prevent any further policy modifications
82
- lockdownDefaultModificationPolicy := b .makeSignedConfigurationItem (configtx .DefaultModificationPolicyID , cb .ConfigurationItem_Policy , sigPolicyToPolicy (cauthdsl .RejectAllPolicy ), configtx .DefaultModificationPolicyID )
83
-
84
- initialConfigTX := errorlessMarshal (& cb.ConfigurationEnvelope {
85
- Items : []* cb.SignedConfigurationItem {
86
- lockdownDefaultModificationPolicy ,
87
- },
88
- })
128
+ lockdownDefaultModificationPolicy := b .makeSignedConfigurationItem (cb .ConfigurationItem_Policy , configtx .DefaultModificationPolicyID , configtx .DefaultModificationPolicyID , sigPolicyToPolicy (cauthdsl .RejectAllPolicy ))
89
129
90
- data := & cb.BlockData {
91
- Data : [][]byte {initialConfigTX },
130
+ blockData := & cb.BlockData {
131
+ Data : [][]byte {errorlessMarshal ( b . makeEnvelope ( b . makeConfigurationEnvelope ( lockdownDefaultModificationPolicy ))) },
92
132
}
93
133
94
134
return & cb.Block {
95
135
Header : & cb.BlockHeader {
96
136
Number : 0 ,
97
- PreviousHash : [] byte ( "GENESIS" ) ,
98
- DataHash : data .Hash (),
137
+ PreviousHash : nil ,
138
+ DataHash : blockData .Hash (),
99
139
},
100
- Data : data ,
140
+ Data : blockData ,
141
+ Metadata : nil ,
101
142
}, nil
102
-
103
143
}
0 commit comments