Skip to content

Commit d3ad687

Browse files
authored
chore: remove unnecessary conversions (#1427)
Use linter to detect unnecessary type conversions https://github.com/mdempsky/unconvert <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Integrated a new linter to enhance code quality checks. - **Refactor** - Simplified variable declarations and method calls by removing unnecessary type conversions. - **Bug Fixes** - Improved error handling in JSON-RPC responses by correcting type casting. - **Tests** - Updated test cases to align with the refactor of type conversions. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 6eb34bc commit d3ad687

7 files changed

Lines changed: 14 additions & 13 deletions

File tree

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ linters:
2121
- revive
2222
- staticcheck
2323
- typecheck
24+
- unconvert
2425
- unused
2526

2627
issues:

block/manager.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ func (m *Manager) SyncLoop(ctx context.Context, cancel context.CancelFunc) {
337337
block := blockEvent.Block
338338
daHeight := blockEvent.DAHeight
339339
blockHash := block.Hash().String()
340-
blockHeight := uint64(block.Height())
340+
blockHeight := block.Height()
341341
m.logger.Debug("block body retrieved",
342342
"height", blockHeight,
343343
"daHeight", daHeight,
@@ -398,7 +398,7 @@ func (m *Manager) trySyncNextBlock(ctx context.Context, daHeight uint64) error {
398398
return nil
399399
}
400400

401-
bHeight := uint64(b.Height())
401+
bHeight := b.Height()
402402
m.logger.Info("Syncing block", "height", bHeight)
403403
// Validate the received block before applying
404404
if err := m.executor.Validate(m.lastState, b); err != nil {
@@ -417,7 +417,7 @@ func (m *Manager) trySyncNextBlock(ctx context.Context, daHeight uint64) error {
417417
return fmt.Errorf("failed to Commit: %w", err)
418418
}
419419

420-
err = m.store.SaveBlockResponses(uint64(bHeight), responses)
420+
err = m.store.SaveBlockResponses(bHeight, responses)
421421
if err != nil {
422422
return fmt.Errorf("failed to save block responses: %w", err)
423423
}

block/manager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ func TestInitialState(t *testing.T) {
6262
name: "state_in_store",
6363
store: fullStore,
6464
genesis: genesis,
65-
expectedInitialHeight: uint64(sampleState.InitialHeight),
66-
expectedLastBlockHeight: uint64(sampleState.LastBlockHeight),
65+
expectedInitialHeight: sampleState.InitialHeight,
66+
expectedLastBlockHeight: sampleState.LastBlockHeight,
6767
expectedChainID: sampleState.ChainID,
6868
},
6969
}

node/full_client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,12 +869,12 @@ func TestStatus(t *testing.T) {
869869

870870
earliestBlock := getRandomBlockWithProposer(1, 1, pubKey.Bytes())
871871
err = rpc.node.Store.SaveBlock(earliestBlock, &types.Commit{})
872-
rpc.node.Store.SetHeight(uint64(earliestBlock.Height()))
872+
rpc.node.Store.SetHeight(earliestBlock.Height())
873873
require.NoError(err)
874874

875875
latestBlock := getRandomBlockWithProposer(2, 1, pubKey.Bytes())
876876
err = rpc.node.Store.SaveBlock(latestBlock, &types.Commit{})
877-
rpc.node.Store.SetHeight(uint64(latestBlock.Height()))
877+
rpc.node.Store.SetHeight(latestBlock.Height())
878878
require.NoError(err)
879879

880880
err = node.Start()

rpc/json/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func (h *handler) encodeAndWriteResponse(w http.ResponseWriter, result interface
187187
} else {
188188
bytes, err := cmjson.Marshal(result)
189189
if err != nil {
190-
resp.Error = &json2.Error{Code: json2.ErrorCode(json2.E_INTERNAL), Data: err.Error()}
190+
resp.Error = &json2.Error{Code: json2.E_INTERNAL, Data: err.Error()}
191191
} else {
192192
resp.Result = bytes
193193
}

store/store.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (s *DefaultStore) SaveBlock(block *types.Block, commit *types.Commit) error
7777

7878
err = multierr.Append(err, bb.Put(s.ctx, ds.NewKey(getBlockKey(hash)), blockBlob))
7979
err = multierr.Append(err, bb.Put(s.ctx, ds.NewKey(getCommitKey(hash)), commitBlob))
80-
err = multierr.Append(err, bb.Put(s.ctx, ds.NewKey(getIndexKey(uint64(block.Height()))), hash[:]))
80+
err = multierr.Append(err, bb.Put(s.ctx, ds.NewKey(getIndexKey(block.Height())), hash[:]))
8181

8282
if err != nil {
8383
bb.Discard(s.ctx)
@@ -192,7 +192,7 @@ func (s *DefaultStore) GetState() (types.State, error) {
192192

193193
var state types.State
194194
err = state.FromProto(&pbState)
195-
atomic.StoreUint64(&s.height, uint64(state.LastBlockHeight))
195+
atomic.StoreUint64(&s.height, state.LastBlockHeight)
196196
return state, err
197197
}
198198

store/store_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestStoreHeight(t *testing.T) {
4949

5050
for _, block := range c.blocks {
5151
err := bstore.SaveBlock(block, &types.Commit{})
52-
bstore.SetHeight(uint64(block.Height()))
52+
bstore.SetHeight(block.Height())
5353
assert.NoError(err)
5454
}
5555

@@ -111,12 +111,12 @@ func TestStoreLoad(t *testing.T) {
111111
}
112112

113113
for _, expected := range c.blocks {
114-
block, err := bstore.GetBlock(uint64(expected.Height()))
114+
block, err := bstore.GetBlock(expected.Height())
115115
assert.NoError(err)
116116
assert.NotNil(block)
117117
assert.Equal(expected, block)
118118

119-
commit, err := bstore.GetCommit(uint64(expected.Height()))
119+
commit, err := bstore.GetCommit(expected.Height())
120120
assert.NoError(err)
121121
assert.NotNil(commit)
122122
}

0 commit comments

Comments
 (0)