Skip to content

Commit 5e032cb

Browse files
qdm12timwu20
authored andcommitted
chore(globalvars): remove EmptyHash, add IsEmpty() method on common.Hash (ChainSafe#1922)
1 parent 3e3e9a6 commit 5e032cb

25 files changed

+98
-68
lines changed

dot/core/service.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ func (s *Service) GetMetadata(bhash *common.Hash) ([]byte, error) {
552552
// QueryStorage returns the key-value data by block based on `keys` params
553553
// on every block starting `from` until `to` block, if `to` is not nil
554554
func (s *Service) QueryStorage(from, to common.Hash, keys ...string) (map[common.Hash]QueryKeyValueChanges, error) {
555-
if to == common.EmptyHash {
555+
if to.IsEmpty() {
556556
to = s.blockState.BestBlockHash()
557557
}
558558

@@ -606,19 +606,20 @@ func (s *Service) tryQueryStorage(block common.Hash, keys ...string) (QueryKeyVa
606606

607607
// GetReadProofAt will return an array with the proofs for the keys passed as params
608608
// based on the block hash passed as param as well, if block hash is nil then the current state will take place
609-
func (s *Service) GetReadProofAt(block common.Hash, keys [][]byte) (common.Hash, [][]byte, error) {
610-
if common.EmptyHash.Equal(block) {
609+
func (s *Service) GetReadProofAt(block common.Hash, keys [][]byte) (
610+
hash common.Hash, proofForKeys [][]byte, err error) {
611+
if block.IsEmpty() {
611612
block = s.blockState.BestBlockHash()
612613
}
613614

614615
stateRoot, err := s.blockState.GetBlockStateRoot(block)
615616
if err != nil {
616-
return common.EmptyHash, nil, err
617+
return hash, nil, err
617618
}
618619

619-
proofForKeys, err := s.storageState.GenerateTrieProof(stateRoot, keys)
620+
proofForKeys, err = s.storageState.GenerateTrieProof(stateRoot, keys)
620621
if err != nil {
621-
return common.EmptyHash, nil, err
622+
return hash, nil, err
622623
}
623624

624625
return block, proofForKeys, nil

dot/core/service_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ func TestGetReadProofAt(t *testing.T) {
817817
storageState: mockStorageStage,
818818
}
819819

820-
b, p, err := s.GetReadProofAt(common.EmptyHash, keysToProof)
820+
b, p, err := s.GetReadProofAt(common.Hash{}, keysToProof)
821821
require.NoError(t, err)
822822
require.Equal(t, p, mockedProofs)
823823
require.Equal(t, expectedBlockHash, b)
@@ -832,14 +832,14 @@ func TestGetReadProofAt(t *testing.T) {
832832

833833
mockBlockState := new(mocks.BlockState)
834834
mockBlockState.On("GetBlockStateRoot", mockedBlockHash).
835-
Return(common.EmptyHash, errors.New("problems while getting state root"))
835+
Return(common.Hash{}, errors.New("problems while getting state root"))
836836

837837
s := &Service{
838838
blockState: mockBlockState,
839839
}
840840

841841
b, p, err := s.GetReadProofAt(mockedBlockHash, keysToProof)
842-
require.Equal(t, common.EmptyHash, b)
842+
require.True(t, b.IsEmpty())
843843
require.Nil(t, p)
844844
require.Error(t, err)
845845
})
@@ -862,7 +862,7 @@ func TestGetReadProofAt(t *testing.T) {
862862
}
863863

864864
b, p, err := s.GetReadProofAt(mockedBlockHash, keysToProof)
865-
require.Equal(t, common.EmptyHash, b)
865+
require.True(t, b.IsEmpty())
866866
require.Nil(t, p)
867867
require.Error(t, err)
868868
})

dot/rpc/modules/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ func (sm *StateModule) GetStorageSize(_ *http.Request, req *StateStorageSizeRequ
405405

406406
// QueryStorage isn't implemented properly yet.
407407
func (sm *StateModule) QueryStorage(_ *http.Request, req *StateStorageQueryRangeRequest, res *[]StorageChangeSetResponse) error {
408-
if req.StartBlock == common.EmptyHash {
408+
if req.StartBlock.IsEmpty() {
409409
return errors.New("the start block hash cannot be an empty value")
410410
}
411411

dot/rpc/modules/state_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,14 +480,13 @@ func TestGetReadProof_WhenCoreAPIReturnsError(t *testing.T) {
480480
coreAPIMock := new(mocks.CoreAPI)
481481
coreAPIMock.
482482
On("GetReadProofAt", mock.AnythingOfType("common.Hash"), mock.AnythingOfType("[][]uint8")).
483-
Return(common.EmptyHash, nil, errors.New("mocked error"))
483+
Return(common.Hash{}, nil, errors.New("mocked error"))
484484

485485
sm := new(StateModule)
486486
sm.coreAPI = coreAPIMock
487487

488488
req := &StateGetReadProofRequest{
489489
Keys: []string{},
490-
Hash: common.EmptyHash,
491490
}
492491
err := sm.GetReadProof(nil, req, nil)
493492
require.Error(t, err, "mocked error")
@@ -507,7 +506,6 @@ func TestGetReadProof_WhenReturnsProof(t *testing.T) {
507506

508507
req := &StateGetReadProofRequest{
509508
Keys: []string{},
510-
Hash: common.EmptyHash,
511509
}
512510

513511
res := new(StateGetReadProofResponse)

dot/rpc/subscription/listeners_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ func TestStorageObserver_Update(t *testing.T) {
6565
Value: []byte("value"),
6666
}}
6767
change := &state.SubscriptionResult{
68-
Hash: common.Hash{},
6968
Changes: data,
7069
}
7170

@@ -261,7 +260,6 @@ func TestGrandpaJustification_Listen(t *testing.T) {
261260
mockedJust := grandpa.Justification{
262261
Round: 1,
263262
Commit: grandpa.Commit{
264-
Hash: common.Hash{},
265263
Number: 1,
266264
Precommits: nil,
267265
},

dot/rpc/subscription/websocket_test.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ func TestWSConn_HandleComm(t *testing.T) {
220220
mockedJust := grandpa.Justification{
221221
Round: 1,
222222
Commit: grandpa.Commit{
223-
Hash: common.Hash{},
224223
Number: 1,
225224
Precommits: nil,
226225
},
@@ -248,8 +247,7 @@ func TestWSConn_HandleComm(t *testing.T) {
248247

249248
listener.Listen()
250249
header := &types.Header{
251-
ParentHash: common.Hash{},
252-
Number: big.NewInt(1),
250+
Number: big.NewInt(1),
253251
}
254252

255253
fCh <- &types.FinalisationInfo{
@@ -307,21 +305,18 @@ func TestSubscribeAllHeads(t *testing.T) {
307305

308306
expected := fmt.Sprintf(
309307
`{"jsonrpc":"2.0","method":"chain_allHead","params":{"result":{"parentHash":"%s","number":"0x00","stateRoot":"%s","extrinsicsRoot":"%s","digest":{"logs":["0x064241424504ff"]}},"subscription":1}}`,
310-
common.EmptyHash,
311-
common.EmptyHash,
312-
common.EmptyHash,
308+
common.Hash{},
309+
common.Hash{},
310+
common.Hash{},
313311
)
314312

315313
digest := types.NewDigest()
316314
err = digest.Add(*types.NewBABEPreRuntimeDigest([]byte{0xff}))
317315
require.NoError(t, err)
318316
fCh <- &types.FinalisationInfo{
319317
Header: types.Header{
320-
ParentHash: common.EmptyHash,
321-
Number: big.NewInt(0),
322-
StateRoot: common.EmptyHash,
323-
ExtrinsicsRoot: common.EmptyHash,
324-
Digest: digest,
318+
Number: big.NewInt(0),
319+
Digest: digest,
325320
},
326321
}
327322

@@ -336,11 +331,8 @@ func TestSubscribeAllHeads(t *testing.T) {
336331

337332
iCh <- &types.Block{
338333
Header: types.Header{
339-
ParentHash: common.EmptyHash,
340-
Number: big.NewInt(0),
341-
StateRoot: common.EmptyHash,
342-
ExtrinsicsRoot: common.EmptyHash,
343-
Digest: digest,
334+
Number: big.NewInt(0),
335+
Digest: digest,
344336
},
345337
}
346338
time.Sleep(time.Millisecond * 500)

dot/services.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func createRuntime(cfg *Config, ns runtime.NodeStorage, st *state.Service, ks *k
9999
// check if code substitute is in use, if so replace code
100100
codeSubHash := st.Base.LoadCodeSubstitutedBlockHash()
101101

102-
if !codeSubHash.Equal(common.Hash{}) {
102+
if !codeSubHash.IsEmpty() {
103103
logger.Info("🔄 detected runtime code substitution, upgrading...", "block", codeSubHash)
104104
genData, err := st.Base.LoadGenesisData() // nolint
105105
if err != nil {

dot/state/block.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,10 +497,11 @@ func (bs *BlockState) BestBlockStateRoot() (common.Hash, error) {
497497
}
498498

499499
// GetBlockStateRoot returns the state root of the given block hash
500-
func (bs *BlockState) GetBlockStateRoot(bhash common.Hash) (common.Hash, error) {
500+
func (bs *BlockState) GetBlockStateRoot(bhash common.Hash) (
501+
hash common.Hash, err error) {
501502
header, err := bs.GetHeader(bhash)
502503
if err != nil {
503-
return common.EmptyHash, err
504+
return hash, err
504505
}
505506

506507
return header.StateRoot, nil

dot/state/block_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func TestAddBlock(t *testing.T) {
158158
t.Fatal("got panic when processing retBlock.Header.Hash() ", r)
159159
}
160160
}()
161-
require.NotEqual(t, hash, common.Hash{})
161+
require.False(t, hash.IsEmpty())
162162
}()
163163

164164
require.Equal(t, block1, retBlock, "Could not validate returned block1 as expected")

dot/sync/bootstrap_syncer.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ func (s *bootstrapSyncer) handleNewPeerState(ps *peerState) (*worker, error) {
5050
}
5151

5252
return &worker{
53-
startHash: common.EmptyHash,
5453
startNumber: big.NewInt(0).Add(head.Number, big.NewInt(1)),
5554
targetHash: ps.hash,
5655
targetNumber: ps.number,
@@ -90,7 +89,7 @@ func (s *bootstrapSyncer) handleWorkerResult(res *worker) (*worker, error) {
9089
}
9190

9291
return &worker{
93-
startHash: common.EmptyHash, // for bootstrap, just use number
92+
startHash: common.Hash{}, // for bootstrap, just use number
9493
startNumber: startNumber,
9594
targetHash: res.targetHash,
9695
targetNumber: res.targetNumber,

0 commit comments

Comments
 (0)