Skip to content

Commit bef5335

Browse files
authored
Merge pull request #6148 from oasisprotocol/peternose/trivial/decouple-apps-client-services
go/consensus/cometbft/full: Decouple apps from client services
2 parents d7d40f6 + 9555dc0 commit bef5335

92 files changed

Lines changed: 821 additions & 1035 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.changelog/6148.trivial.md

Whitespace-only changes.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package abci
2+
3+
import (
4+
"context"
5+
6+
consensusState "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/abci/state"
7+
abciAPI "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/api"
8+
consensusGenesis "github.com/oasisprotocol/oasis-core/go/consensus/genesis"
9+
)
10+
11+
// Query is the consensus backend query interface.
12+
type Query interface {
13+
ChainContext(ctx context.Context) (string, error)
14+
ConsensusParameters(ctx context.Context) (*consensusGenesis.Parameters, error)
15+
}
16+
17+
// QueryFactory is the consensus backend query factory.
18+
type QueryFactory struct {
19+
state abciAPI.ApplicationQueryState
20+
}
21+
22+
// NewQueryFactory returns a new QueryFactory backed by the given state
23+
// instance.
24+
func NewQueryFactory(state abciAPI.ApplicationQueryState) *QueryFactory {
25+
return &QueryFactory{state}
26+
}
27+
28+
// QueryAt returns the consensus backend query interface for a specific height.
29+
func (f *QueryFactory) QueryAt(ctx context.Context, height int64) (Query, error) {
30+
state, err := abciAPI.NewImmutableStateAt(ctx, f.state, height)
31+
if err != nil {
32+
return nil, err
33+
}
34+
return &consensusQuerier{
35+
state: consensusState.NewImmutableState(state),
36+
}, nil
37+
}
38+
39+
type consensusQuerier struct {
40+
state *consensusState.ImmutableState
41+
}
42+
43+
func (q *consensusQuerier) ChainContext(ctx context.Context) (string, error) {
44+
return q.state.ChainContext(ctx)
45+
}
46+
47+
func (q *consensusQuerier) ConsensusParameters(ctx context.Context) (*consensusGenesis.Parameters, error) {
48+
return q.state.ConsensusParameters(ctx)
49+
}

go/consensus/cometbft/abci/state/state.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,6 @@ func NewImmutableState(tree mkvs.ImmutableKeyValueTree) *ImmutableState {
3434
}
3535
}
3636

37-
// NewImmutableStateAt creates a new immutable consensus backend state wrapper
38-
// using the provided application query state and version.
39-
func NewImmutableStateAt(ctx context.Context, state api.ApplicationQueryState, version int64) (*ImmutableState, error) {
40-
is, err := api.NewImmutableStateAt(ctx, state, version)
41-
if err != nil {
42-
return nil, err
43-
}
44-
45-
return &ImmutableState{is}, nil
46-
}
47-
4837
// ChainContext returns the stored chain context.
4938
func (s *ImmutableState) ChainContext(ctx context.Context) (string, error) {
5039
chainContext, err := s.state.Get(ctx, chainContextKeyFmt.Encode())

go/consensus/cometbft/api/api.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,6 @@ func NewBlock(blk *cmttypes.Block) *consensus.Block {
202202
type Backend interface {
203203
consensus.Backend
204204

205-
// RegisterApplication registers an ABCI multiplexer application
206-
// with this service instance and check that its dependencies are
207-
// registered.
208-
RegisterApplication(Application) error
209-
210205
// SetTransactionAuthHandler configures the transaction fee handler for the
211206
// ABCI multiplexer.
212207
SetTransactionAuthHandler(TransactionAuthHandler) error

go/consensus/cometbft/apps/beacon/backend_insecure.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
type backendInsecure struct {
16-
app *beaconApplication
16+
app *Application
1717
}
1818

1919
func (impl *backendInsecure) OnInitChain(

go/consensus/cometbft/apps/beacon/backend_vrf.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
var vrfAlphaDomainsep = []byte("oasis-core:vrf/alpha")
2323

2424
type backendVRF struct {
25-
app *beaconApplication
25+
app *Application
2626
}
2727

2828
func (impl *backendVRF) OnInitChain(

go/consensus/cometbft/apps/beacon/beacon.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,46 +16,42 @@ import (
1616
beaconState "github.com/oasisprotocol/oasis-core/go/consensus/cometbft/apps/beacon/state"
1717
)
1818

19-
var (
20-
prodEntropyCtx = []byte("EkB-tmnt")
19+
var prodEntropyCtx = []byte("EkB-tmnt")
2120

22-
_ api.Application = (*beaconApplication)(nil)
23-
)
24-
25-
type beaconApplication struct {
21+
type Application struct {
2622
state api.ApplicationState
2723

2824
backend internalBackend
2925
}
3026

31-
func (app *beaconApplication) Name() string {
27+
func (app *Application) Name() string {
3228
return AppName
3329
}
3430

35-
func (app *beaconApplication) ID() uint8 {
31+
func (app *Application) ID() uint8 {
3632
return AppID
3733
}
3834

39-
func (app *beaconApplication) Methods() []transaction.MethodName {
35+
func (app *Application) Methods() []transaction.MethodName {
4036
return Methods
4137
}
4238

43-
func (app *beaconApplication) Blessed() bool {
39+
func (app *Application) Blessed() bool {
4440
return false
4541
}
4642

47-
func (app *beaconApplication) Dependencies() []string {
43+
func (app *Application) Dependencies() []string {
4844
return nil
4945
}
5046

51-
func (app *beaconApplication) OnRegister(state api.ApplicationState, _ api.MessageDispatcher) {
47+
func (app *Application) OnRegister(state api.ApplicationState, _ api.MessageDispatcher) {
5248
app.state = state
5349
}
5450

55-
func (app *beaconApplication) OnCleanup() {
51+
func (app *Application) OnCleanup() {
5652
}
5753

58-
func (app *beaconApplication) BeginBlock(ctx *api.Context) error {
54+
func (app *Application) BeginBlock(ctx *api.Context) error {
5955
state := beaconState.NewMutableState(ctx.State())
6056

6157
params, err := state.ConsensusParameters(ctx)
@@ -70,11 +66,11 @@ func (app *beaconApplication) BeginBlock(ctx *api.Context) error {
7066
return app.backend.OnBeginBlock(ctx, state, params)
7167
}
7268

73-
func (app *beaconApplication) ExecuteMessage(*api.Context, any, any) (any, error) {
69+
func (app *Application) ExecuteMessage(*api.Context, any, any) (any, error) {
7470
return nil, fmt.Errorf("beacon: unexpected message")
7571
}
7672

77-
func (app *beaconApplication) ExecuteTx(ctx *api.Context, tx *transaction.Transaction) error {
73+
func (app *Application) ExecuteTx(ctx *api.Context, tx *transaction.Transaction) error {
7874
if app.backend == nil {
7975
// Executing a transaction before BeginBlock -- likely during transaction simulation or
8076
// checks. Fail the transaction, it may be retried.
@@ -93,15 +89,15 @@ func (app *beaconApplication) ExecuteTx(ctx *api.Context, tx *transaction.Transa
9389
return app.backend.ExecuteTx(ctx, state, params, tx)
9490
}
9591

96-
func (app *beaconApplication) EndBlock(*api.Context) (types.ResponseEndBlock, error) {
92+
func (app *Application) EndBlock(*api.Context) (types.ResponseEndBlock, error) {
9793
return types.ResponseEndBlock{}, nil
9894
}
9995

100-
func (app *beaconApplication) doEmitEpochEvent(ctx *api.Context, epoch beacon.EpochTime) {
96+
func (app *Application) doEmitEpochEvent(ctx *api.Context, epoch beacon.EpochTime) {
10197
ctx.EmitEvent(api.NewEventBuilder(app.Name()).TypedAttribute(&beacon.EpochEvent{Epoch: epoch}))
10298
}
10399

104-
func (app *beaconApplication) scheduleEpochTransitionBlock(
100+
func (app *Application) scheduleEpochTransitionBlock(
105101
ctx *api.Context,
106102
state *beaconState.MutableState,
107103
nextEpoch beacon.EpochTime,
@@ -119,7 +115,7 @@ func (app *beaconApplication) scheduleEpochTransitionBlock(
119115
return nil
120116
}
121117

122-
func (app *beaconApplication) onNewBeacon(ctx *api.Context, value []byte) error {
118+
func (app *Application) onNewBeacon(ctx *api.Context, value []byte) error {
123119
state := beaconState.NewMutableState(ctx.State())
124120

125121
if err := state.SetBeacon(ctx, value); err != nil {
@@ -135,8 +131,8 @@ func (app *beaconApplication) onNewBeacon(ctx *api.Context, value []byte) error
135131
}
136132

137133
// New constructs a new beacon application instance.
138-
func New() api.Application {
139-
return &beaconApplication{}
134+
func New() *Application {
135+
return &Application{}
140136
}
141137

142138
// insecureBlockEntropy returns insecure entropy based on deterministic block data.

go/consensus/cometbft/apps/beacon/genesis.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
genesis "github.com/oasisprotocol/oasis-core/go/genesis/api"
1313
)
1414

15-
func (app *beaconApplication) InitChain(ctx *api.Context, _ types.RequestInitChain, doc *genesis.Document) error {
15+
func (app *Application) InitChain(ctx *api.Context, _ types.RequestInitChain, doc *genesis.Document) error {
1616
params := &doc.Beacon.Parameters
1717

1818
// Note: If we ever decide that we need a beacon for the 0th epoch
@@ -38,7 +38,7 @@ func (app *beaconApplication) InitChain(ctx *api.Context, _ types.RequestInitCha
3838
return nil
3939
}
4040

41-
func (app *beaconApplication) doInitBackend(params *beacon.ConsensusParameters) error {
41+
func (app *Application) doInitBackend(params *beacon.ConsensusParameters) error {
4242
if app.backend != nil {
4343
return nil
4444
}

go/consensus/cometbft/apps/beacon/query.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,41 @@ type QueryFactory struct {
2424
}
2525

2626
// QueryAt returns the beacon query interface for a specific height.
27-
func (sf *QueryFactory) QueryAt(ctx context.Context, height int64) (Query, error) {
28-
state, err := beaconState.NewImmutableStateAt(ctx, sf.state, height)
27+
func (f *QueryFactory) QueryAt(ctx context.Context, height int64) (Query, error) {
28+
state, err := abciAPI.NewImmutableStateAt(ctx, f.state, height)
2929
if err != nil {
3030
return nil, err
3131
}
32-
return &beaconQuerier{state}, nil
32+
return &beaconQuerier{
33+
state: beaconState.NewImmutableState(state),
34+
}, nil
3335
}
3436

3537
type beaconQuerier struct {
3638
state *beaconState.ImmutableState
3739
}
3840

39-
func (bq *beaconQuerier) Beacon(ctx context.Context) ([]byte, error) {
40-
return bq.state.Beacon(ctx)
41+
func (q *beaconQuerier) Beacon(ctx context.Context) ([]byte, error) {
42+
return q.state.Beacon(ctx)
4143
}
4244

43-
func (bq *beaconQuerier) Epoch(ctx context.Context) (beacon.EpochTime, int64, error) {
44-
return bq.state.GetEpoch(ctx)
45+
func (q *beaconQuerier) Epoch(ctx context.Context) (beacon.EpochTime, int64, error) {
46+
return q.state.GetEpoch(ctx)
4547
}
4648

47-
func (bq *beaconQuerier) FutureEpoch(ctx context.Context) (*beacon.EpochTimeState, error) {
48-
return bq.state.GetFutureEpoch(ctx)
49+
func (q *beaconQuerier) FutureEpoch(ctx context.Context) (*beacon.EpochTimeState, error) {
50+
return q.state.GetFutureEpoch(ctx)
4951
}
5052

51-
func (bq *beaconQuerier) ConsensusParameters(ctx context.Context) (*beacon.ConsensusParameters, error) {
52-
return bq.state.ConsensusParameters(ctx)
53+
func (q *beaconQuerier) ConsensusParameters(ctx context.Context) (*beacon.ConsensusParameters, error) {
54+
return q.state.ConsensusParameters(ctx)
5355
}
5456

55-
func (bq *beaconQuerier) VRFState(ctx context.Context) (*beacon.VRFState, error) {
56-
return bq.state.VRFState(ctx)
57+
func (q *beaconQuerier) VRFState(ctx context.Context) (*beacon.VRFState, error) {
58+
return q.state.VRFState(ctx)
5759
}
5860

59-
func (app *beaconApplication) QueryFactory() any {
61+
func (app *Application) QueryFactory() any {
6062
return &QueryFactory{app.state}
6163
}
6264

go/consensus/cometbft/apps/beacon/state/state.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,6 @@ func NewImmutableState(tree mkvs.ImmutableKeyValueTree) *ImmutableState {
4848
}
4949
}
5050

51-
// NewImmutableStateAt creates a new immutable beacon state wrapper
52-
// using the provided application query state and version.
53-
func NewImmutableStateAt(ctx context.Context, state abciAPI.ApplicationQueryState, version int64) (*ImmutableState, error) {
54-
is, err := abciAPI.NewImmutableStateAt(ctx, state, version)
55-
if err != nil {
56-
return nil, err
57-
}
58-
59-
return &ImmutableState{is}, nil
60-
}
61-
6251
// Beacon gets the current random beacon value.
6352
func (s *ImmutableState) Beacon(ctx context.Context) ([]byte, error) {
6453
data, err := s.state.Get(ctx, beaconKeyFmt.Encode())

0 commit comments

Comments
 (0)