Skip to content

Commit 439a62a

Browse files
authored
Move registry out of blockchain context (iotexproject#2066)
* remove dependency of factory * address comment * move registry out of blockchain context
1 parent f89638c commit 439a62a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+810
-605
lines changed

action/protocol/account/transfer_test.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ func TestProtocol_HandleTransfer(t *testing.T) {
8181
cfg.Genesis.Rewarding.NumDelegatesForFoundationBonus = 0
8282
cfg.Genesis.Rewarding.FoundationBonusLastEpoch = 0
8383
cfg.Genesis.Rewarding.ProductivityThreshold = 0
84-
ctx = protocol.WithBlockchainCtx(ctx,
84+
ctx = protocol.WithBlockchainCtx(
85+
protocol.WithRegistry(ctx, registry),
8586
protocol.BlockchainCtx{
86-
Registry: registry,
87-
Genesis: cfg.Genesis,
88-
})
87+
Genesis: cfg.Genesis,
88+
},
89+
)
8990
ctx = protocol.WithBlockCtx(ctx,
9091
protocol.BlockCtx{
9192
BlockHeight: 0,
@@ -140,9 +141,12 @@ func TestProtocol_HandleTransfer(t *testing.T) {
140141
Producer: identityset.Address(27),
141142
GasLimit: testutil.TestGasLimit,
142143
})
143-
ctx = protocol.WithBlockchainCtx(ctx, protocol.BlockchainCtx{
144-
Registry: registry,
145-
})
144+
ctx = protocol.WithBlockchainCtx(
145+
protocol.WithRegistry(ctx, registry),
146+
protocol.BlockchainCtx{
147+
Genesis: cfg.Genesis,
148+
},
149+
)
146150

147151
receipt, err := p.Handle(ctx, transfer, sm)
148152
require.NoError(err)

action/protocol/context.go

+22-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/iotexproject/iotex-address/address"
1616
"github.com/iotexproject/iotex-core/blockchain/genesis"
1717
"github.com/iotexproject/iotex-core/pkg/log"
18-
"github.com/iotexproject/iotex-core/state"
1918
)
2019

2120
// TipInfo contains the tip block information
@@ -31,16 +30,14 @@ type blockContextKey struct{}
3130

3231
type actionContextKey struct{}
3332

33+
type registryContextKey struct{}
34+
3435
// BlockchainCtx provides blockchain auxiliary information.
3536
type BlockchainCtx struct {
3637
// Genesis is a copy of current genesis
3738
Genesis genesis.Genesis
38-
// Registry is the pointer protocol registry
39-
Registry *Registry
4039
// Tip is the information of tip block
4140
Tip TipInfo
42-
// Candidates is a list of candidates of current round
43-
Candidates []*state.Candidate
4441
}
4542

4643
// BlockCtx provides block auxiliary information.
@@ -69,6 +66,26 @@ type ActionCtx struct {
6966
Nonce uint64
7067
}
7168

69+
// WithRegistry adds registry to context
70+
func WithRegistry(ctx context.Context, reg *Registry) context.Context {
71+
return context.WithValue(ctx, registryContextKey{}, reg)
72+
}
73+
74+
// GetRegistry returns the registry from context
75+
func GetRegistry(ctx context.Context) (*Registry, bool) {
76+
reg, ok := ctx.Value(registryContextKey{}).(*Registry)
77+
return reg, ok
78+
}
79+
80+
// MustGetRegistry returns the registry from context
81+
func MustGetRegistry(ctx context.Context) *Registry {
82+
reg, ok := ctx.Value(registryContextKey{}).(*Registry)
83+
if !ok {
84+
log.S().Panic("Miss registry context")
85+
}
86+
return reg
87+
}
88+
7289
// WithBlockchainCtx add BlockchainCtx into context.
7390
func WithBlockchainCtx(ctx context.Context, bc BlockchainCtx) context.Context {
7491
return context.WithValue(ctx, blockchainContextKey{}, bc)

action/protocol/context_test.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,32 @@ import (
1919
"github.com/stretchr/testify/require"
2020
)
2121

22+
func TestRegistryCtx(t *testing.T) {
23+
require := require.New(t)
24+
_, ok := GetRegistry(context.Background())
25+
require.False(ok)
26+
require.Panics(func() { MustGetRegistry(context.Background()) }, "Miss registry context")
27+
reg := &Registry{}
28+
ctx := WithRegistry(context.Background(), reg)
29+
require.NotNil(ctx)
30+
regFromCtx, ok := GetRegistry(ctx)
31+
require.True(ok)
32+
require.Equal(reg, regFromCtx)
33+
require.Equal(reg, MustGetRegistry(ctx))
34+
}
35+
2236
func TestWithBlockchainCtx(t *testing.T) {
2337
require := require.New(t)
2438
bcCtx := BlockchainCtx{
25-
Genesis: config.Default.Genesis,
26-
Registry: nil,
39+
Genesis: config.Default.Genesis,
2740
}
2841
require.NotNil(WithBlockchainCtx(context.Background(), bcCtx))
2942
}
3043

3144
func TestGetBlockchainCtx(t *testing.T) {
3245
require := require.New(t)
3346
bcCtx := BlockchainCtx{
34-
Genesis: config.Default.Genesis,
35-
Registry: nil,
47+
Genesis: config.Default.Genesis,
3648
}
3749
ctx := WithBlockchainCtx(context.Background(), bcCtx)
3850
require.NotNil(ctx)
@@ -43,8 +55,7 @@ func TestGetBlockchainCtx(t *testing.T) {
4355
func TestMustGetBlockchainCtx(t *testing.T) {
4456
require := require.New(t)
4557
bcCtx := BlockchainCtx{
46-
Genesis: config.Default.Genesis,
47-
Registry: nil,
58+
Genesis: config.Default.Genesis,
4859
}
4960
require.NotNil(WithBlockchainCtx(context.Background(), bcCtx))
5061
// Case II: Panic

action/protocol/execution/protocol_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ func (sct *SmartContractTest) prepareBlockchain(
304304
indexer, err := blockindex.NewIndexer(db.NewMemKVStore(), cfg.Genesis.Hash())
305305
r.NoError(err)
306306
// create BlockDAO
307-
dao := blockdao.NewBlockDAO(db.NewMemKVStore(), []blockdao.BlockIndexer{indexer}, cfg.Chain.CompressBlock, cfg.DB)
307+
dao := blockdao.NewBlockDAO(db.NewMemKVStore(), []blockdao.BlockIndexer{sf, indexer}, cfg.Chain.CompressBlock, cfg.DB)
308308
r.NotNil(dao)
309309
bc := blockchain.NewBlockchain(
310310
cfg,
@@ -485,7 +485,7 @@ func TestProtocol_Handle(t *testing.T) {
485485
require.NoError(err)
486486
// create BlockDAO
487487
cfg.DB.DbPath = cfg.Chain.ChainDBPath
488-
dao := blockdao.NewBlockDAO(db.NewBoltDB(cfg.DB), []blockdao.BlockIndexer{indexer}, cfg.Chain.CompressBlock, cfg.DB)
488+
dao := blockdao.NewBlockDAO(db.NewBoltDB(cfg.DB), []blockdao.BlockIndexer{sf, indexer}, cfg.Chain.CompressBlock, cfg.DB)
489489
require.NotNil(dao)
490490
bc := blockchain.NewBlockchain(
491491
cfg,

action/protocol/generic_validator.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ func (v *GenericValidator) Validate(ctx context.Context, selp action.SealedEnvel
6565
IntrinsicGas: intrinsicGas,
6666
Nonce: selp.Nonce(),
6767
})
68-
bcCtx := MustGetBlockchainCtx(ctx)
69-
for _, validator := range bcCtx.Registry.All() {
68+
for _, validator := range MustGetRegistry(ctx).All() {
7069
if err := validator.Validate(ctx, selp.Action()); err != nil {
7170
return err
7271
}

action/protocol/generic_validator_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,17 @@ func TestActionProtoAndGenericValidator(t *testing.T) {
4848
Caller: caller,
4949
})
5050

51-
ctx = WithBlockchainCtx(ctx,
51+
ctx = WithBlockchainCtx(
52+
WithRegistry(ctx, reg),
5253
BlockchainCtx{
5354
Genesis: config.Default.Genesis,
5455
Tip: TipInfo{
5556
Height: 0,
5657
Hash: config.Default.Genesis.Hash(),
5758
Timestamp: time.Unix(config.Default.Genesis.Timestamp, 0),
5859
},
59-
Registry: reg,
60-
})
60+
},
61+
)
6162

6263
valid := NewGenericValidator(nil, func(sr StateReader, addr string) (*state.Account, error) {
6364
pk := identityset.PrivateKey(27).PublicKey()

action/protocol/poll/governance_protocol.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (p *governanceChainCommitteeProtocol) CreatePostSystemActions(ctx context.C
115115
func (p *governanceChainCommitteeProtocol) CreatePreStates(ctx context.Context, sm protocol.StateManager) error {
116116
bcCtx := protocol.MustGetBlockchainCtx(ctx)
117117
blkCtx := protocol.MustGetBlockCtx(ctx)
118-
rp := rolldpos.MustGetProtocol(bcCtx.Registry)
118+
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
119119
epochNum := rp.GetEpochNum(blkCtx.BlockHeight)
120120
epochStartHeight := rp.GetEpochHeight(epochNum)
121121
epochLastHeight := rp.GetEpochLastBlockHeight(epochNum)
@@ -225,8 +225,7 @@ func (p *governanceChainCommitteeProtocol) ReadState(
225225
args ...[]byte,
226226
) ([]byte, error) {
227227
blkCtx := protocol.MustGetBlockCtx(ctx)
228-
bcCtx := protocol.MustGetBlockchainCtx(ctx)
229-
rp := rolldpos.MustGetProtocol(bcCtx.Registry)
228+
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
230229
epochNum := rp.GetEpochNum(blkCtx.BlockHeight) // tip
231230
epochStartHeight := rp.GetEpochHeight(epochNum)
232231
switch string(method) {
@@ -339,8 +338,7 @@ func (p *governanceChainCommitteeProtocol) ForceRegister(r *protocol.Registry) e
339338
}
340339

341340
func (p *governanceChainCommitteeProtocol) getGravityHeight(ctx context.Context, height uint64) (uint64, error) {
342-
bcCtx := protocol.MustGetBlockchainCtx(ctx)
343-
rp := rolldpos.MustGetProtocol(bcCtx.Registry)
341+
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
344342
epochNumber := rp.GetEpochNum(height)
345343
epochHeight := rp.GetEpochHeight(epochNumber)
346344
blkTime, err := p.getBlockTime(epochHeight)

action/protocol/poll/governance_protocol_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ func initConstruct(ctrl *gomock.Controller) (Protocol, context.Context, protocol
5454
}
5555
epochStartHeight := rp.GetEpochHeight(2)
5656
ctx = protocol.WithBlockchainCtx(
57-
ctx,
57+
protocol.WithRegistry(ctx, registry),
5858
protocol.BlockchainCtx{
59-
Genesis: cfg.Genesis,
60-
Registry: registry,
59+
Genesis: cfg.Genesis,
6160
Tip: protocol.TipInfo{
6261
Height: epochStartHeight - 1,
6362
},
@@ -261,7 +260,7 @@ func TestCreatePreStates(t *testing.T) {
261260
psc, ok := p.(protocol.PreStatesCreator)
262261
require.True(ok)
263262
bcCtx := protocol.MustGetBlockchainCtx(ctx)
264-
rp := rolldpos.MustGetProtocol(bcCtx.Registry)
263+
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
265264

266265
test := make(map[uint64](map[string]uint32))
267266
test[2] = map[string]uint32{

action/protocol/poll/lifelong_protocol.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ func (p *lifeLongDelegatesProtocol) readBlockProducers() ([]byte, error) {
130130

131131
func (p *lifeLongDelegatesProtocol) readActiveBlockProducers(ctx context.Context, sr protocol.StateReader, readFromNext bool) (state.CandidateList, error) {
132132
var blockProducerList []string
133-
bcCtx := protocol.MustGetBlockchainCtx(ctx)
134-
rp := rolldpos.MustGetProtocol(bcCtx.Registry)
133+
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
135134
blockProducerMap := make(map[string]*state.Candidate)
136135
delegates := p.delegates
137136
if len(p.delegates) > int(rp.NumCandidateDelegates()) {

action/protocol/poll/lifelong_protocol_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ func initLifeLongDelegateProtocol(ctrl *gomock.Controller) (Protocol, context.Co
3131
return nil, nil, nil, err
3232
}
3333
ctx := protocol.WithBlockchainCtx(
34-
context.Background(),
34+
protocol.WithRegistry(context.Background(), registry),
3535
protocol.BlockchainCtx{
36-
Genesis: config.Default.Genesis,
37-
Registry: registry,
36+
Genesis: config.Default.Genesis,
3837
},
3938
)
4039
ctx = protocol.WithActionCtx(

action/protocol/poll/slasher.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ func (sh *Slasher) EmptyBlacklist() *vote.Blacklist {
7777

7878
// GetCandidates returns candidate list
7979
func (sh *Slasher) GetCandidates(ctx context.Context, sr protocol.StateReader, readFromNext bool) (state.CandidateList, error) {
80-
bcCtx := protocol.MustGetBlockchainCtx(ctx)
81-
rp := rolldpos.MustGetProtocol(bcCtx.Registry)
80+
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
8281
targetHeight, err := sr.Height()
8382
if err != nil {
8483
return nil, err
@@ -120,8 +119,7 @@ func (sh *Slasher) GetBlockProducers(ctx context.Context, sr protocol.StateReade
120119

121120
// GetActiveBlockProducers returns active BP list
122121
func (sh *Slasher) GetActiveBlockProducers(ctx context.Context, sr protocol.StateReader, readFromNext bool) (state.CandidateList, error) {
123-
bcCtx := protocol.MustGetBlockchainCtx(ctx)
124-
rp := rolldpos.MustGetProtocol(bcCtx.Registry)
122+
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
125123
targetHeight, err := sr.Height()
126124
if err != nil {
127125
return nil, err
@@ -177,8 +175,7 @@ func (sh *Slasher) GetABPFromIndexer(ctx context.Context, epochStartHeight uint6
177175

178176
// GetKickoutList returns the kick-out list at given epoch
179177
func (sh *Slasher) GetKickoutList(ctx context.Context, sr protocol.StateReader, readFromNext bool) (*vote.Blacklist, error) {
180-
bcCtx := protocol.MustGetBlockchainCtx(ctx)
181-
rp := rolldpos.MustGetProtocol(bcCtx.Registry)
178+
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
182179
targetHeight, err := sr.Height()
183180
if err != nil {
184181
return nil, err
@@ -209,8 +206,7 @@ func (sh *Slasher) CalculateKickoutList(
209206
sm protocol.StateManager,
210207
epochNum uint64,
211208
) (*vote.Blacklist, error) {
212-
bcCtx := protocol.MustGetBlockchainCtx(ctx)
213-
rp := rolldpos.MustGetProtocol(bcCtx.Registry)
209+
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
214210
easterEpochNum := rp.GetEpochNum(sh.hu.EasterBlockHeight())
215211

216212
nextBlacklist := &vote.Blacklist{

action/protocol/poll/staking_committee.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (sc *stakingCommittee) CalculateCandidatesByHeight(ctx context.Context, hei
203203
}
204204

205205
bcCtx := protocol.MustGetBlockchainCtx(ctx)
206-
rp := rolldpos.MustGetProtocol(bcCtx.Registry)
206+
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
207207
hu := config.NewHeightUpgrade(&bcCtx.Genesis)
208208
// convert to epoch start height
209209
if hu.IsPre(config.Cook, rp.GetEpochHeight(rp.GetEpochNum(height))) {
@@ -305,7 +305,7 @@ func (sc *stakingCommittee) persistNativeBuckets(ctx context.Context, receipt *a
305305
// Start to write native buckets archive after cook and only when the action is executed successfully
306306
blkCtx := protocol.MustGetBlockCtx(ctx)
307307
bcCtx := protocol.MustGetBlockchainCtx(ctx)
308-
rp := rolldpos.MustGetProtocol(bcCtx.Registry)
308+
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
309309
epochHeight := rp.GetEpochHeight(rp.GetEpochNum(blkCtx.BlockHeight))
310310
hu := config.NewHeightUpgrade(&bcCtx.Genesis)
311311
if hu.IsPre(config.Cook, epochHeight) {

action/protocol/poll/staking_committee_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,9 @@ func initConstructStakingCommittee(ctrl *gomock.Controller) (Protocol, context.C
5050
return nil, nil, nil, nil, err
5151
}
5252
ctx = protocol.WithBlockchainCtx(
53-
ctx,
53+
protocol.WithRegistry(ctx, registry),
5454
protocol.BlockchainCtx{
55-
Genesis: config.Default.Genesis,
56-
Registry: registry,
55+
Genesis: config.Default.Genesis,
5756
},
5857
)
5958
ctx = protocol.WithActionCtx(

action/protocol/poll/util.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ func validate(ctx context.Context, p Protocol, act action.Action) error {
105105

106106
func createPostSystemActions(ctx context.Context, p Protocol) ([]action.Envelope, error) {
107107
blkCtx := protocol.MustGetBlockCtx(ctx)
108-
bcCtx := protocol.MustGetBlockchainCtx(ctx)
109-
rp := rolldpos.MustGetProtocol(bcCtx.Registry)
108+
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
110109
epochNum := rp.GetEpochNum(blkCtx.BlockHeight)
111110
lastBlkHeight := rp.GetEpochLastBlockHeight(epochNum)
112111
epochHeight := rp.GetEpochHeight(epochNum)
@@ -155,7 +154,7 @@ func setCandidates(
155154
height uint64, // epoch start height
156155
) error {
157156
bcCtx := protocol.MustGetBlockchainCtx(ctx)
158-
rp := rolldpos.MustGetProtocol(bcCtx.Registry)
157+
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
159158
epochNum := rp.GetEpochNum(height)
160159
if height != rp.GetEpochHeight(epochNum) {
161160
return errors.New("put poll result height should be epoch start height")

action/protocol/rewarding/fund.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ func DepositGas(ctx context.Context, sm protocol.StateManager, amount *big.Int)
137137
if blkCtx.BlockHeight == 0 {
138138
return nil
139139
}
140-
bcCtx := protocol.MustGetBlockchainCtx(ctx)
141-
if bcCtx.Registry == nil {
140+
reg, ok := protocol.GetRegistry(ctx)
141+
if !ok {
142142
return nil
143143
}
144-
rp := FindProtocol(bcCtx.Registry)
144+
rp := FindProtocol(reg)
145145
if rp == nil {
146146
return nil
147147
}

action/protocol/rewarding/protocol.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,9 @@ func (p *Protocol) CreatePreStates(ctx context.Context, sm protocol.StateManager
104104

105105
// CreatePostSystemActions creates a list of system actions to be appended to block actions
106106
func (p *Protocol) CreatePostSystemActions(ctx context.Context) ([]action.Envelope, error) {
107-
bcCtx := protocol.MustGetBlockchainCtx(ctx)
108107
blkCtx := protocol.MustGetBlockCtx(ctx)
109108
grants := []action.Envelope{createGrantRewardAction(action.BlockReward, blkCtx.BlockHeight)}
110-
rp := rolldpos.FindProtocol(bcCtx.Registry)
109+
rp := rolldpos.FindProtocol(protocol.MustGetRegistry(ctx))
111110
if rp != nil && blkCtx.BlockHeight == rp.GetEpochLastBlockHeight(rp.GetEpochNum(blkCtx.BlockHeight)) {
112111
grants = append(grants, createGrantRewardAction(action.EpochReward, blkCtx.BlockHeight))
113112
}

0 commit comments

Comments
 (0)