Make client-demo standalone

And remove deposit() methods from the API. Those should only be
used on the server to bootstrap.
This commit is contained in:
Greg Fitzgerald 2018-03-03 21:15:42 -07:00
parent 7cf0d55546
commit b8655e30d4
2 changed files with 24 additions and 41 deletions

View File

@ -22,24 +22,6 @@ impl AccountantStub {
}
}
pub fn deposit_signed(
self: &Self,
key: PublicKey,
val: u64,
sig: Signature,
) -> io::Result<usize> {
let req = Request::Deposit { key, val, sig };
let data = serialize(&req).unwrap();
self.socket.send_to(&data, &self.addr)
}
pub fn deposit(self: &Self, n: u64, keypair: &Ed25519KeyPair) -> io::Result<Signature> {
use event::{get_pubkey, sign_claim_data};
let key = get_pubkey(keypair);
let sig = sign_claim_data(&n, keypair);
self.deposit_signed(key, n, sig).map(|_| sig)
}
pub fn transfer_signed(
self: &Self,
from: PublicKey,
@ -107,23 +89,18 @@ mod tests {
fn test_accountant_stub() {
let addr = "127.0.0.1:9000";
let send_addr = "127.0.0.1:9001";
spawn(move || {
let zero = Sha256Hash::default();
let acc = Accountant::new(&zero, None);
let mut skel = AccountantSkel::new(acc);
skel.serve(addr).unwrap();
});
let zero = Sha256Hash::default();
let alice_keypair = generate_keypair();
let bob_keypair = generate_keypair();
let mut acc = Accountant::new(&zero, None);
acc.deposit(10_000, &alice_keypair).unwrap();
let sig = acc.deposit(1_000, &bob_keypair).unwrap();
acc.wait_on_signature(&sig);
spawn(move || AccountantSkel::new(acc).serve(addr).unwrap());
sleep(Duration::from_millis(30));
let socket = UdpSocket::bind(send_addr).unwrap();
let acc = AccountantStub::new(addr, socket);
let alice_keypair = generate_keypair();
let bob_keypair = generate_keypair();
acc.deposit(10_000, &alice_keypair).unwrap();
let sig = acc.deposit(1_000, &bob_keypair).unwrap();
acc.wait_on_signature(&sig).unwrap();
let bob_pubkey = get_pubkey(&bob_keypair);
let sig = acc.transfer(500, &alice_keypair, bob_pubkey).unwrap();
acc.wait_on_signature(&sig).unwrap();

View File

@ -2,23 +2,29 @@ extern crate silk;
fn main() {
use silk::accountant_stub::AccountantStub;
use silk::accountant_skel::AccountantSkel;
use silk::accountant::Accountant;
use silk::event::{generate_keypair, get_pubkey, sign_transaction_data};
use std::time::Instant;
use std::net::UdpSocket;
use silk::event::{generate_keypair, get_pubkey, sign_transaction_data};
use std::thread::{sleep, spawn};
use std::time::Duration;
let addr = "127.0.0.1:8000";
let send_addr = "127.0.0.1:8001";
let zero = Default::default();
let alice_keypair = generate_keypair();
let mut acc = Accountant::new(&zero, None);
let txs = 200;
let sig = acc.deposit(txs, &alice_keypair).unwrap();
acc.wait_on_signature(&sig);
spawn(move || AccountantSkel::new(acc).serve(addr).unwrap());
sleep(Duration::from_millis(30));
let socket = UdpSocket::bind(send_addr).unwrap();
let acc = AccountantStub::new(addr, socket);
let alice_keypair = generate_keypair();
let alice_pubkey = get_pubkey(&alice_keypair);
let txs = 2_000;
println!("Depositing {} units in Alice's account...", txs);
let sig = acc.deposit(txs, &alice_keypair).unwrap();
acc.wait_on_signature(&sig).unwrap();
assert_eq!(acc.get_balance(&alice_pubkey).unwrap(), txs);
println!("Done.");
let one = 1;
println!("Signing transactions...");
let now = Instant::now();
@ -64,7 +70,7 @@ fn main() {
println!("Transferring 1 unit {} times...", txs);
let now = Instant::now();
let mut sig = sig;
let mut sig = Default::default();
for (k, s) in sigs {
acc.transfer_signed(alice_pubkey, k, one, s).unwrap();
sig = s;