Skip to content

Commit d8420af

Browse files
committed
move block util funcs into blockchain context
1 parent 97a2f95 commit d8420af

31 files changed

+222
-88
lines changed

action/protocol/context.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ type (
5454
ChainID uint32
5555
// EvmNetworkID is the EVM network ID
5656
EvmNetworkID uint32
57+
// GetBlockHash is the function to get block hash by height
58+
GetBlockHash func(uint64) (hash.Hash256, error)
59+
// GetBlockTime is the function to get block time by height
60+
GetBlockTime func(uint64) (time.Time, error)
5761
}
5862

5963
// BlockCtx provides block auxiliary information.

action/protocol/execution/protocol.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,19 @@ const (
2929

3030
// Protocol defines the protocol of handling executions
3131
type Protocol struct {
32-
getBlockHash evm.GetBlockHash
33-
getBlockTime evm.GetBlockTime
34-
depositGas protocol.DepositGas
35-
addr address.Address
32+
depositGas protocol.DepositGas
33+
addr address.Address
3634
}
3735

3836
// NewProtocol instantiates the protocol of exeuction
39-
func NewProtocol(getBlockHash evm.GetBlockHash, depositGas protocol.DepositGas, getBlockTime evm.GetBlockTime) *Protocol {
37+
// TODO: remove unused getBlockHash and getBlockTime
38+
func NewProtocol(_ evm.GetBlockHash, depositGas protocol.DepositGas, _ evm.GetBlockTime) *Protocol {
4039
h := hash.Hash160b([]byte(_protocolID))
4140
addr, err := address.FromBytes(h[:])
4241
if err != nil {
4342
log.L().Panic("Error when constructing the address of vote protocol", zap.Error(err))
4443
}
45-
return &Protocol{getBlockHash: getBlockHash, depositGas: depositGas, addr: addr, getBlockTime: getBlockTime}
44+
return &Protocol{depositGas: depositGas, addr: addr}
4645
}
4746

4847
// FindProtocol finds the registered protocol from registry
@@ -66,9 +65,10 @@ func (p *Protocol) Handle(ctx context.Context, elp action.Envelope, sm protocol.
6665
if _, ok := elp.Action().(*action.Execution); !ok {
6766
return nil, nil
6867
}
68+
bcCtx := protocol.MustGetBlockchainCtx(ctx)
6969
ctx = evm.WithHelperCtx(ctx, evm.HelperContext{
70-
GetBlockHash: p.getBlockHash,
71-
GetBlockTime: p.getBlockTime,
70+
GetBlockHash: bcCtx.GetBlockHash,
71+
GetBlockTime: bcCtx.GetBlockTime,
7272
DepositGasFunc: p.depositGas,
7373
})
7474
_, receipt, err := evm.ExecuteContract(ctx, sm, elp)

action/protocol/execution/protocol_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ func (sct *SmartContractTest) prepareBlockchain(
474474
r.NoError(err)
475475
dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf, indexer}, cfg.DB.MaxCacheSize)
476476
r.NotNil(dao)
477+
btcd := testutil.DummyBlockTimeBuilder()
477478
bc := blockchain.NewBlockchain(
478479
cfg.Chain,
479480
cfg.Genesis,
@@ -483,6 +484,7 @@ func (sct *SmartContractTest) prepareBlockchain(
483484
sf,
484485
protocol.NewGenericValidator(sf, accountutil.AccountState),
485486
)),
487+
blockchain.BlockTimeCalculatorBuilderOption(btcd),
486488
)
487489
reward := rewarding.NewProtocol(cfg.Genesis.Rewarding)
488490
r.NoError(reward.Register(registry))
@@ -735,6 +737,7 @@ func TestProtocol_Handle(t *testing.T) {
735737
sf,
736738
protocol.NewGenericValidator(sf, accountutil.AccountState),
737739
)),
740+
blockchain.BlockTimeCalculatorBuilderOption(testutil.DummyBlockTimeBuilder()),
738741
)
739742
exeProtocol := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGas, getBlockTimeForTest)
740743
require.NoError(exeProtocol.Register(registry))

action/protocol/poll/consortium.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ type consortiumCommittee struct {
5050
bufferResult state.CandidateList
5151
indexer *CandidateIndexer
5252
addr address.Address
53-
getBlockHash evm.GetBlockHash
5453
}
5554

5655
// NewConsortiumCommittee creates a committee for consorium chain
57-
func NewConsortiumCommittee(indexer *CandidateIndexer, readContract ReadContract, getBlockHash evm.GetBlockHash) (Protocol, error) {
56+
// TODO: remove unused getBlockHash
57+
func NewConsortiumCommittee(indexer *CandidateIndexer, readContract ReadContract, _ evm.GetBlockHash) (Protocol, error) {
5858
abi, err := abi.JSON(strings.NewReader(ConsortiumManagementABI))
5959
if err != nil {
6060
return nil, err
@@ -73,7 +73,6 @@ func NewConsortiumCommittee(indexer *CandidateIndexer, readContract ReadContract
7373
abi: abi,
7474
addr: addr,
7575
indexer: indexer,
76-
getBlockHash: getBlockHash,
7776
}, nil
7877
}
7978

@@ -143,7 +142,7 @@ func (cc *consortiumCommittee) CreateGenesisStates(ctx context.Context, sm proto
143142
cc.contract = receipt.ContractAddress
144143

145144
ctx = evm.WithHelperCtx(ctx, evm.HelperContext{
146-
GetBlockHash: cc.getBlockHash,
145+
GetBlockHash: protocol.MustGetBlockchainCtx(ctx).GetBlockHash,
147146
GetBlockTime: getBlockTime,
148147
})
149148
r := getContractReaderForGenesisStates(ctx, sm)

action/protocol/poll/governance_protocol.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
)
2626

2727
type governanceChainCommitteeProtocol struct {
28-
getBlockTime GetBlockTime
2928
electionCommittee committee.Committee
3029
initGravityChainHeight uint64
3130
addr address.Address
@@ -35,20 +34,18 @@ type governanceChainCommitteeProtocol struct {
3534
}
3635

3736
// NewGovernanceChainCommitteeProtocol creates a Poll Protocol which fetch result from governance chain
37+
// TODO: remove getBlockTime
3838
func NewGovernanceChainCommitteeProtocol(
3939
candidatesIndexer *CandidateIndexer,
4040
electionCommittee committee.Committee,
4141
initGravityChainHeight uint64,
42-
getBlockTime GetBlockTime,
42+
_ GetBlockTime,
4343
initialCandidatesInterval time.Duration,
4444
sh *Slasher,
4545
) (Protocol, error) {
4646
if electionCommittee == nil {
4747
return nil, ErrNoElectionCommittee
4848
}
49-
if getBlockTime == nil {
50-
return nil, errors.New("getBlockTime api is not provided")
51-
}
5249

5350
h := hash.Hash160b([]byte(_protocolID))
5451
addr, err := address.FromBytes(h[:])
@@ -59,7 +56,6 @@ func NewGovernanceChainCommitteeProtocol(
5956
return &governanceChainCommitteeProtocol{
6057
electionCommittee: electionCommittee,
6158
initGravityChainHeight: initGravityChainHeight,
62-
getBlockTime: getBlockTime,
6359
addr: addr,
6460
initialCandidatesInterval: initialCandidatesInterval,
6561
sh: sh,
@@ -234,7 +230,8 @@ func (p *governanceChainCommitteeProtocol) getGravityHeight(ctx context.Context,
234230
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
235231
epochNumber := rp.GetEpochNum(height)
236232
epochHeight := rp.GetEpochHeight(epochNumber)
237-
blkTime, err := p.getBlockTime(epochHeight)
233+
bcCtx := protocol.MustGetBlockchainCtx(ctx)
234+
blkTime, err := bcCtx.GetBlockTime(epochHeight)
238235
if err != nil {
239236
return 0, err
240237
}

action/protocol/poll/governance_protocol_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ func initConstruct(ctrl *gomock.Controller) (Protocol, context.Context, protocol
6565
Tip: protocol.TipInfo{
6666
Height: epochStartHeight - 1,
6767
},
68+
GetBlockHash: func(u uint64) (hash.Hash256, error) {
69+
return hash.Hash256b([]byte{0}), nil
70+
},
71+
GetBlockTime: func(h uint64) (time.Time, error) {
72+
return time.Unix(1562382522, 0), nil
73+
},
6874
},
6975
),
7076
cfg.Genesis,

action/protocol/poll/protocol.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ func NewProtocol(
133133
getUnproductiveDelegate GetUnproductiveDelegate,
134134
electionCommittee committee.Committee,
135135
stakingProto *staking.Protocol,
136-
getBlockTimeFunc GetBlockTime,
136+
_ GetBlockTime,
137137
productivity Productivity,
138-
getBlockHash evm.GetBlockHash,
139-
getBlockTime evm.GetBlockTime,
138+
_ evm.GetBlockHash,
139+
_ evm.GetBlockTime,
140140
) (Protocol, error) {
141141
if scheme != _rollDPoSScheme {
142142
return nil, nil
@@ -184,7 +184,7 @@ func NewProtocol(
184184
candidateIndexer,
185185
electionCommittee,
186186
genesisConfig.GravityChainStartHeight,
187-
getBlockTimeFunc,
187+
nil,
188188
chainConfig.PollInitialCandidatesInterval,
189189
slasher,
190190
)
@@ -220,7 +220,7 @@ func NewProtocol(
220220
}
221221
return NewStakingCommand(stakingV1, stakingV2)
222222
case _modeConsortium:
223-
return NewConsortiumCommittee(candidateIndexer, readContract, getBlockHash)
223+
return NewConsortiumCommittee(candidateIndexer, readContract, nil)
224224
default:
225225
return nil, errors.Errorf("unsupported poll mode %s", genesisConfig.PollMode)
226226
}

action/protocol/poll/staking_committee_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ func TestCreatePostSystemActions_StakingCommittee(t *testing.T) {
180180
psac, ok := p.(protocol.PostSystemActionsCreator)
181181
require.True(ok)
182182
ctx = protocol.WithFeatureWithHeightCtx(ctx)
183+
ctx = protocol.WithBlockchainCtx(ctx, protocol.BlockchainCtx{
184+
GetBlockHash: func(uint64) (hash.Hash256, error) {
185+
return hash.ZeroHash256, nil
186+
},
187+
GetBlockTime: func(uint64) (time.Time, error) {
188+
return time.Now(), nil
189+
},
190+
})
183191
elp, err := psac.CreatePostSystemActions(ctx, sr)
184192
require.NoError(err)
185193
require.Equal(1, len(elp))
@@ -331,6 +339,14 @@ func TestHandle_StakingCommittee(t *testing.T) {
331339
},
332340
)
333341
ctx4 = protocol.WithFeatureWithHeightCtx(ctx4)
342+
ctx4 = protocol.WithBlockchainCtx(ctx4, protocol.BlockchainCtx{
343+
GetBlockHash: func(uint64) (hash.Hash256, error) {
344+
return hash.ZeroHash256, nil
345+
},
346+
GetBlockTime: func(uint64) (time.Time, error) {
347+
return time.Now(), nil
348+
},
349+
})
334350
err = p4.Validate(ctx4, elp4, sm4)
335351
require.Contains(err.Error(), "the proposed delegate list length")
336352
})
@@ -361,6 +377,14 @@ func TestHandle_StakingCommittee(t *testing.T) {
361377
Caller: caller,
362378
},
363379
)
380+
ctx5 = protocol.WithBlockchainCtx(ctx5, protocol.BlockchainCtx{
381+
GetBlockHash: func(uint64) (hash.Hash256, error) {
382+
return hash.ZeroHash256, nil
383+
},
384+
GetBlockTime: func(uint64) (time.Time, error) {
385+
return time.Now(), nil
386+
},
387+
})
364388
err = p5.Validate(ctx5, elp5, sm5)
365389
require.Contains(err.Error(), "delegates are not as expected")
366390
})

api/coreservice.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ type (
211211
readCache *ReadCache
212212
actionRadio *ActionRadio
213213
apiStats *nodestats.APILocalStats
214-
getBlockTime evm.GetBlockTime
215214
}
216215

217216
// jobDesc provides a struct to get and store logs in core.LogsInRange
@@ -275,7 +274,6 @@ func newCoreService(
275274
bfIndexer blockindex.BloomFilterIndexer,
276275
actPool actpool.ActPool,
277276
registry *protocol.Registry,
278-
getBlockTime evm.GetBlockTime,
279277
opts ...Option,
280278
) (CoreService, error) {
281279
if cfg == (Config{}) {
@@ -300,7 +298,6 @@ func newCoreService(
300298
chainListener: NewChainListener(cfg.ListenerLimit),
301299
gs: gasstation.NewGasStation(chain, dao, cfg.GasStation),
302300
readCache: NewReadCache(),
303-
getBlockTime: getBlockTime,
304301
}
305302

306303
for _, opt := range opts {
@@ -953,6 +950,10 @@ func (core *coreService) readState(ctx context.Context, p protocol.Protocol, hei
953950
}
954951

955952
// TODO: need to complete the context
953+
ctx, err := core.bc.Context(ctx)
954+
if err != nil {
955+
return nil, 0, err
956+
}
956957
ctx = protocol.WithBlockCtx(ctx, protocol.BlockCtx{
957958
BlockHeight: tipHeight,
958959
})
@@ -2008,15 +2009,20 @@ func (core *coreService) simulateExecution(
20082009
ctx = protocol.WithFeatureCtx(protocol.WithBlockCtx(ctx, protocol.BlockCtx{
20092010
BlockHeight: height,
20102011
}))
2012+
ctx, err = core.bc.Context(ctx)
2013+
if err != nil {
2014+
return nil, nil, status.Error(codes.Internal, err.Error())
2015+
}
2016+
bcCtx := protocol.MustGetBlockchainCtx(ctx)
20112017
if protocol.MustGetFeatureCtx(ctx).UseZeroNonceForFreshAccount {
20122018
pendingNonce = state.PendingNonceConsideringFreshAccount()
20132019
} else {
20142020
pendingNonce = state.PendingNonce()
20152021
}
20162022
elp.SetNonce(pendingNonce)
20172023
ctx = evm.WithHelperCtx(ctx, evm.HelperContext{
2018-
GetBlockHash: core.dao.GetBlockHash,
2019-
GetBlockTime: core.getBlockTime,
2024+
GetBlockHash: bcCtx.GetBlockHash,
2025+
GetBlockTime: bcCtx.GetBlockTime,
20202026
DepositGasFunc: rewarding.DepositGas,
20212027
})
20222028
return evm.SimulateExecution(ctx, ws, addr, elp, opts...)

api/coreservice_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func setupTestCoreService() (CoreService, blockchain.Blockchain, blockdao.BlockD
200200
opts := []Option{WithBroadcastOutbound(func(ctx context.Context, chainID uint32, msg proto.Message) error {
201201
return nil
202202
})}
203-
svr, err := newCoreService(cfg.api, bc, nil, sf, dao, indexer, bfIndexer, ap, registry, func(u uint64) (time.Time, error) { return time.Time{}, nil }, opts...)
203+
svr, err := newCoreService(cfg.api, bc, nil, sf, dao, indexer, bfIndexer, ap, registry, opts...)
204204
if err != nil {
205205
panic(err)
206206
}

0 commit comments

Comments
 (0)