/
obc-pbft.go
executable file
·163 lines (136 loc) · 4.52 KB
/
obc-pbft.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
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package obcpbft
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/TarantulaTechnology/fabric/consensus"
pb "github.com/TarantulaTechnology/fabric/protos"
"github.com/golang/protobuf/proto"
"github.com/spf13/viper"
)
const configPrefix = "CORE_PBFT"
var pluginInstance consensus.Consenter // singleton service
var config *viper.Viper
func init() {
config = loadConfig()
}
// GetPlugin returns the handle to the Consenter singleton
func GetPlugin(c consensus.Stack) consensus.Consenter {
if pluginInstance == nil {
pluginInstance = New(c)
}
return pluginInstance
}
// New creates a new Obc* instance that provides the Consenter interface.
// Internally, it uses an opaque pbft-core instance.
func New(stack consensus.Stack) consensus.Consenter {
handle, _, _ := stack.GetNetworkHandles()
id, _ := getValidatorID(handle)
switch strings.ToLower(config.GetString("general.mode")) {
case "classic":
return newObcClassic(id, config, stack)
case "batch":
return newObcBatch(id, config, stack)
case "sieve":
return newObcSieve(id, config, stack)
default:
panic(fmt.Errorf("Invalid PBFT mode: %s", config.GetString("general.mode")))
}
}
func loadConfig() (config *viper.Viper) {
config = viper.New()
// for environment variables
config.SetEnvPrefix(configPrefix)
config.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
config.SetEnvKeyReplacer(replacer)
config.SetConfigName("config")
config.AddConfigPath("./")
config.AddConfigPath("../consensus/obcpbft/")
config.AddConfigPath("../../consensus/obcpbft")
// Path to look for the config file in based on GOPATH
gopath := os.Getenv("GOPATH")
for _, p := range filepath.SplitList(gopath) {
obcpbftpath := filepath.Join(p, "src/github.com/TarantulaTechnology/fabric/consensus/obcpbft")
config.AddConfigPath(obcpbftpath)
}
err := config.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Error reading %s plugin config: %s", configPrefix, err))
}
return
}
// Returns the uint64 ID corresponding to a peer handle
func getValidatorID(handle *pb.PeerID) (id uint64, err error) {
// as requested here: https://github.com/TarantulaTechnology/fabric/issues/462#issuecomment-170785410
if startsWith := strings.HasPrefix(handle.Name, "vp"); startsWith {
id, err = strconv.ParseUint(handle.Name[2:], 10, 64)
if err != nil {
return id, fmt.Errorf("Error extracting ID from \"%s\" handle: %v", handle.Name, err)
}
return
}
err = fmt.Errorf(`For MVP, set the VP's peer.id to vpX,
where X is a unique integer between 0 and N-1
(N being the maximum number of VPs in the network`)
return
}
// Returns the peer handle that corresponds to a validator ID (uint64 assigned to it for PBFT)
func getValidatorHandle(id uint64) (handle *pb.PeerID, err error) {
// as requested here: https://github.com/TarantulaTechnology/fabric/issues/462#issuecomment-170785410
name := "vp" + strconv.FormatUint(id, 10)
return &pb.PeerID{Name: name}, nil
}
// Returns the peer handles corresponding to a list of replica ids
func getValidatorHandles(ids []uint64) (handles []*pb.PeerID) {
handles = make([]*pb.PeerID, len(ids))
for i, id := range ids {
handles[i], _ = getValidatorHandle(id)
}
return
}
type obcGeneric struct {
stack consensus.Stack
pbft *pbftCore
}
func (op *obcGeneric) skipTo(seqNo uint64, id []byte, replicas []uint64) {
info := &pb.BlockchainInfo{}
err := proto.Unmarshal(id, info)
if err != nil {
logger.Error(fmt.Sprintf("Error unmarshaling: %s", err))
return
}
op.stack.UpdateState(&checkpointMessage{seqNo, id}, info, getValidatorHandles(replicas))
}
func (op *obcGeneric) invalidateState() {
op.stack.InvalidateState()
}
func (op *obcGeneric) validateState() {
op.stack.ValidateState()
}
func (op *obcGeneric) getState() []byte {
return op.stack.GetBlockchainInfoBlob()
}
func (op *obcGeneric) getLastSeqNo() (uint64, error) {
raw, err := op.stack.GetBlockHeadMetadata()
if err != nil {
return 0, err
}
meta := &Metadata{}
proto.Unmarshal(raw, meta)
return meta.SeqNo, nil
}