Skip to content
This repository was archived by the owner on Sep 28, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
904470b
add SetAppendix in remote_transact
gitofdeepanshu Jun 2, 2023
401c1ce
formatting abstract params
gitofdeepanshu Jun 2, 2023
bcff3fd
update remote_transact()
gitofdeepanshu Jun 2, 2023
cc97ce4
remove println
gitofdeepanshu Jun 4, 2023
af7fcef
add send_xcm function to precompile
gitofdeepanshu Jun 8, 2023
527b642
fix remote_transact call
gitofdeepanshu Jun 8, 2023
97b60aa
small fixes xcm.sol
gitofdeepanshu Jun 8, 2023
55886ec
fix typos xcm.sol
gitofdeepanshu Jun 8, 2023
a9b7a5d
change bytes32 to bytes in send_xcm()
gitofdeepanshu Jun 8, 2023
3df8b14
add test for xcm_call
gitofdeepanshu Jun 12, 2023
a064674
fix xcm_send using Bytes
gitofdeepanshu Jun 12, 2023
75a1bea
add multilocation for xcm-precompile
gitofdeepanshu Jun 13, 2023
d1c0d56
update licenses
gitofdeepanshu Jun 13, 2023
b77fcc2
fix scope issue for String
gitofdeepanshu Jun 14, 2023
edc738e
fmt
gitofdeepanshu Jun 14, 2023
450c780
minor refactor, add log:trace
gitofdeepanshu Jun 20, 2023
a4cf0be
Empty-Commit
gitofdeepanshu Jun 20, 2023
83ae4ad
make input field in EvmDataReader pub(crate)
gitofdeepanshu Jun 23, 2023
65a22ef
remove setAppendix from remote_transact()
gitofdeepanshu Jun 26, 2023
0171f31
old interface added for legacy support
gitofdeepanshu Jun 26, 2023
6c5a6ea
fmt
gitofdeepanshu Jun 26, 2023
6038c63
add tests for multilocation and bytes
gitofdeepanshu Jun 26, 2023
60bcbb6
data encapsulation fix
gitofdeepanshu Jun 26, 2023
27cd351
newline in tests
gitofdeepanshu Jul 9, 2023
028e3dc
add mock and test::transfer for xtokens
gitofdeepanshu Jul 14, 2023
37aa31c
test: add transfer_multiasset
gitofdeepanshu Jul 17, 2023
8117882
add new functions for xtokens
gitofdeepanshu Jul 17, 2023
cd3360e
test: transfer_multi_currencies
gitofdeepanshu Jul 18, 2023
fba5ee3
test: transfer_multi_assets
gitofdeepanshu Jul 19, 2023
c61f391
test: transfer_multiassets_cannot_insert_more_than_max
gitofdeepanshu Jul 19, 2023
7dafadb
test: transfer_multi_currencies_cannot_insert_more_than_max
gitofdeepanshu Jul 19, 2023
910eee0
more tests and fixes
gitofdeepanshu Jul 19, 2023
6a6b941
typo fix
gitofdeepanshu Jul 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions precompiles/utils/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ impl<K: Kind, S: Get<u32>> EvmData for BoundedBytesString<K, S> {
let range = inner_reader.move_cursor(array_size)?;

let data = inner_reader
.input
.get(range)
.get_input_from_range(range)
.ok_or_else(|| revert(K::signature()))?;

let bytes = Self {
Expand Down Expand Up @@ -146,7 +145,6 @@ impl<K: Kind, S: Get<u32>> EvmData for BoundedBytesString<K, S> {
fn has_static_size() -> bool {
false
}

}

// BytesString <=> Vec/&[u8]
Expand Down
7 changes: 6 additions & 1 deletion precompiles/utils/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl From<Bytes> for Vec<u8> {
/// Provide functions to parse common types.
#[derive(Clone, Copy, Debug)]
pub struct EvmDataReader<'a> {
pub(crate) input: &'a [u8],
input: &'a [u8],
cursor: usize,
}

Expand Down Expand Up @@ -175,6 +175,11 @@ impl<'a> EvmDataReader<'a> {
})
}

/// Return Option<&[u8]> from a given range for EvmDataReader
pub fn get_input_from_range(&self, range: Range<usize>) -> Option<&[u8]> {
self.input.get(range)
}

/// Read remaining bytes
pub fn read_till_end(&mut self) -> EvmResult<&[u8]> {
let range = self.move_cursor(self.input.len() - self.cursor)?;
Expand Down
261 changes: 261 additions & 0 deletions precompiles/utils/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@
// You should have received a copy of the GNU General Public License
// along with Utils. If not, see <http://www.gnu.org/licenses/>.

use crate::bytes::UnboundedBytes;

use super::*;
use hex_literal::hex;
use sp_core::{H256, U256};
use {
crate::xcm::{network_id_from_bytes, network_id_to_bytes},
::xcm::latest::{Junction, Junctions, NetworkId},
};

fn u256_repeat_byte(byte: u8) -> U256 {
let value = H256::repeat_byte(byte);
Expand Down Expand Up @@ -505,7 +511,19 @@ fn read_bytes() {

assert_eq!(data, parsed.as_bytes());
}
#[test]
Comment thread
gitofdeepanshu marked this conversation as resolved.
fn read_unbounded_bytes() {
let data = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod\
tempor incididunt ut labore et dolore magna aliqua.";
let writer_output = EvmDataWriter::new()
.write(UnboundedBytes::from(&data[..]))
.build();

let mut reader = EvmDataReader::new(&writer_output);
let parsed: UnboundedBytes = reader.read().expect("to correctly parse Bytes");

assert_eq!(data, parsed.as_bytes());
}
#[test]
fn write_bytes() {
let data = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod\
Expand All @@ -529,6 +547,31 @@ fn write_bytes() {
assert_eq!(read("read part 3"), H256::from_slice(&padded[0x40..0x60]));
assert_eq!(read("read part 4"), H256::from_slice(&padded[0x60..0x80]));
}
#[test]
fn write_unbounded_bytes() {
let data = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod\
tempor incididunt ut labore et dolore magna aliqua.";

let writer_output = EvmDataWriter::new()
.write(UnboundedBytes::from(&data[..]))
.build();

// We can read this "manualy" using simpler functions.
let mut reader = EvmDataReader::new(&writer_output);

// We pad data to a multiple of 32 bytes.
let mut padded = data.to_vec();
assert!(data.len() < 0x80);
padded.resize(0x80, 0);

assert_eq!(reader.read::<U256>().expect("read offset"), 32.into());
assert_eq!(reader.read::<U256>().expect("read size"), data.len().into());
let mut read = |e| reader.read::<H256>().expect(e); // shorthand
assert_eq!(read("read part 1"), H256::from_slice(&padded[0x00..0x20]));
assert_eq!(read("read part 2"), H256::from_slice(&padded[0x20..0x40]));
assert_eq!(read("read part 3"), H256::from_slice(&padded[0x40..0x60]));
assert_eq!(read("read part 4"), H256::from_slice(&padded[0x60..0x80]));
}

#[test]
fn read_string() {
Expand Down Expand Up @@ -618,7 +661,61 @@ fn write_vec_bytes() {
assert_eq!(read("read part 3"), H256::from_slice(&padded[0x40..0x60]));
assert_eq!(read("read part 4"), H256::from_slice(&padded[0x60..0x80]));
}
#[test]
fn write_vec_unbounded_bytes() {
let data = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod\
tempor incididunt ut labore et dolore magna aliqua.";

let writer_output = EvmDataWriter::new()
.write(vec![
UnboundedBytes::from(&data[..]),
UnboundedBytes::from(&data[..]),
])
.build();

writer_output
.chunks_exact(32)
.map(|chunk| H256::from_slice(chunk))
.for_each(|hash| println!("{:?}", hash));

// We pad data to a multiple of 32 bytes.
let mut padded = data.to_vec();
assert!(data.len() < 0x80);
padded.resize(0x80, 0);

let mut reader = EvmDataReader::new(&writer_output);

// Offset of vec
assert_eq!(reader.read::<U256>().expect("read offset"), 32.into());

// Length of vec
assert_eq!(reader.read::<U256>().expect("read offset"), 2.into());

// Relative offset of first bytgmes object
assert_eq!(reader.read::<U256>().expect("read offset"), 0x40.into());
// Relative offset of second bytes object
assert_eq!(reader.read::<U256>().expect("read offset"), 0xe0.into());

// Length of first bytes object
assert_eq!(reader.read::<U256>().expect("read size"), data.len().into());

// First byte objects data
let mut read = |e| reader.read::<H256>().expect(e); // shorthand
assert_eq!(read("read part 1"), H256::from_slice(&padded[0x00..0x20]));
assert_eq!(read("read part 2"), H256::from_slice(&padded[0x20..0x40]));
assert_eq!(read("read part 3"), H256::from_slice(&padded[0x40..0x60]));
assert_eq!(read("read part 4"), H256::from_slice(&padded[0x60..0x80]));

// Length of second bytes object
assert_eq!(reader.read::<U256>().expect("read size"), data.len().into());

// Second byte objects data
let mut read = |e| reader.read::<H256>().expect(e); // shorthand
assert_eq!(read("read part 1"), H256::from_slice(&padded[0x00..0x20]));
assert_eq!(read("read part 2"), H256::from_slice(&padded[0x20..0x40]));
assert_eq!(read("read part 3"), H256::from_slice(&padded[0x40..0x60]));
assert_eq!(read("read part 4"), H256::from_slice(&padded[0x60..0x80]));
}
#[test]
fn read_vec_of_bytes() {
let data = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod\
Expand Down Expand Up @@ -744,3 +841,167 @@ fn read_complex_solidity_function() {
// weight
assert_eq!(reader.read::<U256>().unwrap(), 100u32.into());
}

#[test]
fn read_dynamic_size_tuple() {
// (uint8, bytes[]) encoded by web3
let data = hex!(
"0000000000000000000000000000000000000000000000000000000000000020
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000040
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000020
0000000000000000000000000000000000000000000000000000000000000001
0100000000000000000000000000000000000000000000000000000000000000"
);

let mut reader = EvmDataReader::new(&data);

assert_eq!(
reader.read::<(u8, Vec<UnboundedBytes>)>().unwrap(),
(1, vec![UnboundedBytes::from(vec![0x01])])
);
}
#[test]
fn junctions_decoder_works() {
let writer_output = EvmDataWriter::new()
.write(Junctions::X1(Junction::OnlyChild))
.build();

let mut reader = EvmDataReader::new(&writer_output);
let parsed: Junctions = reader
.read::<Junctions>()
.expect("to correctly parse Junctions");

assert_eq!(parsed, Junctions::X1(Junction::OnlyChild));

let writer_output = EvmDataWriter::new()
.write(Junctions::X2(Junction::OnlyChild, Junction::OnlyChild))
.build();

let mut reader = EvmDataReader::new(&writer_output);
let parsed: Junctions = reader
.read::<Junctions>()
.expect("to correctly parse Junctions");

assert_eq!(
parsed,
Junctions::X2(Junction::OnlyChild, Junction::OnlyChild)
);

let writer_output = EvmDataWriter::new()
.write(Junctions::X3(
Junction::OnlyChild,
Junction::OnlyChild,
Junction::OnlyChild,
))
.build();

let mut reader = EvmDataReader::new(&writer_output);
let parsed: Junctions = reader
.read::<Junctions>()
.expect("to correctly parse Junctions");

assert_eq!(
parsed,
Junctions::X3(
Junction::OnlyChild,
Junction::OnlyChild,
Junction::OnlyChild
),
);
}

#[test]
fn junction_decoder_works() {
let writer_output = EvmDataWriter::new().write(Junction::Parachain(0)).build();

let mut reader = EvmDataReader::new(&writer_output);
let parsed: Junction = reader
.read::<Junction>()
.expect("to correctly parse Junctions");

assert_eq!(parsed, Junction::Parachain(0));

let writer_output = EvmDataWriter::new()
.write(Junction::AccountId32 {
network: None,
id: [1u8; 32],
})
.build();

let mut reader = EvmDataReader::new(&writer_output);
let parsed: Junction = reader
.read::<Junction>()
.expect("to correctly parse Junctions");

assert_eq!(
parsed,
Junction::AccountId32 {
network: None,
id: [1u8; 32],
}
);

let writer_output = EvmDataWriter::new()
.write(Junction::AccountIndex64 {
network: None,
index: u64::from_be_bytes([1u8; 8]),
})
.build();

let mut reader = EvmDataReader::new(&writer_output);
let parsed: Junction = reader
.read::<Junction>()
.expect("to correctly parse Junctions");

assert_eq!(
parsed,
Junction::AccountIndex64 {
network: None,
index: u64::from_be_bytes([1u8; 8]),
}
);

let writer_output = EvmDataWriter::new()
.write(Junction::AccountKey20 {
network: None,
key: H160::repeat_byte(0xAA).as_bytes().try_into().unwrap(),
})
.build();

let mut reader = EvmDataReader::new(&writer_output);
let parsed: Junction = reader
.read::<Junction>()
.expect("to correctly parse Junctions");

assert_eq!(
parsed,
Junction::AccountKey20 {
network: None,
key: H160::repeat_byte(0xAA).as_bytes().try_into().unwrap(),
}
);
}

#[test]
fn network_id_decoder_works() {
assert_eq!(network_id_from_bytes(network_id_to_bytes(None)), Ok(None));

let mut name = [0u8; 32];
name[0..6].copy_from_slice(b"myname");
assert_eq!(
network_id_from_bytes(network_id_to_bytes(Some(NetworkId::ByGenesis(name)))),
Ok(Some(NetworkId::ByGenesis(name)))
);

assert_eq!(
network_id_from_bytes(network_id_to_bytes(Some(NetworkId::Kusama))),
Ok(Some(NetworkId::Kusama))
);

assert_eq!(
network_id_from_bytes(network_id_to_bytes(Some(NetworkId::Polkadot))),
Ok(Some(NetworkId::Polkadot))
);
}
Loading