Memory Storage
Data Temporary Persistent
Copy Shallow Deep
Similar Java Functional Programming

Storage Deep Copy


contract PersonAccount {
    struct Account {
        int balance;
    }

    struct Person {
        Account account;
    }
	
    Person alice;
    Person bob;
}
	

function f() {
	alice.account.balance = 10;
	bob.account.balance = 20;

	alice.account = bob.account;
	assert(alice.account.balance == 20);

	alice.account.balance = 30;
	assert(bob.account.balance == 20);
}
	
$\text{a}_1 \leftarrow$ alice:account

function f() {
	Account storage a1 = alice.account;
	a1.balance = 10;
	bob.account.balance = 20;

	alice.account = bob.account;
	assert(alice.account.balance == 20);

	alice.account.balance = 30;
	assert(bob.account.balance == 20);
}
	
$\text{a}_1 \leftarrow$ alice:account
alice
account
balance 10

function f() {
	a1.balance = 10;
	bob.account.balance = 20;

	alice.account = bob.account;
	assert(alice.account.balance == 20);

	alice.account.balance = 30;
	assert(bob.account.balance == 20);
}
	
alice
account
balance 10
bob
account
balance 20

function f() {
	bob.account.balance = 20;

	alice.account = bob.account;
	assert(alice.account.balance == 20);

	alice.account.balance = 30;
	assert(bob.account.balance == 20);
}
	
alice
account
balance 10
account
balance 10
bob
account
balance 20

function f() {
	alice.account = bob.account;
	assert(alice.account.balance == 20);

	alice.account.balance = 30;
	assert(bob.account.balance == 20);
}
	
alice
account
balance 20
account
balance 10
bob
account
balance 20

function f() {
	assert(alice.account.balance == 20);

	alice.account.balance = 30;
	assert(bob.account.balance == 20);
}
	
alice
account
balance 20 30
bob
account
balance 20

function f() {
	alice.account.balance = 30;
	assert(bob.account.balance == 20);
}
	
alice
account
balance 30
bob
account
balance 20

function f() {
	assert(bob.account.balance == 20);
}
	

Memory Shallow Copy


function f() {
	Person memory alice;
	Person memory bob;

	alice.account.balance = 10;
	bob.account.balance = 20;

	alice.account = bob.account;
	assert(alice.account.balance == 20);

	alice.account.balance = 30;
	assert(bob.account.balance == 30);
}
	
alice account alice:account
bob account bob:account

function f() {
	alice.account.balance = 10;
	bob.account.balance = 20;

	alice.account = bob.account;
	assert(alice.account.balance == 20);

	alice.account.balance = 30;
	assert(bob.account.balance == 30);
}
	
$\text{a}_1 \leftarrow$ alice:account
alice account alice:account
bob account bob:account

function f() {
	Account memory a1 = alice.account;
	a1.balance = 10;
	bob.account.balance = 20;

	alice.account = bob.account;
	assert(alice.account.balance == 20);

	alice.account.balance = 30;
	assert(bob.account.balance == 30);
}
	
$\text{a}_1 \leftarrow$ alice:account
alice account alice:account
bob account bob:account
alice:account balance 10

function f() {
	a1.balance = 10;
	bob.account.balance = 20;

	alice.account = bob.account;
	assert(alice.account.balance == 20);

	alice.account.balance = 30;
	assert(bob.account.balance == 30);
}
	
alice account alice:account
bob account bob:account
alice:account balance 10
bob:account balance 20

function f() {
	bob.account.balance = 20;

	alice.account = bob.account;
	assert(alice.account.balance == 20);

	alice.account.balance = 30;
	assert(bob.account.balance == 30);
}
	
alice account bob:account
bob account bob:account
alice:account balance 10
bob:account balance 20

function f() {
	alice.account = bob.account;
	assert(alice.account.balance == 20);

	alice.account.balance = 30;
	assert(bob.account.balance == 30);
}
	
alice account bob:account
bob account bob:account
alice:account balance 10
bob:account balance 20

function f() {
	assert(alice.account.balance == 20);

	alice.account.balance = 30;
	assert(bob.account.balance == 30);
}
	
$\text{a}_1 \leftarrow$ bob:account
alice account bob:account
bob account bob:account
alice:account balance 10
bob:account balance 20

function f() {
	Account memory a1 = alice.account;
	assert(a1.balance == 20);

	alice.account.balance = 30;
	assert(bob.account.balance == 30);
}
	
$\text{a}_1 \leftarrow$ bob:account
alice account bob:account
bob account bob:account
alice:account balance 10
bob:account balance 20

function f() {
	assert(a1.balance == 20);

	alice.account.balance = 30;
	assert(bob.account.balance == 30);
}
	
alice account bob:account
bob account bob:account
alice:account balance 10
bob:account balance 20

function f() {
	alice.account.balance = 30;
	assert(bob.account.balance == 30);
}
	
$\text{a}_2 \leftarrow$ bob:account
alice account bob:account
bob account bob:account
alice:account balance 10
bob:account balance 20

function f() {
	Account memory a2 = alice.account;
	a2.balance = 30;
	assert(bob.account.balance == 30);
}
	
$\text{a}_2 \leftarrow$ bob:account
alice account bob:account
bob account bob:account
alice:account balance 10
bob:account balance 20 30

function f() {
	a2.balance = 30;
	assert(bob.account.balance == 30);
}
	
alice account bob:account
bob account bob:account
alice:account balance 10
bob:account balance 30

function f() {
	assert(bob.account.balance == 30);
}
	

Storage Path Variable


function f() {
	alice.account.balance = 10;
	bob.account.balance = 20;
	Account storage acc = alice.account;

	alice.account = bob.account;
	assert(acc.balance == 20);

	acc.balance = 30;
	assert(alice.account.balance == 30);
	assert(bob.account.balance   == 20);
}
	
$\text{a}_1 \leftarrow$ alice:account

function f() {
	Account storage a1 = alice.account;
	a1.balance = 10;
	bob.account.balance = 20;
	Account storage acc = alice.account;

	alice.account = bob.account;
	assert(acc.balance == 20);

	acc.balance = 30;
	assert(alice.account.balance == 30);
	assert(bob.account.balance   == 20);
}
	
$\text{a}_1 \leftarrow$ alice:account
alice
account
balance 10

function f() {
	a1.balance = 10;
	bob.account.balance = 20;
	Account storage acc = alice.account;

	alice.account = bob.account;
	assert(acc.balance == 20);

	acc.balance = 30;
	assert(alice.account.balance == 30);
	assert(bob.account.balance   == 20);
}
	
acc $\leftarrow$ alice:account
alice
account
balance 10
bob
account
balance 20

function f() {
	bob.account.balance = 20;
	Account storage acc = alice.account;

	alice.account = bob.account;
	assert(acc.balance == 20);

	acc.balance = 30;
	assert(alice.account.balance == 30);
	assert(bob.account.balance   == 20);
}
	
acc $\leftarrow$ alice:account
alice
account
balance 10
account
balance 10
bob
account
balance 20

function f() {
	alice.account = bob.account;
	assert(acc.balance == 20);

	acc.balance = 30;
	assert(alice.account.balance == 30);
	assert(bob.account.balance   == 20);
}
	
acc $\leftarrow$ alice:account
alice
account
balance 20
account
balance 10
bob
account
balance 20

function f() {
	assert(acc.balance == 20);

	acc.balance = 30;
	assert(alice.account.balance == 30);
	assert(bob.account.balance   == 20);
}
	
acc $\leftarrow$ alice:account
alice
account
balance 20 30
bob
account
balance 20

function f() {
	acc.balance = 30;
	assert(alice.account.balance == 30);
	assert(bob.account.balance   == 20);
}
	
acc $\leftarrow$ alice:account
alice
account
balance 30
bob
account
balance 20

function f() {
	assert(alice.account.balance == 30);
	assert(bob.account.balance   == 20);
}
	
acc $\leftarrow$ alice:account
alice
account
balance 30
bob
account
balance 20

function f() {
	assert(bob.account.balance   == 20);
}
	

Memory


function f() {
    Person memory alice;
    Person memory bob;
    alice.account.balance = 10;
    bob.account.balance = 20;
    Account memory acc = bob.account;

    alice.account = bob.account;
    assert(acc.balance == 20);

    acc.balance = 30;
    assert(alice.account.balance == 30);
    assert(bob.account.balance   == 30);
}
    
alice account alice:account
bob account bob:account
alice:account balance 10
bob:account balance 20

function f() {
	alice.account.balance = 10;
	bob.account.balance = 20;
	Account memory acc = bob.account;

	alice.account = bob.account;
	assert(acc.balance == 20);

	acc.balance = 30;
	assert(alice.account.balance == 30);
	assert(bob.account.balance   == 30);
}
	
acc $\leftarrow$ bob:account
alice account alice:account bob:account
bob account bob:account
alice:account balance 10
bob:account balance 20

function f() {
	alice.account = bob.account;
	assert(acc.balance == 20);

	acc.balance = 30;
	assert(alice.account.balance == 30);
	assert(bob.account.balance   == 30);
}
	
acc $\leftarrow$ bob:account
alice account bob:account
bob account bob:account
alice:account balance 10
bob:account balance 20

function f() {
	assert(acc.balance == 20);

	acc.balance = 30;
	assert(alice.account.balance == 30);
	assert(bob.account.balance   == 30);
}
	
acc $\leftarrow$ bob:account
alice account bob:account
bob account bob:account
alice:account balance 10
bob:account balance
20
30

function f() {
	acc.balance = 30;
	assert(alice.account.balance == 30);
	assert(bob.account.balance   == 30);
}
	
alice account bob:account
bob account bob:account
alice:account balance 10
bob:account balance
30
20

function f() {
	assert(alice.account.balance == 30);
	assert(bob.account.balance   == 30);
}
	
$\text{a}_1 \leftarrow$ bob:account
alice account bob:account
bob account bob:account
alice:account balance 10
bob:account balance 30

function f() {
	Account memory a1 = alice.account;
	assert(a1.balance == 30);
	assert(bob.account.balance   == 30);
}
	
$\text{a}_1 \leftarrow$ bob:account
alice account bob:account
bob account bob:account
alice:account balance 10
bob:account balance 30

function f() {
	assert(a1.balance == 30);
	assert(bob.account.balance   == 30);
}
	
alice account bob:account
bob account bob:account
alice:account balance 10
bob:account balance 30

function f() {
	assert(bob.account.balance   == 30);
}
	
$b_1 \leftarrow$ bob:account
alice account bob:account
bob account bob:account
alice:account balance 10
bob:account balance 30

function f() {
	Account memory b1 = bob.account;
	assert(b1.balance   == 30);
}
	
$b_1 \leftarrow$ bob:account
alice account bob:account
bob account bob:account
alice:account balance 10
bob:account balance 30

function f() {
	assert(b1.balance   == 30);
}
	

Storage


contract PersonStorage {
    struct Account {
        int balance;
    }
    struct Person {
        Account account;
    }

    Person alice;
    Person bob;

    function f() {
        assert(alice.account.balance == 0);
    }
}
	
alice new

function f() {
	assert(alice.account.balance == 0);
}
	
\[ a_1 \leftarrow \text{alice:account} \]
alice new

function f() {
	Account storage a1 = alice.account;
	assert(a1.balance == 0);
}
	
\[ a_1 \leftarrow \textcolor{orange}{\text{alice}}\text{:account} \]
alice new

function f() {
	assert(a1.balance == 0);
}
	

Memory


function f() {
  Person memory alice;
  Person memory bob;

  assert(alice.account.balance == 0);
}
		
alice new

function f() {
  Person memory bob = new Person();

  assert(alice.account.balance == 0);
}
		
\[ \text{alice} \neq \text{bob} \]
alice new
bob new

function f() {
  assert(alice.account.balance == 0);
}
		
\[ \text{alice} \neq \text{bob} \]
$a_1 \leftarrow$ alice:account
alice new
bob new

function f() {
  Account storage a1 = alice.account;
  assert(a1.balance == 0);
}
		
\[ \textcolor{aqua}{\text{alice}} \neq \textcolor{red}{\text{bob}} \]
$a_1 \leftarrow$ alice:account
alice new
$ \Rightarrow $ bob new $ \textcolor{aqua}{\text{alice}}\textcolor{chartreuse}{\text{:account}} $ balance

function f() {
  assert(a1.balance == 0);
}
		
\[ \textcolor{aqua}{\text{alice}} \neq \text{bob} \]
$a_1 \leftarrow$ alice:account
$ \Rightarrow $ alice new $ \textcolor{aqua}{\text{alice}}\textcolor{chartreuse}{\text{:account}} $ balance
bob new

function f() {
  assert(a1.balance == 0);
}