Skip to content

Commit

Permalink
[fastpay] remove confusing option --initial-balance
Browse files Browse the repository at this point in the history
  • Loading branch information
ma2bd committed Sep 13, 2021
1 parent be773a9 commit ebff470
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ FastPay allows a set of distributed authorities, some of which are Byzantine, to
```bash
cargo build --release
cd target/release
rm -f *.json
rm -f *.json *.txt

# Create configuration files for 4 authorities with 4 shards each.
# * Private server states are stored in `server*.json`.
Expand All @@ -26,21 +26,21 @@ done

# Create configuration files for 1000 user accounts.
# * Private account states are stored in one local wallet `accounts.json`.
# * `initial_accounts.json` is used to mint initial balances on the server side.
./client --committee committee.json --accounts accounts.json create_accounts 1000 --initial-funding 100 >> initial_accounts.json
# * `initial_accounts.txt` is used to mint the corresponding initial balances at startup on the server side.
./client --committee committee.json --accounts accounts.json create_accounts 1000 --initial-funding 100 >> initial_accounts.txt

# Start servers
for I in 1 2 3 4
do
for J in $(seq 0 3)
do
./server --server server"$I".json run --shard "$J" --initial-accounts initial_accounts.json --initial-balance 100 --committee committee.json &
./server --server server"$I".json run --shard "$J" --initial-accounts initial_accounts.txt --committee committee.json &
done
done

# Query (locally cached) balance for first and last user account
ACCOUNT1="`head -n 1 initial_accounts.json`"
ACCOUNT2="`tail -n -1 initial_accounts.json`"
ACCOUNT1="`head -n 1 initial_accounts.txt | awk -F: '{ print $1 }'`"
ACCOUNT2="`tail -n -1 initial_accounts.txt | awk -F: '{ print $1 }'`"
./client --committee committee.json --accounts accounts.json query_balance "$ACCOUNT1"
./client --committee committee.json --accounts accounts.json query_balance "$ACCOUNT2"

Expand Down
2 changes: 1 addition & 1 deletion fastpay/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ fn main() {
let num_accounts: u32 = num;
for _ in 0..num_accounts {
let account = UserAccount::new(initial_funding);
println!("{}", encode_address(&account.address));
println!("{}:{}", encode_address(&account.address), initial_funding);
accounts_config.insert(account);
}
accounts_config
Expand Down
20 changes: 13 additions & 7 deletions fastpay/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,26 +193,32 @@ impl AccountsConfig {
}

pub struct InitialStateConfig {
pub addresses: Vec<FastPayAddress>,
pub accounts: Vec<(FastPayAddress, Balance)>,
}

impl InitialStateConfig {
pub fn read(path: &str) -> Result<Self, failure::Error> {
let file = File::open(path)?;
let reader = BufReader::new(file);
let mut addresses = Vec::new();
let mut accounts = Vec::new();
for line in reader.lines() {
addresses.push(decode_address(&line?)?);
let line = line?;
let elements = line.split(':').collect::<Vec<_>>();
if elements.len() != 2 {
failure::bail!("expecting two columns separated with ':'")
}
let address = decode_address(elements[0])?;
let balance = elements[1].parse()?;
accounts.push((address, balance));
}
Ok(Self { addresses })
Ok(Self { accounts })
}

pub fn write(&self, path: &str) -> Result<(), std::io::Error> {
let file = OpenOptions::new().create(true).write(true).open(path)?;
let mut writer = BufWriter::new(file);
for address in &self.addresses {
writer.write_all(encode_address(address).as_ref())?;
writer.write_all(b"\n")?;
for (address, balance) in &self.accounts {
writeln!(writer, "{}:{}", encode_address(address), balance)?;
}
Ok(())
}
Expand Down
14 changes: 2 additions & 12 deletions fastpay/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ fn make_shard_server(
server_config_path: &str,
committee_config_path: &str,
initial_accounts_config_path: &str,
initial_balance: Balance,
buffer_size: usize,
cross_shard_queue_size: usize,
shard: u32,
Expand All @@ -41,12 +40,12 @@ fn make_shard_server(
);

// Load initial states
for address in &initial_accounts_config.addresses {
for (address, balance) in &initial_accounts_config.accounts {
if AuthorityState::get_shard(num_shards, address) != shard {
continue;
}
let client = AccountOffchainState {
balance: initial_balance,
balance: *balance,
next_sequence_number: SequenceNumber::from(0),
pending_confirmation: None,
confirmed_log: Vec::new(),
Expand All @@ -71,7 +70,6 @@ fn make_servers(
server_config_path: &str,
committee_config_path: &str,
initial_accounts_config_path: &str,
initial_balance: Balance,
buffer_size: usize,
cross_shard_queue_size: usize,
) -> Vec<network::Server> {
Expand All @@ -86,7 +84,6 @@ fn make_servers(
server_config_path,
committee_config_path,
initial_accounts_config_path,
initial_balance,
buffer_size,
cross_shard_queue_size,
shard,
Expand Down Expand Up @@ -131,10 +128,6 @@ enum ServerCommands {
#[structopt(long)]
initial_accounts: String,

/// Path to the file describing the initial balance of user accounts
#[structopt(long)]
initial_balance: Balance,

/// Runs a specific shard (from 0 to shards-1)
#[structopt(long)]
shard: Option<u32>,
Expand Down Expand Up @@ -173,7 +166,6 @@ fn main() {
cross_shard_queue_size,
committee,
initial_accounts,
initial_balance,
shard,
} => {
// Run the server
Expand All @@ -185,7 +177,6 @@ fn main() {
server_config_path,
&committee,
&initial_accounts,
initial_balance,
buffer_size,
cross_shard_queue_size,
shard,
Expand All @@ -199,7 +190,6 @@ fn main() {
server_config_path,
&committee,
&initial_accounts,
initial_balance,
buffer_size,
cross_shard_queue_size,
)
Expand Down
6 changes: 6 additions & 0 deletions fastpay_core/src/base_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ impl Balance {
}
}

impl std::fmt::Display for Balance {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

impl std::str::FromStr for Balance {
type Err = std::num::ParseIntError;

Expand Down

0 comments on commit ebff470

Please sign in to comment.