forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timebounds.go
71 lines (61 loc) · 2.88 KB
/
timebounds.go
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
package txnbuild
import (
"errors"
"time"
)
// TimeoutInfinite allows an indefinite upper bound to be set for Transaction.MaxTime. This is usually not
// what you want.
const TimeoutInfinite = int64(0)
// Timebounds represents the time window during which a AiBlocks transaction is considered valid.
//
// MinTime and MaxTime represent AiBlocks timebounds - a window of time over which the Transaction will be
// considered valid. In general, almost all Transactions benefit from setting an upper timebound, because once submitted,
// the status of a pending Transaction may remain unresolved for a long time if the network is congested.
// With an upper timebound, the submitter has a guaranteed time at which the Transaction is known to have either
// succeeded or failed, and can then take appropriate action (e.g. to resubmit or mark as resolved).
//
// Create a Timebounds struct using one of NewTimebounds(), NewTimeout(), or NewInfiniteTimeout().
type Timebounds struct {
MinTime int64
MaxTime int64
wasBuilt bool
}
// Validate for Timebounds sanity-checks the configured Timebound limits, and confirms the object was built
// using a factory method. This is done to ensure that default Timebound structs (which have no limits) are not
// valid - you must explicitly specifiy the Timebound you require.
func (tb *Timebounds) Validate() error {
if !tb.wasBuilt {
return errors.New("timebounds must be constructed using NewTimebounds(), NewTimeout(), or NewInfiniteTimeout()")
}
if tb.MinTime < 0 {
return errors.New("invalid timebound: minTime cannot be negative")
}
if tb.MaxTime < 0 {
return errors.New("invalid timebound: maxTime cannot be negative")
}
if tb.MaxTime != TimeoutInfinite {
if tb.MaxTime < tb.MinTime {
return errors.New("invalid timebound: maxTime < minTime")
}
}
return nil
}
// NewTimebounds is a factory method that constructs a Timebounds object from a min and max time.
// A Transaction cannot be built unless a Timebounds object is provided through a factory method.
func NewTimebounds(minTime, maxTime int64) Timebounds {
return Timebounds{minTime, maxTime, true}
}
// NewTimeout is a factory method that sets the MaxTime to be the duration in seconds in the
// future specified by 'timeout'.
// A Transaction cannot be built unless a Timebounds object is provided through a factory method.
// This method uses the provided system time - make sure it is accurate.
func NewTimeout(timeout int64) Timebounds {
return Timebounds{0, time.Now().UTC().Unix() + timeout, true}
}
// NewInfiniteTimeout is a factory method that sets the MaxTime to a value representing an indefinite
// upper time bound. This is rarely needed, but is helpful for certain smart contracts, and for
// deterministic testing. A Transaction cannot be built unless a Timebounds object is provided through
// a factory method.
func NewInfiniteTimeout() Timebounds {
return Timebounds{0, TimeoutInfinite, true}
}