|
| 1 | +package tbtc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/keep-network/keep-core/pkg/chain" |
| 7 | + "github.com/keep-network/keep-core/pkg/generator" |
| 8 | + "github.com/keep-network/keep-core/pkg/net" |
| 9 | + "github.com/keep-network/keep-core/pkg/protocol/group" |
| 10 | + "golang.org/x/sync/semaphore" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + // coordinationFrequencyBlocks is the number of blocks between two |
| 15 | + // consecutive coordination windows. |
| 16 | + coordinationFrequencyBlocks = 900 |
| 17 | + // coordinationActivePhaseDurationBlocks is the number of blocks in the |
| 18 | + // active phase of the coordination window. The active phase is the |
| 19 | + // phase during which the communication between the coordination leader and |
| 20 | + // their followers is allowed. |
| 21 | + coordinationActivePhaseDurationBlocks = 80 |
| 22 | + // coordinationPassivePhaseDurationBlocks is the number of blocks in the |
| 23 | + // passive phase of the coordination window. The passive phase is the |
| 24 | + // phase during which communication is not allowed. Participants are |
| 25 | + // expected to validate the result of the coordination and prepare for |
| 26 | + // execution of the proposed wallet action. |
| 27 | + coordinationPassivePhaseDurationBlocks = 20 |
| 28 | + // coordinationDurationBlocks is the number of blocks in a single |
| 29 | + // coordination window. |
| 30 | + coordinationDurationBlocks = coordinationActivePhaseDurationBlocks + |
| 31 | + coordinationPassivePhaseDurationBlocks |
| 32 | +) |
| 33 | + |
| 34 | +// errCoordinationExecutorBusy is an error returned when the coordination |
| 35 | +// executor cannot execute the requested coordination due to an ongoing one. |
| 36 | +var errCoordinationExecutorBusy = fmt.Errorf("coordination executor is busy") |
| 37 | + |
| 38 | +// coordinationWindow represents a single coordination window. The coordination |
| 39 | +// block is the first block of the window. |
| 40 | +type coordinationWindow struct { |
| 41 | + // coordinationBlock is the first block of the coordination window. |
| 42 | + coordinationBlock uint64 |
| 43 | +} |
| 44 | + |
| 45 | +// newCoordinationWindow creates a new coordination window for the given |
| 46 | +// coordination block. |
| 47 | +func newCoordinationWindow(coordinationBlock uint64) *coordinationWindow { |
| 48 | + return &coordinationWindow{ |
| 49 | + coordinationBlock: coordinationBlock, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// ActivePhaseEndBlock returns the block number at which the active phase |
| 54 | +// of the coordination window ends. |
| 55 | +func (cw *coordinationWindow) activePhaseEndBlock() uint64 { |
| 56 | + return cw.coordinationBlock + coordinationActivePhaseDurationBlocks |
| 57 | +} |
| 58 | + |
| 59 | +// EndBlock returns the block number at which the coordination window ends. |
| 60 | +func (cw *coordinationWindow) endBlock() uint64 { |
| 61 | + return cw.coordinationBlock + coordinationDurationBlocks |
| 62 | +} |
| 63 | + |
| 64 | +// isAfter returns true if this coordination window is after the other |
| 65 | +// window. |
| 66 | +func (cw *coordinationWindow) isAfter(other *coordinationWindow) bool { |
| 67 | + if other == nil { |
| 68 | + return true |
| 69 | + } |
| 70 | + |
| 71 | + return cw.coordinationBlock > other.coordinationBlock |
| 72 | +} |
| 73 | + |
| 74 | +// watchCoordinationWindows watches for new coordination windows and runs |
| 75 | +// the given callback when a new window is detected. The callback is run |
| 76 | +// in a separate goroutine. It is guaranteed that the callback is not run |
| 77 | +// twice for the same window. The context passed as the first parameter |
| 78 | +// is used to cancel the watch. |
| 79 | +func watchCoordinationWindows( |
| 80 | + ctx context.Context, |
| 81 | + watchBlocksFn func(ctx context.Context) <-chan uint64, |
| 82 | + onWindowFn func(window *coordinationWindow), |
| 83 | +) { |
| 84 | + blocksChan := watchBlocksFn(ctx) |
| 85 | + var lastWindow *coordinationWindow |
| 86 | + |
| 87 | + for { |
| 88 | + select { |
| 89 | + case block := <-blocksChan: |
| 90 | + if block%coordinationFrequencyBlocks == 0 { |
| 91 | + // Make sure the current window is not the same as the last one. |
| 92 | + // There is no guarantee that the block channel will not emit |
| 93 | + // the same block again. |
| 94 | + if window := newCoordinationWindow(block); window.isAfter(lastWindow) { |
| 95 | + lastWindow = window |
| 96 | + // Run the callback in a separate goroutine to avoid blocking |
| 97 | + // this loop and potentially missing the next block. |
| 98 | + go onWindowFn(window) |
| 99 | + } |
| 100 | + } |
| 101 | + case <-ctx.Done(): |
| 102 | + return |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// CoordinationFaultType represents a type of the coordination fault. |
| 108 | +type CoordinationFaultType uint8 |
| 109 | + |
| 110 | +const ( |
| 111 | + // FaultUnknown is a fault type used when the fault type is unknown. |
| 112 | + FaultUnknown CoordinationFaultType = iota |
| 113 | + // FaultLeaderIdleness is a fault type used when the leader was idle, i.e. |
| 114 | + // missed their turn to propose a wallet action. |
| 115 | + FaultLeaderIdleness |
| 116 | + // FaultLeaderMistake is a fault type used when the leader's proposal |
| 117 | + // turned out to be invalid. |
| 118 | + FaultLeaderMistake |
| 119 | + // FaultLeaderImpersonation is a fault type used when the leader was |
| 120 | + // impersonated by another operator who raised their own proposal. |
| 121 | + FaultLeaderImpersonation |
| 122 | +) |
| 123 | + |
| 124 | +func (cft CoordinationFaultType) String() string { |
| 125 | + switch cft { |
| 126 | + case FaultUnknown: |
| 127 | + return "Unknown" |
| 128 | + case FaultLeaderIdleness: |
| 129 | + return "LeaderIdleness" |
| 130 | + case FaultLeaderMistake: |
| 131 | + return "FaultLeaderMistake" |
| 132 | + case FaultLeaderImpersonation: |
| 133 | + return "LeaderImpersonation" |
| 134 | + default: |
| 135 | + panic("unknown coordination fault type") |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +// coordinationFault represents a single coordination fault. |
| 140 | +type coordinationFault struct { |
| 141 | + // culprit is the address of the operator that is responsible for the fault. |
| 142 | + culprit chain.Address |
| 143 | + // faultType is the type of the fault. |
| 144 | + faultType CoordinationFaultType |
| 145 | +} |
| 146 | + |
| 147 | +func (cf *coordinationFault) String() string { |
| 148 | + return fmt.Sprintf( |
| 149 | + "operator [%s], fault [%s]", |
| 150 | + cf.culprit, |
| 151 | + cf.faultType, |
| 152 | + ) |
| 153 | +} |
| 154 | + |
| 155 | +// coordinationProposal represents a single action proposal for the given wallet. |
| 156 | +type coordinationProposal interface { |
| 157 | + // actionType returns the specific type of the walletAction being subject |
| 158 | + // of this proposal. |
| 159 | + actionType() WalletActionType |
| 160 | + // validityBlocks returns the number of blocks for which the proposal is |
| 161 | + // valid. |
| 162 | + validityBlocks() uint64 |
| 163 | +} |
| 164 | + |
| 165 | +// noopProposal is a proposal that does not propose any action. |
| 166 | +type noopProposal struct{} |
| 167 | + |
| 168 | +func (np *noopProposal) actionType() WalletActionType { |
| 169 | + return ActionNoop |
| 170 | +} |
| 171 | + |
| 172 | +func (np *noopProposal) validityBlocks() uint64 { |
| 173 | + // Panic to make sure that the proposal is not processed by the node. |
| 174 | + panic("noop proposal does not have validity blocks") |
| 175 | +} |
| 176 | + |
| 177 | +// coordinationResult represents the result of the coordination procedure |
| 178 | +// executed for the given wallet in the given coordination window. |
| 179 | +type coordinationResult struct { |
| 180 | + wallet wallet |
| 181 | + window *coordinationWindow |
| 182 | + leader chain.Address |
| 183 | + proposal coordinationProposal |
| 184 | + faults []*coordinationFault |
| 185 | +} |
| 186 | + |
| 187 | +func (cr *coordinationResult) String() string { |
| 188 | + return fmt.Sprintf( |
| 189 | + "wallet [%s], window [%v], leader [%s], proposal [%s], faults [%s]", |
| 190 | + &cr.wallet, |
| 191 | + cr.window.coordinationBlock, |
| 192 | + cr.leader, |
| 193 | + cr.proposal.actionType(), |
| 194 | + cr.faults, |
| 195 | + ) |
| 196 | +} |
| 197 | + |
| 198 | +// coordinationExecutor is responsible for executing the coordination |
| 199 | +// procedure for the given wallet. |
| 200 | +type coordinationExecutor struct { |
| 201 | + lock *semaphore.Weighted |
| 202 | + |
| 203 | + signers []*signer // TODO: Do we need whole signers? |
| 204 | + broadcastChannel net.BroadcastChannel |
| 205 | + membershipValidator *group.MembershipValidator |
| 206 | + protocolLatch *generator.ProtocolLatch |
| 207 | +} |
| 208 | + |
| 209 | +// newCoordinationExecutor creates a new coordination executor for the |
| 210 | +// given wallet. |
| 211 | +func newCoordinationExecutor( |
| 212 | + signers []*signer, |
| 213 | + broadcastChannel net.BroadcastChannel, |
| 214 | + membershipValidator *group.MembershipValidator, |
| 215 | + protocolLatch *generator.ProtocolLatch, |
| 216 | +) *coordinationExecutor { |
| 217 | + return &coordinationExecutor{ |
| 218 | + lock: semaphore.NewWeighted(1), |
| 219 | + signers: signers, |
| 220 | + broadcastChannel: broadcastChannel, |
| 221 | + membershipValidator: membershipValidator, |
| 222 | + protocolLatch: protocolLatch, |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +// wallet returns the wallet this executor is responsible for. |
| 227 | +func (ce *coordinationExecutor) wallet() wallet { |
| 228 | + // All signers belong to one wallet. Take that wallet from the |
| 229 | + // first signer. |
| 230 | + return ce.signers[0].wallet |
| 231 | +} |
| 232 | + |
| 233 | +// coordinate executes the coordination procedure for the given coordination |
| 234 | +// window. |
| 235 | +func (ce *coordinationExecutor) coordinate( |
| 236 | + window *coordinationWindow, |
| 237 | +) (*coordinationResult, error) { |
| 238 | + if lockAcquired := ce.lock.TryAcquire(1); !lockAcquired { |
| 239 | + return nil, errCoordinationExecutorBusy |
| 240 | + } |
| 241 | + defer ce.lock.Release(1) |
| 242 | + |
| 243 | + // TODO: Implement coordination logic. Remember about: |
| 244 | + // - Setting up the right context |
| 245 | + // - Using the protocol latch |
| 246 | + // - Using the membership validator |
| 247 | + // Example result: |
| 248 | + result := &coordinationResult{ |
| 249 | + wallet: ce.wallet(), |
| 250 | + window: window, |
| 251 | + leader: ce.wallet().signingGroupOperators[0], |
| 252 | + proposal: &noopProposal{}, |
| 253 | + faults: nil, |
| 254 | + } |
| 255 | + |
| 256 | + return result, nil |
| 257 | +} |
0 commit comments