@@ -3,34 +3,29 @@ package full
33import (
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
1817type 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.
2826func (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.
4136func (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.
5345func (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}
0 commit comments