Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(c-bridge): force min collateral #2468

Open
wants to merge 3 commits into
base: 2.0-master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions bridges/centralized-ethereum/src/actors/dr_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::{fmt, time::Duration};
use witnet_config::defaults::PSEUDO_CONSENSUS_CONSTANTS_WIP0022_REWARD_COLLATERAL_RATIO;
use witnet_data_structures::{
chain::{tapi::current_active_wips, DataRequestOutput, Hashable},
data_request::calculate_reward_collateral_ratio,
error::TransactionError,
proto::ProtobufConvert,
radon_error::RadonErrors,
Expand Down Expand Up @@ -177,7 +178,7 @@ impl DrSender {
Err(err) => {
// Error deserializing or validating data request: mark data request as
// error and report error as result to ethereum.
log::error!("[{}] >< unacceptable data request bytecode: {}", dr_id, err);
log::error!("[{}] >< unacceptable data request: {}", dr_id, err);
let result = err.encode_cbor();
// In this case there is no data request transaction, so
// we set both the dr_tx_hash and dr_tally_tx_hash to zero values.
Expand Down Expand Up @@ -310,12 +311,11 @@ fn deserialize_and_validate_dr_bytes(
match DataRequestOutput::from_pb_bytes(dr_bytes) {
Err(e) => Err(DrSenderError::Deserialization { msg: e.to_string() }),
Ok(dr_output) => {
let required_reward_collateral_ratio =
PSEUDO_CONSENSUS_CONSTANTS_WIP0022_REWARD_COLLATERAL_RATIO;
let mut dr_output = dr_output;
validate_data_request_output(
&dr_output,
dr_output.collateral, // we don't want to ever alter the dro_hash
required_reward_collateral_ratio,
dr_min_collateral_nanowits, // dro_hash may be altered if dr_output.collateral goes below this value
PSEUDO_CONSENSUS_CONSTANTS_WIP0022_REWARD_COLLATERAL_RATIO,
&current_active_wips(),
)
.map_err(|e| match e {
Expand All @@ -326,7 +326,36 @@ fn deserialize_and_validate_dr_bytes(
})?;

// Collateral value validation
// If collateral is equal to 0 means that is equal to collateral_minimum value
if dr_output.collateral < dr_min_collateral_nanowits {
// modify data request's collateral if below some minimum,
// while maintaining same reward collateral ratio in such case:
let reward_collateral_ratio = calculate_reward_collateral_ratio(
dr_output.collateral,
dr_min_collateral_nanowits,
dr_output.witness_reward,
);
let dro_hash = dr_output.hash();
let dro_prev_collateral = dr_output.collateral;
let dro_prev_witness_reward = dr_output.witness_reward;
dr_output.collateral = dr_min_collateral_nanowits;
dr_output.witness_reward = calculate_reward_collateral_ratio(
dr_min_collateral_nanowits,
dr_min_collateral_nanowits,
reward_collateral_ratio,
);
log::warn!(
"DRO [{}]: witnessing collateral ({}) increased to minimum ({})",
dro_hash,
dro_prev_collateral,
dr_min_collateral_nanowits
);
log::warn!(
"DRO [{}]: witnessing reward ({}) proportionally increased ({})",
dro_hash,
dro_prev_witness_reward,
dr_output.witness_reward
)
}
if (dr_output.collateral != 0) && (dr_output.collateral < dr_min_collateral_nanowits) {
return Err(DrSenderError::InvalidCollateral {
msg: format!(
Expand Down
1 change: 1 addition & 0 deletions data_structures/src/data_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ pub fn calculate_reward_collateral_ratio(
witness_reward: u64,
) -> u64 {
let dr_collateral = if collateral == 0 {
// if collateral is equal to 0 means that is equal to collateral_minimum value
collateral_minimum
} else {
collateral
Expand Down
2 changes: 1 addition & 1 deletion data_structures/src/staking/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::staking::aux::StakeKey;
use crate::staking::helpers::StakeKey;
use failure::Fail;
use std::{
convert::From,
Expand Down
6 changes: 3 additions & 3 deletions data_structures/src/staking/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#![deny(missing_docs)]

/// Auxiliary convenience types and data structures.
pub mod helpers;
/// Constants related to the staking functionality.
pub mod constants;
/// Errors related to the staking functionality.
pub mod errors;
/// Auxiliary convenience types and data structures.
pub mod helpers;
/// The data structure and related logic for stake entries.
pub mod stake;
/// The data structure and related logic for keeping track of multiple stake entries.
Expand All @@ -16,9 +16,9 @@ pub mod stakes;
pub mod prelude {
pub use crate::capabilities::*;

pub use super::helpers::*;
pub use super::constants::*;
pub use super::errors::*;
pub use super::helpers::*;
pub use super::stake::*;
pub use super::stakes::*;
}
Expand Down
2 changes: 1 addition & 1 deletion data_structures/src/wit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{fmt, ops::*};

use serde::{Deserialize, Serialize};

use crate::{chain::Epoch, staking::aux::Power};
use crate::{chain::Epoch, staking::helpers::Power};

/// 1 nanowit is the minimal unit of value
/// 1 wit = 10^9 nanowits
Expand Down
2 changes: 1 addition & 1 deletion node/src/actors/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use witnet_data_structures::{
},
fee::{deserialize_fee_backwards_compatible, Fee},
radon_report::RadonReport,
staking::{aux::StakeKey, stakes::QueryStakesKey},
staking::{helpers::StakeKey, stakes::QueryStakesKey},
transaction::{
CommitTransaction, DRTransaction, RevealTransaction, StakeTransaction, Transaction,
VTTransaction,
Expand Down
Loading