Skip to content

Commit

Permalink
Fix a bug in logs and allow multiple accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
sorpaas committed Sep 1, 2017
1 parent f19a826 commit b16993d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 30 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ fn main() {
(@arg PRIVATE_KEY: -p --private-key +takes_value "Private key for the account to be generated, if not provided, a random private key will be generated.")
(@arg BALANCE: -b --balance +takes_value "Balance in Wei for the account to be generated, default is 0x10000000000000000000000000000.")
(@arg LISTEN: -l --listen +takes_value "Listen address and port for the RPC, e.g. 127.0.0.1:8545")
(@arg ACCOUNTS: -a --accounts +takes_value "Additional accounts to be generated, default to 9")
).get_matches();

let secret_key = match matches.value_of("PRIVATE_KEY") {
Expand All @@ -66,11 +67,22 @@ fn main() {
U256::from_dec_str(s).unwrap()
}
};
let accounts_len: usize = match matches.value_of("ACCOUNTS") {
Some(val) => val.parse().unwrap(),
None => 9,
};

let mut genesis = Vec::new();
genesis.push((secret_key, balance));

for _ in 0..accounts_len {
genesis.push((SecretKey::new(&SECP256K1, &mut rng), balance));
}

let (sender, receiver) = channel::<bool>();

thread::spawn(move || {
miner::mine_loop(secret_key, balance, receiver);
miner::mine_loop(genesis, receiver);
});

rpc::rpc_loop(&matches.value_of("LISTEN").unwrap_or("127.0.0.1:8545").parse().unwrap(),
Expand Down
41 changes: 23 additions & 18 deletions src/miner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,13 @@ fn current_timestamp() -> u64 {
SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()
}

pub fn mine_loop(secret_key: SecretKey, balance: U256, channel: Receiver<bool>) {
pub fn mine_loop(genesis_accounts: Vec<(SecretKey, U256)>, channel: Receiver<bool>) {
let patch = &vm::EIP160_PATCH;

let address = Address::from_secret_key(&secret_key).unwrap();
println!("address: {:?}", address);
println!("private key: {}", to_hex(&secret_key[..]));

state::append_account(secret_key);

{
let mut stateful = state::stateful();

let genesis = Block {
let mut genesis = Block {
header: Header {
parent_hash: H256::default(),
// TODO: use the known good result from etclient
Expand All @@ -123,21 +117,31 @@ pub fn mine_loop(secret_key: SecretKey, balance: U256, channel: Receiver<bool>)
ommers: Vec::new(),
};

let _: SeqTransactionVM = stateful.execute(ValidTransaction {
caller: None,
gas_price: Gas::zero(),
gas_limit: Gas::from(100000usize),
action: TransactionAction::Call(address),
value: balance,
input: Vec::new(),
nonce: U256::zero(),
}, HeaderParams::from(&genesis.header), patch, &[]);
for (secret_key, balance) in genesis_accounts {
let address = Address::from_secret_key(&secret_key).unwrap();

let vm: SeqTransactionVM = stateful.execute(ValidTransaction {
caller: None,
gas_price: Gas::zero(),
gas_limit: Gas::from(100000usize),
action: TransactionAction::Call(address),
value: balance,
input: Vec::new(),
nonce: U256::zero(),
}, HeaderParams::from(&genesis.header), patch, &[]);

println!("address: {:?}", address);
println!("private key: {}", to_hex(&secret_key[..]));

state::append_account(secret_key);
}

genesis.header.state_root = stateful.root();
state::append_block(genesis);
}

loop {
mine_one(address, patch);
mine_one(Address::default(), patch);

channel.recv_timeout(Duration::new(10, 0));
}
Expand Down Expand Up @@ -182,5 +186,6 @@ pub fn mine_one(address: Address, patch: &'static Patch) {
let next_block = next(&current_block, transactions.as_ref(), receipts.as_ref(),
beneficiary, Gas::from_str("0x10000000000000000000000").unwrap(),
stateful.root());
debug!("block number: 0x{:x}", next_block.header.number);
state::append_block(next_block);
}
12 changes: 6 additions & 6 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod util;
use error::Error;
use super::miner;

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum Either<T, U> {
Left(T),
Expand All @@ -30,7 +30,7 @@ pub enum RPCTopicFilter {
Or(Vec<String>)
}

#[derive(Serialize, Deserialize, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RPCLogFilter {
pub from_block: Option<String>,
Expand All @@ -39,7 +39,7 @@ pub struct RPCLogFilter {
pub topics: Option<Vec<Option<RPCTopicFilter>>>,
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RPCLog {
pub removed: bool,
Expand All @@ -52,7 +52,7 @@ pub struct RPCLog {
pub topics: Vec<String>,
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RPCReceipt {
pub transaction_hash: String,
Expand All @@ -65,7 +65,7 @@ pub struct RPCReceipt {
pub logs: Vec<RPCLog>
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RPCBlock {
pub number: String,
Expand All @@ -89,7 +89,7 @@ pub struct RPCBlock {
pub uncles: Vec<String>,
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RPCTransaction {
pub from: String,
Expand Down
6 changes: 4 additions & 2 deletions src/rpc/serves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,9 @@ impl EthereumRPC for MinerEthereumRPC {
}

fn logs(&self, log: RPCLogFilter) -> Result<Vec<RPCLog>, Error> {
let filter = from_log_filter(log)?;
Ok(get_logs(filter)?)
match from_log_filter(log) {
Ok(filter) => Ok(get_logs(filter)?),
Err(_) => Ok(Vec::new()),
}
}
}

0 comments on commit b16993d

Please sign in to comment.