guards.gno
1.31 Kb · 41 lines
1package bolo
2
3import (
4 "math"
5 "time"
6)
7
8// assertCurrent is the first statement of every crossing function (API C2).
9func assertCurrent(cur realm) {
10 if !cur.IsCurrent() {
11 panic("bolo: not the current realm")
12 }
13}
14
15// checkAddress refuses an address argument that isn't a well-formed address (API
16// C12): exactly 40 characters, a lowercase "g" first, then IsValid(). MsgCall
17// passes an address argument through unchecked, and a malformed one stored once
18// would make every later send to it fail. IsValid alone also accepts an address in
19// capitals, which the bank pays as the same account but which never equals the
20// lowercase spelling; it refuses mixed case, so a lowercase "g" means a lowercase
21// address. The length comes first, so IsValid only ever parses address-sized
22// input.
23func checkAddress(a address, err Error) {
24 if len(a) != 40 || a[0] != 'g' || !a.IsValid() {
25 panic(err)
26 }
27}
28
29const secondsPerDay = 24 * 60 * 60
30
31// daysToSeconds converts a duration in whole days to seconds, refusing a negative
32// one and one whose seconds would overflow (API §2.3).
33func daysToSeconds(days int, err Error) int64 {
34 if days < 0 || int64(days) > math.MaxInt64/secondsPerDay {
35 panic(err)
36 }
37 return int64(days) * secondsPerDay
38}
39
40// now is block time in Unix seconds (API C8).
41func now() int64 { return time.Now().Unix() }