| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628 |
- // SPDX-License-Identifier: Apache-2.0
- use parity_scale_codec::{Decode, Encode};
- use primitive_types::U256;
- use rand::Rng;
- use crate::build_solidity;
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct Val32(u32);
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct Val8(u8);
- #[test]
- fn const_array_array() {
- let mut runtime = build_solidity(
- r##"
- contract foo {
- int8[8] constant bar = [ int8(1), 2, 3, 4, 5, 6, 7, 8 ];
- function f(uint32 i1) public returns (int8) {
- return bar[i1];
- }
- }"##,
- );
- runtime.function("f", Val32(1).encode());
- assert_eq!(runtime.output(), Val8(2).encode());
- }
- #[test]
- fn votes() {
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct Votes([bool; 11]);
- let mut runtime = build_solidity(
- r##"
- contract foo {
- /// In a jury, do the ayes have it?
- function f(bool[11] votes) public pure returns (bool) {
- uint32 i;
- uint32 ayes = 0;
- for (i=0; i<votes.length; i++) {
- if (votes[i]) {
- ayes += 1;
- }
- }
- return ayes > votes.length / 2;
- }
- }"##,
- );
- runtime.function(
- "f",
- Votes([
- true, true, true, true, true, true, false, false, false, false, false,
- ])
- .encode(),
- );
- assert_eq!(runtime.output(), true.encode());
- runtime.function(
- "f",
- Votes([
- true, true, true, true, true, false, false, false, false, false, false,
- ])
- .encode(),
- );
- assert_eq!(runtime.output(), false.encode());
- }
- #[test]
- fn return_array() {
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct Res([u64; 4]);
- let mut runtime = build_solidity(
- r##"
- contract foo {
- function array() pure public returns (int64[4]) {
- return [ int64(4), 84564, 31213, 1312 ];
- }
- }"##,
- );
- runtime.function("array", Vec::new());
- assert_eq!(runtime.output(), Res([4, 84564, 31213, 1312]).encode());
- }
- #[test]
- fn storage_arrays() {
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct Val(i32);
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct SetArg(u32, i32);
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct GetArg(u32);
- let mut runtime = build_solidity(
- r##"
- contract foo {
- int32[type(uint32).max] bigarray;
- function set(uint32 index, int32 val) public {
- bigarray[index] = val;
- }
- function get(uint32 index) public returns (int32) {
- return bigarray[index];
- }
- }"##,
- );
- let mut rng = rand::thread_rng();
- let mut vals = Vec::new();
- for _ in 0..100 {
- let index = rng.gen::<u32>();
- let val = rng.gen::<i32>();
- runtime.function("set", SetArg(index, val).encode());
- vals.push((index, val));
- }
- for val in vals {
- runtime.function("get", GetArg(val.0).encode());
- assert_eq!(runtime.output(), Val(val.1).encode());
- }
- }
- #[test]
- fn enum_arrays() {
- #[derive(Encode, Decode)]
- struct Arg([u8; 100]);
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct Ret(u32);
- let mut runtime = build_solidity(
- r##"
- contract enum_array {
- enum Foo { Bar1, Bar2, Bar3, Bar4 }
- function count_bar2(Foo[100] calldata foos) public returns (uint32) {
- uint32 count = 0;
- uint32 i;
- for (i = 0; i < foos.length; i++) {
- if (foos[i] == Foo.Bar2) {
- count++;
- }
- }
- return count;
- }
- }"##,
- );
- let mut rng = rand::thread_rng();
- let mut a = [0u8; 100];
- let mut count = 0;
- #[allow(clippy::needless_range_loop)]
- for i in 0..a.len() {
- a[i] = rng.gen::<u8>() % 4;
- if a[i] == 1 {
- count += 1;
- }
- }
- runtime.function("count_bar2", Arg(a).encode());
- assert_eq!(runtime.output(), Ret(count).encode());
- }
- #[test]
- fn storage_ref_arg() {
- let mut runtime = build_solidity(
- r##"
- contract storage_refs {
- int32[10] a;
- int32[10] b;
- function set(int32[10] storage array, uint8 index, int32 val) private {
- array[index] = val;
- }
- function test() public {
- set(a, 2, 5);
- set(b, 2, 102);
- assert(a[2] == 5);
- assert(b[2] == 102);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- }
- #[test]
- fn storage_ref_var() {
- let mut runtime = build_solidity(
- r##"
- contract storage_refs {
- int32[10] a;
- int32[10] b;
- function set(int32[10] storage array, uint8 index, int32 val) private {
- array[index] = val;
- }
- function test() public {
- int32[10] storage ref = a;
- set(ref, 2, 5);
- ref = b;
- set(ref, 2, 102);
- assert(a[2] == 5);
- assert(b[2] == 102);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- }
- #[test]
- fn storage_ref_returns() {
- let mut runtime = build_solidity(
- r##"
- contract storage_refs {
- int32[10] a;
- int32[10] b;
- function a_or_b(bool want_a) private returns (int32[10] storage) {
- if (want_a) {
- return a;
- }
- return b;
- }
- function set(int32[10] storage array, uint8 index, int32 val) private {
- array[index] = val;
- }
- function test() public {
- int32[10] storage ref = a_or_b(true);
- set(ref, 2, 5);
- ref = a_or_b(false);
- set(ref, 2, 102);
- assert(a[2] == 5);
- assert(b[2] == 102);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- }
- #[test]
- fn storage_to_memory() {
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct Ret([u32; 10]);
- let mut runtime = build_solidity(
- r##"
- contract storage_refs {
- uint32[10] a;
- function test() public returns (uint32[10]) {
- for (uint32 i = 0; i < 10; ) {
- uint32 index = i;
- a[index] = 7 * ++i;
- }
- return a;
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- let val = Ret([7, 14, 21, 28, 35, 42, 49, 56, 63, 70]);
- assert_eq!(runtime.output(), val.encode());
- }
- #[test]
- fn memory_to_storage() {
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct Ret([u32; 10]);
- let mut runtime = build_solidity(
- r##"
- contract storage_refs {
- int32[10] a;
- function test() public returns (int32[10]) {
- int32[10] b = [ int32(7), 14, 21, 28, 35, 42, 49, 56, 63, 70 ];
- a = b;
- return a;
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- let val = Ret([7, 14, 21, 28, 35, 42, 49, 56, 63, 70]);
- assert_eq!(runtime.output(), val.encode());
- }
- #[test]
- fn array_dimensions() {
- let mut runtime = build_solidity(
- r##"
- contract storage_refs {
- int32[32] a;
- function test() public {
- assert(a.length == 32);
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- }
- #[test]
- fn array_in_struct() {
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct Ret([u32; 10]);
- let mut runtime = build_solidity(
- r##"
- contract storage_refs {
- struct foo {
- int32[10] f1;
- }
- function test() public returns (int32[10]) {
- foo a = foo({f1: [ int32(7), 14, 21, 28, 35, 42, 49, 56, 63, 0 ]});
- assert(a.f1[1] == 14);
- a.f1[9] = 70;
- return a.f1;
- }
- }"##,
- );
- runtime.function("test", Vec::new());
- let val = Ret([7, 14, 21, 28, 35, 42, 49, 56, 63, 70]);
- assert_eq!(runtime.output(), val.encode());
- }
- #[test]
- fn struct_in_array() {
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct S(u64, bool);
- let mut runtime = build_solidity(
- r##"
- struct S {
- uint64 f1;
- bool f2;
- }
- contract foo {
- S[] store;
- function set(S[] memory n) public {
- store = n;
- }
- function copy() public returns (S[] memory) {
- return store;
- }
- }"##,
- );
- let val = vec![S(102, true), S(u64::MAX, false)];
- runtime.function("set", val.encode());
- runtime.function("copy", vec![]);
- assert_eq!(runtime.output(), val.encode());
- }
- #[test]
- fn struct_in_fixed_array() {
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct S(u64, bool);
- let mut runtime = build_solidity(
- r##"
- struct S {
- uint64 f1;
- bool f2;
- }
- contract foo {
- S[2] store;
- function set(S[2] memory n) public {
- store = n;
- }
- function copy() public returns (S[2] memory) {
- return store;
- }
- }"##,
- );
- let val = [S(102, true), S(u64::MAX, false)];
- runtime.function("set", val.encode());
- runtime.function("copy", vec![]);
- assert_eq!(runtime.output(), val.encode());
- }
- #[test]
- fn struct_array_struct() {
- let mut runtime = build_solidity(
- r##"
- contract flipper {
- struct bool_struct {
- bool foo_bool;
- }
- struct struct_bool_struct_array {
- bool_struct[1] foo_struct_array;
- }
- function get_memory() public pure returns (bool) {
- bool_struct memory foo = bool_struct({foo_bool: true});
- bool_struct[1] memory foo_array = [foo];
- struct_bool_struct_array memory foo_struct = struct_bool_struct_array({foo_struct_array: foo_array});
- /* return foo_array[0].foo_bool; */
- return foo_struct.foo_struct_array[0].foo_bool;
- }
- }"##,
- );
- runtime.function("get_memory", Vec::new());
- assert_eq!(runtime.output(), true.encode());
- }
- #[test]
- fn struct_array_struct_abi() {
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct Foo {
- f1: u32,
- f2: bool,
- }
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct Bar {
- bars: [Foo; 10],
- }
- let mut runtime = build_solidity(
- r##"
- contract flipper {
- struct foo {
- int32 f1;
- bool f2;
- }
- struct bar {
- foo[10] bars;
- }
- bar s;
- function get_bar() public returns (bar) {
- bar memory a = bar({ bars: [
- foo({ f1: 1, f2: true}),
- foo({ f1: 2, f2: true}),
- foo({ f1: 3, f2: true}),
- foo({ f1: 4, f2: true}),
- foo({ f1: 5, f2: true}),
- foo({ f1: 6, f2: true}),
- foo({ f1: 7, f2: false}),
- foo({ f1: 8, f2: true}),
- foo({ f1: 9, f2: true}),
- foo({ f1: 10, f2: true})
- ]});
- s = a;
- return a;
- }
- function set_bar(bar memory a) public {
- for (uint32 i = 0; i < 10; i++) {
- assert(a.bars[i].f1 == i + 1);
- assert(a.bars[i].f2 == (i != 6));
- }
- a = s;
- }
- }"##,
- );
- let b = Bar {
- bars: [
- Foo { f1: 1, f2: true },
- Foo { f1: 2, f2: true },
- Foo { f1: 3, f2: true },
- Foo { f1: 4, f2: true },
- Foo { f1: 5, f2: true },
- Foo { f1: 6, f2: true },
- Foo { f1: 7, f2: false },
- Foo { f1: 8, f2: true },
- Foo { f1: 9, f2: true },
- Foo { f1: 10, f2: true },
- ],
- };
- runtime.function("get_bar", Vec::new());
- assert_eq!(runtime.output(), b.encode());
- runtime.function("set_bar", b.encode());
- }
- #[test]
- fn memory_dynamic_array_new() {
- let mut runtime = build_solidity(
- r#"
- contract foo {
- function test() public {
- int32[] memory a = new int32[](5);
- assert(a.length == 5);
- }
- }"#,
- );
- runtime.function("test", Vec::new());
- // The Ethereum Foundation solc allows you to create arrays of length 0
- let mut runtime = build_solidity(
- r#"
- contract foo {
- function test() public {
- bool[] memory a = new bool[](0);
- assert(a.length == 0);
- }
- }"#,
- );
- runtime.function("test", Vec::new());
- }
- #[test]
- fn memory_dynamic_array_deref() {
- // The Ethereum Foundation solc allows you to create arrays of length 0
- let mut runtime = build_solidity(
- r#"
- contract foo {
- function test() public {
- int32[] memory a = new int32[](5);
- assert(a.length == 5);
- a[0] = 102;
- a[1] = -5;
- a[4] = 0x7cafeeed;
- assert(a[0] == 102);
- assert(a[1] == -5);
- assert(a[4] == 0x7cafeeed);
- }
- }"#,
- );
- runtime.function("test", Vec::new());
- }
- #[test]
- fn array_bounds_dynamic_array() {
- let mut runtime = build_solidity(
- r#"
- contract foo {
- function test() public returns (int32) {
- int32[] memory a = new int32[](5);
- a[5] = 102;
- return a[3];
- }
- }"#,
- );
- runtime.function_expect_failure("test", Vec::new());
- }
- #[test]
- fn empty_array_bounds_dynamic_array() {
- let mut runtime = build_solidity(
- r#"
- contract foo {
- function test() public returns (bytes32) {
- bytes32[] memory a = new bytes32[](0);
- a[0] = "yo";
- return a[0];
- }
- }"#,
- );
- runtime.function_expect_failure("test", Vec::new());
- }
- #[test]
- fn memory_dynamic_array_types_call_return() {
- let mut runtime = build_solidity(
- r#"
- contract foo {
- function a(bool cond) public returns (bytes27[]) {
- bytes27[] foo;
- foo = new bytes27[](5);
- foo[1] = "cond was true";
- return foo;
- }
- function b(bytes27[] x) private {
- x[1] = "b was called";
- x = new bytes27[](3);
- x[1] = "should not be";
- }
- function test() public {
- bytes27[] x = a(true);
- assert(x.length == 5);
- assert(x[1] == "cond was true");
- b(x);
- assert(x.length == 5);
- assert(x[1] == "b was called");
- }
- }"#,
- );
- runtime.function("test", Vec::new());
- }
- #[test]
- fn dynamic_arrays_need_phi_node() {
- let mut runtime = build_solidity(
- r#"
- pragma solidity 0;
- contract foo {
- enum bar { bar1, bar2, bar3, bar4 }
- function a(bool cond) public returns (bar[] memory) {
- bar[] memory foo;
- if (cond) {
- foo = new bar[](5);
- foo[1] = bar.bar2;
- } else {
- foo = new bar[](3);
- foo[1] = bar.bar3;
- }
- return foo;
- }
- function test() public {
- bar[] memory x = a(true);
- assert(x.length == 5);
- assert(x[1] == bar.bar2);
- x = a(false);
- assert(x.length == 3);
- assert(x[1] == bar.bar3);
- }
- }"#,
- );
- runtime.function("test", Vec::new());
- }
- // test:
- // alignment of array elements
- // arrays of other structs/arrays/darrays
- // nil pointer should fail
- // copy to/from storage <=> memory
- // abi encode/decode
- // string/bytes
- #[test]
- fn storage_dynamic_array_length() {
- let mut runtime = build_solidity(
- r#"
- pragma solidity 0;
- contract foo {
- int32[] bar;
- function test() public {
- assert(bar.length == 0);
- }
- }"#,
- );
- runtime.function("test", Vec::new());
- }
- #[test]
- fn dynamic_array_push() {
- let mut runtime = build_solidity(
- r#"
- pragma solidity 0;
- contract foo {
- function test() public {
- int[] bar = (new int[])(1);
- bar[0] = 128;
- bar.push(64);
- assert(bar.length == 2);
- assert(bar[1] == 64);
- }
- }
- "#,
- );
- runtime.function("test", Vec::new());
- let mut runtime = build_solidity(
- r#"
- pragma solidity 0;
- contract foo {
- function test() public {
- bytes bar = (new bytes)(1);
- bar[0] = 128;
- bar.push(64);
- assert(bar.length == 2);
- assert(bar[1] == 64);
- }
- }
- "#,
- );
- runtime.function("test", Vec::new());
- let mut runtime = build_solidity(
- r#"
- pragma solidity 0;
- contract foo {
- struct s {
- int32 f1;
- bool f2;
- }
- function test() public {
- s[] bar = new s[](1);
- bar[0] = s({f1: 0, f2: false});
- bar.push(s({f1: 1, f2: true}));
- assert(bar.length == 2);
- assert(bar[1].f1 == 1);
- assert(bar[1].f2 == true);
- }
- }
- "#,
- );
- runtime.function("test", Vec::new());
- let mut runtime = build_solidity(
- r#"
- pragma solidity 0;
- contract foo {
- enum enum1 { val1, val2, val3 }
- function test() public {
- enum1[] bar = new enum1[](1);
- bar[0] = enum1.val1;
- bar.push(enum1.val2);
- assert(bar.length == 2);
- assert(bar[1] == enum1.val2);
- }
- }
- "#,
- );
- runtime.function("test", Vec::new());
- // push() returns a reference to the thing
- let mut runtime = build_solidity(
- r#"
- pragma solidity 0;
- contract foo {
- struct s {
- int32 f1;
- bool f2;
- }
- function test() public {
- s[] bar = new s[](0);
- s memory n = bar.push();
- n.f1 = 102;
- n.f2 = true;
- assert(bar[0].f1 == 102);
- assert(bar[0].f2 == true);
- }
- }"#,
- );
- runtime.function("test", Vec::new());
- }
- #[test]
- fn dynamic_array_pop() {
- let mut runtime = build_solidity(
- r#"
- pragma solidity 0;
- contract foo {
- function test() public {
- int[] bar = new int[](1);
- bar[0] = 128;
- assert(bar.length == 1);
- assert(128 == bar.pop());
- assert(bar.length == 0);
- }
- }
- "#,
- );
- runtime.function("test", Vec::new());
- let mut runtime = build_solidity(
- r#"
- pragma solidity 0;
- contract foo {
- function test() public {
- bytes bar = new bytes(1);
- bar[0] = 128;
- assert(bar.length == 1);
- assert(128 == bar.pop());
- assert(bar.length == 0);
- }
- }
- "#,
- );
- runtime.function("test", Vec::new());
- let mut runtime = build_solidity(
- r#"
- pragma solidity 0;
- contract foo {
- struct s {
- int32 f1;
- bool f2;
- }
- function test() public {
- s[] bar = new s[](1);
- bar[0] = s(128, true);
- assert(bar.length == 1);
- s baz = bar.pop();
- assert(baz.f1 == 128);
- assert(baz.f2 == true);
- assert(bar.length == 0);
- }
- }
- "#,
- );
- runtime.function("test", Vec::new());
- let mut runtime = build_solidity(
- r#"
- pragma solidity 0;
- contract foo {
- enum enum1 { val1, val2, val3 }
- function test() public {
- enum1[] bar = new enum1[](1);
- bar[0] = enum1.val2;
- assert(bar.length == 1);
- assert(enum1.val2 == bar.pop());
- assert(bar.length == 0);
- }
- }
- "#,
- );
- runtime.function("test", Vec::new());
- }
- #[test]
- fn dynamic_array_pop_empty_array() {
- let mut runtime = build_solidity(
- r#"
- pragma solidity 0;
- contract foo {
- function test() public returns (int) {
- int[] bar = new int[](0);
- return bar.pop();
- }
- }"#,
- );
- runtime.function_expect_failure("test", Vec::new());
- }
- #[test]
- fn dynamic_array_pop_bounds() {
- let mut runtime = build_solidity(
- r#"
- pragma solidity 0;
- contract foo {
- function test() public {
- int[] bar = new int[](1);
- bar[0] = 12;
- bar.pop();
- assert(bar[0] == 12);
- }
- }"#,
- );
- runtime.function_expect_failure("test", Vec::new());
- }
- #[test]
- fn storage_dynamic_array_push() {
- let mut runtime = build_solidity(
- r#"
- pragma solidity 0;
- contract foo {
- int32[] bar;
- function test() public {
- assert(bar.length == 0);
- bar.push(102);
- assert(bar[0] == 102);
- assert(bar.length == 1);
- bar.push();
- assert(bar[1] == 0);
- assert(bar.length == 2);
- }
- }"#,
- );
- runtime.function("test", Vec::new());
- // push() returns a reference to the thing
- let mut runtime = build_solidity(
- r#"
- pragma solidity 0;
- contract foo {
- struct s {
- int32 f1;
- bool f2;
- }
- s[] bar;
- function test() public {
- s storage n = bar.push();
- n.f1 = 102;
- n.f2 = true;
- assert(bar[0].f1 == 102);
- }
- }"#,
- );
- runtime.function("test", Vec::new());
- }
- #[test]
- fn storage_dynamic_array_pop() {
- let mut runtime = build_solidity(
- r#"
- pragma solidity 0;
- contract foo {
- int32[] bar;
- function test() public {
- assert(bar.length == 0);
- bar.push(102);
- assert(bar[0] == 102);
- assert(bar.length == 1);
- int32 v = bar.pop();
- assert(bar.length == 0);
- assert(v == 102);
- }
- }"#,
- );
- runtime.function("test", Vec::new());
- // We should have one entry for the length; pop should have removed the 102 entry
- assert_eq!(runtime.storage().len(), 1);
- // ensure that structs and fixed arrays are wiped by pop
- let mut runtime = build_solidity(
- r#"
- pragma solidity 0;
- contract foo {
- enum enum1 { val1, val2, val3 }
- struct s {
- bool f1;
- bytes3 f2;
- enum1 f3;
- uint64 f4;
- int64[2] f5;
- }
- s[] bar;
- function test() public {
- s storage first = bar.push();
- first.f1 = true;
- first.f2 = "abc";
- first.f3 = enum1.val2;
- first.f4 = 65536;
- first.f5[0] = -1;
- first.f5[1] = 5;
- assert(bar[0].f5[1] == 5);
- // now erase it again
- bar.pop();
- }
- }"#,
- );
- runtime.function("test", Vec::new());
- // We should have one entry for the length; pop should have removed the 102 entry
- assert_eq!(runtime.storage().len(), 1);
- }
- #[test]
- fn storage_delete() {
- // ensure that structs and fixed arrays are wiped by pop
- let mut runtime = build_solidity(
- r#"
- pragma solidity 0;
- contract foo {
- uint64 bar;
- function test() public {
- bar = 0xdeaddeaddeaddead;
- delete bar;
- }
- }"#,
- );
- runtime.function("test", Vec::new());
- // We should have one entry for the length; pop should have removed the 102 entry
- assert!(runtime.storage().is_empty());
- // ensure that structs and fixed arrays are wiped by delete
- let mut runtime = build_solidity(
- r#"
- pragma solidity 0;
- contract foo {
- enum enum1 { val1, val2, val3 }
- struct s {
- bool f1;
- bytes3 f2;
- enum1 f3;
- uint64 f4;
- int64[2] f5;
- }
- s[] bar;
- function test() public {
- s storage first = bar.push();
- first.f1 = true;
- first.f2 = "abc";
- first.f3 = enum1.val2;
- first.f4 = 65536;
- first.f5[0] = -1;
- first.f5[1] = 5;
- assert(bar[0].f5[1] == 5);
- // now erase it again
- delete bar[0];
- }
- }"#,
- );
- runtime.function("test", Vec::new());
- // We should have one entry for the length; delete should have removed the entry
- assert_eq!(runtime.storage().len(), 1);
- // ensure that structs and fixed arrays are wiped by delete
- let mut runtime = build_solidity(
- r#"
- pragma solidity 0;
- contract foo {
- int[] bar;
- function setup() public {
- bar.push(102);
- bar.push(305);
- }
- function clear() public {
- delete bar;
- }
- }"#,
- );
- runtime.function("setup", Vec::new());
- assert_eq!(runtime.storage().len(), 3);
- runtime.function("clear", Vec::new());
- assert_eq!(runtime.storage().len(), 0);
- // our delete operator has to iterate over the dynamic array. Ensure it works if the array is empty
- runtime.function("clear", Vec::new());
- assert_eq!(runtime.storage().len(), 0);
- }
- #[test]
- fn storage_dynamic_copy() {
- let mut runtime = build_solidity(
- r#"
- contract c {
- int32[] foo;
- constructor() public {
- for (int32 i = 0; i <11; i++) {
- foo.push(i * 3);
- }
- }
- function storage_to_memory() view public {
- int32[] memory bar = foo;
- assert(bar.length == 11);
- for (int32 i = 0; i <11; i++) {
- assert(bar[uint32(i)] == i * 3);
- }
- }
- function memory_to_storage() public {
- int32[] memory bar = new int32[](5);
- for (int32 i = 0; i < 5; i++) {
- bar[uint32(i)] = 5 * (i + 7);
- }
- foo = bar;
- assert(foo.length == 5);
- for (int32 i = 0; i < 5; i++) {
- assert(foo[uint32(i)] == 5 * (i + 7));
- }
- }
- }
- "#,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("storage_to_memory", Vec::new());
- runtime.function("memory_to_storage", Vec::new());
- assert_eq!(runtime.storage().len(), 6);
- }
- #[test]
- fn abi_encode_dynamic_array() {
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct Int32Array(Vec<i32>);
- let mut runtime = build_solidity(
- r#"
- contract c {
- function encode() pure public returns (int32[]) {
- int32[] memory bar = new int32[](11);
- for (int32 i = 0; i <11; i++) {
- bar[uint32(i)] = i * 3;
- }
- return bar;
- }
- }
- "#,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("encode", Vec::new());
- assert_eq!(
- runtime.output(),
- Int32Array(vec!(0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30)).encode()
- );
- }
- #[test]
- fn abi_decode_dynamic_array() {
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct Int32Array(Vec<i32>);
- let mut runtime = build_solidity(
- r#"
- contract c {
- function decode(int32[] bar) pure public {
- assert(bar.length == 11);
- for (int32 i = 0; i <11; i++) {
- assert(bar[uint32(i)] == i * 3);
- }
- }
- function decode_empty(int32[] bar) pure public {
- assert(bar.length == 0);
- }
- }
- "#,
- );
- runtime.constructor(0, Vec::new());
- runtime.function(
- "decode",
- Int32Array(vec![0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30]).encode(),
- );
- runtime.function("decode_empty", Int32Array(vec![]).encode());
- }
- #[test]
- fn abi_encode_dynamic_array2() {
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct Array(Vec<(bool, u32)>);
- let mut runtime = build_solidity(
- r#"
- contract structs {
- struct foo {
- bool x;
- uint32 y;
- }
- function test() public returns (foo[]) {
- foo[] x = new foo[](3);
- x[0] = foo({x: true, y: 64});
- x[1] = foo({x: false, y: 102});
- x[2] = foo({x: true, y: 0x800});
- return x;
- }
- }
- "#,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("test", Vec::new());
- assert_eq!(
- runtime.output(),
- Array(vec!((true, 64), (false, 102), (true, 0x800))).encode()
- );
- }
- #[test]
- fn abi_encode_dynamic_array3() {
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct Array(Vec<(bool, u32, String)>);
- let mut runtime = build_solidity(
- r#"
- contract structs {
- struct foo {
- bool x;
- uint32 y;
- string z;
- }
- function test() public returns (foo[]) {
- foo[] x = new foo[](3);
- x[0] = foo({x: true, y: 64, z: "abc"});
- x[1] = foo({x: false, y: 102, z: "a"});
- x[2] = foo({x: true, y: 0x800, z: "abcdef"});
- return x;
- }
- }
- "#,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("test", Vec::new());
- assert_eq!(
- runtime.output(),
- Array(vec!(
- (true, 64, "abc".to_owned()),
- (false, 102, "a".to_owned()),
- (true, 0x800, "abcdef".to_owned())
- ))
- .encode()
- );
- }
- #[test]
- fn abi_encode_dynamic_array4() {
- #[derive(Debug, PartialEq, Eq, Encode, Decode)]
- struct Array([(bool, u32, String); 3]);
- let mut runtime = build_solidity(
- r#"
- contract structs {
- struct foo {
- bool x;
- uint32 y;
- string z;
- }
- function test() public returns (foo[3]) {
- foo[3] x;
- x[0] = foo({x: true, y: 64, z: "abc"});
- x[1] = foo({x: false, y: 102, z: "a"});
- x[2] = foo({x: true, y: 0x800, z: "abcdef"});
- return x;
- }
- }
- "#,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("test", Vec::new());
- runtime.heap_verify();
- assert_eq!(
- runtime.output(),
- Array([
- (true, 64, "abc".to_owned()),
- (false, 102, "a".to_owned()),
- (true, 0x800, "abcdef".to_owned())
- ])
- .encode()
- );
- }
- #[test]
- fn abi_encode_dynamic_array5() {
- let uint_str_0 = "201f1e1d1c1b1a191817161514131211100f0e0d0c0b0a090807060504030201";
- let uint_val_0 = U256::from_str_radix(uint_str_0, 16).unwrap();
- let uint_str_1 = "dbb85cf1ffefee258d1bdf7bc573838c30a6bc5a64fdc5c708280f56e4972fe7";
- let uint_val_1 = U256::from_str_radix(uint_str_1, 16).unwrap();
- let mut runtime = build_solidity(&format!(
- "contract Test {{
- function test() external pure returns (uint256[] memory _amounts) {{
- _amounts = new uint256[](2);
- _amounts[0] = 0x{uint_str_0};
- _amounts[1] = 0x{uint_str_1};
- }}
- }}"
- ));
- runtime.function("test", Vec::new());
- assert_eq!(runtime.output(), vec![uint_val_0, uint_val_1].encode());
- }
- #[test]
- fn abi_encode_dynamic_array6() {
- let mut runtime = build_solidity(
- r#"contract Test {
- function test(uint256[] _init) external pure returns (uint256[] memory _amounts) {
- _amounts = new uint256[](_init.length);
- for (uint i = 0; i<_init.length; i++) {
- _amounts[i] = _init[i];
- }
- }
- }"#,
- );
- let max_array_length = 128;
- let mut r = rand::thread_rng();
- let mut values = Vec::with_capacity(max_array_length);
- for _ in 0..max_array_length {
- let val = U256::from_big_endian(&r.gen::<[u8; 32]>());
- println!("{val}");
- values.push(val);
- let identity = values.encode();
- runtime.function("test", identity.to_vec());
- assert_eq!(runtime.output(), identity);
- }
- }
- #[test]
- fn large_index_ty_in_bounds() {
- let mut runtime = build_solidity(
- r#"
- contract foo {
- uint128 storage_index;
- function test(uint128 index) public returns (uint16) {
- uint16[] memory a = new uint16[](16);
- storage_index = index;
- return a[storage_index];
- }
- }"#,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("test", 15u128.encode());
- runtime.function_expect_failure("test", 17u128.encode());
- runtime.function_expect_failure("test", 0xfffffffffffffu128.encode());
- }
- #[test]
- fn alloc_size_from_storage() {
- let mut runtime = build_solidity(
- r#"
- contract Test {
- uint32 length = 1;
- function contfunc() public view returns (uint64[] memory) {
- uint64[] memory values = new uint64[](length);
- return values;
- }
- }"#,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("contfunc", Vec::new());
- assert_eq!(runtime.output(), vec![0u64].encode());
- }
- #[test]
- fn fixed_bytes() {
- let mut runtime = build_solidity(
- r#"
- contract Storage {
- bytes32[] data;
- constructor() {
- data.push(hex"0000000000000000000000000000000000000000000000000000000000000000");
- data.push(hex"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
- }
- function uploadData(uint8 i, uint8 j) public view returns(bytes1) {
- return(data[j][i]);
- }
- }
- "#,
- );
- runtime.constructor(0, vec![]);
- for i in 0..32u8 {
- runtime.function("uploadData", vec![i, 0]);
- assert_eq!(runtime.output()[..], [0]);
- runtime.function("uploadData", vec![i, 1]);
- assert_eq!(runtime.output()[..], [i]);
- }
- let mut runtime = build_solidity(
- r#"
- contract Memory {
- constructor() {
- }
- function uploadData(uint8 i, uint8 j) public view returns(bytes1) {
- bytes32[] data = new bytes32[](2);
- data[0] = hex"0000000000000000000000000000000000000000000000000000000000000000";
- data[1] = hex"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
- return(data[j][i]);
- }
- }
- "#,
- );
- runtime.constructor(0, vec![]);
- for i in 0..32u8 {
- runtime.function("uploadData", vec![i, 0]);
- assert_eq!(runtime.output()[..], [0]);
- runtime.function("uploadData", vec![i, 1]);
- assert_eq!(runtime.output()[..], [i]);
- }
- }
- #[test]
- fn abi_decode_dynamic_array2() {
- let mut runtime = build_solidity(
- r#"
- contract c {
- function decode() pure public {
- bytes enc = hex"2c000000000300000006000000090000000c0000000f0000001200000015000000180000001b0000001e000000";
- int32[] bar = abi.decode(enc, (int32[]));
- assert(bar.length == 11);
- for (int32 i = 0; i <11; i++) {
- assert(bar[uint32(i)] == i * 3);
- }
- }
- function decode_empty() pure public {
- bytes enc = hex"00";
- int32[] bar = abi.decode(enc, (int32[]));
- assert(bar.length == 0);
- }
- }
- "#,
- );
- runtime.function("decode", vec![]);
- runtime.function("decode_empty", vec![]);
- }
- #[test]
- fn abi_decode_dynamic_array3() {
- let mut runtime = build_solidity(
- r#"
- contract Arr {
- function decode() pure public {
- bytes enc = hex"14140001020304140102030405140203040506140304050607140405060708";
- uint8[][] bar = abi.decode(enc, (uint8[][]));
- assert(bar.length == 5);
-
- for (uint8 i = 0; i <5; i++) {
- for (uint8 j = 0; j <5; j++) {
- assert(bar[uint32(i)][uint32(j)] == i + j);
- }
- }
- }
-
- function decode_empty() pure public {
- bytes enc = hex"00";
- uint8[][] bar = abi.decode(enc, (uint8[][]));
- assert(bar.length == 0);
- }
- }
- "#,
- );
- // The array in the first function was generated like this:
- // let mut matrix = vec![];
- // for i in 0..5 {
- // matrix.push(vec![]);
- // for j in 0..5 {
- // matrix[i].push((i + j) as u8);
- // }
- // }
- runtime.function("decode", vec![]);
- runtime.function("decode_empty", vec![]);
- }
|