Skip to content

Commit

Permalink
Merge remote-tracking branch 'dojo/main' into feat/solis-integration
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Nov 16, 2023
2 parents 7d9d906 + 62980bb commit 1336e80
Show file tree
Hide file tree
Showing 20 changed files with 376 additions and 339 deletions.
3 changes: 2 additions & 1 deletion crates/dojo-lang/src/introspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub fn handle_introspect_enum(
"
dojo::database::schema::Ty::Enum(
dojo::database::schema::Enum {{
name: 'Direction',
name: '{name}',
attrs: array![].span(),
children: array![
{}
Expand All @@ -200,6 +200,7 @@ pub fn handle_introspect_enum(
)",
arms_ty.join(",\n")
);

// Enums have 1 size and 8 bit layout by default
let layout = vec![RewriteNode::Text("layout.append(8);\n".into())];
let size_precompute = 1;
Expand Down
16 changes: 8 additions & 8 deletions crates/dojo-lang/src/plugin_test_data/introspect
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl PlainEnumSchemaIntrospection of dojo::database::schema::SchemaIntrospection
fn ty() -> dojo::database::schema::Ty {
dojo::database::schema::Ty::Enum(
dojo::database::schema::Enum {
name: 'Direction',
name: 'PlainEnum',
attrs: array![].span(),
children: array![
(
Expand Down Expand Up @@ -164,7 +164,7 @@ impl EnumPrimitiveSchemaIntrospection of dojo::database::schema::SchemaIntrospec
fn ty() -> dojo::database::schema::Ty {
dojo::database::schema::Ty::Enum(
dojo::database::schema::Enum {
name: 'Direction',
name: 'EnumPrimitive',
attrs: array![].span(),
children: array![
(
Expand Down Expand Up @@ -225,7 +225,7 @@ impl EnumTupleSchemaIntrospection of dojo::database::schema::SchemaIntrospection
fn ty() -> dojo::database::schema::Ty {
dojo::database::schema::Ty::Enum(
dojo::database::schema::Enum {
name: 'Direction',
name: 'EnumTuple',
attrs: array![].span(),
children: array![
(
Expand Down Expand Up @@ -291,7 +291,7 @@ impl EnumCustomSchemaIntrospection of dojo::database::schema::SchemaIntrospectio
fn ty() -> dojo::database::schema::Ty {
dojo::database::schema::Ty::Enum(
dojo::database::schema::Enum {
name: 'Direction',
name: 'EnumCustom',
attrs: array![].span(),
children: array![
(
Expand Down Expand Up @@ -694,7 +694,7 @@ impl PlainEnumDrop of Drop::<PlainEnum>;

dojo::database::schema::Ty::Enum(
dojo::database::schema::Enum {
name: 'Direction',
name: 'PlainEnum',
attrs: array![].span(),
children: array![

Expand Down Expand Up @@ -752,7 +752,7 @@ layout.append(16);

dojo::database::schema::Ty::Enum(
dojo::database::schema::Enum {
name: 'Direction',
name: 'EnumPrimitive',
attrs: array![].span(),
children: array![

Expand Down Expand Up @@ -815,7 +815,7 @@ layout.append(8);

dojo::database::schema::Ty::Enum(
dojo::database::schema::Enum {
name: 'Direction',
name: 'EnumTuple',
attrs: array![].span(),
children: array![

Expand Down Expand Up @@ -883,7 +883,7 @@ dojo::database::schema::SchemaIntrospection::<Vec2>::layout(ref layout);

dojo::database::schema::Ty::Enum(
dojo::database::schema::Enum {
name: 'Direction',
name: 'EnumCustom',
attrs: array![].span(),
children: array![

Expand Down
56 changes: 0 additions & 56 deletions crates/dojo-types/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,62 +20,6 @@ impl Member {
}
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)]
pub struct EntityQuery {
pub model: String,
pub clause: Clause,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)]
pub enum Clause {
Keys(KeysClause),
Attribute(AttributeClause),
Composite(CompositeClause),
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)]
pub struct KeysClause {
pub keys: Vec<FieldElement>,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)]
pub struct AttributeClause {
pub attribute: String,
pub operator: ComparisonOperator,
pub value: Value,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)]
pub struct CompositeClause {
pub operator: LogicalOperator,
pub clauses: Vec<Clause>,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)]
pub enum LogicalOperator {
And,
Or,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)]
pub enum ComparisonOperator {
Eq,
Neq,
Gt,
Gte,
Lt,
Lte,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Eq, Clone)]
pub enum Value {
String(String),
Int(i64),
UInt(u64),
Bool(bool),
Bytes(Vec<u8>),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelMetadata {
pub schema: Ty,
Expand Down
4 changes: 2 additions & 2 deletions crates/katana/core/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ impl Backend {

let mut estimations = Vec::with_capacity(transactions.len());

let results = TransactionExecutor::new(&mut state, &block_context, false)
let results = TransactionExecutor::new(&mut state, &block_context, false, transactions)
.with_error_log()
.execute_many(transactions);
.execute();

for res in results {
let exec_info = res?;
Expand Down
131 changes: 72 additions & 59 deletions crates/katana/core/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,22 @@ impl Default for ExecutionOutcome {
}
}

/// The result of a transaction execution.
pub type TxExecutionResult = Result<TransactionExecutionInfo, TransactionExecutionError>;

/// A transaction executor.
///
/// The transactions will be executed in an iterator fashion, sequentially, in the
/// exact order they are provided to the executor. The execution is done within its implementation
/// of the [`Iterator`] trait.
pub struct TransactionExecutor<'a> {
/// A flag to enable/disable fee charging.
charge_fee: bool,
/// The block context the transactions will be executed on.
block_context: &'a BlockContext,
/// The transactions to be executed (in the exact order they are in the iterator).
transactions: std::vec::IntoIter<Transaction>,
/// The state the transactions will be executed on.
state: &'a mut CachedStateWrapper<StateRefDb>,

// logs flags
Expand All @@ -111,6 +124,7 @@ impl<'a> TransactionExecutor<'a> {
state: &'a mut CachedStateWrapper<StateRefDb>,
block_context: &'a BlockContext,
charge_fee: bool,
transactions: Vec<Transaction>,
) -> Self {
Self {
state,
Expand All @@ -119,6 +133,7 @@ impl<'a> TransactionExecutor<'a> {
error_log: false,
events_log: false,
resources_log: false,
transactions: transactions.into_iter(),
}
}

Expand All @@ -133,78 +148,76 @@ impl<'a> TransactionExecutor<'a> {
pub fn with_resources_log(self) -> Self {
Self { resources_log: true, ..self }
}
}

impl<'a> TransactionExecutor<'a> {
pub fn execute_many(
&mut self,
transactions: Vec<Transaction>,
) -> Vec<Result<TransactionExecutionInfo, TransactionExecutionError>> {
transactions.into_iter().map(|tx| self.execute(tx)).collect()
/// A method to conveniently execute all the transactions and return their results.
pub fn execute(self) -> Vec<TxExecutionResult> {
self.collect()
}
}

pub fn execute(
&mut self,
transaction: Transaction,
) -> Result<TransactionExecutionInfo, TransactionExecutionError> {
let sierra = if let Transaction::Declare(DeclareTransaction {
sierra_class: Some(sierra_class),
inner,
..
}) = &transaction
{
Some((inner.class_hash(), sierra_class.clone()))
} else {
None
};

let res = match transaction.into() {
ExecutionTransaction::AccountTransaction(tx) => {
tx.execute(&mut self.state.inner_mut(), self.block_context, self.charge_fee)
}
ExecutionTransaction::L1HandlerTransaction(tx) => {
tx.execute(&mut self.state.inner_mut(), self.block_context, self.charge_fee)
}
};

match res {
Ok(exec_info) => {
if let Some((class_hash, sierra_class)) = sierra {
self.state
.set_sierra_class(class_hash, sierra_class)
.expect("failed to set sierra class");
impl<'a> Iterator for TransactionExecutor<'a> {
type Item = TxExecutionResult;
fn next(&mut self) -> Option<Self::Item> {
self.transactions.next().map(|tx| {
let sierra = if let Transaction::Declare(DeclareTransaction {
sierra_class: Some(sierra_class),
inner,
..
}) = &tx
{
Some((inner.class_hash(), sierra_class.clone()))
} else {
None
};

let res = match tx.into() {
ExecutionTransaction::AccountTransaction(tx) => {
tx.execute(&mut self.state.inner_mut(), self.block_context, self.charge_fee)
}
ExecutionTransaction::L1HandlerTransaction(tx) => {
tx.execute(&mut self.state.inner_mut(), self.block_context, self.charge_fee)
}
};

match res {
Ok(exec_info) => {
if let Some((class_hash, sierra_class)) = sierra {
self.state
.set_sierra_class(class_hash, sierra_class)
.expect("failed to set sierra class");
}

if self.error_log {
if let Some(err) = &exec_info.revert_error {
let formatted_err = format!("{:?}", err).replace("\\n", "\n");
warn!(target: "executor", "Transaction execution error: {formatted_err}");
if self.error_log {
if let Some(err) = &exec_info.revert_error {
let formatted_err = format!("{:?}", err).replace("\\n", "\n");
warn!(target: "executor", "Transaction execution error: {formatted_err}");
}
}
}

if self.resources_log {
trace!(
target: "executor",
"Transaction resource usage: {}",
pretty_print_resources(&exec_info.actual_resources)
);
}
if self.resources_log {
trace!(
target: "executor",
"Transaction resource usage: {}",
pretty_print_resources(&exec_info.actual_resources)
);
}

if self.events_log {
trace_events(&events_from_exec_info(&exec_info));
}

if self.events_log {
trace_events(&events_from_exec_info(&exec_info));
Ok(exec_info)
}

Ok(exec_info)
}
Err(err) => {
if self.error_log {
warn_message_transaction_error_exec_error(&err);
}

Err(err) => {
if self.error_log {
warn_message_transaction_error_exec_error(&err);
Err(err)
}

Err(err)
}
}
})
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/katana/core/src/service/block_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,15 @@ impl IntervalBlockProducer {
&mut state,
&self.backend.env.read().block,
!self.backend.config.read().disable_fee,
transactions.clone(),
)
.with_error_log()
.with_events_log()
.with_resources_log()
.execute_many(transactions.clone())
.into_iter()
.zip(transactions)
.map(|(res, tx)| match res {
Ok(exec_info) => {
let executed_tx = ExecutedTransaction::new(tx, exec_info);
Ok(execution_info) => {
let executed_tx = ExecutedTransaction::new(tx, execution_info);
MaybeInvalidExecutedTransaction::Valid(Arc::new(executed_tx))
}
Err(err) => {
Expand Down Expand Up @@ -354,11 +353,12 @@ impl InstantBlockProducer {
&mut state,
&block_context,
!backend.config.read().disable_fee,
transactions.clone(),
)
.with_error_log()
.with_events_log()
.with_resources_log()
.execute_many(transactions.clone());
.execute();

let outcome = backend
.do_mine_block(create_execution_outcome(
Expand Down
Loading

0 comments on commit 1336e80

Please sign in to comment.