forked from wmeyer/smart-contract-validation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleauction.als
167 lines (130 loc) · 3.91 KB
/
simpleauction.als
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
open util/ordering[Time] as time
open util/ordering[SimpleAuction] as ord
sig Address {}
sig Time {}
sig SimpleAuction {
beneficiary: Address,
auctionEnd: Time,
highestBidder: lone Address,
highestBid: lone Int,
pendingReturns: Address -> Int,
now: Time,
ended: Int,
// strictly spreaking, the account holdings do not belong
// but it is easiest to handle as part of SimpleAuction
account: Address -> one Int
} {
all a: Address | account[a] >= 0
}
fact traces {
some beneficiary: Address, t: Time | newAuction[ord/first, t, beneficiary]
// define all legal state transitions
all a: SimpleAuction - ord/last |
let a' = a.next |
tick[a, a'] or
(some sender: Address, value: Int | bid[sender, a, a', value])
or (some sender: Address | withdraw[sender, a, a'])
or auctionEnd[a, a']
}
pred newAuction(a: SimpleAuction, t: Time, b: Address) {
a.beneficiary = b
a.auctionEnd = t
no a.highestBidder
no a.highestBid
no a.pendingReturns
a.now = time/first
a.ended = 0
// s.account may contain anything
}
pred tick(a, a': SimpleAuction) {
// change
a'.now = a.now.next
// copy
a'.beneficiary = a.beneficiary
a'.auctionEnd = a.auctionEnd
a'.highestBidder = a.highestBidder
a'.highestBid = a.highestBid
a'.pendingReturns = a.pendingReturns
a'.ended = a.ended
a'.account = a.account
}
pred bid(sender: Address, a, a': SimpleAuction, value: Int) {
// preconditions
a.account[sender] >= value
a.now in time/prevs[a.auctionEnd]
value > a.highestBid
// return previous bid
some a.highestBidder
=> a'.pendingReturns = a.pendingReturns ++ (a.highestBidder -> a.pendingReturns[a.highestBidder].plus[a.highestBid])
else a'.pendingReturns = a.pendingReturns
a'.highestBidder = sender
a'.highestBid = value
a'.account = a.account ++ (sender -> a.account[sender].minus[value])
// copy
a'.beneficiary = a.beneficiary
a'.auctionEnd = a.auctionEnd
a'.now = a.now
a'.ended = a.ended
}
pred withdraw(sender: Address, a, a': SimpleAuction) {
a'.pendingReturns = a.pendingReturns ++ (sender -> 0)
let pending = a.pendingReturns[sender] |
pending > 0
&& a'.account = a.account ++ (sender -> a.account[sender].plus[pending])
// copy
a'.beneficiary = a.beneficiary
a'.auctionEnd = a.auctionEnd
a'.highestBidder = a.highestBidder
a'.highestBid = a.highestBid
a'.now = a.now
a'.ended = a.ended
}
pred auctionEnd(a, a': SimpleAuction) {
// preconditions
a.now in time/nexts[a.auctionEnd] or a.now = a.auctionEnd
a.ended = 0
// change
a'.ended = 1
// send money to beneficiary
a'.account = a.account ++ (a.beneficiary -> a.account[a.beneficiary].plus[a.highestBid])
// copy
a'.beneficiary = a.beneficiary
a'.auctionEnd = a.auctionEnd
a'.highestBidder = a.highestBidder
a'.highestBid = a.highestBid
a'.now = a.now
a'.pendingReturns = a.pendingReturns
}
fun accountSum[s: SimpleAuction] : Int {
sum a: Address | s.account[a]
}
// the EVM will make sure there is no new money;
// but by checking this, we can validate that our contract does not even try
// to create new money
assert noNewMoney {
let initialMoney = accountSum[ord/first] |
all s: SimpleAuction | accountSum[s] <= initialMoney
}
fun allFunds(s:SimpleAuction, a: Address) : Int {
s.account[a].plus[s.pendingReturns[a]]
}
fun initialAccount[a: Address] : Int {
(ord/first).account[a]
}
assert beneficiaryGetsMoney {
all s: SimpleAuction |
let b = s.beneficiary|
s.ended = 1 =>
(s.highestBidder = s.beneficiary => allFunds[s, b] = initialAccount[b]
else
allFunds[s, b] = initialAccount[b].plus[s.highestBid])
}
check beneficiaryGetsMoney for 7
check noNewMoney for 5
// example traces
run {
#Address >= 2
some a: SimpleAuction | some a.highestBid
some a: SimpleAuction | a.ended = 1
some a, a': SimpleAuction, addr: Address | a' in ord/nexts[a] && a'.ended=0 && a'.account[addr] > a.account[addr]
} for 8