Pipe all Claim constructors through a function
This commit is contained in:
parent
8fe464cfa3
commit
a0410c4677
|
@ -49,7 +49,7 @@ impl Accountant {
|
|||
}
|
||||
|
||||
pub fn deposit_signed(self: &mut Self, to: PublicKey, data: u64, sig: Signature) -> Result<()> {
|
||||
let event = Event::Claim { to, data, sig };
|
||||
let event = Event::new_claim(to, data, sig);
|
||||
if !self.historian.verify_event(&event) {
|
||||
return Err(AccountingError::InvalidEvent);
|
||||
}
|
||||
|
|
|
@ -11,11 +11,7 @@ fn create_log(hist: &Historian<Sha256Hash>) -> Result<(), SendError<Event<Sha256
|
|||
sleep(Duration::from_millis(15));
|
||||
let data = Sha256Hash::default();
|
||||
let keypair = generate_keypair();
|
||||
let event0 = Event::Claim {
|
||||
to: get_pubkey(&keypair),
|
||||
data,
|
||||
sig: sign_serialized(&data, &keypair),
|
||||
};
|
||||
let event0 = Event::new_claim(get_pubkey(&keypair), data, sign_serialized(&data, &keypair));
|
||||
hist.sender.send(event0)?;
|
||||
sleep(Duration::from_millis(10));
|
||||
Ok(())
|
||||
|
|
|
@ -42,6 +42,12 @@ pub enum Event<T> {
|
|||
},
|
||||
}
|
||||
|
||||
impl<T> Event<T> {
|
||||
pub fn new_claim(to: PublicKey, data: T, sig: Signature) -> Self {
|
||||
Event::Claim { to, data, sig }
|
||||
}
|
||||
}
|
||||
|
||||
/// Return a new ED25519 keypair
|
||||
pub fn generate_keypair() -> Ed25519KeyPair {
|
||||
use ring::{rand, signature};
|
||||
|
|
|
@ -215,11 +215,7 @@ mod tests {
|
|||
fn test_bad_event_signature() {
|
||||
let keypair = generate_keypair();
|
||||
let sig = sign_serialized(&hash(b"hello, world"), &keypair);
|
||||
let event0 = Event::Claim {
|
||||
to: get_pubkey(&keypair),
|
||||
data: hash(b"goodbye cruel world"),
|
||||
sig,
|
||||
};
|
||||
let event0 = Event::new_claim(get_pubkey(&keypair), hash(b"goodbye cruel world"), sig);
|
||||
let mut sigs = HashSet::new();
|
||||
assert!(!verify_event_and_reserve_signature(&mut sigs, &event0));
|
||||
assert!(!sigs.contains(&sig));
|
||||
|
@ -231,7 +227,7 @@ mod tests {
|
|||
let to = get_pubkey(&keypair);
|
||||
let data = &hash(b"hello, world");
|
||||
let sig = sign_serialized(data, &keypair);
|
||||
let event0 = Event::Claim { to, data, sig };
|
||||
let event0 = Event::new_claim(to, data, sig);
|
||||
let mut sigs = HashSet::new();
|
||||
assert!(verify_event_and_reserve_signature(&mut sigs, &event0));
|
||||
assert!(!verify_event_and_reserve_signature(&mut sigs, &event0));
|
||||
|
|
33
src/log.rs
33
src/log.rs
|
@ -209,24 +209,15 @@ mod tests {
|
|||
let zero = Sha256Hash::default();
|
||||
let one = hash(&zero);
|
||||
|
||||
// First, verify Claim events
|
||||
// First, verify entries
|
||||
let keypair = generate_keypair();
|
||||
let event0 = Event::Claim {
|
||||
to: get_pubkey(&keypair),
|
||||
data: zero,
|
||||
sig: sign_serialized(&zero, &keypair),
|
||||
};
|
||||
|
||||
let event1 = Event::Claim {
|
||||
to: get_pubkey(&keypair),
|
||||
data: one,
|
||||
sig: sign_serialized(&one, &keypair),
|
||||
};
|
||||
let event0 = Event::new_claim(get_pubkey(&keypair), zero, sign_serialized(&zero, &keypair));
|
||||
let event1 = Event::new_claim(get_pubkey(&keypair), one, sign_serialized(&one, &keypair));
|
||||
let events = vec![event0, event1];
|
||||
let mut entries = create_entries(&zero, 0, events);
|
||||
assert!(verify_slice(&entries, &zero));
|
||||
|
||||
// Next, swap two Claim events and ensure verification fails.
|
||||
// Next, swap two events and ensure verification fails.
|
||||
let event0 = entries[0].event.clone();
|
||||
let event1 = entries[1].event.clone();
|
||||
entries[0].event = event1;
|
||||
|
@ -238,11 +229,7 @@ mod tests {
|
|||
fn test_claim() {
|
||||
let keypair = generate_keypair();
|
||||
let data = hash(b"hello, world");
|
||||
let event0 = Event::Claim {
|
||||
to: get_pubkey(&keypair),
|
||||
data,
|
||||
sig: sign_serialized(&data, &keypair),
|
||||
};
|
||||
let event0 = Event::new_claim(get_pubkey(&keypair), data, sign_serialized(&data, &keypair));
|
||||
let zero = Sha256Hash::default();
|
||||
let entries = create_entries(&zero, 0, vec![event0]);
|
||||
assert!(verify_slice(&entries, &zero));
|
||||
|
@ -251,11 +238,11 @@ mod tests {
|
|||
#[test]
|
||||
fn test_wrong_data_claim_attack() {
|
||||
let keypair = generate_keypair();
|
||||
let event0 = Event::Claim {
|
||||
to: get_pubkey(&keypair),
|
||||
data: hash(b"goodbye cruel world"),
|
||||
sig: sign_serialized(&hash(b"hello, world"), &keypair),
|
||||
};
|
||||
let event0 = Event::new_claim(
|
||||
get_pubkey(&keypair),
|
||||
hash(b"goodbye cruel world"),
|
||||
sign_serialized(&hash(b"hello, world"), &keypair),
|
||||
);
|
||||
let zero = Sha256Hash::default();
|
||||
let entries = create_entries(&zero, 0, vec![event0]);
|
||||
assert!(!verify_slice(&entries, &zero));
|
||||
|
|
Loading…
Reference in New Issue