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

Fixing Mint with External URI Event and Adding Corresponding Tests #130

Merged
merged 11 commits into from
Oct 31, 2023
37 changes: 18 additions & 19 deletions ownership-chain/precompile/laos-evolution/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//! LAOS precompile module.

#![cfg_attr(not(feature = "std"), no_std)]
use fp_evm::{Log, Precompile, PrecompileHandle, PrecompileOutput};
use fp_evm::{Precompile, PrecompileHandle, PrecompileOutput};
use pallet_laos_evolution::{traits::LaosEvolution as LaosEvolutionT, Slot, TokenId};
use parity_scale_codec::Encode;
use precompile_utils::{
keccak256, revert, revert_dispatch_error, succeed, Address, Bytes, EvmDataWriter, EvmResult,
FunctionModifier, LogExt, LogsBuilder, PrecompileHandleExt,
};

use sp_core::{H160, H256};
use sp_core::H160;
use sp_std::{fmt::Debug, marker::PhantomData, vec::Vec};

/// Solidity selector of the CreateCollection log, which is the Keccak of the Log signature.
Expand Down Expand Up @@ -110,8 +110,10 @@ where
let slot = input.read::<Slot>()?;
let to = input.read::<Address>()?.0;
let token_uri_raw = input.read::<Bytes>()?.0;
let token_uri =
token_uri_raw.try_into().map_err(|_| revert("invalid token uri length"))?;
let token_uri = token_uri_raw
.clone()
.try_into()
.map_err(|_| revert("invalid token uri length"))?;

match LaosEvolution::mint_with_external_uri(
caller.into(),
Expand All @@ -121,21 +123,18 @@ where
token_uri,
) {
Ok(token_id) => {
let mut token_id_bytes = [0u8; 32];
token_id.to_big_endian(&mut token_id_bytes);

Log {
address: context.address,
topics: sp_std::vec![
H256(SELECTOR_LOG_MINTED_WITH_EXTERNAL_TOKEN_URI),
H256::from(H160::zero()),
H256::from(to),
H256::from_low_u64_be(collection_id),
H256(token_id_bytes),
],
data: Vec::new(),
}
.record(handle)?;
LogsBuilder::new(context.address)
.log2(
SELECTOR_LOG_MINTED_WITH_EXTERNAL_TOKEN_URI,
to,
EvmDataWriter::new()
.write(collection_id)
.write(slot)
.write(Bytes(token_uri_raw))
asiniscalchi marked this conversation as resolved.
Show resolved Hide resolved
.write(token_id)
.build(),
)
.record(handle)?;

Ok(succeed(EvmDataWriter::new().write(token_id).build()))
},
Expand Down
56 changes: 53 additions & 3 deletions ownership-chain/precompile/laos-evolution/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ use precompile_utils::{
revert, succeed,
testing::{create_mock_handle, create_mock_handle_from_input},
};
use sp_core::{H160, U256};
use sp_core::{H160, H256, U256};
use sp_std::vec::Vec;

type AccountId = H160;
type AddressMapping = pallet_evm::IdentityAddressMapping;

const ALICE: &str = "0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac";

#[test]
fn check_selectors() {
assert_eq!(Action::CreateCollection as u32, 0x2069E953);
Expand Down Expand Up @@ -83,7 +85,7 @@ fn create_collection_should_generate_log() {
impl_precompile_mock_simple!(Mock, Ok(123), None, Ok(0.into()), None);

let input = EvmDataWriter::new_with_selector(Action::CreateCollection)
.write(Address(H160([1u8; 20])))
.write(Address(H160::from_str(ALICE).unwrap()))
.build();
let mut handle = create_mock_handle_from_input(input);

Expand All @@ -96,7 +98,7 @@ fn create_collection_should_generate_log() {
assert_eq!(logs[0].topics[0], SELECTOR_LOG_NEW_COLLECTION.into());
assert_eq!(
logs[0].topics[1],
H256::from_str("0x0000000000000000000000000101010101010101010101010101010101010101")
H256::from_str("0x000000000000000000000000f24ff3a9cf04c71dbc94d0b566f7a27b94566cac")
.unwrap()
);
assert_eq!(
Expand All @@ -108,6 +110,54 @@ fn create_collection_should_generate_log() {
);
}

#[test]
fn mint_with_external_uri_should_generate_log() {
impl_precompile_mock_simple!(
Mock,
Ok(0),
None,
Ok(U256::from_str("0x010203").unwrap()), // return token id
None
);

let input = EvmDataWriter::new_with_selector(Action::Mint)
.write(U256::from(123)) // collection_id
.write(U256::from(9)) // slot
.write(Address(H160::from_str(ALICE).unwrap())) // to
.write(Bytes("ciao".into())) // token_uri
.build();
let mut handle = create_mock_handle_from_input(input);

let result = Mock::execute(&mut handle);
assert!(result.is_ok());
let logs = handle.logs;
assert_eq!(logs.len(), 1);
assert_eq!(logs[0].address, H160::zero());
assert_eq!(logs[0].topics.len(), 2);
assert_eq!(logs[0].topics[0], SELECTOR_LOG_MINTED_WITH_EXTERNAL_TOKEN_URI.into());
assert_eq!(
logs[0].topics[1],
H256::from_str("0x000000000000000000000000f24ff3a9cf04c71dbc94d0b566f7a27b94566cac")
.unwrap()
);
assert_eq!(
logs[0].data,
vec![
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 123, // collection id
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 9, // slot
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 2, 3, // token id
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 4, // token uri length
99, 105, 97, 111, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0
] // token uri
);
}

#[test]
fn create_collection_on_mock_with_nonzero_value_fails() {
impl_precompile_mock_simple!(Mock, Ok(5), None, Ok(0.into()), None);
Expand Down