-
Notifications
You must be signed in to change notification settings - Fork 0
/
consensus.go
115 lines (98 loc) · 4.8 KB
/
consensus.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
/*
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 consensus
import (
pb "github.com/hyperledger/fabric/protos"
)
// ExecutionConsumer allows callbacks from asycnhronous execution and statetransfer
type ExecutionConsumer interface {
Executed(tag interface{}) // Called whenever Execute completes
Committed(tag interface{}, target *pb.BlockchainInfo) // Called whenever Commit completes
RolledBack(tag interface{}) // Called whenever a Rollback completes
StateUpdated(tag interface{}, target *pb.BlockchainInfo) // Called when state transfer completes, if target is nil, this indicates a failure and a new target should be supplied
}
// Consenter is used to receive messages from the network
// Every consensus plugin needs to implement this interface
type Consenter interface {
RecvMsg(msg *pb.Message, senderHandle *pb.PeerID) error // Called serially with incoming messages from gRPC
ExecutionConsumer
}
// Inquirer is used to retrieve info about the validating network
type Inquirer interface {
GetNetworkInfo() (self *pb.PeerEndpoint, network []*pb.PeerEndpoint, err error)
GetNetworkHandles() (self *pb.PeerID, network []*pb.PeerID, err error)
}
// Communicator is used to send messages to other validators
type Communicator interface {
Broadcast(msg *pb.Message, peerType pb.PeerEndpoint_Type) error
Unicast(msg *pb.Message, receiverHandle *pb.PeerID) error
}
// NetworkStack is used to retrieve network info and send messages
type NetworkStack interface {
Communicator
Inquirer
}
// SecurityUtils is used to access the sign/verify methods from the crypto package
type SecurityUtils interface {
Sign(msg []byte) ([]byte, error)
Verify(peerID *pb.PeerID, signature []byte, message []byte) error
}
// ReadOnlyLedger is used for interrogating the blockchain
type ReadOnlyLedger interface {
GetBlock(id uint64) (block *pb.Block, err error)
GetBlockchainSize() uint64
GetBlockchainInfo() *pb.BlockchainInfo
GetBlockchainInfoBlob() []byte
GetBlockHeadMetadata() ([]byte, error)
}
// LegacyExecutor is used to invoke transactions, potentially modifying the backing ledger
type LegacyExecutor interface {
BeginTxBatch(id interface{}) error
ExecTxs(id interface{}, txs []*pb.Transaction) ([]byte, error)
CommitTxBatch(id interface{}, metadata []byte) (*pb.Block, error)
RollbackTxBatch(id interface{}) error
PreviewCommitTxBatch(id interface{}, metadata []byte) ([]byte, error)
}
// Executor is intended to eventually supplant the old Executor interface
// The problem with invoking the calls directly above, is that they must be coordinated
// with state transfer, to eliminate possible races and ledger corruption
type Executor interface {
Start() // Bring up the resources needed to use this interface
Halt() // Tear down the resources needed to use this interface
Execute(tag interface{}, txs []*pb.Transaction) // Executes a set of transactions, this may be called in succession
Commit(tag interface{}, metadata []byte) // Commits whatever transactions have been executed
Rollback(tag interface{}) // Rolls back whatever transactions have been executed
UpdateState(tag interface{}, target *pb.BlockchainInfo, peers []*pb.PeerID) // Attempts to synchronize state to a particular target, implicitly calls rollback if needed
}
// LedgerManager is used to manipulate the state of the ledger
type LedgerManager interface {
InvalidateState() // Invalidate informs the ledger that it is out of date and should reject queries
ValidateState() // Validate informs the ledger that it is back up to date and should resume replying to queries
}
// StatePersistor is used to store consensus state which should survive a process crash
type StatePersistor interface {
StoreState(key string, value []byte) error
ReadState(key string) ([]byte, error)
ReadStateSet(prefix string) (map[string][]byte, error)
DelState(key string)
}
// Stack is the set of stack-facing methods available to the consensus plugin
type Stack interface {
NetworkStack
SecurityUtils
Executor
LegacyExecutor
LedgerManager
ReadOnlyLedger
StatePersistor
}