| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777 |
- // SPDX-License-Identifier: Apache-2.0
- use parity_scale_codec::{Decode, Encode};
- use crate::build_solidity;
- #[test]
- fn abi_decode() {
- let mut runtime = build_solidity(
- r##"
- contract bar {
- function test() public {
- (int16 a, bool b) = abi.decode(hex"7f0001", (int16, bool));
- assert(a == 127);
- assert(b == true);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- let mut runtime = build_solidity(
- r##"
- contract bar {
- function test() public {
- uint8 a = abi.decode(hex"40", (uint8));
- assert(a == 64);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- }
- #[test]
- fn abi_encode() {
- let mut runtime = build_solidity(
- r##"
- struct s {
- int32 f1;
- uint8 f2;
- string f3;
- uint16[2] f4;
- }
- contract bar {
- function test() public {
- uint16 a = 0xfd01;
- assert(abi.encode(a) == hex"01fd");
- uint32 b = 0xaabbccdd;
- assert(abi.encode(true, b, false) == hex"01ddccbbaa00");
- }
- function test2() public {
- string b = "foobar";
- assert(abi.encode(b) == hex"18666f6f626172");
- assert(abi.encode("foobar") == hex"18666f6f626172");
- }
- function test3() public {
- s x = s({ f1: 511, f2: 0xf7, f3: "testie", f4: [ uint16(4), 5 ] });
- assert(abi.encode(x) == hex"ff010000f71874657374696504000500");
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- runtime.heap_verify();
- runtime.function("test2", Vec::new());
- runtime.heap_verify();
- runtime.function("test3", Vec::new());
- runtime.heap_verify();
- }
- #[test]
- fn abi_encode_packed() {
- let mut runtime = build_solidity(
- r##"
- struct s {
- int32 f1;
- uint8 f2;
- string f3;
- uint16[2] f4;
- }
- contract bar {
- function test() public {
- uint16 a = 0xfd01;
- assert(abi.encodePacked(a) == hex"01fd");
- uint32 b = 0xaabbccdd;
- assert(abi.encodePacked(true, b, false) == hex"01ddccbbaa00");
- }
- function test2() public {
- string b = "foobar";
- assert(abi.encodePacked(b) == "foobar");
- assert(abi.encodePacked("foobar") == "foobar");
- assert(abi.encodePacked("foo", "bar") == "foobar");
- }
- function test3() public {
- s x = s({ f1: 511, f2: 0xf7, f3: "testie", f4: [ uint16(4), 5 ] });
- assert(abi.encodePacked(x) == hex"ff010000f774657374696504000500");
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- runtime.function("test2", Vec::new());
- runtime.function("test3", Vec::new());
- }
- #[test]
- fn abi_encode_with_selector() {
- let mut runtime = build_solidity(
- r##"
- contract bar {
- function test1() public {
- uint16 a = 0xfd01;
- assert(abi.encodeWithSelector(hex"44332211", a) == hex"4433221101fd");
- uint32 b = 0xaabbccdd;
- assert(abi.encodeWithSelector(hex"aabbccdd", true, b, false) == hex"aabbccdd01ddccbbaa00");
- assert(abi.encodeWithSelector(hex"aabbccdd") == hex"aabbccdd");
- }
- function test2() public {
- uint8[] arr = new uint8[](3);
- arr[0] = 0xfe;
- arr[1] = 0xfc;
- arr[2] = 0xf8;
- assert(abi.encodeWithSelector(hex"01020304", arr) == hex"010203040cfefcf8");
- }
- }"##,
- );
- runtime.function("test1", Vec::new());
- runtime.function("test2", Vec::new());
- }
- #[test]
- fn abi_encode_with_signature() {
- let mut runtime = build_solidity(
- r##"
- contract bar {
- string bla = "Hello, World!";
- function test1() public {
- assert(keccak256("Hello, World!") == hex"acaf3289d7b601cbd114fb36c4d29c85bbfd5e133f14cb355c3fd8d99367964f");
- assert(abi.encodeWithSignature("Hello, World!") == hex"acaf3289");
- assert(abi.encodeWithSignature(bla) == hex"acaf3289");
- }
- function test2() public {
- uint8[] arr = new uint8[](3);
- arr[0] = 0xfe;
- arr[1] = 0xfc;
- arr[2] = 0xf8;
- assert(abi.encodeWithSelector(hex"01020304", arr) == hex"010203040cfefcf8");
- }
- }"##,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("test1", Vec::new());
- runtime.function("test2", Vec::new());
- }
- #[test]
- fn call() {
- let mut runtime = build_solidity(
- r##"
- contract superior {
- function test1() public {
- inferior i = new inferior();
- i.test1();
- assert(keccak256("test1()") == hex"6b59084dfb7dcf1c687dd12ad5778be120c9121b21ef90a32ff73565a36c9cd3");
- bytes bs;
- bool success;
- (success, bs) = address(i).call(hex"6b59084d");
- assert(success == true);
- assert(bs == hex"");
- }
- function test2() public {
- inferior i = new inferior();
- assert(i.test2(257) == 256);
- assert(keccak256("test2(uint64)") == hex"296dacf0801def8823747fbd751fbc1444af573e88de40d29c4d01f6013bf095");
- bytes bs;
- bool success;
- (success, bs) = address(i).call(hex"296dacf0_0101_0000__0000_0000");
- assert(success == true);
- assert(bs == hex"0001_0000__0000_0000");
- }
- }
- contract inferior {
- function test1() public {
- print("Baa!");
- }
- function test2(uint64 x) public returns (uint64) {
- return x ^ 1;
- }
- }"##,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("test1", Vec::new());
- runtime.function("test2", Vec::new());
- let mut runtime = build_solidity(
- r##"
- contract superior {
- function test1() public {
- inferior i = new inferior();
- assert(keccak256("test1()") == hex"6b59084dfb7dcf1c687dd12ad5778be120c9121b21ef90a32ff73565a36c9cd3");
- bytes bs;
- bool success;
- (success, bs) = address(i).call(abi.encodeWithSelector(hex"6b59084d"));
- assert(success == true);
- assert(bs == hex"");
- (success, bs) = address(i).call(abi.encodeWithSignature("test1()"));
- assert(success == true);
- assert(bs == hex"");
- }
- function test2() public {
- inferior i = new inferior();
- assert(keccak256("test2(uint64)") == hex"296dacf0801def8823747fbd751fbc1444af573e88de40d29c4d01f6013bf095");
- bytes bs;
- bool success;
- (success, bs) = address(i).call(abi.encodeWithSelector(hex"296dacf0", uint64(257)));
- assert(success == true);
- assert(abi.decode(bs, (uint64)) == 256);
- (success, bs) = address(i).call(abi.encodeWithSignature("test2(uint64)", uint64(0xfeec)));
- assert(success == true);
- assert(abi.decode(bs, (uint64)) == 0xfeed);
- }
- }
- contract inferior {
- function test1() public {
- print("Baa!");
- }
- function test2(uint64 x) public returns (uint64) {
- return x ^ 1;
- }
- }"##,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("test1", Vec::new());
- runtime.function("test2", Vec::new());
- }
- #[test]
- fn block() {
- let mut runtime = build_solidity(
- r##"
- contract bar {
- function test() public {
- uint64 b = block.number;
- assert(b == 950_119_597);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- let mut runtime = build_solidity(
- r##"
- contract bar {
- function test() public {
- uint64 b = block.timestamp;
- assert(b == 1594035638);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- let mut runtime = build_solidity(
- r##"
- contract bar {
- function test() public {
- uint128 b = block.minimum_balance;
- assert(b == 500);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- }
- #[test]
- fn tx() {
- let mut runtime = build_solidity(
- r##"
- contract bar {
- function test() public {
- uint128 b = tx.gasprice(1);
- assert(b == 59_541_253_813_967);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- let mut runtime = build_solidity(
- r##"
- contract bar {
- function test() public {
- uint128 b = tx.gasprice(1000);
- assert(b == 59_541_253_813_967_000);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- }
- #[test]
- fn msg() {
- let mut runtime = build_solidity(
- r##"
- contract bar {
- function test() public payable {
- uint128 b = msg.value;
- assert(b == 145_594_775_678_703_046_797_448_357_509_034_994_219);
- }
- }"##,
- );
- let value = 145_594_775_678_703_046_797_448_357_509_034_994_219;
- runtime.set_transferred_value(value);
- runtime.raw_function(runtime.contracts()[0].code.messages["test"].to_vec());
- let mut runtime = build_solidity(
- r##"
- contract c {
- function test() public {
- other o = new other();
- address foo = o.test();
- assert(foo == address(this));
- }
- }
- contract other {
- function test() public returns (address) {
- return msg.sender;
- }
- }
- "##,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("test", Vec::new());
- }
- #[test]
- fn functions() {
- let mut runtime = build_solidity(
- r##"
- contract bar {
- function test() public {
- uint64 b = gasleft();
- assert(b == 2_224_097_461);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- }
- #[test]
- fn data() {
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct Uint32(u32);
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct String(Vec<u8>);
- let mut runtime = build_solidity(
- r##"
- contract bar {
- constructor(string memory s) public {
- assert(msg.data == hex"98dd1bb318666f6f626172");
- assert(msg.sig == hex"98dd_1bb3");
- }
- function test(uint32 x) public {
- assert(msg.data == hex"e3cff634addeadde");
- assert(msg.sig == hex"e3cf_f634");
- }
- }"##,
- );
- runtime.constructor(0, String(b"foobar".to_vec()).encode());
- runtime.function("test", Uint32(0xdeaddead).encode());
- }
- #[test]
- fn addmod() {
- // does it work with small numbers
- let mut runtime = build_solidity(
- r##"
- contract x {
- function test() public {
- assert(addmod(500, 100, 3) == 0);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- // divide by zero
- let mut runtime = build_solidity(
- r##"
- contract x {
- function test() public {
- assert(addmod(500, 100, 0) == 0);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- // bigger numbers (64 bit)
- let mut runtime = build_solidity(
- r##"
- contract x {
- function test() public {
- // 8_163_321_534_310_945_187 * 16_473_784_705_703_234_153 = 134_480_801_439_669_508_040_541_782_812_209_371_611
- assert(addmod(
- 0,
- 134_480_801_439_669_508_040_541_782_812_209_371_611,
- 16_473_784_705_703_234_153) == 0);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- // bigger numbers (128 bit)
- let mut runtime = build_solidity(
- r##"
- contract x {
- function test() public {
- // 254_765_928_331_839_140_628_748_569_208_536_440_801 * 148_872_967_607_295_528_830_315_866_466_318_446_379 = 37_927_759_795_988_462_606_362_647_643_228_779_300_269_446_446_871_437_380_583_919_404_728_626_309_579
- assert(addmod(
- 0,
- 37_927_759_795_988_462_606_362_647_643_228_779_300_269_446_446_871_437_380_583_919_404_728_626_309_579,
- 148_872_967_607_295_528_830_315_866_466_318_446_379) == 0);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- // bigger numbers (256 bit)
- let mut runtime = build_solidity(
- r##"
- contract x {
- function test() public {
- assert(addmod(
- 109802613191917590715814365746623394364442484359636492253827647701845853490667,
- 49050800785888222684575674817707208319566972397745729319314900174750088808217,
- 233) == 204);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- let mut runtime = build_solidity(
- r##"
- contract x {
- function test() public {
- assert(addmod(
- 109802613191917590715814365746623394364442484359636492253827647701845853490667,
- 109802613191917590715814365746623394364442484359636492253827647701845853490667,
- 2) == 0);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- }
- #[test]
- fn mulmod() {
- // does it work with small numbers
- let mut runtime = build_solidity(
- r##"
- contract x {
- function test() public {
- assert(mulmod(500, 100, 5) == 0);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- // divide by zero
- let mut runtime = build_solidity(
- r##"
- contract x {
- function test() public {
- assert(mulmod(500, 100, 0) == 0);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- // bigger numbers
- let mut runtime = build_solidity(
- r##"
- contract x {
- function test() public {
- assert(mulmod(50000, 10000, 5) == 0);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- let mut runtime = build_solidity(
- r##"
- contract x {
- function test() public {
- assert(mulmod(18446744073709551616, 18446744073709550403, 1024) == 0);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- // 2^127 = 170141183460469231731687303715884105728
- let mut runtime = build_solidity(
- r##"
- contract x {
- function test() public {
- assert(mulmod(170141183460469231731687303715884105728, 170141183460469231731687303715884105728, 170141183460469231731687303715884105728) == 0);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- // 2^128 = 340282366920938463463374607431768211456
- let mut runtime = build_solidity(
- r##"
- contract x {
- function test() public {
- assert(mulmod(340282366920938463463374607431768211456, 340282366920938463463374607431768211456, 340282366920938463463374607431768211456) == 0);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- // 2^240 = 1766847064778384329583297500742918515827483896875618958121606201292619776
- let mut runtime = build_solidity(
- r##"
- contract x {
- function test() public {
- assert(mulmod(1766847064778384329583297500742918515827483896875618958121606201292619776,
- 1766847064778384329583297500742918515827483896875618958121606201292619776,
- 1766847064778384329583297500742918515827483896875618958121606201292619776)
- == 0);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- // 240 bit prime: 824364134751099588297822369420176791913922347811791536817152126684405253
- let mut runtime = build_solidity(
- r##"
- contract x {
- function test() public {
- assert(mulmod(824364134751099588297822369420176791913922347811791536817152126684405253,
- 824364134751099588297822369420176791913922347811791536817152126684405253,
- 824364134751099588297822369420176791913922347811791536817152126684405253)
- == 0);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- // 256 bit prime: 113477814626329405513123655892059150026234290706112418221315641434319827527851
- let mut runtime = build_solidity(
- r##"
- contract x {
- function test() public {
- assert(mulmod(113477814626329405513123655892059150026234290706112418221315641434319827527851,
- 113477814626329405513123655892059150026234290706112418221315641434319827527851,
- 113477814626329405513123655892059150026234290706112418221315641434319827527851)
- == 0);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- let mut runtime = build_solidity(
- r##"
- contract x {
- function test() public {
- assert(mulmod(113477814626329405513123655892059150026234290706112418221315641434319827527851,
- 113477814626329405513123655892059150026234290706112418221315641434319827527841,
- 233)
- == 12);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- }
- #[test]
- fn my_token() {
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct TokenTest([u8; 32], bool);
- let mut runtime = build_solidity(
- "
- contract mytoken {
- function test(address account, bool sender) public view returns (address) {
- if (sender) {
- return msg.sender;
- }
- return account;
- }
- }
- ",
- );
- let addr: [u8; 32] = [
- 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
- 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,
- 0x31, 0x32,
- ];
- runtime.function("test", TokenTest(addr, true).encode());
- assert_eq!(&runtime.caller()[..], &runtime.output()[..]);
- runtime.function("test", TokenTest(addr, false).encode());
- assert_eq!(&runtime.output()[..], &addr[..]);
- runtime.function(
- "test",
- TokenTest(<[u8; 32]>::try_from(&runtime.caller()[..]).unwrap(), true).encode(),
- );
- assert_eq!(&runtime.caller()[..], &runtime.output()[..]);
- runtime.function(
- "test",
- TokenTest(<[u8; 32]>::try_from(&runtime.caller()[..]).unwrap(), false).encode(),
- );
- assert_eq!(&runtime.caller()[..], &runtime.output()[..]);
- }
- #[test]
- fn hash() {
- let mut runtime = build_solidity(
- r##"
- import "substrate";
- contract Foo {
- Hash current;
- bytes32 current2;
- function set(Hash h) public returns (bytes32) {
- current = h;
- current2 = Hash.unwrap(h);
- return current2;
- }
- function get() public view returns (Hash) {
- return Hash.wrap(current2);
- }
- function test_encoding() public view {
- Hash h = Hash.wrap(current2);
- assert(abi.encode(current2) == abi.encode(h));
- }
- }
- "##,
- );
- #[derive(Encode)]
- struct Hash([u8; 32]);
- let h = Hash([
- 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
- 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,
- 0x31, 0x32,
- ]);
- runtime.function("set", h.encode());
- assert_eq!(&runtime.output()[..], &h.0[..]);
- runtime.function("get", vec![]);
- assert_eq!(&runtime.output()[..], &h.0[..]);
- runtime.function("test_encoding", vec![]);
- }
- #[test]
- fn call_chain_extension() {
- let mut runtime = build_solidity(
- r##"
- import {chain_extension as ChainExtension} from "substrate";
- contract Foo {
- function chain_extension(bytes input) public returns (uint32, bytes) {
- return ChainExtension(123, input);
- }
- }"##,
- );
- let data = 0xdeadbeefu32.to_be_bytes().to_vec();
- runtime.function("chain_extension", data.encode());
- let ret = <(u32, Vec<u8>)>::decode(&mut &runtime.output()[..]).unwrap();
- assert_eq!(ret.0, data.iter().map(|i| *i as u32).sum::<u32>());
- assert_eq!(ret.1, data.iter().cloned().rev().collect::<Vec<_>>());
- }
|