@@ -2,13 +2,20 @@ package cometbft
22
33import (
44 "context"
5+ "encoding/hex"
6+ "fmt"
7+
8+ cmtlight "github.com/cometbft/cometbft/light"
9+ cmttypes "github.com/cometbft/cometbft/types"
510
611 "github.com/oasisprotocol/oasis-core/go/common/identity"
712 "github.com/oasisprotocol/oasis-core/go/config"
813 consensusAPI "github.com/oasisprotocol/oasis-core/go/consensus/api"
914 "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/api"
1015 "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/full"
1116 "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/light"
17+ "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/stateless"
18+ "github.com/oasisprotocol/oasis-core/go/consensus/pricediscovery"
1219 genesisAPI "github.com/oasisprotocol/oasis-core/go/genesis/api"
1320 p2pAPI "github.com/oasisprotocol/oasis-core/go/p2p/api"
1421 "github.com/oasisprotocol/oasis-core/go/p2p/rpc"
@@ -30,7 +37,163 @@ func New(
3037 return nil , err
3138 }
3239
33- commonCfg := full.CommonConfig {
40+ switch config .GlobalConfig .Mode {
41+ case config .ModeArchive :
42+ node , err := createArchiveNode (ctx , dataDir , identity , genesis , doc , genesisDoc )
43+ if err != nil {
44+ return nil , fmt .Errorf ("failed to create archive node: %w" , err )
45+ }
46+ return node , nil
47+ case config .ModeStatelessClient :
48+ node , err := createStatelessNode (ctx , genesis , doc , genesisDoc , p2p )
49+ if err != nil {
50+ return nil , fmt .Errorf ("failed to create stateless node: %w" , err )
51+ }
52+ return node , nil
53+ default :
54+ node , err := createFullNode (ctx , dataDir , identity , genesis , doc , genesisDoc , upgrader , p2p )
55+ if err != nil {
56+ return nil , fmt .Errorf ("failed to create full node: %w" , err )
57+ }
58+ return node , nil
59+ }
60+ }
61+
62+ func createArchiveNode (
63+ ctx context.Context ,
64+ dataDir string ,
65+ identity * identity.Identity ,
66+ genesis genesisAPI.Provider ,
67+ doc * genesisAPI.Document ,
68+ genesisDoc * cmttypes.GenesisDoc ,
69+ ) (consensusAPI.Service , error ) {
70+ cfg := full.ArchiveConfig {
71+ CommonConfig : createCommonConfig (dataDir , identity , genesis , doc , genesisDoc ),
72+ }
73+
74+ return full .NewArchive (ctx , cfg )
75+ }
76+
77+ func createFullNode (
78+ ctx context.Context ,
79+ dataDir string ,
80+ identity * identity.Identity ,
81+ genesis genesisAPI.Provider ,
82+ doc * genesisAPI.Document ,
83+ genesisDoc * cmttypes.GenesisDoc ,
84+ upgrader upgradeAPI.Backend ,
85+ p2p p2pAPI.Service ,
86+ ) (consensusAPI.Service , error ) {
87+ cfg := full.Config {
88+ CommonConfig : createCommonConfig (dataDir , identity , genesis , doc , genesisDoc ),
89+ TimeoutCommit : doc .Consensus .Parameters .TimeoutCommit ,
90+ EmptyBlockInterval : doc .Consensus .Parameters .EmptyBlockInterval ,
91+ SkipTimeoutCommit : doc .Consensus .Parameters .SkipTimeoutCommit ,
92+ Upgrader : upgrader ,
93+ }
94+
95+ return full .New (ctx , p2p , cfg )
96+ }
97+
98+ func createStatelessNode (
99+ ctx context.Context ,
100+ genesis genesisAPI.Provider ,
101+ doc * genesisAPI.Document ,
102+ genesisDoc * cmttypes.GenesisDoc ,
103+ p2p p2pAPI.Service ,
104+ ) (consensusAPI.Service , error ) {
105+ provider , err := createProvider ()
106+ if err != nil {
107+ return nil , fmt .Errorf ("failed to create provider: %w" , err )
108+ }
109+
110+ lightClient , err := createLightClient (ctx , genesisDoc , doc , p2p )
111+ if err != nil {
112+ return nil , fmt .Errorf ("failed to create light client: %w" , err )
113+ }
114+
115+ services , err := createStatelessServices (doc , genesis , genesisDoc , provider , lightClient )
116+ if err != nil {
117+ return nil , fmt .Errorf ("failed to create stateless client: %w" , err )
118+ }
119+
120+ submitter , err := createSubmissionManager (ctx , services )
121+ if err != nil {
122+ return nil , fmt .Errorf ("failed to create submission manager: %w" , err )
123+ }
124+
125+ return stateless .NewService (services , submitter )
126+ }
127+
128+ func createStatelessServices (
129+ doc * genesisAPI.Document ,
130+ genesis genesisAPI.Provider ,
131+ genesisDoc * cmttypes.GenesisDoc ,
132+ provider * consensusAPI.Client ,
133+ lightClient * light.Client ,
134+ ) (* stateless.Services , error ) {
135+ cfg := stateless.Config {
136+ ChainID : doc .ChainID ,
137+ ChainContext : doc .ChainContext (),
138+ Genesis : genesis ,
139+ GenesisDoc : genesisDoc ,
140+ GenesisHeight : doc .Height ,
141+ BaseEpoch : doc .Beacon .Base ,
142+ BaseHeight : doc .Height ,
143+ }
144+
145+ return stateless .NewServices (provider , lightClient , cfg )
146+ }
147+
148+ func createProvider () (* consensusAPI.Client , error ) {
149+ addresses := config .GlobalConfig .Consensus .Providers
150+ if len (addresses ) == 0 {
151+ return nil , fmt .Errorf ("no providers configured" )
152+ }
153+
154+ return stateless .NewProvider (addresses [0 ])
155+ }
156+
157+ func createLightClient (
158+ ctx context.Context ,
159+ genesisDoc * cmttypes.GenesisDoc ,
160+ doc * genesisAPI.Document ,
161+ p2p p2pAPI.Service ,
162+ ) (* light.Client , error ) {
163+ hash , err := hex .DecodeString (config .GlobalConfig .Consensus .LightClient .Trust .Hash )
164+ if err != nil {
165+ return nil , fmt .Errorf ("failed to decode trust hash: %w" , err )
166+ }
167+
168+ cfg := light.Config {
169+ GenesisDocument : genesisDoc ,
170+ TrustOptions : cmtlight.TrustOptions {
171+ Period : config .GlobalConfig .Consensus .LightClient .Trust .Period ,
172+ Height : int64 (config .GlobalConfig .Consensus .LightClient .Trust .Height ),
173+ Hash : hash ,
174+ },
175+ }
176+
177+ return light .NewClient (ctx , doc .ChainContext (), p2p , cfg )
178+ }
179+
180+ func createSubmissionManager (ctx context.Context , services consensusAPI.Services ) (consensusAPI.SubmissionManager , error ) {
181+ pd , err := pricediscovery .New (ctx , services .Core (), config .GlobalConfig .Consensus .Submission .GasPrice )
182+ if err != nil {
183+ return nil , fmt .Errorf ("failed to create price discovery: %w" , err )
184+ }
185+
186+ return consensusAPI .NewSubmissionManager (services , pd , config .GlobalConfig .Consensus .Submission .MaxFee ), nil
187+ }
188+
189+ func createCommonConfig (
190+ dataDir string ,
191+ identity * identity.Identity ,
192+ genesis genesisAPI.Provider ,
193+ doc * genesisAPI.Document ,
194+ genesisDoc * cmttypes.GenesisDoc ,
195+ ) full.CommonConfig {
196+ return full.CommonConfig {
34197 DataDir : dataDir ,
35198 Identity : identity ,
36199 ChainID : doc .ChainID ,
@@ -42,23 +205,6 @@ func New(
42205 BaseHeight : doc .Height ,
43206 PublicKeyBlacklist : doc .Consensus .Parameters .PublicKeyBlacklist ,
44207 }
45-
46- switch config .GlobalConfig .Mode {
47- case config .ModeArchive :
48- cfg := full.ArchiveConfig {
49- CommonConfig : commonCfg ,
50- }
51- return full .NewArchive (ctx , cfg )
52- default :
53- cfg := full.Config {
54- CommonConfig : commonCfg ,
55- TimeoutCommit : doc .Consensus .Parameters .TimeoutCommit ,
56- EmptyBlockInterval : doc .Consensus .Parameters .EmptyBlockInterval ,
57- SkipTimeoutCommit : doc .Consensus .Parameters .SkipTimeoutCommit ,
58- Upgrader : upgrader ,
59- }
60- return full .New (ctx , p2p , cfg )
61- }
62208}
63209
64210// NewLightService creates a new CometBFT light client service.
0 commit comments