Skip to content

Commit

Permalink
A bunch of fixes for the RPC (#874)
Browse files Browse the repository at this point in the history
* initialization phase gucci

* fix casing

* fix mpool push, add mpool select message api

* add messagesendspec struct to correctly specify mpool_push_message's params

* fix keystore handling in rpc

* fix some mpool_push_message stuff. needs more

* stub estimate gas message

* refactor gas api

* fixed msg pool push

* lint

* remove println

* remove todo

* fix warnings

* some todos and unncesseary renames
  • Loading branch information
ec2 authored Dec 1, 2020
1 parent 8193a4b commit ce5c28b
Show file tree
Hide file tree
Showing 14 changed files with 390 additions and 87 deletions.
6 changes: 5 additions & 1 deletion blockchain/chain/src/store/chain_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ where
genesis(self.blockstore())
}

/// Returns heaviest tipset from blockstore
/// Returns the currently tracked heaviest tipset
pub async fn heaviest_tipset(&self) -> Option<Arc<Tipset>> {
// TODO: Figure out how to remove optional and return something everytime.
self.heaviest.read().await.clone()
}

Expand All @@ -201,6 +202,9 @@ where

/// Returns Tipset from key-value store from provided cids
pub async fn tipset_from_keys(&self, tsk: &TipsetKeys) -> Result<Arc<Tipset>, Error> {
if tsk.cids().is_empty() {
return Ok(self.heaviest_tipset().await.unwrap());
}
tipset_from_keys(&self.ts_cache, self.blockstore(), tsk).await
}

Expand Down
20 changes: 10 additions & 10 deletions blockchain/message_pool/src/msgpool/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ where
{
/// Selects messages for including in a block.
pub async fn select_messages(
&mut self,
ts: Tipset,
&self,
ts: &Tipset,
_tq: f64,
) -> Result<Vec<SignedMessage>, Error> {
let cur_ts = self.cur_tipset.read().await.clone();
Expand All @@ -29,9 +29,9 @@ where
}

async fn select_messages_greedy(
&mut self,
&self,
cur_ts: &Tipset,
ts: Tipset,
ts: &Tipset,
) -> Result<Vec<SignedMessage>, Error> {
let base_fee = self.api.read().await.chain_compute_base_fee(&ts)?;

Expand Down Expand Up @@ -224,7 +224,7 @@ mod test_selection {

#[async_std::test]
async fn basic_message_selection() {
let mut mpool = make_test_mpool();
let mpool = make_test_mpool();

let mut w1 = Wallet::new(MemKeyStore::new());
let a1 = w1.generate_addr(SignatureType::Secp256k1).unwrap();
Expand Down Expand Up @@ -272,7 +272,7 @@ mod test_selection {
mpool.add(m).await.unwrap();
}

let msgs = mpool.select_messages(ts, 1.0).await.unwrap();
let msgs = mpool.select_messages(&ts, 1.0).await.unwrap();
assert_eq!(msgs.len(), 20);
let mut next_nonce = 0;
for i in 0..10 {
Expand Down Expand Up @@ -343,7 +343,7 @@ mod test_selection {
// first we need to update the nonce on the api
mpool.api.write().await.set_state_sequence(&a1, 10);
mpool.api.write().await.set_state_sequence(&a2, 10);
let msgs = mpool.select_messages(ts3, 1.0).await.unwrap();
let msgs = mpool.select_messages(&ts3, 1.0).await.unwrap();

assert_eq!(msgs.len(), 20);

Expand Down Expand Up @@ -371,7 +371,7 @@ mod test_selection {

#[async_std::test]
async fn message_selection_trimming() {
let mut mpool = make_test_mpool();
let mpool = make_test_mpool();

let mut w1 = Wallet::new(MemKeyStore::new());
let a1 = w1.generate_addr(SignatureType::Secp256k1).unwrap();
Expand Down Expand Up @@ -433,7 +433,7 @@ mod test_selection {
mpool.add(m).await.unwrap();
}

let msgs = mpool.select_messages(ts, 1.0).await.unwrap();
let msgs = mpool.select_messages(&ts, 1.0).await.unwrap();

let expected = types::BLOCK_GAS_LIMIT / gas_limit;
assert_eq!(msgs.len(), expected as usize);
Expand Down Expand Up @@ -515,7 +515,7 @@ mod test_selection {
mpool.add(m).await.unwrap();
}

let msgs = mpool.select_messages(ts, 1.0).await.unwrap();
let msgs = mpool.select_messages(&ts, 1.0).await.unwrap();

assert_eq!(msgs.len(), 20);

Expand Down
4 changes: 1 addition & 3 deletions forest/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,7 @@ pub(super) async fn start(config: Config) {
network_send,
network_name,
events_pubsub: Arc::new(RwLock::new(Publisher::new(1000))),
beacon: Schedule {
0: vec![BeaconPoint { start: 0, beacon }],
},
beacon: Schedule(vec![BeaconPoint { start: 0, beacon }]),
chain_store,
},
&rpc_listen,
Expand Down
119 changes: 106 additions & 13 deletions node/rpc/src/gas_api.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use super::mpool_api::MessageSendSpec;
use crate::RpcState;
use address::Address;
use address::{json::AddressJson, Address};
use beacon::Beacon;
use blocks::TipsetKeys;
use blocks::{tipset_keys_json::TipsetKeysJson, TipsetKeys};
use blockstore::BlockStore;
use chain::{BASE_FEE_MAX_CHANGE_DENOM, BLOCK_GAS_TARGET, MINIMUM_BASE_FEE};
use fil_types::{verifier::ProofVerifier, BLOCK_GAS_LIMIT};
use jsonrpc_v2::{Data, Error as JsonRpcError, Params};
use message::unsigned_message::json::UnsignedMessageJson;
use message::{unsigned_message::json::UnsignedMessageJson, UnsignedMessage};
use message::{ChainMessage, Message};
use num_bigint::BigInt;
use num_traits::{FromPrimitive, Zero};
Expand All @@ -21,15 +22,31 @@ const MAX_SPEND_ON_FEE_DENOM: i64 = 100;
/// Estimate the fee cap
pub(crate) async fn gas_estimate_fee_cap<DB, KS, B>(
data: Data<RpcState<DB, KS, B>>,
Params(params): Params<(UnsignedMessageJson, i64, TipsetKeys)>,
Params(params): Params<(UnsignedMessageJson, i64, TipsetKeysJson)>,
) -> Result<String, JsonRpcError>
where
DB: BlockStore + Send + Sync + 'static,
KS: KeyStore + Send + Sync + 'static,
B: Beacon + Send + Sync + 'static,
{
let (UnsignedMessageJson(msg), max_queue_blks, _tsk) = params;
let (UnsignedMessageJson(msg), max_queue_blks, TipsetKeysJson(tsk)) = params;

estimate_fee_cap::<DB, KS, B>(&data, msg, max_queue_blks, tsk)
.await
.map(|n| BigInt::to_string(&n))
}

async fn estimate_fee_cap<DB, KS, B>(
data: &Data<RpcState<DB, KS, B>>,
msg: UnsignedMessage,
max_queue_blks: i64,
_tsk: TipsetKeys,
) -> Result<BigInt, JsonRpcError>
where
DB: BlockStore + Send + Sync + 'static,
KS: KeyStore + Send + Sync + 'static,
B: Beacon + Send + Sync + 'static,
{
let ts = data
.state_manager
.chain_store()
Expand Down Expand Up @@ -60,21 +77,36 @@ where
} else {
fee_in_future
};
Ok(out.to_string())
Ok(out)
}

/// Estimate the fee cap
pub(crate) async fn gas_estimate_gas_premium<DB, KS, B>(
data: Data<RpcState<DB, KS, B>>,
Params(params): Params<(u64, Address, i64, TipsetKeys)>,
Params(params): Params<(u64, AddressJson, i64, TipsetKeysJson)>,
) -> Result<String, JsonRpcError>
where
DB: BlockStore + Send + Sync + 'static,
KS: KeyStore + Send + Sync + 'static,
B: Beacon + Send + Sync + 'static,
{
let (mut nblocksincl, _sender, _gas_limit, _) = params;

let (nblocksincl, AddressJson(sender), gas_limit, TipsetKeysJson(tsk)) = params;
estimate_gas_premium::<DB, KS, B>(&data, nblocksincl, sender, gas_limit, tsk)
.await
.map(|n| BigInt::to_string(&n))
}
async fn estimate_gas_premium<DB, KS, B>(
data: &Data<RpcState<DB, KS, B>>,
mut nblocksincl: u64,
_sender: Address,
_gas_limit: i64,
_tsk: TipsetKeys,
) -> Result<BigInt, JsonRpcError>
where
DB: BlockStore + Send + Sync + 'static,
KS: KeyStore + Send + Sync + 'static,
B: Beacon + Send + Sync + 'static,
{
if nblocksincl == 0 {
nblocksincl = 1;
}
Expand Down Expand Up @@ -132,7 +164,7 @@ where
}
if prev == 0.into() {
let ret: BigInt = price.price + 1;
return Ok(ret.to_string());
return Ok(ret);
}
premium = (&price.price + &prev) / 2 + 1
}
Expand All @@ -156,21 +188,36 @@ where
.ok_or("failed to converrt gas premium f64 to bigint")?;
premium /= 1 << precision;

Ok(premium.to_string())
Ok(premium)
}

/// Estimate the gas limit
pub(crate) async fn gas_estimate_gas_limit<DB, KS, B, V>(
data: Data<RpcState<DB, KS, B>>,
Params(params): Params<(UnsignedMessageJson, TipsetKeys)>,
Params(params): Params<(UnsignedMessageJson, TipsetKeysJson)>,
) -> Result<i64, JsonRpcError>
where
DB: BlockStore + Send + Sync + 'static,
KS: KeyStore + Send + Sync + 'static,
B: Beacon + Send + Sync + 'static,
V: ProofVerifier + Send + Sync + 'static,
{
let (UnsignedMessageJson(msg), TipsetKeysJson(tsk)) = params;
estimate_gas_limit::<DB, KS, B, V>(&data, msg, tsk).await
}

async fn estimate_gas_limit<DB, KS, B, V>(
data: &Data<RpcState<DB, KS, B>>,
msg: UnsignedMessage,
_: TipsetKeys,
) -> Result<i64, JsonRpcError>
where
DB: BlockStore + Send + Sync + 'static,
KS: KeyStore + Send + Sync + 'static,
B: Beacon + Send + Sync + 'static,
V: ProofVerifier + Send + Sync + 'static,
{
let (UnsignedMessageJson(mut msg), _) = params;
let mut msg = msg;
msg.set_gas_limit(BLOCK_GAS_LIMIT);
msg.set_gas_fee_cap(MINIMUM_BASE_FEE.clone() + 1);
msg.set_gas_premium(1.into());
Expand Down Expand Up @@ -208,3 +255,49 @@ where
None => Ok(-1),
}
}

/// Estimates the gas paramaters for a given message
pub(crate) async fn gas_estimate_message_gas<DB, KS, B, V>(
data: Data<RpcState<DB, KS, B>>,
Params(params): Params<(UnsignedMessageJson, Option<MessageSendSpec>, TipsetKeysJson)>,
) -> Result<UnsignedMessageJson, JsonRpcError>
where
DB: BlockStore + Send + Sync + 'static,
KS: KeyStore + Send + Sync + 'static,
B: Beacon + Send + Sync + 'static,
V: ProofVerifier + Send + Sync + 'static,
{
let (UnsignedMessageJson(msg), spec, TipsetKeysJson(tsk)) = params;
estimate_message_gas::<DB, KS, B, V>(&data, msg, spec, tsk)
.await
.map(UnsignedMessageJson::from)
}

pub(crate) async fn estimate_message_gas<DB, KS, B, V>(
data: &Data<RpcState<DB, KS, B>>,
msg: UnsignedMessage,
_spec: Option<MessageSendSpec>,
tsk: TipsetKeys,
) -> Result<UnsignedMessage, JsonRpcError>
where
DB: BlockStore + Send + Sync + 'static,
KS: KeyStore + Send + Sync + 'static,
B: Beacon + Send + Sync + 'static,
V: ProofVerifier + Send + Sync + 'static,
{
let mut msg = msg;
if msg.gas_limit() == 0 {
let gl = estimate_gas_limit::<DB, KS, B, V>(&data, msg.clone(), tsk.clone()).await?;
msg.gas_limit = gl;
}
if msg.gas_premium().is_zero() {
let gp = estimate_gas_premium(&data, 10, *msg.from(), msg.gas_limit(), tsk.clone()).await?;
msg.gas_premium = gp;
}
if msg.gas_fee_cap().is_zero() {
let gfp = estimate_fee_cap(&data, msg.clone(), 20, tsk).await?;
msg.gas_fee_cap = gfp;
}
// TODO: Cap Gas Fee
Ok(msg)
}
Loading

0 comments on commit ce5c28b

Please sign in to comment.