Fix delivery transaction estimation used by rational relayer (#1109)

* fix delivery transaction estimation in greedy relayer

* fixed typo

* improve logging

* improve logging

* fmt

* fix compilation

* fmt

* Update relays/lib-substrate-relay/src/messages_target.rs

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* review

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
This commit is contained in:
Svyatoslav Nikolsky
2021-09-06 12:04:33 +03:00
committed by Bastian Köcher
parent 2101ed9cc5
commit 03a54df398
35 changed files with 391 additions and 71 deletions
@@ -210,6 +210,7 @@ pub trait TargetClient<P: MessageLane>: RelayClient {
async fn estimate_delivery_transaction_in_source_tokens(
&self,
nonces: RangeInclusive<MessageNonce>,
total_prepaid_nonces: MessageNonce,
total_dispatch_weight: Weight,
total_size: u32,
) -> Result<P::SourceChainBalance, Self::Error>;
@@ -773,6 +774,7 @@ pub(crate) mod tests {
async fn estimate_delivery_transaction_in_source_tokens(
&self,
nonces: RangeInclusive<MessageNonce>,
_total_prepaid_nonces: MessageNonce,
total_dispatch_weight: Weight,
total_size: u32,
) -> Result<TestSourceChainBalance, TestError> {
@@ -561,6 +561,7 @@ async fn select_nonces_for_delivery_transaction<P: MessageLane>(
let mut selected_weight: Weight = 0;
let mut selected_unpaid_weight: Weight = 0;
let mut selected_prepaid_nonces = 0;
let mut selected_size: u32 = 0;
let mut selected_count: MessageNonce = 0;
let mut selected_reward = P::SourceChainBalance::zero();
@@ -570,6 +571,8 @@ async fn select_nonces_for_delivery_transaction<P: MessageLane>(
let mut total_confirmations_cost = P::SourceChainBalance::zero();
let mut total_cost = P::SourceChainBalance::zero();
let hard_selected_begin_nonce = nonces_queue[nonces_queue_range.start].1.begin();
// technically, multiple confirmations will be delivered in a single transaction,
// meaning less loses for relayer. But here we don't know the final relayer yet, so
// we're adding a separate transaction for every message. Normally, this cost is covered
@@ -637,8 +640,12 @@ async fn select_nonces_for_delivery_transaction<P: MessageLane>(
// dispatch origin account AND reward is not covering this fee.
//
// So in the latter case we're not adding the dispatch weight to the delivery transaction weight.
let mut new_selected_prepaid_nonces = selected_prepaid_nonces;
let new_selected_unpaid_weight = match details.dispatch_fee_payment {
DispatchFeePayment::AtSourceChain => selected_unpaid_weight.saturating_add(details.dispatch_weight),
DispatchFeePayment::AtSourceChain => {
new_selected_prepaid_nonces += 1;
selected_unpaid_weight.saturating_add(details.dispatch_weight)
}
DispatchFeePayment::AtTargetChain => selected_unpaid_weight,
};
@@ -651,7 +658,8 @@ async fn select_nonces_for_delivery_transaction<P: MessageLane>(
RelayerMode::Rational => {
let delivery_transaction_cost = lane_target_client
.estimate_delivery_transaction_in_source_tokens(
0..=(new_selected_count as MessageNonce - 1),
hard_selected_begin_nonce..=(hard_selected_begin_nonce + index as MessageNonce),
new_selected_prepaid_nonces,
new_selected_unpaid_weight,
new_selected_size as u32,
)
@@ -711,11 +719,11 @@ async fn select_nonces_for_delivery_transaction<P: MessageLane>(
hard_selected_count = index + 1;
selected_weight = new_selected_weight;
selected_unpaid_weight = new_selected_unpaid_weight;
selected_prepaid_nonces = new_selected_prepaid_nonces;
selected_size = new_selected_size;
selected_count = new_selected_count;
}
let hard_selected_begin_nonce = nonces_queue[nonces_queue_range.start].1.begin();
if hard_selected_count != soft_selected_count {
let hard_selected_end_nonce = hard_selected_begin_nonce + hard_selected_count as MessageNonce - 1;
let soft_selected_begin_nonce = hard_selected_begin_nonce;