forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
265 lines (227 loc) · 8.52 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package types
import (
"context"
"time"
"github.com/gogo/protobuf/proto"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/store/gaskv"
stypes "github.com/cosmos/cosmos-sdk/store/types"
)
/*
Context is an immutable object contains all information needed to
process a request.
It contains a context.Context object inside if you want to use that,
but please do not over-use it. We try to keep all data structured
and standard additions here would be better just to add to the Context struct
*/
type Context struct {
ctx context.Context
ms MultiStore
header tmproto.Header
chainID string
txBytes []byte
logger log.Logger
voteInfo []abci.VoteInfo
gasMeter GasMeter
blockGasMeter GasMeter
checkTx bool
recheckTx bool // if recheckTx == true, then checkTx must also be true
minGasPrice DecCoins
consParams *abci.ConsensusParams
eventManager *EventManager
}
// Proposed rename, not done to avoid API breakage
type Request = Context
// Read-only accessors
func (c Context) Context() context.Context { return c.ctx }
func (c Context) MultiStore() MultiStore { return c.ms }
func (c Context) BlockHeight() int64 { return c.header.Height }
func (c Context) BlockTime() time.Time { return c.header.Time }
func (c Context) ChainID() string { return c.chainID }
func (c Context) TxBytes() []byte { return c.txBytes }
func (c Context) Logger() log.Logger { return c.logger }
func (c Context) VoteInfos() []abci.VoteInfo { return c.voteInfo }
func (c Context) GasMeter() GasMeter { return c.gasMeter }
func (c Context) BlockGasMeter() GasMeter { return c.blockGasMeter }
func (c Context) IsCheckTx() bool { return c.checkTx }
func (c Context) IsReCheckTx() bool { return c.recheckTx }
func (c Context) MinGasPrices() DecCoins { return c.minGasPrice }
func (c Context) EventManager() *EventManager { return c.eventManager }
// clone the header before returning
func (c Context) BlockHeader() tmproto.Header {
var msg = proto.Clone(&c.header).(*tmproto.Header)
return *msg
}
func (c Context) ConsensusParams() *abci.ConsensusParams {
return proto.Clone(c.consParams).(*abci.ConsensusParams)
}
// create a new context
func NewContext(ms MultiStore, header tmproto.Header, isCheckTx bool, logger log.Logger) Context {
// https://github.com/gogo/protobuf/issues/519
header.Time = header.Time.UTC()
return Context{
ctx: context.Background(),
ms: ms,
header: header,
chainID: header.ChainID,
checkTx: isCheckTx,
logger: logger,
gasMeter: stypes.NewInfiniteGasMeter(),
minGasPrice: DecCoins{},
eventManager: NewEventManager(),
}
}
// WithContext returns a Context with an updated context.Context.
func (c Context) WithContext(ctx context.Context) Context {
c.ctx = ctx
return c
}
// WithMultiStore returns a Context with an updated MultiStore.
func (c Context) WithMultiStore(ms MultiStore) Context {
c.ms = ms
return c
}
// WithBlockHeader returns a Context with an updated tendermint block header in UTC time.
func (c Context) WithBlockHeader(header tmproto.Header) Context {
// https://github.com/gogo/protobuf/issues/519
header.Time = header.Time.UTC()
c.header = header
return c
}
// WithBlockTime returns a Context with an updated tendermint block header time in UTC time
func (c Context) WithBlockTime(newTime time.Time) Context {
newHeader := c.BlockHeader()
// https://github.com/gogo/protobuf/issues/519
newHeader.Time = newTime.UTC()
return c.WithBlockHeader(newHeader)
}
// WithProposer returns a Context with an updated proposer consensus address.
func (c Context) WithProposer(addr ConsAddress) Context {
newHeader := c.BlockHeader()
newHeader.ProposerAddress = addr.Bytes()
return c.WithBlockHeader(newHeader)
}
// WithBlockHeight returns a Context with an updated block height.
func (c Context) WithBlockHeight(height int64) Context {
newHeader := c.BlockHeader()
newHeader.Height = height
return c.WithBlockHeader(newHeader)
}
// WithChainID returns a Context with an updated chain identifier.
func (c Context) WithChainID(chainID string) Context {
c.chainID = chainID
return c
}
// WithTxBytes returns a Context with an updated txBytes.
func (c Context) WithTxBytes(txBytes []byte) Context {
c.txBytes = txBytes
return c
}
// WithLogger returns a Context with an updated logger.
func (c Context) WithLogger(logger log.Logger) Context {
c.logger = logger
return c
}
// WithVoteInfos returns a Context with an updated consensus VoteInfo.
func (c Context) WithVoteInfos(voteInfo []abci.VoteInfo) Context {
c.voteInfo = voteInfo
return c
}
// WithGasMeter returns a Context with an updated transaction GasMeter.
func (c Context) WithGasMeter(meter GasMeter) Context {
c.gasMeter = meter
return c
}
// WithBlockGasMeter returns a Context with an updated block GasMeter
func (c Context) WithBlockGasMeter(meter GasMeter) Context {
c.blockGasMeter = meter
return c
}
// WithIsCheckTx enables or disables CheckTx value for verifying transactions and returns an updated Context
func (c Context) WithIsCheckTx(isCheckTx bool) Context {
c.checkTx = isCheckTx
return c
}
// WithIsRecheckTx called with true will also set true on checkTx in order to
// enforce the invariant that if recheckTx = true then checkTx = true as well.
func (c Context) WithIsReCheckTx(isRecheckTx bool) Context {
if isRecheckTx {
c.checkTx = true
}
c.recheckTx = isRecheckTx
return c
}
// WithMinGasPrices returns a Context with an updated minimum gas price value
func (c Context) WithMinGasPrices(gasPrices DecCoins) Context {
c.minGasPrice = gasPrices
return c
}
// WithConsensusParams returns a Context with an updated consensus params
func (c Context) WithConsensusParams(params *abci.ConsensusParams) Context {
c.consParams = params
return c
}
// WithEventManager returns a Context with an updated event manager
func (c Context) WithEventManager(em *EventManager) Context {
c.eventManager = em
return c
}
// TODO: remove???
func (c Context) IsZero() bool {
return c.ms == nil
}
// WithValue is deprecated, provided for backwards compatibility
// Please use
// ctx = ctx.WithContext(context.WithValue(ctx.Context(), key, false))
// instead of
// ctx = ctx.WithValue(key, false)
func (c Context) WithValue(key, value interface{}) Context {
c.ctx = context.WithValue(c.ctx, key, value)
return c
}
// Value is deprecated, provided for backwards compatibility
// Please use
// ctx.Context().Value(key)
// instead of
// ctx.Value(key)
func (c Context) Value(key interface{}) interface{} {
return c.ctx.Value(key)
}
// ----------------------------------------------------------------------------
// Store / Caching
// ----------------------------------------------------------------------------
// KVStore fetches a KVStore from the MultiStore.
func (c Context) KVStore(key StoreKey) KVStore {
return gaskv.NewStore(c.MultiStore().GetKVStore(key), c.GasMeter(), stypes.KVGasConfig())
}
// TransientStore fetches a TransientStore from the MultiStore.
func (c Context) TransientStore(key StoreKey) KVStore {
return gaskv.NewStore(c.MultiStore().GetKVStore(key), c.GasMeter(), stypes.TransientGasConfig())
}
// CacheContext returns a new Context with the multi-store cached and a new
// EventManager. The cached context is written to the context when writeCache
// is called.
func (c Context) CacheContext() (cc Context, writeCache func()) {
cms := c.MultiStore().CacheMultiStore()
cc = c.WithMultiStore(cms).WithEventManager(NewEventManager())
return cc, cms.Write
}
// ContextKey defines a type alias for a stdlib Context key.
type ContextKey string
// SdkContextKey is the key in the context.Context which holds the sdk.Context.
const SdkContextKey ContextKey = "sdk-context"
// WrapSDKContext returns a stdlib context.Context with the provided sdk.Context's internal
// context as a value. It is useful for passing an sdk.Context through methods that take a
// stdlib context.Context parameter such as generated gRPC methods. To get the original
// sdk.Context back, call UnwrapSDKContext.
func WrapSDKContext(ctx Context) context.Context {
return context.WithValue(ctx.ctx, SdkContextKey, ctx)
}
// UnwrapSDKContext retrieves a Context from a context.Context instance
// attached with WrapSDKContext. It panics if a Context was not properly
// attached
func UnwrapSDKContext(ctx context.Context) Context {
return ctx.Value(SdkContextKey).(Context)
}