Skip to content

Commit

Permalink
notes
Browse files Browse the repository at this point in the history
  • Loading branch information
tomtomjhj committed Feb 15, 2019
1 parent 37dc2aa commit 398875b
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 51 deletions.
99 changes: 55 additions & 44 deletions tutorial/Coin.evm
Expand Up @@ -385,7 +385,7 @@ sub_0: assembly {
/* "--CODEGEN--":2:4 */
iszero
tag_28
jumpi // (cds-4) 4 tag
jumpi // (cds-4) 4 tag_27
/* "--CODEGEN--":29:30 */
0x0
/* "--CODEGEN--":26:27 */
Expand All @@ -400,7 +400,7 @@ sub_0: assembly {
swap1 // 4 cds tag
dup1
dup1 // 4 4 4 cds tag
calldataload // cd[4:36](offset) 4 4 cds tag
calldataload // cd[4:36](offset=32) 4 4 cds tag
swap1 // 4 offset 4 cds tag
0x20 //
add // 36 offset 4 cds tag
Expand Down Expand Up @@ -450,7 +450,7 @@ sub_0: assembly {
tag_30:
/* "Coin.sol":1567:1719 function addOwners(address[] memory _owners) public {... */
dup1 // (4+offset) (4+offset) 36 4 cds tag
calldataload // cd[4+offset:4+offset+32] (4+offset) 36 4 cds tag
calldataload // cd[4+offset:4+offset+32](length of _owners) (4+offset) 36 4 cds tag
swap1 // (4+offset) len 36 4 cds tag
0x20
add // (4+offset+32) len 36 4 cds tag
Expand Down Expand Up @@ -502,8 +502,9 @@ sub_0: assembly {
swap1 // size NEXT_LOC len len (4+offset+32) 36 4 cds tag
dup2 // NEXT_LOC size NEXT_LOC len len (4+offset+32) 36 4 cds tag
add // (NEXT_LOC+size) NEXT_LOC len len (4+offset+32) 36 4 cds tag
0x40
0x40 // 0x40 (NEXT_LOC+size) NEXT_LOC len len (4+offset+32) 36 4 cds tag
mstore // NEXT_LOC len len (4+offset+32) 36 4 cds tag

dup1 // NEXT_LOC NEXT_LOC len len (4+offset+32) 36 4 cds tag
swap4 // (4+offset+32) NEXT_LOC len len NEXT_LOC 36 4 cds tag
swap3 // len NEXT_LOC len (4+offset+32) NEXT_LOC 36 4 cds tag
Expand All @@ -512,15 +513,16 @@ sub_0: assembly {
dup2 // len NEXT_LOC len len (4+offset+32) NEXT_LOC 36 4 cds tag
dup2 // NEXT_LOC len NEXT_LOC len len (4+offset+32) NEXT_LOC 36 4 cds tag
mstore // NEXT_LOC len len (4+offset+32) NEXT_LOC 36 4 cds tag

0x20 // 32 NEXT_LOC len len (4+offset+32) NEXT_LOC 36 4 cds tag
add // (NEXT_LOC+32) len len (4+offset+32) NEXT_LOC 36 4 cds tag
dup4 // (4+offset+32) (NEXT_LOC+32) len len (4+offset+32) NEXT_LOC 36 4 cds tag
dup4 // len (4+offset+32) (NEXT_LOC+32) len len (4+offset+32) NEXT_LOC 36 4 cds tag
0x20 // 32 len (4+offset+32) (NEXT_LOC+32) len len (4+offset+32) NEXT_LOC 36 4 cds tag
mul // (len*32) (4+offset+32) (NEXT_LOC+32) len len (4+offset+32) NEXT_LOC 36 4 cds tag
dup1 // (len*32) (len*32) (4+offset+32) (NEXT_LOC+32) len len (4+offset+32) NEXT_LOC 36 4 cds tag
dup3 // (4*offset+32) (len*32) (len*32) (4+offset+32) (NEXT_LOC+32) len len (4+offset+32) NEXT_LOC 36 4 cds tag
dup5 // (NEXT_LOC+32) (4*offset+32) (len*32) (len*32) (4+offset+32) (NEXT_LOC+32) len len (4+offset+32) NEXT_LOC 36 4 cds tag
dup3 // (4+offset+32) (len*32) (len*32) (4+offset+32) (NEXT_LOC+32) len len (4+offset+32) NEXT_LOC 36 4 cds tag
dup5 // (NEXT_LOC+32) (4+offset+32) (len*32) (len*32) (4+offset+32) (NEXT_LOC+32) len len (4+offset+32) NEXT_LOC 36 4 cds tag
calldatacopy // (len*32) (4+offset+32) (NEXT_LOC+32) len len (4+offset+32) NEXT_LOC 36 4 cds tag
0x0 // 0 (len*32) (4+offset+32) (NEXT_LOC+32) len len (4+offset+32) NEXT_LOC 36 4 cds tag
dup2 // (len*32) 0 (len*32) (4+offset+32) (NEXT_LOC+32) len len (4+offset+32) NEXT_LOC 36 4 cds tag
Expand Down Expand Up @@ -1187,17 +1189,17 @@ sub_0: assembly {
/* "Coin.sol":1567:1719 function addOwners(address[] memory _owners) public {... */
tag_32:
/* "Coin.sol":1651:1657 master */
0x2
0x0
swap1
sload
swap1
0x100
exp
swap1
div
0xffffffffffffffffffffffffffffffffffffffff
and
0x2 // 2 _owners
0x0 // 0 2 _owners
swap1 // 2 0 _owners
sload // sload(2) 0 _owners
swap1 // 0 sload(2) _owners
0x100 // 0x100 0 sload(2) _owners
exp // 1 sload(2) _owners
swap1 // sload(2) 1 _owners
div // sload(2) _owners
0xffffffffffffffffffffffffffffffffffffffff // 20 bytes of 1's
and // master _owners
/* "Coin.sol":1637:1657 msg.sender == master */
0xffffffffffffffffffffffffffffffffffffffff
and
Expand All @@ -1219,7 +1221,7 @@ sub_0: assembly {
/* "Coin.sol":1688:1712 super.addOwners(_owners) */
tag_64
/* "Coin.sol":1704:1711 _owners */
dup2
dup2 // _owners
/* "Coin.sol":1688:1703 super.addOwners */
tag_65
/* "Coin.sol":1688:1712 super.addOwners(_owners) */
Expand Down Expand Up @@ -1365,15 +1367,16 @@ sub_0: assembly {
jump // out
/* "Coin.sol":1302:1325 bool public placeholder */
tag_42:
0x2
0x14
swap1
sload
swap1
0x2 // 2
0x14 // 20 2
swap1 // 2 20
sload // sload(2) 20
swap1 // 20 sload(2)
0x100
exp
exp // (2^8)^20 sload(2) (= [0....0][placeholder][master])
swap1
div
div // (sload(2) >> 20bytes) (= [0......0][placeholder])
// 31bytes 1 byte
0xff
and
dup2
Expand Down Expand Up @@ -1787,27 +1790,35 @@ sub_0: assembly {
0xffffffffffffffffffffffffffffffffffffffff
and
/* "Coin.sol":746:752 owners */
0x0
0x0 // 0
/* "Coin.sol":746:759 owners[owner] */
dup1
dup1 // 0 0
/* "Coin.sol":753:758 owner */
dup4
/* "Coin.sol":746:759 owners[owner] */
0xffffffffffffffffffffffffffffffffffffffff
and
0xffffffffffffffffffffffffffffffffffffffff
and
dup2
mstore
0x20
add
swap1
dup2
mstore
0x20
add
0x0
keccak256
dup4 // owner 0 0
/* "Coin.sol":746:759 owners[owner] #hashedLocation("Solidity", 0, owner) */
// [0..0][0..0|owner]
// p p+32 p+64
// -> keccak256(p, 64)
0xffffffffffffffffffffffffffffffffffffffff
and
0xffffffffffffffffffffffffffffffffffffffff
and // owner 0 0
dup2 // 0 owner 0 0
// 0x00: owner
// 0x20: ???????? -> used to compute keccak256
// 0x40: NEXT_LOC
mstore // 0 0
0x20 /
add // 32 0
swap1 // 0 32
dup2 // 32 0 32
mstore // 32
// 0x00: owner
// 0x20: 0
0x20
add // 64
0x0 // 0 64
keccak256 // #hashedLocation("Solidity", 0, owner)
0x0
swap1
sload
Expand Down
13 changes: 12 additions & 1 deletion tutorial/Coin.sol
Expand Up @@ -25,6 +25,7 @@ contract Owned {
address owner = _owners[i];
require(owner != address(0) && owner != SENTINEL && owners[owner] == address(0),
"Invalid owner address");
// storage location of owners[owner]: #hashedLocation("Solidity", 0, owner)
owners[owner] = owners[SENTINEL];
owners[SENTINEL] = owner;
}
Expand All @@ -45,8 +46,11 @@ contract Owned {
contract Coin is Owned {
using SafeArithmetic for uint;

// 32 bytes in one storage slot
address public master; // 20 bytes
bool public placeholder; // 1 byte -> two vars packed in to one location
// [0....0][placeholder][master]
// 11 bytes| 1byte | 20 bytes
mapping (address => uint) public balances;
mapping (address => mapping (address => uint256)) public minted;

Expand All @@ -57,6 +61,10 @@ contract Coin is Owned {
}

function addOwners(address[] memory _owners) public {
// localMem: ... [len] [1st] [2nd]...
// _owners _owners+32 .....
// ........[len][1st][2nd].....[last elem]
// ^ NEXT_LOC ^ NEXT_LOC+size
require(msg.sender == master);
// internal
super.addOwners(_owners);
Expand All @@ -66,6 +74,9 @@ contract Coin is Owned {
function mint(address receiver, uint amount) public onlyOwner {
// equivalent to add(..., ...), syntatic sugar enabled by `using ... for ...`
balances[receiver] = balances[receiver].add(amount);
// #hashedLocation("Solidity", 3, owner receiver)
// => #hashedLocation("Solidity", keccakIntList(3 owner), receiver)
// => #hashedLocation("Solidity", keccakIntList(keccakIntList(3 owner) receiver))
minted[msg.sender][receiver] = minted[msg.sender][receiver].add(amount);
}

Expand All @@ -76,7 +87,7 @@ contract Coin is Owned {
emit Sent(msg.sender, receiver, amount);
}

function () external payable {}
function () external payable {} //fallback

function withdrawEther() public onlyOwner {
msg.sender.transfer(address(this).balance);
Expand Down
34 changes: 28 additions & 6 deletions tutorial/note.md
Expand Up @@ -62,14 +62,36 @@ may ignore super-contracts section, as all of the codes are already in the sub-c

## array

`addOwners`
`set(123)`
```
_ : _ : _ : _ :
0| #buf ( 32 , 123 )
```

f(uint[] a, uint[] b)
cd[4:36] = 64
cd[36:68] = 64 + size of a

```
_ : _ : _ : _ :
0| #buf ( 32 , 64 )
32| #buf ( 32 , 64 + (length of a)*32 + 32 )
64| #buf ( 32 , length )
96| #buf ( 32 , first elem of a )
..| #buf ( 32 , second elem of a )
....
```

`addOwners([A ,B])`

```
108 : 70 : 162 : 197 :
0| #buf ( 32 , 32 ) ++
32| #buf ( 32 , 2 ) ++
64| #buf ( 32 , A ) ++
96| #buf ( 32 , B )
0|108 : 70 : 162 : 197 :
0+4| #buf ( 32 , 32 ) ++
32+4| #buf ( 32 , 2 ) ++
64+4| #buf ( 32 , A ) ++
96+4| #buf ( 32 , B )
2 * 32
0 4 36 68
|sig|head1(offset to arr)|len| ......|
Expand Down

0 comments on commit 398875b

Please sign in to comment.