Skip to content

Commit beecabe

Browse files
committed
Step 11 - Rules of Assignment and Data Location
1 parent b218a63 commit beecabe

File tree

8 files changed

+335
-0
lines changed

8 files changed

+335
-0
lines changed

11_rules_of_datatypes/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Rules of Assignment and Data Location
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
pragma solidity ^0.4.23;
2+
contract Contract1 {
3+
4+
//Rule 1
5+
// State Variables are storage variables
6+
int public stateVar1 = 5;
7+
8+
// Rule 2 Starts
9+
int public stateVar2 = 5;
10+
int[3] public stateRefVar3 = [int(3),4,5];
11+
// function parameter are memory data variables
12+
function changeMemoryValueType(int a) public returns (int) {
13+
a = stateVar2;
14+
// changing a will not change stateVar2
15+
a = 6;
16+
return stateVar2;
17+
}
18+
19+
// Rule 2
20+
// function ref parameter are also memory data variables
21+
function changeMemoryRefType(int[3] a) public returns (int) {
22+
a = stateRefVar3;
23+
a[1] = 45;
24+
return stateRefVar3[1];
25+
}
26+
27+
// Rule 3 Ends
28+
29+
// Rule 3 starts
30+
int[2] public rule3StateRefVar = [int(1),2];
31+
function demoRule3RefAndVal1 () public {
32+
int a = 5; // Default to memoery
33+
34+
// Default to Storage but this will not work because it needs values
35+
// from storage data type which can come from state variable
36+
//int[2] arr = [int(1),2];
37+
38+
// b is default to storage in this case and need state variable
39+
// changs in value of b will change rule3StateRefVar
40+
// changs in value of rule3StateRefVar will change b
41+
int[2] b = rule3StateRefVar;
42+
43+
// changs in value of d will NOT change rule3StateRefVar
44+
// changs in value of rule3StateRefVar will NOT change d
45+
int[2] memory d = rule3StateRefVar;
46+
47+
// overriding default form memory to storage
48+
// if you want to use ref type then you have you use memory keyword
49+
int[3] memory c = [int(11),12,13];
50+
51+
b[1] = 5; // rule3StateRefVar[1] also changed to 5
52+
d[0] = 18; // rule3StateRefVar[0] will NOT change and stay 1
53+
}
54+
55+
function demoRule3RefAndVal2 () public returns (string) {
56+
57+
// Default to Storage but this will not work because it needs values
58+
// from storage data type which can come from state variable
59+
//string a = "Hello World";
60+
61+
// To make it work you need to provide memory keyword
62+
string memory a = "Hello World";
63+
64+
// Value type can not be overridden to stroage they can only be memory
65+
//int storage b = 45;
66+
67+
return a;
68+
69+
}
70+
71+
mapping(int => string) keyValueList;
72+
function demoRule3RefAndVal3 () public {
73+
74+
// local mapping variable must use state variable
75+
// it can not be declared locally
76+
mapping(int => string) keyValueListLocal = keyValueList;
77+
keyValueListLocal[23] = "Hello";
78+
}
79+
80+
// Rule 3 ends
81+
82+
// Rule 4 does not have example
83+
84+
// Rule 5 starts
85+
int public rule5StateVar1 = 10;
86+
int public rule5StateVar2 = 20;
87+
function demoStorageToStorageValueAssignment1() public returns (int) {
88+
89+
// Assignment of value type from storage variable to another storage
90+
// variable creates copy so both maintain separate copy of data
91+
rule5StateVar1 = rule5StateVar2;
92+
rule5StateVar2 = 60;
93+
94+
return rule5StateVar1;
95+
}
96+
97+
int[2] rule5StateVar3 = [int(1),2];
98+
int[2] rule5StateVar4 = [int(3),4];
99+
function demoStorageToStorageRefAssignment2() public returns (int) {
100+
101+
102+
// This is diffrent from almost all programming languages
103+
// as in other programming languages assignment of referance
104+
// variable to another referance variable create referance and
105+
// both variable points to same object
106+
// BUT here in solidity it creates copy of ref variable and both
107+
// points to different object
108+
rule5StateVar3 = rule5StateVar4;
109+
rule5StateVar4[1] = 10;
110+
111+
return rule5StateVar3[1]; // returns 4
112+
}
113+
114+
// Rule 5 ends
115+
116+
117+
118+
// Rule 6 starts
119+
int public rule6StateVar1 = 10;
120+
function demoMemoryToStorageValueAssignment1() public returns (int) {
121+
// Assignment from memory variable to storage variable creates copy
122+
// so both maintain separate copy of data
123+
int rule6LocalVar = 20;
124+
rule6StateVar1 = rule6LocalVar;
125+
rule6LocalVar = 60;
126+
127+
return rule6StateVar1; // return 20
128+
}
129+
130+
int[2] rule6StateVar2 = [int(1),2];
131+
function demoMemoryToStorageRefAssignment2() public returns (int) {
132+
// Assignment from memory variable to storage variable creates copy
133+
// so both maintain separate copy of data
134+
135+
// This is also diffrent from other programming languages
136+
// In other language it creates referance and both variable points
137+
// to same data
138+
// BUT here both points to separate copy of data
139+
int[2] memory rule6LocalVar1 = [int(3),4];
140+
141+
rule6StateVar2 = rule6LocalVar1;
142+
rule6LocalVar1[1] = 10;
143+
144+
return rule6StateVar2[1]; // returns 4
145+
}
146+
147+
// Rule 6 ends
148+
149+
150+
// Rule 7 starts -- Nothing special same as rule 6 and 5
151+
int public rule7StateVar1 = 10;
152+
function demoStorageToMemoryValueAssignment1() public returns (int) {
153+
// Assignment from storage variable to memory variable creates copy
154+
// so both maintain separate copy of data
155+
int rule7LocalVar = 20;
156+
rule7LocalVar = rule7StateVar1;
157+
rule7StateVar1 = 60;
158+
159+
return rule7LocalVar; // return 10
160+
}
161+
162+
int[2] rule7StateVar2 = [int(1),2];
163+
function demoStorageToMemoryRefAssignment2() public returns (int) {
164+
// Assignment from storage variable to memory variable creates copy
165+
// so both maintain separate copy of data
166+
167+
// This is also diffrent from other programming languages
168+
// In other language it creates referance and both variable points
169+
// to same data
170+
// BUT here both points to separate copy of data
171+
int[2] memory rule7LocalVar1 = [int(3),4];
172+
173+
rule7LocalVar1 = rule7StateVar2;
174+
rule7StateVar2[1] = 10;
175+
176+
return rule7LocalVar1[1]; // returns 2
177+
}
178+
179+
// Rule 7 ends
180+
181+
182+
183+
// Rule 8 starts -- This also same as others but the difference is
184+
// assignment of memory to memory of referance type DO NOT create copy
185+
186+
function demoMemoryToMemoryValueAssignment1() public returns (int) {
187+
// Assignment from memory variable to memory variable creates copy
188+
// so both maintain separate copy of data
189+
int rule8LocalVar1 = 20;
190+
int rule8LocalVar2 = 30;
191+
rule8LocalVar1 = rule8LocalVar2;
192+
rule8LocalVar2 = 60;
193+
194+
return rule8LocalVar1; // return 30
195+
}
196+
197+
function demoMemoryToMemoryRefAssignment2() public returns (int) {
198+
// Assignment from memory variable to memory for referance variable
199+
// DO NOT creates copy so both have same data
200+
201+
int[2] memory rule8LocalVar1 = [int(1),2];
202+
int[2] memory rule8LocalVar2 = [int(3),4];
203+
204+
// This works are ref type, changes in one will change other
205+
rule8LocalVar1 = rule8LocalVar2;
206+
rule8LocalVar2[1] = 10;
207+
208+
return rule8LocalVar1[1]; // returns 10
209+
}
210+
211+
// Rule 8 ends
212+
213+
214+
215+
216+
217+
218+
219+
220+
221+
222+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
pragma solidity ^0.4.23;
2+
3+
contract Migrations {
4+
address public owner;
5+
uint public last_completed_migration;
6+
7+
8+
constructor() public {
9+
owner = msg.sender;
10+
}
11+
12+
modifier restricted() {
13+
if (msg.sender == owner) _;
14+
}
15+
16+
function setCompleted(uint completed) public restricted {
17+
last_completed_migration = completed;
18+
}
19+
20+
function upgrade(address new_address) public restricted {
21+
Migrations upgraded = Migrations(new_address);
22+
upgraded.setCompleted(last_completed_migration);
23+
}
24+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var Migrations = artifacts.require("./Migrations.sol");
2+
3+
module.exports = function(deployer) {
4+
deployer.deploy(Migrations);
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var contract1 = artifacts.require("./Contract1.sol");
2+
3+
module.exports = function(deployer) {
4+
deployer.deploy(contract1);
5+
};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
pragma solidity ^0.4.24;
2+
import "truffle/Assert.sol";
3+
import "truffle/DeployedAddresses.sol";
4+
import "../contracts/Contract1.sol";
5+
6+
contract TestContract1 {
7+
8+
//Rule 2
9+
int[3] list = [int(21),22,23];
10+
function testRule2() public {
11+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
12+
13+
int localVal = 45;
14+
int a = meta.changeMemoryValueType(localVal);
15+
int b = meta.changeMemoryRefType(list);
16+
17+
Assert.equal(localVal,45,"localVal should be 45");
18+
Assert.equal(list[1],22,"List 1 should be 22");
19+
Assert.equal(a,5,"A should be 5");
20+
Assert.equal(b,4,"B should be 4");
21+
}
22+
23+
//Rule 5
24+
function testRule5() public {
25+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
26+
int a = meta.demoStorageToStorageValueAssignment1();
27+
int b = meta.demoStorageToStorageRefAssignment2();
28+
Assert.equal(a,20,"a should be 20");
29+
Assert.equal(b,4,"a should be 4");
30+
31+
}
32+
33+
//Rule 6
34+
function testRule6() public {
35+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
36+
int a = meta.demoMemoryToStorageValueAssignment1();
37+
int b = meta.demoMemoryToStorageRefAssignment2();
38+
Assert.equal(a,20,"a should be 20");
39+
Assert.equal(b,4,"a should be 4");
40+
41+
}
42+
43+
//Rule 7
44+
function testRule7() public {
45+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
46+
int a = meta.demoStorageToMemoryValueAssignment1();
47+
int b = meta.demoStorageToMemoryRefAssignment2();
48+
Assert.equal(a,10,"a should be 10");
49+
Assert.equal(b,2,"a should be 2");
50+
51+
}
52+
53+
//Rule 8
54+
function testRule8() public {
55+
Contract1 meta = Contract1(DeployedAddresses.Contract1());
56+
int a = meta.demoMemoryToMemoryValueAssignment1();
57+
int b = meta.demoMemoryToMemoryRefAssignment2();
58+
Assert.equal(a,30,"a should be 30");
59+
Assert.equal(b,10,"a should be 10");
60+
61+
}
62+
63+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
// See <http://truffleframework.com/docs/advanced/configuration>
3+
// to customize your Truffle configuration!
4+
};

11_rules_of_datatypes/truffle.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
// See <http://truffleframework.com/docs/advanced/configuration>
3+
// to customize your Truffle configuration!
4+
networks: {
5+
development: {
6+
host: "127.0.0.1",
7+
port: 8545,
8+
network_id: "*" // Match any network id
9+
}
10+
}
11+
};

0 commit comments

Comments
 (0)