-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract.dfy
52 lines (45 loc) · 1.3 KB
/
contract.dfy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
trait Contract {
ghost var GlobalSpace: map<nat, Contract>;
var Balance: int;
method fallback(from: Contract, amount: int) returns (exception: bool)
modifies this
ensures !exception ==> Balance == old(Balance) + amount
{
Balance := Balance + amount;
exception := FallbackSpec();
}
method FallbackSpec() returns (exception: bool)
ensures this == old(this)
{
}
}
method transfer(from: Contract, to: Contract, amount: nat) returns (exception: bool)
modifies from, to
requires from != to && from.Balance >= amount
ensures !exception ==> from.Balance == old(from.Balance) - amount && to.Balance == old(to.Balance) + amount
{
from.Balance := from.Balance - amount;
exception := to.fallback(from, amount);
}
method send(from: Contract, to: Contract, amount: int) returns (success: bool)
modifies from, to
requires from != to && from.Balance >= amount
ensures success ==> from.Balance == old(from.Balance) - amount && to.Balance == old(to.Balance) + amount
{
from.Balance := from.Balance - amount;
var failed := to.fallback(from, amount);
success := !failed;
}
class Block {
var timestamp: nat;
}
class Message {
var sender: Contract;
var value: int;
constructor (s: Contract, v: int)
ensures sender == s && value == v
{
sender := s;
value := v;
}
}