Skip to content

Commit 2c59bdc

Browse files
authored
Merge pull request #6175 from oasisprotocol/peternose/trivial/rm-services-backend
go/consensus/api: Remove services backend
2 parents 9aa96d4 + 31c994d commit 2c59bdc

7 files changed

Lines changed: 99 additions & 106 deletions

File tree

.changelog/6175.trivial.md

Whitespace-only changes.

go/consensus/api/api.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ type P2PStatus struct {
374374
// Backend is an interface that a consensus backend must provide.
375375
type Backend interface {
376376
service.BackgroundService
377-
ServicesBackend
377+
ClientBackend
378378

379379
// SupportedFeatures returns the features supported by this consensus backend.
380380
SupportedFeatures() FeatureMask
@@ -394,25 +394,11 @@ type Backend interface {
394394
// Pruner returns the state pruner.
395395
Pruner() StatePruner
396396

397-
// RegisterP2PService registers the P2P service used for light client state sync.
398-
RegisterP2PService(p2pAPI.Service) error
399-
}
400-
401-
// HaltHook is a function that gets called when consensus needs to halt for some reason.
402-
type HaltHook func(ctx context.Context, blockHeight int64, epoch beacon.EpochTime, err error)
403-
404-
// ServicesBackend is an interface for consensus backends which indicate support for
405-
// communicating with consensus services.
406-
//
407-
// In case the feature is absent, these methods may return nil or ErrUnsupported.
408-
type ServicesBackend interface {
409-
ClientBackend
410-
411-
// RegisterHaltHook registers a function to be called when the consensus needs to halt.
412-
RegisterHaltHook(hook HaltHook)
413-
414397
// SubmissionManager returns the transaction submission manager.
415398
SubmissionManager() SubmissionManager
399+
400+
// RegisterP2PService registers the P2P service used for light client state sync.
401+
RegisterP2PService(p2pAPI.Service) error
416402
}
417403

418404
// StatePruner is a state pruner implementation.

go/consensus/cometbft/abci/mux.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (a *ApplicationServer) Register(app api.Application) error {
134134

135135
// RegisterHaltHook registers a function to be called when the
136136
// consensus Halt epoch height is reached.
137-
func (a *ApplicationServer) RegisterHaltHook(hook consensus.HaltHook) {
137+
func (a *ApplicationServer) RegisterHaltHook(hook api.HaltHook) {
138138
a.mux.registerHaltHook(hook)
139139
}
140140

@@ -224,7 +224,7 @@ type abciMux struct {
224224
appBlessed api.Application
225225

226226
haltOnce sync.Once
227-
haltHooks []consensus.HaltHook
227+
haltHooks []api.HaltHook
228228

229229
// invalidatedTxs maps transaction hashes (hash.Hash) to a subscriber
230230
// waiting for that transaction to become invalid.
@@ -262,7 +262,7 @@ func (mux *abciMux) watchInvalidatedTx(txHash hash.Hash) (<-chan error, pubsub.C
262262
return resultCh, sub, nil
263263
}
264264

265-
func (mux *abciMux) registerHaltHook(hook consensus.HaltHook) {
265+
func (mux *abciMux) registerHaltHook(hook api.HaltHook) {
266266
mux.Lock()
267267
defer mux.Unlock()
268268

go/consensus/cometbft/api/api.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
cmtrpctypes "github.com/cometbft/cometbft/rpc/core/types"
1515
cmttypes "github.com/cometbft/cometbft/types"
1616

17+
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api"
1718
"github.com/oasisprotocol/oasis-core/go/common"
1819
"github.com/oasisprotocol/oasis-core/go/common/cbor"
1920
"github.com/oasisprotocol/oasis-core/go/common/crypto/hash"
@@ -214,6 +215,9 @@ type Backend interface {
214215
WatchCometBFTBlocks() (<-chan *cmttypes.Block, *pubsub.Subscription, error)
215216
}
216217

218+
// HaltHook is a function that gets called when consensus needs to halt for some reason.
219+
type HaltHook func(ctx context.Context, height int64, epoch beacon.EpochTime, err error)
220+
217221
// TransactionAuthHandler is the interface for ABCI applications that handle
218222
// authenticating transactions (checking nonces and fees).
219223
type TransactionAuthHandler interface {

go/consensus/cometbft/full/common.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -506,15 +506,6 @@ func (n *commonNode) Pruner() consensusAPI.StatePruner {
506506
return n.mux.Pruner()
507507
}
508508

509-
// Implements consensusAPI.Backend.
510-
func (n *commonNode) RegisterHaltHook(hook consensusAPI.HaltHook) {
511-
if !n.initialized() {
512-
return
513-
}
514-
515-
n.mux.RegisterHaltHook(hook)
516-
}
517-
518509
func (n *commonNode) heightToCometBFTHeight(height int64) (int64, error) {
519510
var tmHeight int64
520511
if height == consensusAPI.HeightLatest {

go/consensus/cometbft/full/full.go

Lines changed: 88 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/spf13/viper"
3131

3232
beaconAPI "github.com/oasisprotocol/oasis-core/go/beacon/api"
33+
"github.com/oasisprotocol/oasis-core/go/common"
3334
"github.com/oasisprotocol/oasis-core/go/common/cbor"
3435
"github.com/oasisprotocol/oasis-core/go/common/crypto/hash"
3536
"github.com/oasisprotocol/oasis-core/go/common/errors"
@@ -67,6 +68,8 @@ const (
6768
// tmSubscriberID is the subscriber identifier used for all internal CometBFT pubsub
6869
// subscriptions. If any other subscriber IDs need to be derived they will be under this prefix.
6970
tmSubscriberID = "oasis-core"
71+
72+
exportsSubDir = "exports"
7073
)
7174

7275
var (
@@ -764,40 +767,13 @@ func (t *fullService) lazyInit() error { // nolint: gocyclo
764767
t.client = cmtcli.New(t.node)
765768
t.failMonitor = newFailMonitor(t.ctx, t.Logger, t.node.ConsensusState().Wait)
766769

767-
// Register a halt hook that handles upgrades gracefully.
768-
t.RegisterHaltHook(func(_ context.Context, _ int64, _ beaconAPI.EpochTime, err error) {
769-
if !errors.Is(err, upgradeAPI.ErrStopForUpgrade) {
770-
return
771-
}
772-
773-
// Mark this as a clean shutdown and request the node to stop gracefully.
774-
t.failMonitor.markCleanShutdown()
775-
776-
// Wait before stopping to give time for P2P messages to propagate. Sleep for at least
777-
// minUpgradeStopWaitPeriod or the configured commit timeout.
778-
t.Logger.Info("waiting a bit before stopping the node for upgrade")
779-
waitPeriod := minUpgradeStopWaitPeriod
780-
if tc := t.timeoutCommit; tc > waitPeriod {
781-
waitPeriod = tc
782-
}
783-
time.Sleep(waitPeriod)
784-
785-
go func() {
786-
// Sleep another period so there is some time between when consensus shuts down and
787-
// when all the other services start shutting down.
788-
//
789-
// Randomize the period so that not all nodes shut down at the same time.
790-
delay := random.GetRandomValueFromInterval(0.5, rand.Float64(), config.GlobalConfig.Consensus.UpgradeStopDelay)
791-
time.Sleep(delay)
792-
793-
t.Logger.Info("stopping the node for upgrade")
794-
t.Stop()
795-
796-
// Close the quit channel early to force the node to stop. This is needed because
797-
// the CometBFT node will otherwise never quit.
798-
close(t.quitCh)
799-
}()
800-
})
770+
hooks := []api.HaltHook{
771+
t.upgradeHaltHook(),
772+
t.dumpGenesisHaltHook(),
773+
}
774+
for _, hook := range hooks {
775+
t.mux.RegisterHaltHook(hook)
776+
}
801777

802778
return nil
803779
}
@@ -915,6 +891,84 @@ func (t *fullService) metrics() {
915891
}
916892
}
917893

894+
// upgradeHaltHook returns a halt hook that handles upgrades gracefully.
895+
func (t *fullService) upgradeHaltHook() api.HaltHook {
896+
return func(_ context.Context, _ int64, _ beaconAPI.EpochTime, err error) {
897+
if !errors.Is(err, upgradeAPI.ErrStopForUpgrade) {
898+
return
899+
}
900+
901+
// Mark this as a clean shutdown and request the node to stop gracefully.
902+
t.failMonitor.markCleanShutdown()
903+
904+
// Wait before stopping to give time for P2P messages to propagate. Sleep for at least
905+
// minUpgradeStopWaitPeriod or the configured commit timeout.
906+
t.Logger.Info("waiting a bit before stopping the node for upgrade")
907+
waitPeriod := minUpgradeStopWaitPeriod
908+
if tc := t.timeoutCommit; tc > waitPeriod {
909+
waitPeriod = tc
910+
}
911+
time.Sleep(waitPeriod)
912+
913+
go func() {
914+
// Sleep another period so there is some time between when consensus shuts down and
915+
// when all the other services start shutting down.
916+
//
917+
// Randomize the period so that not all nodes shut down at the same time.
918+
delay := random.GetRandomValueFromInterval(0.5, rand.Float64(), config.GlobalConfig.Consensus.UpgradeStopDelay)
919+
time.Sleep(delay)
920+
921+
t.Logger.Info("stopping the node for upgrade")
922+
t.Stop()
923+
924+
// Close the quit channel early to force the node to stop. This is needed because
925+
// the CometBFT node will otherwise never quit.
926+
close(t.quitCh)
927+
}()
928+
}
929+
}
930+
931+
// dumpGenesisHaltHook returns a halt hook which dump genesis.
932+
func (t *fullService) dumpGenesisHaltHook() api.HaltHook {
933+
return func(ctx context.Context, height int64, epoch beaconAPI.EpochTime, _ error) {
934+
t.Logger.Info("consensus halt hook: dumping genesis",
935+
"epoch", epoch,
936+
"height", height,
937+
)
938+
if err := t.dumpGenesis(ctx, height); err != nil {
939+
t.Logger.Error("halt hook: failed to dump genesis",
940+
"err", err,
941+
)
942+
return
943+
}
944+
t.Logger.Info("consensus halt hook: genesis dumped",
945+
"epoch", epoch,
946+
"height", height,
947+
)
948+
}
949+
}
950+
951+
// dumpGenesis writes state at the given height to a genesis file.
952+
func (t *fullService) dumpGenesis(ctx context.Context, height int64) error {
953+
doc, err := t.StateToGenesis(ctx, height)
954+
if err != nil {
955+
return fmt.Errorf("dumpGenesis: failed to get genesis: %w", err)
956+
}
957+
958+
exportsDir := filepath.Join(t.dataDir, exportsSubDir)
959+
960+
if err := common.Mkdir(exportsDir); err != nil {
961+
return fmt.Errorf("dumpGenesis: failed to create exports dir: %w", err)
962+
}
963+
964+
filename := filepath.Join(exportsDir, fmt.Sprintf("genesis-%s-at-%d.json", doc.ChainID, doc.Height))
965+
if err := doc.WriteFileJSON(filename); err != nil {
966+
return fmt.Errorf("dumpGenesis: failed to write genesis file: %w", err)
967+
}
968+
969+
return nil
970+
}
971+
918972
// New creates a new CometBFT consensus backend.
919973
func New(ctx context.Context, cfg Config) (consensusAPI.Backend, error) {
920974
commonNode := newCommonNode(ctx, cfg.CommonConfig)

go/oasis-node/cmd/node/node.go

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ package node
44
import (
55
"context"
66
"fmt"
7-
"path/filepath"
87
"sync"
98

109
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api"
11-
"github.com/oasisprotocol/oasis-core/go/common"
1210
"github.com/oasisprotocol/oasis-core/go/common/crash"
1311
"github.com/oasisprotocol/oasis-core/go/common/grpc"
1412
"github.com/oasisprotocol/oasis-core/go/common/identity"
@@ -54,8 +52,6 @@ import (
5452
workerStorage "github.com/oasisprotocol/oasis-core/go/worker/storage"
5553
)
5654

57-
const exportsSubDir = "exports"
58-
5955
// Node is the Oasis node service.
6056
//
6157
// WARNING: This is exposed for the benefit of tests and the interface
@@ -172,24 +168,6 @@ func (n *Node) startRuntimeServices(genesisDoc *genesisAPI.Document) error {
172168
governanceAPI.RegisterService(grpcSrv, n.Consensus.Governance())
173169
vaultAPI.RegisterService(grpcSrv, n.Consensus.Vault())
174170

175-
// Register dump genesis halt hook.
176-
n.Consensus.RegisterHaltHook(func(ctx context.Context, blockHeight int64, epoch beacon.EpochTime, _ error) {
177-
n.logger.Info("Consensus halt hook: dumping genesis",
178-
"epoch", epoch,
179-
"block_height", blockHeight,
180-
)
181-
if err = n.dumpGenesis(ctx, blockHeight); err != nil {
182-
n.logger.Error("halt hook: failed to dump genesis",
183-
"err", err,
184-
)
185-
return
186-
}
187-
n.logger.Info("Consensus halt hook: genesis dumped",
188-
"epoch", epoch,
189-
"block_height", blockHeight,
190-
)
191-
})
192-
193171
// Initialize runtime workers.
194172
if err = n.initRuntimeWorkers(genesisDoc); err != nil {
195173
n.logger.Error("failed to initialize workers",
@@ -399,26 +377,6 @@ func (n *Node) startRuntimeWorkers() error {
399377
return nil
400378
}
401379

402-
func (n *Node) dumpGenesis(ctx context.Context, blockHeight int64) error {
403-
doc, err := n.Consensus.StateToGenesis(ctx, blockHeight)
404-
if err != nil {
405-
return fmt.Errorf("dumpGenesis: failed to get genesis: %w", err)
406-
}
407-
408-
exportsDir := filepath.Join(n.dataDir, exportsSubDir)
409-
410-
if err := common.Mkdir(exportsDir); err != nil {
411-
return fmt.Errorf("dumpGenesis: failed to create exports dir: %w", err)
412-
}
413-
414-
filename := filepath.Join(exportsDir, fmt.Sprintf("genesis-%s-at-%d.json", doc.ChainID, doc.Height))
415-
if err := doc.WriteFileJSON(filename); err != nil {
416-
return fmt.Errorf("dumpGenesis: failed to write genesis file: %w", err)
417-
}
418-
419-
return nil
420-
}
421-
422380
// NewNode initializes and launches the Oasis node service.
423381
//
424382
// WARNING: This will misbehave iff cmd != RootCommand(). This is exposed

0 commit comments

Comments
 (0)