Skip to content

Commit f18b490

Browse files
authored
Merge pull request #6138 from oasisprotocol/peternose/trivial/minor-fixes
go: Fix and simplify code
2 parents 364e01e + 8637d78 commit f18b490

10 files changed

Lines changed: 53 additions & 118 deletions

File tree

.changelog/6138.trivial.md

Whitespace-only changes.

go/consensus/cometbft/full/common.go

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -684,32 +684,26 @@ func (n *commonNode) GetTransactions(ctx context.Context, height int64) ([][]byt
684684
}
685685

686686
txs := make([][]byte, 0, len(blk.Data.Txs))
687-
for _, v := range blk.Data.Txs {
688-
txs = append(txs, v[:])
687+
for _, tx := range blk.Data.Txs {
688+
txs = append(txs, tx[:])
689689
}
690690
return txs, nil
691691
}
692692

693693
// Implements consensusAPI.Backend.
694694
func (n *commonNode) GetTransactionsWithResults(ctx context.Context, height int64) (*consensusAPI.TransactionsWithResults, error) {
695-
var txsWithResults consensusAPI.TransactionsWithResults
696-
697-
blk, err := n.GetCometBFTBlock(ctx, height)
695+
txs, err := n.GetTransactions(ctx, height)
698696
if err != nil {
699697
return nil, err
700698
}
701-
if blk == nil {
702-
return nil, consensusAPI.ErrNoCommittedBlocks
703-
}
704-
for _, tx := range blk.Data.Txs {
705-
txsWithResults.Transactions = append(txsWithResults.Transactions, tx[:])
706-
}
707699

708-
res, err := n.GetBlockResults(ctx, blk.Height)
700+
blockResults, err := n.GetBlockResults(ctx, height)
709701
if err != nil {
710702
return nil, err
711703
}
712-
for txIdx, rs := range res.TxsResults {
704+
705+
txResults := make([]*results.Result, 0, len(txs))
706+
for idx, rs := range blockResults.TxsResults {
713707
// Transaction result.
714708
result := &results.Result{
715709
Error: results.Error{
@@ -721,11 +715,7 @@ func (n *commonNode) GetTransactionsWithResults(ctx context.Context, height int6
721715
}
722716

723717
// Transaction staking events.
724-
stakingEvents, err := tmstaking.EventsFromCometBFT(
725-
txsWithResults.Transactions[txIdx],
726-
blk.Height,
727-
rs.Events,
728-
)
718+
stakingEvents, err := tmstaking.EventsFromCometBFT(txs[idx], height, rs.Events)
729719
if err != nil {
730720
return nil, err
731721
}
@@ -734,11 +724,7 @@ func (n *commonNode) GetTransactionsWithResults(ctx context.Context, height int6
734724
}
735725

736726
// Transaction registry events.
737-
registryEvents, _, err := tmregistry.EventsFromCometBFT(
738-
txsWithResults.Transactions[txIdx],
739-
blk.Height,
740-
rs.Events,
741-
)
727+
registryEvents, _, err := tmregistry.EventsFromCometBFT(txs[idx], height, rs.Events)
742728
if err != nil {
743729
return nil, err
744730
}
@@ -747,11 +733,7 @@ func (n *commonNode) GetTransactionsWithResults(ctx context.Context, height int6
747733
}
748734

749735
// Transaction roothash events.
750-
roothashEvents, err := tmroothash.EventsFromCometBFT(
751-
txsWithResults.Transactions[txIdx],
752-
blk.Height,
753-
rs.Events,
754-
)
736+
roothashEvents, err := tmroothash.EventsFromCometBFT(txs[idx], height, rs.Events)
755737
if err != nil {
756738
return nil, err
757739
}
@@ -760,21 +742,21 @@ func (n *commonNode) GetTransactionsWithResults(ctx context.Context, height int6
760742
}
761743

762744
// Transaction governance events.
763-
governanceEvents, err := tmgovernance.EventsFromCometBFT(
764-
txsWithResults.Transactions[txIdx],
765-
blk.Height,
766-
rs.Events,
767-
)
745+
governanceEvents, err := tmgovernance.EventsFromCometBFT(txs[idx], height, rs.Events)
768746
if err != nil {
769747
return nil, err
770748
}
771749
for _, e := range governanceEvents {
772750
result.Events = append(result.Events, &results.Event{Governance: e})
773751
}
774752

775-
txsWithResults.Results = append(txsWithResults.Results, result)
753+
txResults = append(txResults, result)
776754
}
777-
return &txsWithResults, nil
755+
756+
return &consensusAPI.TransactionsWithResults{
757+
Transactions: txs,
758+
Results: txResults,
759+
}, nil
778760
}
779761

780762
// Implements consensusAPI.Backend.

go/consensus/cometbft/full/full.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,12 +734,14 @@ func (t *fullService) lazyInit() error { // nolint: gocyclo
734734
Hash: cometConfig.StateSync.TrustHashBytes(),
735735
},
736736
}
737-
if stateProvider, err = newStateProvider(t.ctx, t.chainContext, cfg, t.p2p); err != nil {
738-
t.Logger.Error("failed to create state sync state provider",
737+
lightClient, err := light.NewClient(t.ctx, t.chainContext, t.p2p, cfg)
738+
if err != nil {
739+
t.Logger.Error("failed to create light client",
739740
"err", err,
740741
)
741-
return fmt.Errorf("failed to create state sync state provider: %w", err)
742+
return fmt.Errorf("failed to create light client: %w", err)
742743
}
744+
stateProvider = newStateProvider(t.chainContext, t.genesisHeight, lightClient)
743745
}
744746

745747
t.node, err = cmtnode.NewNode(cometConfig,

go/consensus/cometbft/full/statesync.go

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,29 @@ package full
33
import (
44
"context"
55
"fmt"
6-
"sync"
76

87
cmtstate "github.com/cometbft/cometbft/state"
98
cmtstatesync "github.com/cometbft/cometbft/statesync"
109
cmttypes "github.com/cometbft/cometbft/types"
1110

1211
"github.com/oasisprotocol/oasis-core/go/common/logging"
1312
"github.com/oasisprotocol/oasis-core/go/common/version"
13+
cmtAPI "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/api"
1414
"github.com/oasisprotocol/oasis-core/go/consensus/cometbft/light"
15-
"github.com/oasisprotocol/oasis-core/go/p2p/rpc"
1615
)
1716

1817
type stateProvider struct {
19-
sync.Mutex
20-
21-
lc *light.Client
22-
genesisDocument *cmttypes.GenesisDoc
18+
chainID string
19+
genesisHeight int64
20+
lightClient *light.Client
2321

2422
logger *logging.Logger
2523
}
2624

2725
// Implements cmtstatesync.StateProvider.
2826
func (sp *stateProvider) AppHash(ctx context.Context, height uint64) ([]byte, error) {
29-
sp.Lock()
30-
defer sp.Unlock()
31-
3227
// We have to fetch the next height, which contains the app hash for the previous height.
33-
lb, err := sp.lc.GetVerifiedLightBlock(ctx, int64(height+1))
28+
lb, err := sp.lightClient.GetVerifiedLightBlock(ctx, int64(height+1))
3429
if err != nil {
3530
return nil, err
3631
}
@@ -39,10 +34,7 @@ func (sp *stateProvider) AppHash(ctx context.Context, height uint64) ([]byte, er
3934

4035
// Implements cmtstatesync.StateProvider.
4136
func (sp *stateProvider) Commit(ctx context.Context, height uint64) (*cmttypes.Commit, error) {
42-
sp.Lock()
43-
defer sp.Unlock()
44-
45-
lb, err := sp.lc.GetVerifiedLightBlock(ctx, int64(height))
37+
lb, err := sp.lightClient.GetVerifiedLightBlock(ctx, int64(height))
4638
if err != nil {
4739
return nil, err
4840
}
@@ -51,13 +43,10 @@ func (sp *stateProvider) Commit(ctx context.Context, height uint64) (*cmttypes.C
5143

5244
// Implements cmtstatesync.StateProvider.
5345
func (sp *stateProvider) State(ctx context.Context, height uint64) (cmtstate.State, error) {
54-
sp.Lock()
55-
defer sp.Unlock()
56-
5746
state := cmtstate.State{
58-
ChainID: sp.genesisDocument.ChainID,
47+
ChainID: sp.chainID,
5948
Version: cmtstate.InitStateVersion,
60-
InitialHeight: sp.genesisDocument.InitialHeight,
49+
InitialHeight: sp.genesisHeight,
6150
}
6251
// XXX: This will fail in case an upgrade happened in-between.
6352
state.Version.Consensus.App = version.CometBFTAppVersion
@@ -70,15 +59,15 @@ func (sp *stateProvider) State(ctx context.Context, height uint64) (cmtstate.Sta
7059
//
7160
// We need to fetch the NextValidators from height+2 because if the application changed
7261
// the validator set at the snapshot height then this only takes effect at height+2.
73-
lastLightBlock, err := sp.lc.GetVerifiedLightBlock(ctx, int64(height))
62+
lastLightBlock, err := sp.lightClient.GetVerifiedLightBlock(ctx, int64(height))
7463
if err != nil {
7564
return cmtstate.State{}, err
7665
}
77-
curLightBlock, err := sp.lc.GetVerifiedLightBlock(ctx, int64(height)+1)
66+
curLightBlock, err := sp.lightClient.GetVerifiedLightBlock(ctx, int64(height)+1)
7867
if err != nil {
7968
return cmtstate.State{}, err
8069
}
81-
nextLightBlock, err := sp.lc.GetVerifiedLightBlock(ctx, int64(height)+2)
70+
nextLightBlock, err := sp.lightClient.GetVerifiedLightBlock(ctx, int64(height)+2)
8271
if err != nil {
8372
return cmtstate.State{}, err
8473
}
@@ -93,7 +82,7 @@ func (sp *stateProvider) State(ctx context.Context, height uint64) (cmtstate.Sta
9382
state.LastHeightValidatorsChanged = nextLightBlock.Height
9483

9584
// Fetch consensus parameters with light client verification.
96-
params, err := sp.lc.GetVerifiedParameters(ctx, nextLightBlock.Height)
85+
params, err := sp.lightClient.GetVerifiedParameters(ctx, nextLightBlock.Height)
9786
if err != nil {
9887
return cmtstate.State{}, fmt.Errorf("failed to fetch consensus parameters for height %d: %w",
9988
nextLightBlock.Height,
@@ -105,15 +94,13 @@ func (sp *stateProvider) State(ctx context.Context, height uint64) (cmtstate.Sta
10594
return state, nil
10695
}
10796

108-
func newStateProvider(ctx context.Context, chainContext string, cfg light.Config, p2p rpc.P2P) (cmtstatesync.StateProvider, error) {
109-
lc, err := light.NewClient(ctx, chainContext, p2p, cfg)
110-
if err != nil {
111-
return nil, err
112-
}
97+
func newStateProvider(chainContext string, genesisHeight int64, lightClient *light.Client) cmtstatesync.StateProvider {
98+
chainID := cmtAPI.CometBFTChainID(chainContext)
11399

114100
return &stateProvider{
115-
lc: lc,
116-
genesisDocument: cfg.GenesisDocument,
117-
logger: logging.GetLogger("consensus/cometbft/stateprovider"),
118-
}, nil
101+
chainID: chainID,
102+
genesisHeight: genesisHeight,
103+
lightClient: lightClient,
104+
logger: logging.GetLogger("consensus/cometbft/stateprovider"),
105+
}
119106
}

go/consensus/cometbft/registry/registry.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ type NodeListEpochInternalEvent struct {
5050
Height int64 `json:"height"`
5151
}
5252

53-
func (sc *serviceClient) Querier() *app.QueryFactory {
54-
return sc.querier
55-
}
56-
5753
func (sc *serviceClient) GetEntity(ctx context.Context, query *api.IDQuery) (*entity.Entity, error) {
5854
q, err := sc.querier.QueryAt(ctx, query.Height)
5955
if err != nil {

go/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ require (
5656
go.uber.org/zap v1.27.0
5757
golang.org/x/crypto v0.36.0
5858
golang.org/x/exp v0.0.0-20250128182459-e0ece0dbea4c
59-
golang.org/x/net v0.37.0
59+
golang.org/x/net v0.38.0
6060
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1
6161
google.golang.org/grpc v1.68.0
6262
google.golang.org/grpc/security/advancedtls v0.0.0-20221004221323-12db695f1648

go/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,8 +1939,8 @@ golang.org/x/net v0.13.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
19391939
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
19401940
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
19411941
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
1942-
golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c=
1943-
golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
1942+
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
1943+
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
19441944
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
19451945
golang.org/x/oauth2 v0.0.0-20181017192945-9dcd33a902f4/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
19461946
golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=

go/runtime/history/indexer.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func (bi *BlockIndexer) run(ctx context.Context) {
167167
}
168168

169169
func (bi *BlockIndexer) index(ctx context.Context, blkCh <-chan *roothash.AnnotatedBlock) {
170-
bi.logger.Debug("indexing")
170+
bi.logger.Info("indexing")
171171

172172
bi.mu.Lock()
173173
bi.status = statusIndexing
@@ -184,7 +184,6 @@ func (bi *BlockIndexer) index(ctx context.Context, blkCh <-chan *roothash.Annota
184184
blks = append(blks, blk)
185185
case <-time.After(retry):
186186
case <-ctx.Done():
187-
bi.logger.Info("stopping")
188187
return
189188
}
190189

@@ -194,6 +193,7 @@ func (bi *BlockIndexer) index(ctx context.Context, blkCh <-chan *roothash.Annota
194193
}
195194

196195
if err := bi.commitBlocks(blks); err != nil {
196+
bi.logger.Warn("failed to commit blocks", "err", err)
197197
retry = boff.NextBackOff()
198198
continue
199199
}
@@ -205,7 +205,7 @@ func (bi *BlockIndexer) index(ctx context.Context, blkCh <-chan *roothash.Annota
205205
}
206206

207207
func (bi *BlockIndexer) reindex(ctx context.Context, blkCh <-chan *roothash.AnnotatedBlock) error {
208-
bi.logger.Debug("reindexing",
208+
bi.logger.Info("reindexing",
209209
logging.LogEvent, roothash.LogEventHistoryReindexing,
210210
)
211211

@@ -237,7 +237,7 @@ func (bi *BlockIndexer) reindex(ctx context.Context, blkCh <-chan *roothash.Anno
237237
}
238238
// Reindex blocks up to the specified height.
239239
if err := bi.reindexTo(ctx, height); err != nil {
240-
bi.logger.Error("failed to reindex blocks",
240+
bi.logger.Warn("failed to reindex blocks",
241241
"err", err,
242242
"height", height,
243243
)
@@ -275,27 +275,21 @@ func (bi *BlockIndexer) reindex(ctx context.Context, blkCh <-chan *roothash.Anno
275275
// This ensures that there is always at least one block in the history.
276276
height, err := bi.history.LastConsensusHeight()
277277
if err != nil {
278-
bi.logger.Error("failed to fetch last consensus height",
279-
"err", err,
280-
)
281-
return err
278+
return fmt.Errorf("failed to fetch last consensus height: %w", err)
282279
}
283280
if height != blk.Height {
284281
if err := bi.commitBlocks([]*roothash.AnnotatedBlock{blk}); err != nil {
285-
return err
282+
return fmt.Errorf("failed to commit blocks: %w", err)
286283
}
287284
}
288285

289-
bi.logger.Debug("reindex completed")
286+
bi.logger.Info("reindex completed")
290287
return nil
291288
}
292289

293290
func (bi *BlockIndexer) reindexTo(ctx context.Context, height int64) error {
294291
lastHeight, err := bi.history.LastConsensusHeight()
295292
if err != nil {
296-
bi.logger.Error("failed to get last indexed height",
297-
"err", err,
298-
)
299293
return fmt.Errorf("failed to get last indexed height: %w", err)
300294
}
301295
startHeight := lastHeight + 1 // +1 since we want the last non-seen height.
@@ -361,10 +355,6 @@ func (bi *BlockIndexer) reindexRange(ctx context.Context, start int64, end int64
361355
)
362356
continue
363357
default:
364-
bi.logger.Error("failed to get runtime state",
365-
"err", err,
366-
"height", height,
367-
)
368358
return fmt.Errorf("failed to get runtime state: %w", err)
369359
}
370360
if state.LastBlockHeight != height {
@@ -380,7 +370,7 @@ func (bi *BlockIndexer) reindexRange(ctx context.Context, start int64, end int64
380370
}
381371

382372
if err := bi.commitBlocks(blocks); err != nil {
383-
return err
373+
return fmt.Errorf("failed to commit blocks: %w", err)
384374
}
385375

386376
bi.mu.Lock()
@@ -402,10 +392,7 @@ func (bi *BlockIndexer) commitBlocks(blocks []*roothash.AnnotatedBlock) error {
402392
)
403393

404394
if err := bi.history.Commit(blocks); err != nil {
405-
bi.logger.Error("failed to commit blocks",
406-
"err", err,
407-
)
408-
return fmt.Errorf("failed to commit blocks: %w", err)
395+
return err
409396
}
410397

411398
bi.mu.Lock()

0 commit comments

Comments
 (0)