Skip to content

Commit

Permalink
feat(application): implement transaction validation for the query runner
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-wright committed Jun 29, 2023
1 parent 191a3cd commit f2ac0a2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
19 changes: 17 additions & 2 deletions core/application/src/query_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ use draco_interfaces::{
application::SyncQueryRunnerInterface,
types::{
AccountInfo, CommodityServed, CommodityTypes, Epoch, EpochInfo, Metadata, NodeInfo,
ProtocolParams, ReportedReputationMeasurements, Service, ServiceId, TotalServed, Value,
ProtocolParams, ReportedReputationMeasurements, Service, ServiceId, TotalServed,
TransactionResponse, UpdateRequest, Value,
},
};
use fleek_crypto::{AccountOwnerPublicKey, ClientPublicKey, NodePublicKey};
use hp_float::unsigned::HpUfloat;

use crate::state::Committee;
use crate::{
state::{Committee, State},
table::StateTables,
};

#[derive(Clone)]
pub struct QueryRunner {
Expand Down Expand Up @@ -283,4 +287,15 @@ impl SyncQueryRunnerInterface for QueryRunner {
self.param_table.get(ctx).get(param).unwrap_or(0)
})
}

fn validate_txn(&self, txn: UpdateRequest) -> TransactionResponse {
self.inner.run(|ctx| {
// Create the app/execution enviroment
let backend = StateTables {
table_selector: ctx,
};
let app = State::new(backend);
app.execute_txn(txn.clone())
})
}
}
37 changes: 35 additions & 2 deletions core/application/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use anyhow::{anyhow, Result};
use draco_interfaces::{
application::ExecutionEngineSocket,
types::{
Block, Epoch, ExecutionError, NodeInfo, ProofOfConsensus, Tokens, TotalServed,
TransactionResponse, UpdateMethod, UpdatePayload, UpdateRequest,
Block, Epoch, ExecutionData, ExecutionError, NodeInfo, ProofOfConsensus, Tokens,
TotalServed, TransactionResponse, UpdateMethod, UpdatePayload, UpdateRequest,
},
ApplicationInterface, BlockExecutionResponse, DeliveryAcknowledgment, SyncQueryRunnerInterface,
};
Expand Down Expand Up @@ -807,3 +807,36 @@ async fn test_supply_across_epoch() {
}
}
}

#[test]
async fn test_validate_txn() {
let (update_socket, query_runner) = init_app(None).await;
let (_, genesis_committee) = get_genesis();

// Submit a ChangeEpoch transaction that will revert (EpochHasNotStarted) and ensure that the
// `validate_txn` method of the query runner returns the same response as the update runner.
let req = get_update_request_node(
UpdateMethod::ChangeEpoch { epoch: 1 },
genesis_committee[0].public_key,
);
let res = run_transaction(vec![req.clone()], &update_socket)
.await
.unwrap();
assert_eq!(
res.txn_receipts[0],
//TransactionResponse::Revert(ExecutionError::EpochHasNotStarted)
query_runner.validate_txn(req)
);

// Submit a ChangeEpoch transaction that will succeed and ensure that the
// `validate_txn` method of the query runner returns the same response as the update runner.
let req = get_update_request_node(
UpdateMethod::ChangeEpoch { epoch: 0 },
genesis_committee[0].public_key,
);
let res = run_transaction(vec![req], &update_socket).await.unwrap();
assert_eq!(
res.txn_receipts[0],
TransactionResponse::Success(ExecutionData::None)
);
}
5 changes: 4 additions & 1 deletion core/interfaces/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
config::ConfigConsumer,
types::{
Block, CommodityServed, Epoch, EpochInfo, NodeInfo, ProtocolParams,
ReportedReputationMeasurements, TotalServed, TransactionResponse,
ReportedReputationMeasurements, TotalServed, TransactionResponse, UpdateRequest,
},
};

Expand Down Expand Up @@ -150,6 +150,9 @@ pub trait SyncQueryRunnerInterface: Clone + Send + Sync + 'static {

/// Returns the passed in protocol parameter
fn get_protocol_params(&self, param: ProtocolParams) -> u128;

/// Validates the passed in transaction
fn validate_txn(&self, txn: UpdateRequest) -> TransactionResponse;
}

#[derive(Clone, Debug)]
Expand Down

0 comments on commit f2ac0a2

Please sign in to comment.