-
Notifications
You must be signed in to change notification settings - Fork 10
/
echo.go
127 lines (104 loc) · 3.23 KB
/
echo.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
package chari
import (
"encoding/binary"
"fmt"
"sync"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/orderer/multichain"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/tendermint/abci/types"
"github.com/tendermint/tendermint/version"
)
const (
AppHash = "ChariPBFTConsenter"
)
type echo struct {
lock sync.Mutex
lastBlockHeight uint64
supports map[string]multichain.ConsenterSupport
}
func (this *echo) NewSupport(channelId string, support multichain.ConsenterSupport) {
this.lock.Lock()
defer this.lock.Unlock()
this.supports[channelId] = support
}
func (this *echo) getSupport(channelId string) multichain.ConsenterSupport {
this.lock.Lock()
defer this.lock.Unlock()
return this.supports[channelId]
}
func (this *echo) Info(req types.RequestInfo) types.ResponseInfo {
// logger.Info("Info", req)
if val, err := db.Get([]byte("lastBlockHeight"), nil); err == nil {
this.lastBlockHeight = binary.BigEndian.Uint64(val)
}
logger.Info("Info LastBlockHeight", this.lastBlockHeight)
return types.ResponseInfo{
Version: version.Version,
LastBlockHeight: this.lastBlockHeight,
LastBlockAppHash: []byte(AppHash),
}
}
func (this *echo) SetOption(key string, value string) (log string) {
logger.Info("SetOption", key, value)
return ""
}
func (this *echo) InitChain(req types.RequestInitChain) {
logger.Info("InitChain", req)
}
func (this *echo) CheckTx(tx []byte) types.Result {
// logger.Info("CheckTx" /*, string(tx)*/)
return types.OK
}
func (this *echo) BeginBlock(req types.RequestBeginBlock) {
// logger.Info("BeginBlock", req)
}
func (this *echo) DeliverTx(tx []byte) types.Result {
channelId := string(tx[1 : tx[0]+1])
// logger.Info("DeliverTx", channelId /*, string(tx[tx[0]+1:])*/)
env := new(cb.Envelope)
if err := proto.Unmarshal(tx[tx[0]+1:], env); err != nil {
logger.Error(err)
return types.NewResult(types.CodeType_InternalError, nil, err.Error())
}
support := this.getSupport(channelId)
if support == nil {
return types.NewResult(types.CodeType_InternalError, nil, fmt.Sprint("not found channelID ", channelId, " 's support"))
}
batches, committers, _, _ := support.BlockCutter().Ordered(env)
if len(batches) > 0 {
for i, batch := range batches {
block := support.CreateNextBlock(batch)
support.WriteBlock(block, committers[i], nil)
}
}
return types.OK
}
func (this *echo) EndBlock(height uint64) (resp types.ResponseEndBlock) {
logger.Info("EndBlock", height)
this.lastBlockHeight = height
return
}
func (this *echo) Commit() types.Result {
// logger.Info("Commit")
this.lock.Lock()
defer this.lock.Unlock()
for _, support := range this.supports {
batch, committers := support.BlockCutter().Cut()
if len(batch) != 0 {
block := support.CreateNextBlock(batch)
support.WriteBlock(block, committers, nil)
}
}
height := make([]byte, 8)
binary.BigEndian.PutUint64(height, this.lastBlockHeight)
if err := db.Put([]byte("lastBlockHeight"), height, nil); err != nil {
logger.Error(err)
return types.NewResult(types.CodeType_InternalError, nil, err.Error())
}
return types.NewResultOK([]byte(AppHash), "")
}
func (this *echo) Query(req types.RequestQuery) (resp types.ResponseQuery) {
// logger.Info("Query", req)
return
}