-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontext.go
61 lines (49 loc) · 1.65 KB
/
context.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
package tracing
import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// IsTraceable returns true when context is applicable for tracing
func IsTraceable(ctx sdk.Context) bool {
return isTraceable(ctx, IsSimulation(ctx))
}
// IsTraceable returns true when context is applicable for tracing
func isTraceable(ctx sdk.Context, simulate bool) bool {
return !ctx.IsCheckTx() || simulate && !disableSimulations
}
type key int
var (
simulationKey key = 1
clockKey key = 2
)
// WithSimulation set simulation flag
func WithSimulation(rootCtx sdk.Context, sim bool) sdk.Context {
return rootCtx.WithValue(simulationKey, sim)
}
// IsSimulation is simulation context
func IsSimulation(ctx sdk.Context) bool {
v, ok := ctx.Value(simulationKey).(bool)
return ok && v
}
type BlockTimeClock struct {
startTime time.Time
blockTime time.Time
}
// NewBlockTimeClock constructor
func NewBlockTimeClock(currentSystemTime time.Time, blockTime time.Time) *BlockTimeClock {
return &BlockTimeClock{startTime: currentSystemTime.UTC(), blockTime: blockTime.UTC()}
}
// Now returns the relative block time
func (b BlockTimeClock) Now(currentSystemTime time.Time) time.Time {
passed := currentSystemTime.UTC().Sub(b.startTime)
return b.blockTime.Add(passed).UTC()
}
// WithBlockTimeClock return context with block time clock set and current relative block time
func WithBlockTimeClock(rootCtx sdk.Context) (sdk.Context, time.Time) {
if c, ok := rootCtx.Value(clockKey).(*BlockTimeClock); ok {
return rootCtx, c.Now(time.Now())
}
blockTime := rootCtx.BlockTime().UTC()
clock := NewBlockTimeClock(time.Now(), blockTime)
return rootCtx.WithValue(clockKey, clock), blockTime
}