-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
176 lines (164 loc) · 8.2 KB
/
validation.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
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright IBM Corp. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package migration
import (
"github.com/hyperledger/fabric/protos/orderer"
"github.com/pkg/errors"
)
// Validate checks the validity of the state transitions of a possible migration config update tx by comparing the
// current ConsensusTypeInfo config with the next (proposed) ConsensusTypeInfo. It is called during the broadcast
// phase and never changes the status of the underlying chain.
func Validate(systemChannel bool, current, next *ConsensusTypeInfo) error {
if systemChannel {
return validateSystem(current, next)
}
return validateStandard(current, next)
}
func validateSystem(current, next *ConsensusTypeInfo) error {
// Check validity of new state, type, and context
unExpCtx := "Consensus-type migration, state=%s, unexpected context, actual=%d (expected=%s)"
unExpType := "Consensus-type migration, state=%s, unexpected type, actual=%s (expected=%s)"
switch next.State {
case orderer.ConsensusType_MIG_STATE_NONE:
if next.Context != 0 {
return errors.Errorf(unExpCtx, next.State, next.Context, "0")
}
case orderer.ConsensusType_MIG_STATE_START:
if next.Type != "kafka" {
return errors.Errorf(unExpType, next.State, next.Type, "kafka")
}
if next.Context != 0 {
return errors.Errorf(unExpCtx, next.State, next.Context, "0")
}
case orderer.ConsensusType_MIG_STATE_COMMIT:
if next.Type != "etcdraft" {
return errors.Errorf(unExpType, next.State, next.Type, "etcdraft")
}
if next.Context <= 0 {
return errors.Errorf(unExpCtx, next.State, next.Context, ">0")
}
case orderer.ConsensusType_MIG_STATE_ABORT:
if next.Type != "kafka" {
return errors.Errorf(unExpType, next.State, next.Type, "kafka")
}
if next.Context <= 0 {
return errors.Errorf(unExpCtx, next.State, next.Context, ">0")
}
default:
return errors.Errorf("Consensus-type migration, state=%s, not permitted on system channel", next.State)
}
// The following code explicitly checks for permitted transitions; all other transitions return an error.
if current.Type != next.Type {
if current.Type == "kafka" && next.Type == "etcdraft" {
// On the system channels, this is permitted, green path commit
isSysCommit := (current.State == orderer.ConsensusType_MIG_STATE_START) && (next.State == orderer.ConsensusType_MIG_STATE_COMMIT)
if !isSysCommit {
return errors.Errorf("Attempted to change consensus type from %s to %s, unexpected state transition: %s to %s",
current.Type, next.Type, current.State, next.State)
}
} else if current.Type == "etcdraft" && next.Type == "kafka" {
return errors.Errorf("Attempted to change consensus type from %s to %s, not permitted on system channel", current.Type, next.Type)
} else {
return errors.Errorf("Attempted to change consensus type from %s to %s, not supported", current.Type, next.Type)
}
} else {
// This is always permitted, not a migration
isNotMig := (current.State == orderer.ConsensusType_MIG_STATE_NONE) && (next.State == current.State)
if isNotMig {
return nil
}
// Migration state may change when the type stays the same
if current.Type == "kafka" {
// In the "green" path: the system channel starts migration
isStart := (current.State == orderer.ConsensusType_MIG_STATE_NONE) && (next.State == orderer.ConsensusType_MIG_STATE_START)
// In the "abort" path: the system channel aborts a migration
isAbort := (current.State == orderer.ConsensusType_MIG_STATE_START) && (next.State == orderer.ConsensusType_MIG_STATE_ABORT)
// In the "abort" path: the system channel reconfigures after an abort, not a migration
isNotMigAfterAbort := (current.State == orderer.ConsensusType_MIG_STATE_ABORT) && (next.State == orderer.ConsensusType_MIG_STATE_NONE)
// In the "abort" path: the system channel starts a new migration attempt after an abort
isStartAfterAbort := (current.State == orderer.ConsensusType_MIG_STATE_ABORT) && (next.State == orderer.ConsensusType_MIG_STATE_START)
if !(isNotMig || isStart || isAbort || isNotMigAfterAbort || isStartAfterAbort) {
return errors.Errorf("Consensus type %s, unexpected migration state transition: %s to %s",
current.Type, current.State, next.State)
}
} else if current.Type == "etcdraft" {
// In the "green" path: the system channel reconfigures after a successful migration
isConfAfterSuccess := (current.State == orderer.ConsensusType_MIG_STATE_COMMIT) && (next.State == orderer.ConsensusType_MIG_STATE_NONE)
if !(isNotMig || isConfAfterSuccess) {
return errors.Errorf("Consensus type %s, unexpected migration state transition: %s to %s",
current.Type, current.State.String(), next.State)
}
}
}
return nil
}
func validateStandard(current, next *ConsensusTypeInfo) error {
// Check validity of new state, type, and context
unExpCtx := "Consensus-type migration, state=%s, unexpected context, actual=%d (expected=%s)"
switch next.State {
case orderer.ConsensusType_MIG_STATE_NONE:
if next.Context != 0 {
return errors.Errorf(unExpCtx, next.State, next.Context, "0")
}
case orderer.ConsensusType_MIG_STATE_CONTEXT:
if next.Type != "etcdraft" {
unExpType := "Consensus-type migration, state=%s, unexpected type, actual=%s (expected=%s)"
return errors.Errorf(unExpType, next.State, next.Type, "etcdraft")
}
if next.Context <= 0 {
return errors.Errorf(unExpCtx, next.State, next.Context, ">0")
}
case orderer.ConsensusType_MIG_STATE_ABORT:
if next.Type != "kafka" {
unExpType := "Consensus-type migration, state=%s, unexpected type, actual=%s (expected=%s)"
return errors.Errorf(unExpType, next.State, next.Type, "kafka")
}
if next.Context <= 0 {
return errors.Errorf(unExpCtx, next.State, next.Context, ">0")
}
default:
return errors.Errorf("Consensus-type migration, state=%s, not permitted on standard channel", next.State)
}
// The following code explicitly checks for permitted transitions; all other transitions return an error.
if current.Type != next.Type {
badAttemptStr := "Attempted to change consensus type from %s to %s, unexpected state transition: %s to %s"
if current.Type == "kafka" && next.Type == "etcdraft" {
// On the standard channels, this is permitted, green path context
isCtx := (current.State == orderer.ConsensusType_MIG_STATE_NONE) && (next.State == orderer.ConsensusType_MIG_STATE_CONTEXT)
isCtxAfterAbort := (current.State == orderer.ConsensusType_MIG_STATE_ABORT) && (next.State == orderer.ConsensusType_MIG_STATE_CONTEXT)
if !(isCtx || isCtxAfterAbort) {
return errors.Errorf(badAttemptStr, current.Type, next.Type, current.State, next.State)
}
} else if current.Type == "etcdraft" && next.Type == "kafka" {
// On the standard channels, this is permitted, abort path
isAbort := (current.State == orderer.ConsensusType_MIG_STATE_CONTEXT) && (next.State == orderer.ConsensusType_MIG_STATE_ABORT)
if !isAbort {
return errors.Errorf(badAttemptStr, current.Type, next.Type, current.State, next.State)
}
} else {
return errors.Errorf("Attempted to change consensus type from %s to %s, not supported", current.Type, next.Type)
}
} else {
// This is always permitted, not a migration
isNotMig := (current.State == orderer.ConsensusType_MIG_STATE_NONE) && (next.State == current.State)
if isNotMig {
return nil
}
// Migration state may change when the type stays the same
if current.Type == "etcdraft" {
// In the "green" path: a channel reconfigures after a successful migration
isConfigAfterSuccess := (current.State == orderer.ConsensusType_MIG_STATE_CONTEXT) && (next.State == orderer.ConsensusType_MIG_STATE_NONE)
if !isConfigAfterSuccess {
return errors.Errorf("Consensus type %s, unexpected migration state transition: %s to %s", current.Type, current.State, next.State)
}
} else if current.Type == "kafka" {
isAbort := (current.State == orderer.ConsensusType_MIG_STATE_NONE) && (next.State == orderer.ConsensusType_MIG_STATE_ABORT)
isConfigAfterAbort := (current.State == orderer.ConsensusType_MIG_STATE_ABORT) && (next.State == orderer.ConsensusType_MIG_STATE_NONE)
// Not a migration
if !(isAbort || isConfigAfterAbort) {
return errors.Errorf("Consensus type %s, unexpected migration state transition: %s to %s", current.Type, current.State, next.State)
}
}
}
return nil
}