correct rpc save_stakes and add save_votes

This commit is contained in:
musitdev 2023-09-29 12:03:17 +02:00
parent 05e6dcd318
commit 47ccd6c4d2
3 changed files with 46 additions and 29 deletions

View File

@ -18,7 +18,7 @@ curl http://localhost:3000 -X POST -H "Content-Type: application/json" -d '
"method": "stake_accounts",
"params": []
}
' -o extract_stake_529_end.json
' -o extract_stake_533_3.json
curl http://localhost:3000 -X POST -H "Content-Type: application/json" -d '

View File

@ -244,6 +244,34 @@ pub(crate) async fn run_server(request_tx: Sender<Requests>) -> Result<ServerHan
.unwrap_or_else(|err| serde_json::Value::String(err.to_string()))
})?;
module.register_async_method("stake_accounts", |_params, request_tx| async move {
log::trace!("RPC bootstrap_accounts");
let (tx, rx) = oneshot::channel();
if let Err(err) = request_tx.send(Requests::GetStakestore(tx)).await {
return serde_json::Value::String(format!("error during query processing:{err}",));
}
rx.await
.map_err(|err| format!("error during bootstrap query processing:{err}"))
.and_then(|(accounts, slot)| {
println!("RPC end request status");
if slot == 0 {
Err("Error stake store extracted".to_string())
} else {
//replace pubkey with String. Json only allow distionary key with string.
let ret: HashMap<String, StoredStake> = accounts
.into_iter()
.map(|(pk, acc)| (pk.to_string(), acc))
.collect();
serde_json::to_value((slot, ret)).map_err(|err| {
format!(
"error during json serialisation:{}",
JsonRpcError::ParseError(err)
)
})
}
})
.unwrap_or_else(|err| serde_json::Value::String(err.to_string()))
})?;
module.register_async_method("vote_accounts", |_params, request_tx| async move {
log::trace!("RPC bootstrap_accounts");
let (tx, rx) = oneshot::channel();
if let Err(err) = request_tx.send(Requests::GetVotestore(tx)).await {

View File

@ -51,28 +51,17 @@ fn stake_map_notify_stake(map: &mut StakeMap, stake: StoredStake, current_epoch:
//doesn't erase new state with an old one. Can arrive during bootstrapping.
//several instructions can be done in the same slot.
if strstake.last_update_slot <= stake.last_update_slot {
if stake.is_removed(current_epoch) {
log::info!(
"stake_map_notify_stake Stake store remove stake: {} stake:{stake:?} current_epoch:{current_epoch}",
stake.pubkey
);
//TODO activate when remove algo is validated.
//map.remove(&stake.pubkey);
} else {
log::info!("stake_map_notify_stake Stake store updated stake: {} old_stake:{strstake:?} stake:{stake:?}", stake.pubkey);
*strstake = stake;
}
log::info!("stake_map_notify_stake Stake store updated stake: {} old_stake:{strstake:?} stake:{stake:?}", stake.pubkey);
*strstake = stake;
}
}
// If value doesn't exist yet, then insert a new value of 1
std::collections::hash_map::Entry::Vacant(vacant) => {
if stake.is_inserted(current_epoch) {
log::info!(
"stake_map_notify_stake Stake store insert stake: {} stake:{stake:?}",
stake.pubkey
);
vacant.insert(stake);
}
log::info!(
"stake_map_notify_stake Stake store insert stake: {} stake:{stake:?}",
stake.pubkey
);
vacant.insert(stake);
}
};
}
@ -119,16 +108,16 @@ pub struct StoredStake {
pub write_version: u64,
}
impl StoredStake {
fn is_removed(&self, current_epoch: u64) -> bool {
self.stake.activation_epoch != crate::leader_schedule::MAX_EPOCH_VALUE
&& self.stake.deactivation_epoch < current_epoch
}
fn is_inserted(&self, current_epoch: u64) -> bool {
self.stake.activation_epoch == crate::leader_schedule::MAX_EPOCH_VALUE
|| self.stake.deactivation_epoch >= current_epoch
}
}
// impl StoredStake {
// fn is_removed(&self, current_epoch: u64) -> bool {
// self.stake.activation_epoch != crate::leader_schedule::MAX_EPOCH_VALUE
// && self.stake.deactivation_epoch < current_epoch
// }
// fn is_inserted(&self, current_epoch: u64) -> bool {
// self.stake.activation_epoch == crate::leader_schedule::MAX_EPOCH_VALUE
// || self.stake.deactivation_epoch >= current_epoch
// }
// }
#[derive(Debug, Default)]
pub struct StakeStore {
stakes: StakeMap,