forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 11
/
blockcreator.go
46 lines (36 loc) · 1005 Bytes
/
blockcreator.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
/*
Copyright IBM Corp. 2017 All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package etcdraft
import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/flogging"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protoutil"
)
// blockCreator holds number and hash of latest block
// so that next block will be created based on it.
type blockCreator struct {
hash []byte
number uint64
logger *flogging.FabricLogger
}
func (bc *blockCreator) createNextBlock(envs []*cb.Envelope) *cb.Block {
data := &cb.BlockData{
Data: make([][]byte, len(envs)),
}
var err error
for i, env := range envs {
data.Data[i], err = proto.Marshal(env)
if err != nil {
bc.logger.Panicf("Could not marshal envelope: %s", err)
}
}
bc.number++
block := protoutil.NewBlock(bc.number, bc.hash)
block.Header.DataHash = protoutil.BlockDataHash(data)
block.Data = data
bc.hash = protoutil.BlockHeaderHash(block.Header)
return block
}