Skip to content

Commit 60916eb

Browse files
authored
Merge pull request #65 from eqlabs/krisztian/improve-tip-calculation
feat: improve tip calculation
2 parents a9070d8 + a622a9e commit 60916eb

5 files changed

Lines changed: 160 additions & 6 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ The API should expose a single `/sign` endpoint:
9393

9494
An example implementation of the API is available [here](./examples/signer.rs).
9595

96+
97+
### Tip
98+
99+
The transaction tip value used during submission is calculated based on the median tip value in the `latest` block. The exact value used during submission is `MAX(latest_median_tip * ${tip_boost}, ${minimum_tip})`, where `tip_boost` and `minimum_tip` can be configured using the `--tip-boost` and `--minimum-tip` CLI arguments.
100+
96101
## Monitoring
97102

98103
A metrics endpoint is provided for scraping with Prometheus. By default the endpoint is available at `http://127.0.0.1:9090/metrics`. You can use the `--metrics-address` CLI option to change this address.

src/jsonrpc.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use starknet::{
1616
use crate::{
1717
attestation_info::AttestationInfo,
1818
signer::{AttestationSigner, SignError},
19+
tip::TipCalculationParams,
1920
};
2021

2122
#[derive(Debug, thiserror::Error)]
@@ -64,6 +65,7 @@ pub trait Client {
6465
&self,
6566
operational_address: Felt,
6667
signer: &AttestationSigner,
68+
tip_calculation_params: &TipCalculationParams,
6769
block_hash: Felt,
6870
) -> Result<Felt, ClientError>;
6971
async fn attestation_done_in_current_epoch(
@@ -94,10 +96,19 @@ impl Client for StarknetRpcClient {
9496
&self,
9597
operational_address: Felt,
9698
signer: &AttestationSigner,
99+
tip_calculation_params: &TipCalculationParams,
97100
block_hash: Felt,
98101
) -> Result<Felt, ClientError> {
99102
let chain_id = self.client.chain_id().await.context("Getting chain ID")?;
100103

104+
// Calculate tip as the median value from the latest block and apply params.
105+
let latest_block = self
106+
.client
107+
.get_block_with_txs(BlockId::Tag(BlockTag::Latest))
108+
.await?;
109+
let current_median_tip = latest_block.median_tip();
110+
let tip = tip_calculation_params.calculate_tip(current_median_tip);
111+
101112
let account = ClearSigningAccount::new(&self.client, signer, operational_address, chain_id);
102113

103114
let result = account
@@ -108,6 +119,7 @@ impl Client for StarknetRpcClient {
108119
}])
109120
.gas_price_estimate_multiplier(3.0)
110121
.gas_estimate_multiplier(3.0)
122+
.tip(tip)
111123
.send()
112124
.await
113125
.context("Sending transaction")?;

src/main.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod jsonrpc;
1818
mod metrics_exporter;
1919
mod signer;
2020
mod state;
21+
mod tip;
2122

2223
#[derive(Parser)]
2324
#[command(version, about, long_about = None)]
@@ -90,6 +91,22 @@ struct Config {
9091

9192
#[arg(long, default_value = "compact", value_name = "FORMAT")]
9293
pub log_format: LogFormat,
94+
95+
#[arg(
96+
long,
97+
long_help = "The median tip value from recent transactions is multiplied by this scaling factor when calculating the transaction tip.",
98+
default_value = "1.0",
99+
env = "VALIDATOR_ATTESTATION_TIP_BOOST"
100+
)]
101+
pub tip_boost: f64,
102+
103+
#[arg(
104+
long,
105+
long_help = "Minimum value of the transaction tip to use when submitting the attestation transaction.",
106+
default_value = "0",
107+
env = "VALIDATOR_ATTESTATION_MINIMUM_TIP"
108+
)]
109+
pub minimum_tip: u64,
93110
}
94111

95112
#[derive(Clone, clap::ValueEnum)]
@@ -154,6 +171,11 @@ async fn main() -> anyhow::Result<()> {
154171
return Err(anyhow::anyhow!("Inappropriate JSON-RPC API version"));
155172
}
156173

174+
let tip_calculation_params = tip::TipCalculationParams {
175+
tip_boost: config.tip_boost,
176+
minimum_tip: config.minimum_tip,
177+
};
178+
157179
// Set up JSON-RPC client
158180
let chain_id = client.chain_id().await.context("Getting chain ID")?;
159181
let (staking_contract_address, attestation_contract_address) =
@@ -305,7 +327,7 @@ async fn main() -> anyhow::Result<()> {
305327
metrics::gauge!("validator_attestation_starknet_latest_block_number").set(header.block_number as f64);
306328

307329
let old_state = state.clone();
308-
let result = state.handle_new_block_header(&client, config.staker_operational_address, &signer, header.block_number, header.block_hash).await;
330+
let result = state.handle_new_block_header(&client, config.staker_operational_address, &signer, &tip_calculation_params, header.block_number, header.block_hash).await;
309331
match result {
310332
Ok(new_state) => {
311333
tracing::debug!(?new_state, "State transition complete");

0 commit comments

Comments
 (0)