Skip to content

Commit 398ec50

Browse files
authored
definition of splitted contexts (iotexproject#1691)
* define splitted contexts
1 parent 6b1e339 commit 398ec50

File tree

2 files changed

+311
-8
lines changed

2 files changed

+311
-8
lines changed

action/protocol/context.go

+109-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,115 @@ func GetValidateActionsCtx(ctx context.Context) (ValidateActionsCtx, bool) {
9898
func MustGetValidateActionsCtx(ctx context.Context) ValidateActionsCtx {
9999
va, ok := ctx.Value(validateActionsCtxKey{}).(ValidateActionsCtx)
100100
if !ok {
101-
log.S().Panic("Miss run actions context")
101+
log.S().Panic("Miss validate actions context")
102102
}
103103
return va
104104
}
105+
106+
// TODO: replace RunActionsCtx and ValidateActionsCtx with below classified independent contexts
107+
108+
type blockchainContextKey struct{}
109+
110+
type blockContextKey struct{}
111+
112+
type actionContextKey struct{}
113+
114+
// BlockchainCtx provides blockchain auxiliary information.
115+
type BlockchainCtx struct {
116+
// Genesis is a copy of current genesis
117+
Genesis genesis.Genesis
118+
// History indicates whether to save account/contract history or not
119+
History bool
120+
// Registry is the pointer protocol registry
121+
Registry *Registry
122+
}
123+
124+
// BlockCtx provides block auxiliary information.
125+
type BlockCtx struct {
126+
// height of block containing those actions
127+
BlockHeight uint64
128+
// timestamp of block containing those actions
129+
BlockTimeStamp time.Time
130+
// gas Limit for perform those actions
131+
GasLimit uint64
132+
// Producer is the address of whom composes the block containing this action
133+
Producer address.Address
134+
}
135+
136+
// ActionCtx provides action auxiliary information.
137+
type ActionCtx struct {
138+
// Caller is the address of whom issues this action
139+
Caller address.Address
140+
// ActionHash is the hash of the action with the sealed envelope
141+
ActionHash hash.Hash256
142+
// GasPrice is the action gas price
143+
GasPrice *big.Int
144+
// IntrinsicGas is the action intrinsic gas
145+
IntrinsicGas uint64
146+
// Nonce is the nonce of the action
147+
Nonce uint64
148+
// History indicates whether to save account/contract history or not
149+
}
150+
151+
// WithBlockchainCtx add BlockchainCtx into context.
152+
func WithBlockchainCtx(ctx context.Context, bc BlockchainCtx) context.Context {
153+
return context.WithValue(ctx, blockchainContextKey{}, bc)
154+
}
155+
156+
// GetBlockchainCtx gets BlockchainCtx
157+
func GetBlockchainCtx(ctx context.Context) (BlockchainCtx, bool) {
158+
bc, ok := ctx.Value(blockchainContextKey{}).(BlockchainCtx)
159+
return bc, ok
160+
}
161+
162+
// MustGetBlockchainCtx must get BlockchainCtx.
163+
// If context doesn't exist, this function panic.
164+
func MustGetBlockchainCtx(ctx context.Context) BlockchainCtx {
165+
bc, ok := ctx.Value(blockchainContextKey{}).(BlockchainCtx)
166+
if !ok {
167+
log.S().Panic("Miss blockchain context")
168+
}
169+
return bc
170+
}
171+
172+
// WithBlockCtx add BlockCtx into context.
173+
func WithBlockCtx(ctx context.Context, blk BlockCtx) context.Context {
174+
return context.WithValue(ctx, blockContextKey{}, blk)
175+
}
176+
177+
// GetBlockCtx gets BlockCtx
178+
func GetBlockCtx(ctx context.Context) (BlockCtx, bool) {
179+
blk, ok := ctx.Value(blockContextKey{}).(BlockCtx)
180+
return blk, ok
181+
}
182+
183+
// MustGetBlockCtx must get BlockCtx .
184+
// If context doesn't exist, this function panic.
185+
func MustGetBlockCtx(ctx context.Context) BlockCtx {
186+
blk, ok := ctx.Value(blockContextKey{}).(BlockCtx)
187+
if !ok {
188+
log.S().Panic("Miss block context")
189+
}
190+
return blk
191+
}
192+
193+
// WithActionCtx add ActionCtx into context.
194+
func WithActionCtx(ctx context.Context, ac ActionCtx) context.Context {
195+
return context.WithValue(ctx, actionContextKey{}, ac)
196+
}
197+
198+
// GetActionCtx gets ActionCtx
199+
func GetActionCtx(ctx context.Context) (ActionCtx, bool) {
200+
ac, ok := ctx.Value(actionContextKey{}).(ActionCtx)
201+
return ac, ok
202+
}
203+
204+
// MustGetActionCtx must get ActionCtx .
205+
// If context doesn't exist, this function panic.
206+
func MustGetActionCtx(ctx context.Context) ActionCtx {
207+
ac, ok := ctx.Value(actionContextKey{}).(ActionCtx)
208+
if !ok {
209+
log.S().Panic("Miss action context")
210+
}
211+
return ac
212+
}

action/protocol/context_test.go

+202-7
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,41 @@ func TestWithRunActionsCtx(t *testing.T) {
2323
require := require.New(t)
2424
addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
2525
require.NoError(err)
26-
actionCtx := RunActionsCtx{1, time.Now(), 1, config.Default.Genesis, addr, addr, hash.ZeroHash256, nil, 0, 0, false, nil}
26+
actionCtx := RunActionsCtx{
27+
BlockHeight: 1111,
28+
BlockTimeStamp: time.Now(),
29+
GasLimit: 1,
30+
Genesis: config.Default.Genesis,
31+
Producer: addr,
32+
Caller: addr,
33+
ActionHash: hash.ZeroHash256,
34+
GasPrice: nil,
35+
IntrinsicGas: 0,
36+
Nonce: 0,
37+
History: false,
38+
Registry: nil,
39+
}
2740
require.NotNil(WithRunActionsCtx(context.Background(), actionCtx))
2841
}
2942

3043
func TestGetRunActionsCtx(t *testing.T) {
3144
require := require.New(t)
3245
addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
3346
require.NoError(err)
34-
actionCtx := RunActionsCtx{1111, time.Now(), 1, config.Default.Genesis, addr, addr, hash.ZeroHash256, nil, 0, 0, false, nil}
47+
actionCtx := RunActionsCtx{
48+
BlockHeight: 1111,
49+
BlockTimeStamp: time.Now(),
50+
GasLimit: 1,
51+
Genesis: config.Default.Genesis,
52+
Producer: addr,
53+
Caller: addr,
54+
ActionHash: hash.ZeroHash256,
55+
GasPrice: nil,
56+
IntrinsicGas: 0,
57+
Nonce: 0,
58+
History: false,
59+
Registry: nil,
60+
}
3561
ctx := WithRunActionsCtx(context.Background(), actionCtx)
3662
require.NotNil(ctx)
3763
ret, ok := GetRunActionsCtx(ctx)
@@ -43,7 +69,20 @@ func TestMustGetRunActionsCtx(t *testing.T) {
4369
require := require.New(t)
4470
addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
4571
require.NoError(err)
46-
actionCtx := RunActionsCtx{1111, time.Now(), 1, config.Default.Genesis, addr, addr, hash.ZeroHash256, nil, 0, 0, false, nil}
72+
actionCtx := RunActionsCtx{
73+
BlockHeight: 1111,
74+
BlockTimeStamp: time.Now(),
75+
GasLimit: 1,
76+
Genesis: config.Default.Genesis,
77+
Producer: addr,
78+
Caller: addr,
79+
ActionHash: hash.ZeroHash256,
80+
GasPrice: nil,
81+
IntrinsicGas: 0,
82+
Nonce: 0,
83+
History: false,
84+
Registry: nil,
85+
}
4786
ctx := WithRunActionsCtx(context.Background(), actionCtx)
4887
require.NotNil(ctx)
4988
// Case I: Normal
@@ -57,14 +96,24 @@ func TestWithValidateActionsCtx(t *testing.T) {
5796
require := require.New(t)
5897
addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
5998
require.NoError(err)
60-
validateCtx := ValidateActionsCtx{1, "io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms", addr, config.Default.Genesis}
99+
validateCtx := ValidateActionsCtx{
100+
BlockHeight: 1,
101+
ProducerAddr: "io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms",
102+
Caller: addr,
103+
Genesis: config.Default.Genesis,
104+
}
61105
require.NotNil(WithValidateActionsCtx(context.Background(), validateCtx))
62106
}
63107
func TestGetValidateActionsCtx(t *testing.T) {
64108
require := require.New(t)
65109
addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
66110
require.NoError(err)
67-
validateCtx := ValidateActionsCtx{1111, "io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms", addr, config.Default.Genesis}
111+
validateCtx := ValidateActionsCtx{
112+
BlockHeight: 1111,
113+
ProducerAddr: "io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms",
114+
Caller: addr,
115+
Genesis: config.Default.Genesis,
116+
}
68117
ctx := WithValidateActionsCtx(context.Background(), validateCtx)
69118
require.NotNil(ctx)
70119
ret, ok := GetValidateActionsCtx(ctx)
@@ -75,12 +124,158 @@ func TestMustGetValidateActionsCtx(t *testing.T) {
75124
require := require.New(t)
76125
addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
77126
require.NoError(err)
78-
validateCtx := ValidateActionsCtx{1111, "io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms", addr, config.Default.Genesis}
127+
validateCtx := ValidateActionsCtx{
128+
BlockHeight: 1111,
129+
ProducerAddr: "io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms",
130+
Caller: addr,
131+
Genesis: config.Default.Genesis,
132+
}
79133
ctx := WithValidateActionsCtx(context.Background(), validateCtx)
80134
require.NotNil(ctx)
81135
// Case I: Normal
82136
ret := MustGetValidateActionsCtx(ctx)
83137
require.Equal(uint64(1111), ret.BlockHeight)
84138
// Case II: Panic
85-
require.Panics(func() { MustGetValidateActionsCtx(context.Background()) }, "Miss run actions context")
139+
require.Panics(func() { MustGetValidateActionsCtx(context.Background()) }, "Miss validate action context")
140+
}
141+
142+
func TestWithBlockchainCtx(t *testing.T) {
143+
require := require.New(t)
144+
bcCtx := BlockchainCtx{
145+
Genesis: config.Default.Genesis,
146+
History: false,
147+
Registry: nil,
148+
}
149+
require.NotNil(WithBlockchainCtx(context.Background(), bcCtx))
150+
}
151+
152+
func TestGetBlockchainCtx(t *testing.T) {
153+
require := require.New(t)
154+
bcCtx := BlockchainCtx{
155+
Genesis: config.Default.Genesis,
156+
History: false,
157+
Registry: nil,
158+
}
159+
ctx := WithBlockchainCtx(context.Background(), bcCtx)
160+
require.NotNil(ctx)
161+
ret, ok := GetBlockchainCtx(ctx)
162+
require.True(ok)
163+
require.Equal(false, ret.History)
164+
}
165+
166+
func TestMustGetBlockchainCtx(t *testing.T) {
167+
require := require.New(t)
168+
bcCtx := BlockchainCtx{
169+
Genesis: config.Default.Genesis,
170+
History: false,
171+
Registry: nil,
172+
}
173+
ctx := WithBlockchainCtx(context.Background(), bcCtx)
174+
require.NotNil(ctx)
175+
// Case I: Normal
176+
ret := MustGetBlockchainCtx(ctx)
177+
require.Equal(false, ret.History)
178+
// Case II: Panic
179+
require.Panics(func() { MustGetBlockchainCtx(context.Background()) }, "Miss blockchain context")
180+
}
181+
182+
func TestWithBlockCtx(t *testing.T) {
183+
require := require.New(t)
184+
addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
185+
require.NoError(err)
186+
blkCtx := BlockCtx{
187+
BlockHeight: 1111,
188+
BlockTimeStamp: time.Now(),
189+
GasLimit: 1,
190+
Producer: addr,
191+
}
192+
require.NotNil(WithBlockCtx(context.Background(), blkCtx))
193+
}
194+
195+
func TestGetBlockCtx(t *testing.T) {
196+
require := require.New(t)
197+
addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
198+
require.NoError(err)
199+
blkCtx := BlockCtx{
200+
BlockHeight: 1111,
201+
BlockTimeStamp: time.Now(),
202+
GasLimit: 1,
203+
Producer: addr,
204+
}
205+
ctx := WithBlockCtx(context.Background(), blkCtx)
206+
require.NotNil(ctx)
207+
ret, ok := GetBlockCtx(ctx)
208+
require.True(ok)
209+
require.Equal(uint64(1111), ret.BlockHeight)
210+
}
211+
212+
func TestMustGetBlockCtx(t *testing.T) {
213+
require := require.New(t)
214+
addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
215+
require.NoError(err)
216+
blkCtx := BlockCtx{
217+
BlockHeight: 1111,
218+
BlockTimeStamp: time.Now(),
219+
GasLimit: 1,
220+
Producer: addr,
221+
}
222+
ctx := WithBlockCtx(context.Background(), blkCtx)
223+
require.NotNil(ctx)
224+
// Case I: Normal
225+
ret := MustGetBlockCtx(ctx)
226+
require.Equal(uint64(1111), ret.BlockHeight)
227+
// Case II: Panic
228+
require.Panics(func() { MustGetBlockCtx(context.Background()) }, "Miss block context")
229+
}
230+
231+
func TestWithActionCtx(t *testing.T) {
232+
require := require.New(t)
233+
addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
234+
require.NoError(err)
235+
actionCtx := ActionCtx{
236+
Caller: addr,
237+
ActionHash: hash.ZeroHash256,
238+
GasPrice: nil,
239+
IntrinsicGas: 0,
240+
Nonce: 0,
241+
}
242+
require.NotNil(WithActionCtx(context.Background(), actionCtx))
243+
}
244+
245+
func TestGetActionCtx(t *testing.T) {
246+
require := require.New(t)
247+
addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
248+
require.NoError(err)
249+
actionCtx := ActionCtx{
250+
Caller: addr,
251+
ActionHash: hash.ZeroHash256,
252+
GasPrice: nil,
253+
IntrinsicGas: 0,
254+
Nonce: 0,
255+
}
256+
ctx := WithActionCtx(context.Background(), actionCtx)
257+
require.NotNil(ctx)
258+
ret, ok := GetActionCtx(ctx)
259+
require.True(ok)
260+
require.Equal(hash.ZeroHash256, ret.ActionHash)
261+
}
262+
263+
func TestMustGetActionCtx(t *testing.T) {
264+
require := require.New(t)
265+
addr, err := address.FromString("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms")
266+
require.NoError(err)
267+
actionCtx := ActionCtx{
268+
Caller: addr,
269+
ActionHash: hash.ZeroHash256,
270+
GasPrice: nil,
271+
IntrinsicGas: 0,
272+
Nonce: 0,
273+
}
274+
ctx := WithActionCtx(context.Background(), actionCtx)
275+
require.NotNil(ctx)
276+
// Case I: Normal
277+
ret := MustGetActionCtx(ctx)
278+
require.Equal(hash.ZeroHash256, ret.ActionHash)
279+
// Case II: Panic
280+
require.Panics(func() { MustGetActionCtx(context.Background()) }, "Miss action context")
86281
}

0 commit comments

Comments
 (0)