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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use super::keys::{PublicKey, Signature, SignatureShare};
use super::money::Money;
use crdts::Dot;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use threshold_crypto::PublicKeySet;

/// Actor id
pub type AccountId = PublicKey;

/// Transfer ID.
pub type TransferId = Dot<AccountId>;

/// A transfer of money between two keys.
#[derive(Clone, Hash, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Debug)]
pub struct Transfer {
    /// Transfer ID, containing source key.
    pub id: TransferId,
    /// The destination to transfer to.
    pub to: AccountId,
    /// The amount to transfer.
    pub amount: Money,
}

impl Transfer {
    /// Get the transfer id
    pub fn id(&self) -> TransferId {
        self.id
    }

    /// Get the amount of this transfer
    pub fn amount(&self) -> Money {
        self.amount
    }

    /// Get the sender of this transfer
    pub fn from(&self) -> AccountId {
        self.id.actor
    }

    /// Get the recipient of this transfer
    pub fn to(&self) -> PublicKey {
        self.to
    }
}

/// The aggregated Replica signatures of the Actor debit cmd.
#[derive(Clone, Hash, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Debug)]
pub struct DebitAgreementProof {
    /// The cmd generated by sender Actor.
    pub signed_transfer: SignedTransfer,
    /// Quorum of Replica sigs over the transfer cmd.
    pub debiting_replicas_sig: Signature,
    /// PublicKeySet of the replica when it validated the transfer.
    pub replica_key: ReplicaPublicKeySet,
}

impl DebitAgreementProof {
    /// Get the transfer id
    pub fn id(&self) -> TransferId {
        self.signed_transfer.id()
    }

    /// Get the amount of this transfer
    pub fn amount(&self) -> Money {
        self.signed_transfer.amount()
    }

    /// Get the sender of this transfer
    pub fn from(&self) -> PublicKey {
        self.signed_transfer.from()
    }

    /// Get the recipient of this transfer
    pub fn to(&self) -> PublicKey {
        self.signed_transfer.to()
    }

    /// Get the PublicKeySet of the replica that validated this transfer
    pub fn replica_keys(&self) -> ReplicaPublicKeySet {
        self.replica_key.clone()
    }
}

/// An Actor cmd.
#[derive(Clone, Hash, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Debug)]
pub struct SignedTransfer {
    /// The transfer.
    pub transfer: Transfer,
    /// Actor signature over the transfer.
    pub actor_signature: Signature,
}

impl SignedTransfer {
    /// Get the transfer id
    pub fn id(&self) -> TransferId {
        self.transfer.id
    }

    /// Get the amount of this transfer
    pub fn amount(&self) -> Money {
        self.transfer.amount
    }

    /// Get the sender of this transfer
    pub fn from(&self) -> PublicKey {
        self.transfer.id.actor
    }

    /// Get the recipient of this transfer
    pub fn to(&self) -> PublicKey {
        self.transfer.to
    }
}

// ------------------------------------------------------------
//                      Replica
// ------------------------------------------------------------

/// Events raised by the Replica.
#[allow(clippy::large_enum_variant)]
#[derive(Clone, Hash, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Debug)]
pub enum ReplicaEvent {
    /// The event raised when
    /// ValidateTransfer cmd has been successful.
    TransferValidated(TransferValidated),
    /// The event raised when
    /// RegisterTransfer cmd has been successful.
    TransferRegistered(TransferRegistered),
    /// The event raised when
    /// PropagateTransfer cmd has been successful.
    TransferPropagated(TransferPropagated),
    // /// The event raised when
    // /// peers changed so that we have a new PublicKeySet.
    // PeersChanged(PeersChanged),
    /// The event raised when
    /// we learn of a new group PK set.
    KnownGroupAdded(KnownGroupAdded),
}

/// The debiting Replica event raised when
/// ValidateTransfer cmd has been successful.
#[derive(Clone, Hash, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Debug)]
pub struct TransferValidated {
    /// The cmd generated by Actor.
    pub signed_transfer: SignedTransfer,
    /// Replica signature over the transfer cmd.
    pub replica_signature: SignatureShare,
    /// The PK Set of the Replicas
    pub replicas: PublicKeySet,
    // NB: I'm a bit ambivalent to this implicit communication of public key change.
    // I generally prefer an explicit cmd + event for such a significant part of the logic.
    // Including it here minimizes msg types and traffic, and seamlessly - apparently -
    // updates Actors on any public key change, which they can accumulate in order to
    // apply the change to local state.
    // But it inflicts on, and complicates the logic for validating a transfer..
    // Cost / benefit to be discussed..
}

impl TransferValidated {
    /// Get the transfer id
    pub fn id(&self) -> TransferId {
        self.signed_transfer.id()
    }

    /// Get the amount of this transfer
    pub fn amount(&self) -> Money {
        self.signed_transfer.amount()
    }

    /// Get the recipient of this transfer
    pub fn from(&self) -> PublicKey {
        self.signed_transfer.from()
    }

    /// Get the recipient of this transfer
    pub fn to(&self) -> PublicKey {
        self.signed_transfer.to()
    }
}

/// The debiting Replica event raised when
/// RegisterTransfer cmd has been successful.
#[derive(Clone, Hash, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Debug)]
pub struct TransferRegistered {
    /// The debit proof.
    pub debit_proof: DebitAgreementProof,
}

impl TransferRegistered {
    /// Get the transfer id
    pub fn id(&self) -> TransferId {
        self.debit_proof.id()
    }

    /// Get the amount of this transfer
    pub fn amount(&self) -> Money {
        self.debit_proof.amount()
    }

    /// Get the recipient of this transfer
    pub fn from(&self) -> PublicKey {
        self.debit_proof.from()
    }

    /// Get the recipient of this transfer
    pub fn to(&self) -> PublicKey {
        self.debit_proof.to()
    }
}

/// The crediting Replica event raised when
/// PropagateTransfer cmd has been successful.
#[derive(Clone, Hash, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Debug)]
pub struct TransferPropagated {
    /// The debiting Replicas' proof.
    pub debit_proof: DebitAgreementProof,
    /// The pub key of the debiting Replicas.
    pub debiting_replicas: PublicKey,
    /// The crediting Replica signature.
    pub crediting_replica_sig: SignatureShare,
}

impl TransferPropagated {
    /// Get the transfer id
    pub fn id(&self) -> TransferId {
        self.debit_proof.id()
    }

    /// Get the amount of this transfer
    pub fn amount(&self) -> Money {
        self.debit_proof.amount()
    }

    /// Get the recipient of this transfer
    pub fn from(&self) -> PublicKey {
        self.debit_proof.from()
    }

    /// Get the recipient of this transfer
    pub fn to(&self) -> PublicKey {
        self.debit_proof.to()
    }
}

/// Public Key Set for a group of transfer replicas.
pub type ReplicaPublicKeySet = PublicKeySet;
/// The Replica event raised when
/// we learn of a new group PK set.
#[derive(Clone, Hash, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Debug)]
pub struct KnownGroupAdded {
    /// The PublicKeySet of the group.
    pub group: PublicKeySet,
}

// /// (Draft) An Actor cmd to roll back a failed transfer.
// #[derive(Clone, Hash, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize, Debug)]
// pub struct CancelTransfer {
//     /// The transfer id.
//     pub transfer_id: TransferId,
//     /// Actor signature over the transfer id.
//     pub actor_signature: Signature,
// }

/// Notification of a Transfer sent to a recipient.
#[derive(Hash, Eq, PartialEq, PartialOrd, Clone, Serialize, Deserialize, Debug)]
pub struct TransferNotification(pub DebitAgreementProof);