Skip to content

Commit

Permalink
Add custom messages to asserts
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Dimitrov <[email protected]>
  • Loading branch information
dimitrovmaksim committed Jun 8, 2023
1 parent 232184b commit 7895a3b
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 9 deletions.
134 changes: 125 additions & 9 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,15 @@ impl<C: Blockchain> MatchstickInstanceContext<C> {
let expected_val: String = asc_get(&self.wasm_ctx, expected_val_ptr, &GasCounter::new())?;

if !self.store.contains_key(&entity_type) {
logging::error!(
"(assert.fieldEquals) No entities with type '{}' found.",
&entity_type
);
logging::error!("No entities with type '{}' found.", &entity_type);

return Ok(false);
}

let entities = self.store.get(&entity_type).unwrap();
if !entities.contains_key(&id) {
logging::error!(
"(assert.fieldEquals) No entity with type '{}' and id '{}' found.",
"No entity with type '{}' and id '{}' found.",
&entity_type,
&id
);
Expand All @@ -276,7 +273,7 @@ impl<C: Blockchain> MatchstickInstanceContext<C> {
let entity = entities.get(&id).unwrap();
if !entity.contains_key(&field_name) {
logging::error!(
"(assert.fieldEquals) No field named '{}' on entity with type '{}' and id '{}' found.",
"No field named '{}' on entity with type '{}' and id '{}' found.",
&field_name,
&entity_type,
&id
Expand All @@ -288,7 +285,7 @@ impl<C: Blockchain> MatchstickInstanceContext<C> {
let val = entity.get(&field_name).unwrap();
if val.to_string() != expected_val {
logging::error!(
"(assert.fieldEquals) Expected field '{}' to equal '{}', but was '{}' instead.",
"Expected field '{}' to equal '{}', but was '{}' instead.",
&field_name,
&expected_val,
val
Expand Down Expand Up @@ -323,7 +320,7 @@ impl<C: Blockchain> MatchstickInstanceContext<C> {

if exp_val != act_val {
logging::error!(
"(assert.equals) Expected value was '{}' but actual value was '{}'",
"Expected value was '{}' but actual value was '{}'",
exp_val,
act_val
);
Expand All @@ -348,7 +345,7 @@ impl<C: Blockchain> MatchstickInstanceContext<C> {
&& self.store.get(&entity_type).unwrap().contains_key(&id)
{
logging::error!(
"(assert.notInStore) Value for entity type: '{}' and id: '{}' was found in store.",
"Value for entity type: '{}' and id: '{}' was found in store.",
entity_type,
id
);
Expand All @@ -358,6 +355,125 @@ impl<C: Blockchain> MatchstickInstanceContext<C> {
Ok(true)
}

/// Overloading the assert function with custom error message for backwards compatibility with matchstick-as
/// function _assert.fieldEquals(
/// entityType: string, id: string,
/// fieldName: string, expectedVal: string,
/// message: string,
/// ): bool
pub fn assert_field_equals_with_message(
&mut self,
_gas: &GasCounter,
entity_type_ptr: AscPtr<AscString>,
id_ptr: AscPtr<AscString>,
field_name_ptr: AscPtr<AscString>,
expected_val_ptr: AscPtr<AscString>,
message_ptr: AscPtr<AscString>,
) -> Result<bool, HostExportError> {
update_derived_relations_in_store(self);
let entity_type: String = asc_get(&self.wasm_ctx, entity_type_ptr, &GasCounter::new())?;
let id: String = asc_get(&self.wasm_ctx, id_ptr, &GasCounter::new())?;
let field_name: String = asc_get(&self.wasm_ctx, field_name_ptr, &GasCounter::new())?;
let expected_val: String = asc_get(&self.wasm_ctx, expected_val_ptr, &GasCounter::new())?;
let message: String = asc_get(&self.wasm_ctx, message_ptr, &GasCounter::new())?;

if !self.store.contains_key(&entity_type) {
logging::error!("No entities with type '{}' found.", &entity_type);

return Ok(false);
}

let entities = self.store.get(&entity_type).unwrap();
if !entities.contains_key(&id) {
logging::error!(
"No entity with type '{}' and id '{}' found.",
&entity_type,
&id
);

return Ok(false);
}

let entity = entities.get(&id).unwrap();
if !entity.contains_key(&field_name) {
logging::error!(
"No field named '{}' on entity with type '{}' and id '{}' found.",
&field_name,
&entity_type,
&id
);

return Ok(false);
}

let val = entity.get(&field_name).unwrap();
if val.to_string() != expected_val {
logging::error!("{}", message);

return Ok(false);
};

Ok(true)
}

/// function _assert.equals(expected: ethereum.Value, actual: ethereum.Value, message: string): bool
pub fn assert_equals_with_message(
&mut self,
_gas: &GasCounter,
expected_ptr: u32,
actual_ptr: u32,
message_ptr: AscPtr<AscString>,
) -> Result<bool, HostExportError> {
update_derived_relations_in_store(self);
let expected: Token = asc_get::<_, AscEnum<EthereumValueKind>, _>(
&self.wasm_ctx,
expected_ptr.into(),
&GasCounter::new(),
)?;
let actual: Token = asc_get::<_, AscEnum<EthereumValueKind>, _>(
&self.wasm_ctx,
actual_ptr.into(),
&GasCounter::new(),
)?;
let message: String = asc_get(&self.wasm_ctx, message_ptr, &GasCounter::new())?;

let exp_val = get_token_value(expected);
let act_val = get_token_value(actual);

if exp_val != act_val {
logging::error!("{}", message);

return Ok(false);
}

Ok(true)
}

/// function _assert.notInStore(entityType: string, id: string, message: string): bool
pub fn assert_not_in_store_with_message(
&mut self,
_gas: &GasCounter,
entity_type_ptr: AscPtr<AscString>,
id_ptr: AscPtr<AscString>,
message_ptr: AscPtr<AscString>,
) -> Result<bool, HostExportError> {
update_derived_relations_in_store(self);
let entity_type: String = asc_get(&self.wasm_ctx, entity_type_ptr, &GasCounter::new())?;
let id: String = asc_get(&self.wasm_ctx, id_ptr, &GasCounter::new())?;
let message: String = asc_get(&self.wasm_ctx, message_ptr, &GasCounter::new())?;

if self.store.contains_key(&entity_type)
&& self.store.get(&entity_type).unwrap().contains_key(&id)
{
logging::error!("{}", message);

return Ok(false);
}

Ok(true)
}

/// function store.get(entityType: string, id: string): Entity
pub fn mock_store_get(
&mut self,
Expand Down
24 changes: 24 additions & 0 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,30 @@ impl<C: Blockchain> MatchstickInstance<C> {
id_ptr
);

link!(
"_assert.fieldEqualsWithMessage",
assert_field_equals_with_message,
entity_type_ptr,
id_ptr,
field_name_ptr,
expected_val_ptr,
message_ptr
);
link!(
"_assert.equalsWithMessage",
assert_equals_with_message,
expected_ptr,
actual_ptr,
message_ptr
);
link!(
"_assert.notInStoreWithMessage",
assert_not_in_store_with_message,
entity_type_ptr,
id_ptr,
message_ptr
);

link!("countEntities", count_entities, entity_type);

// Linking gas function
Expand Down

0 comments on commit 7895a3b

Please sign in to comment.