Skip to content

Commit

Permalink
fix: consensus error is missing in tx results (#1458)
Browse files Browse the repository at this point in the history
Co-authored-by: QuantumExplorer <[email protected]>
  • Loading branch information
shumkov and QuantumExplorer authored Oct 10, 2023
1 parent a255276 commit b466524
Show file tree
Hide file tree
Showing 26 changed files with 242 additions and 182 deletions.
2 changes: 1 addition & 1 deletion packages/rs-dpp/src/errors/consensus/basic/basic_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use crate::consensus::basic::json_schema_error::JsonSchemaError;
use crate::consensus::basic::unsupported_version_error::UnsupportedVersionError;
use crate::consensus::basic::value_error::ValueError;

#[derive(Error, Debug, PlatformSerialize, PlatformDeserialize, Encode, Decode)]
#[derive(Error, Debug, PlatformSerialize, PlatformDeserialize, Encode, Decode, Clone)]
pub enum BasicError {
/*
Expand Down
2 changes: 1 addition & 1 deletion packages/rs-dpp/src/errors/consensus/consensus_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::errors::consensus::basic::BasicError;

// TODO It must be versioned as all other serializable types

#[derive(Error, Debug, Encode, Decode, PlatformSerialize, PlatformDeserialize)]
#[derive(Error, Debug, Encode, Decode, PlatformSerialize, PlatformDeserialize, Clone)]
#[platform_serialize(limit = 2000)]
pub enum ConsensusError {
/*
Expand Down
2 changes: 1 addition & 1 deletion packages/rs-dpp/src/errors/consensus/fee/fee_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use thiserror::Error;
use crate::errors::ProtocolError;
use platform_serialization_derive::{PlatformDeserialize, PlatformSerialize};

#[derive(Error, Debug, Encode, Decode, PlatformSerialize, PlatformDeserialize)]
#[derive(Error, Debug, Encode, Decode, PlatformSerialize, PlatformDeserialize, Clone)]
pub enum FeeError {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use thiserror::Error;
use crate::errors::ProtocolError;
use platform_serialization_derive::{PlatformDeserialize, PlatformSerialize};

#[derive(Error, Debug, Encode, Decode, PlatformSerialize, PlatformDeserialize)]
#[derive(Error, Debug, Encode, Decode, PlatformSerialize, PlatformDeserialize, Clone)]
pub enum SignatureError {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod data_trigger_condition_error;
pub mod data_trigger_execution_error;
pub mod data_trigger_invalid_result_error;

#[derive(Error, Debug, Encode, Decode, PlatformSerialize, PlatformDeserialize)]
#[derive(Error, Debug, Encode, Decode, PlatformSerialize, PlatformDeserialize, Clone)]
pub enum DataTriggerError {
/*
Expand Down
2 changes: 1 addition & 1 deletion packages/rs-dpp/src/errors/consensus/state/state_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::consensus::state::identity::identity_public_key_already_exists_for_un

use super::document::document_timestamps_are_equal_error::DocumentTimestampsAreEqualError;

#[derive(Error, Debug, Encode, Decode, PlatformSerialize, PlatformDeserialize)]
#[derive(Error, Debug, Encode, Decode, PlatformSerialize, PlatformDeserialize, Clone)]
pub enum StateError {
/*
Expand Down
2 changes: 1 addition & 1 deletion packages/rs-dpp/src/validation/validation_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub type ConsensusValidationResult<TData> = ValidationResult<TData, ConsensusErr

pub type SimpleConsensusValidationResult = ConsensusValidationResult<()>;

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ValidationResult<TData: Clone, E: Debug> {
pub errors: Vec<E>,
pub data: Option<TData>,
Expand Down
37 changes: 37 additions & 0 deletions packages/rs-drive-abci/src/abci/handler/error/consensus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::error::Error;
use dpp::consensus::ConsensusError;
use dpp::platform_value::platform_value;
use dpp::platform_value::string_encoding::{encode, Encoding};
use dpp::serialization::PlatformSerializableWithPlatformVersion;
use dpp::version::PlatformVersion;

pub trait AbciResponseInfoGetter {
/// Returns a base64 encoded consensus error for Tenderdash response info
fn response_info_for_version(
&self,
platform_version: &PlatformVersion,
) -> Result<String, Error>;
}

impl AbciResponseInfoGetter for ConsensusError {
fn response_info_for_version(
&self,
platform_version: &PlatformVersion,
) -> Result<String, Error> {
let consensus_error_bytes = self
.serialize_to_bytes_with_platform_version(platform_version)
.map_err(Error::Protocol)?;

let error_data_buffer = platform_value!({
"data": {
"serializedError": consensus_error_bytes
}
})
.to_cbor_buffer()
.map_err(|e| Error::Protocol(e.into()))?;

let encoded_info = encode(&error_data_buffer, Encoding::Base64);

Ok(encoded_info)
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod consensus;

use crate::error::query::QueryError;
use crate::error::Error;
use dpp::platform_value::platform_value;
Expand Down
61 changes: 61 additions & 0 deletions packages/rs-drive-abci/src/abci/handler/execution_result.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use crate::abci::handler::error::consensus::AbciResponseInfoGetter;
use crate::error::Error;
use crate::platform_types::state_transition_execution_result::StateTransitionExecutionResult;
use dpp::consensus::codes::ErrorWithCode;
use dpp::fee::SignedCredits;
use dpp::version::PlatformVersion;
use dpp::version::TryIntoPlatformVersioned;
use tenderdash_abci::proto::abci::ExecTxResult;

// State transitions are never free, so we should filter out SuccessfulFreeExecution
// So we use an option
impl TryIntoPlatformVersioned<ExecTxResult> for StateTransitionExecutionResult {
type Error = Error;

fn try_into_platform_versioned(
self,
platform_version: &PlatformVersion,
) -> Result<ExecTxResult, Self::Error> {
let response = match self {
Self::SuccessfulPaidExecution(dry_run_fee_result, fee_result) => ExecTxResult {
code: 0,
data: vec![],
log: "".to_string(),
info: "".to_string(),
gas_wanted: dry_run_fee_result.total_base_fee() as SignedCredits,
gas_used: fee_result.total_base_fee() as SignedCredits,
events: vec![],
codespace: "".to_string(),
},
Self::SuccessfulFreeExecution => ExecTxResult {
code: 0,
data: vec![],
log: "".to_string(),
info: "".to_string(),
gas_wanted: 0,
gas_used: 0,
events: vec![],
codespace: "".to_string(),
},
Self::ConsensusExecutionError(validation_result) => {
let error = validation_result
.errors
.first()
.expect("invalid execution result should have a consensus error");

ExecTxResult {
code: error.code(),
data: vec![],
log: "".to_string(),
info: error.response_info_for_version(platform_version)?,
gas_wanted: 0,
gas_used: 0,
events: vec![],
codespace: "".to_string(),
}
}
};

Ok(response)
}
}
Loading

0 comments on commit b466524

Please sign in to comment.