-
Notifications
You must be signed in to change notification settings - Fork 320
/
context.go
314 lines (277 loc) · 9.59 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package protocol
import (
"context"
"math/big"
"time"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/pkg/log"
)
type (
blockchainContextKey struct{}
blockContextKey struct{}
actionContextKey struct{}
registryContextKey struct{}
featureContextKey struct{}
featureWithHeightContextKey struct{}
vmConfigContextKey struct{}
// TipInfo contains the tip block information
TipInfo struct {
Height uint64
Hash hash.Hash256
Timestamp time.Time
}
// BlockchainCtx provides blockchain auxiliary information.
BlockchainCtx struct {
// Tip is the information of tip block
Tip TipInfo
//ChainID is the native chain ID
ChainID uint32
// EvmNetworkID is the EVM network ID
EvmNetworkID uint32
}
// BlockCtx provides block auxiliary information.
BlockCtx struct {
// height of block containing those actions
BlockHeight uint64
// timestamp of block containing those actions
BlockTimeStamp time.Time
// gas Limit for perform those actions
GasLimit uint64
// Producer is the address of whom composes the block containing this action
Producer address.Address
}
// ActionCtx provides action auxiliary information.
ActionCtx struct {
// Caller is the address of whom issues this action
Caller address.Address
// ActionHash is the hash of the action with the sealed envelope
ActionHash hash.Hash256
// GasPrice is the action gas price
GasPrice *big.Int
// IntrinsicGas is the action intrinsic gas
IntrinsicGas uint64
// Nonce is the nonce of the action
Nonce uint64
}
// CheckFunc is function type to check by height.
CheckFunc func(height uint64) bool
// FeatureCtx provides features information.
FeatureCtx struct {
FixDoubleChargeGas bool
SystemWideActionGasLimit bool
NotFixTopicCopyBug bool
SetRevertMessageToReceipt bool
FixGetHashFnHeight bool
UsePendingNonceOption bool
AsyncContractTrie bool
AddOutOfGasToTransactionLog bool
AddChainIDToConfig bool
UseV2Storage bool
CannotUnstakeAgain bool
SkipStakingIndexer bool
ReturnFetchError bool
CannotTranferToSelf bool
NewStakingReceiptFormat bool
UpdateBlockMeta bool
CurrentEpochProductivity bool
FixSnapshotOrder bool
AllowCorrectDefaultChainID bool
CorrectGetHashFn bool
CorrectTxLogIndex bool
RevertLog bool
TolerateLegacyAddress bool
ValidateRewardProtocol bool
}
// FeatureWithHeightCtx provides feature check functions.
FeatureWithHeightCtx struct {
GetUnproductiveDelegates CheckFunc
ReadStateFromDB CheckFunc
UseV2Staking CheckFunc
EnableNativeStaking CheckFunc
StakingCorrectGas CheckFunc
CalculateProbationList CheckFunc
LoadCandidatesLegacy CheckFunc
}
)
// WithRegistry adds registry to context
func WithRegistry(ctx context.Context, reg *Registry) context.Context {
return context.WithValue(ctx, registryContextKey{}, reg)
}
// GetRegistry returns the registry from context
func GetRegistry(ctx context.Context) (*Registry, bool) {
reg, ok := ctx.Value(registryContextKey{}).(*Registry)
return reg, ok
}
// MustGetRegistry returns the registry from context
func MustGetRegistry(ctx context.Context) *Registry {
reg, ok := ctx.Value(registryContextKey{}).(*Registry)
if !ok {
log.S().Panic("Miss registry context")
}
return reg
}
// WithBlockchainCtx add BlockchainCtx into context.
func WithBlockchainCtx(ctx context.Context, bc BlockchainCtx) context.Context {
return context.WithValue(ctx, blockchainContextKey{}, bc)
}
// GetBlockchainCtx gets BlockchainCtx
func GetBlockchainCtx(ctx context.Context) (BlockchainCtx, bool) {
bc, ok := ctx.Value(blockchainContextKey{}).(BlockchainCtx)
return bc, ok
}
// MustGetBlockchainCtx must get BlockchainCtx.
// If context doesn't exist, this function panic.
func MustGetBlockchainCtx(ctx context.Context) BlockchainCtx {
bc, ok := ctx.Value(blockchainContextKey{}).(BlockchainCtx)
if !ok {
log.S().Panic("Miss blockchain context")
}
return bc
}
// WithBlockCtx add BlockCtx into context.
func WithBlockCtx(ctx context.Context, blk BlockCtx) context.Context {
return context.WithValue(ctx, blockContextKey{}, blk)
}
// GetBlockCtx gets BlockCtx
func GetBlockCtx(ctx context.Context) (BlockCtx, bool) {
blk, ok := ctx.Value(blockContextKey{}).(BlockCtx)
return blk, ok
}
// MustGetBlockCtx must get BlockCtx .
// If context doesn't exist, this function panic.
func MustGetBlockCtx(ctx context.Context) BlockCtx {
blk, ok := ctx.Value(blockContextKey{}).(BlockCtx)
if !ok {
log.S().Panic("Miss block context")
}
return blk
}
// WithActionCtx add ActionCtx into context.
func WithActionCtx(ctx context.Context, ac ActionCtx) context.Context {
return context.WithValue(ctx, actionContextKey{}, ac)
}
// GetActionCtx gets ActionCtx
func GetActionCtx(ctx context.Context) (ActionCtx, bool) {
ac, ok := ctx.Value(actionContextKey{}).(ActionCtx)
return ac, ok
}
// MustGetActionCtx must get ActionCtx .
// If context doesn't exist, this function panic.
func MustGetActionCtx(ctx context.Context) ActionCtx {
ac, ok := ctx.Value(actionContextKey{}).(ActionCtx)
if !ok {
log.S().Panic("Miss action context")
}
return ac
}
// WithFeatureCtx add FeatureCtx into context.
func WithFeatureCtx(ctx context.Context) context.Context {
g := genesis.MustExtractGenesisContext(ctx)
height := MustGetBlockCtx(ctx).BlockHeight
return context.WithValue(
ctx,
featureContextKey{},
FeatureCtx{
FixDoubleChargeGas: g.IsPacific(height),
SystemWideActionGasLimit: !g.IsAleutian(height),
NotFixTopicCopyBug: !g.IsAleutian(height),
SetRevertMessageToReceipt: g.IsHawaii(height),
FixGetHashFnHeight: g.IsHawaii(height),
UsePendingNonceOption: g.IsHawaii(height),
AsyncContractTrie: g.IsGreenland(height),
AddOutOfGasToTransactionLog: !g.IsGreenland(height),
AddChainIDToConfig: g.IsIceland(height),
UseV2Storage: g.IsGreenland(height),
CannotUnstakeAgain: g.IsGreenland(height),
SkipStakingIndexer: !g.IsFairbank(height),
ReturnFetchError: !g.IsGreenland(height),
CannotTranferToSelf: g.IsHawaii(height),
NewStakingReceiptFormat: g.IsFbkMigration(height),
UpdateBlockMeta: g.IsGreenland(height),
CurrentEpochProductivity: g.IsGreenland(height),
FixSnapshotOrder: g.IsKamchatka(height),
AllowCorrectDefaultChainID: g.IsMidway(height),
CorrectGetHashFn: g.IsMidway(height),
CorrectTxLogIndex: g.IsMidway(height),
RevertLog: g.IsMidway(height),
TolerateLegacyAddress: !g.IsNewfoundland(height),
ValidateRewardProtocol: g.IsNewfoundland(height),
},
)
}
// GetFeatureCtx gets FeatureCtx.
func GetFeatureCtx(ctx context.Context) (FeatureCtx, bool) {
fc, ok := ctx.Value(featureContextKey{}).(FeatureCtx)
return fc, ok
}
// MustGetFeatureCtx must get FeatureCtx.
// If context doesn't exist, this function panic.
func MustGetFeatureCtx(ctx context.Context) FeatureCtx {
fc, ok := ctx.Value(featureContextKey{}).(FeatureCtx)
if !ok {
log.S().Panic("Miss feature context")
}
return fc
}
// WithFeatureWithHeightCtx add FeatureWithHeightCtx into context.
func WithFeatureWithHeightCtx(ctx context.Context) context.Context {
g := genesis.MustExtractGenesisContext(ctx)
return context.WithValue(
ctx,
featureWithHeightContextKey{},
FeatureWithHeightCtx{
GetUnproductiveDelegates: func(height uint64) bool {
return !g.IsEaster(height)
},
ReadStateFromDB: func(height uint64) bool {
return g.IsGreenland(height)
},
UseV2Staking: func(height uint64) bool {
return g.IsFairbank(height)
},
EnableNativeStaking: func(height uint64) bool {
return g.IsCook(height)
},
StakingCorrectGas: func(height uint64) bool {
return g.IsDaytona(height)
},
CalculateProbationList: func(height uint64) bool {
return g.IsEaster(height)
},
LoadCandidatesLegacy: func(height uint64) bool {
return !g.IsEaster(height)
},
},
)
}
// GetFeatureWithHeightCtx gets FeatureWithHeightCtx.
func GetFeatureWithHeightCtx(ctx context.Context) (FeatureWithHeightCtx, bool) {
fc, ok := ctx.Value(featureWithHeightContextKey{}).(FeatureWithHeightCtx)
return fc, ok
}
// MustGetFeatureWithHeightCtx must get FeatureWithHeightCtx.
// If context doesn't exist, this function panic.
func MustGetFeatureWithHeightCtx(ctx context.Context) FeatureWithHeightCtx {
fc, ok := ctx.Value(featureWithHeightContextKey{}).(FeatureWithHeightCtx)
if !ok {
log.S().Panic("Miss feature context")
}
return fc
}
// WithVMConfigCtx adds vm config to context
func WithVMConfigCtx(ctx context.Context, vmConfig vm.Config) context.Context {
return context.WithValue(ctx, vmConfigContextKey{}, vmConfig)
}
// GetVMConfigCtx returns the vm config from context
func GetVMConfigCtx(ctx context.Context) (vm.Config, bool) {
cfg, ok := ctx.Value(vmConfigContextKey{}).(vm.Config)
return cfg, ok
}