Skip to content

Commit f7978e8

Browse files
authored
Merge pull request #6177 from oasisprotocol/peternose/trivial/refactor-consensus-backend
go/consensus/api: Refactor consensus interfaces
2 parents 793aa8f + 22b24e2 commit f7978e8

85 files changed

Lines changed: 723 additions & 703 deletions

Some content is hidden

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

.changelog/6177.trivial.md

Whitespace-only changes.

go/beacon/tests/tester.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/oasisprotocol/oasis-core/go/beacon/api"
1313
memorySigner "github.com/oasisprotocol/oasis-core/go/common/crypto/signature/signers/memory"
1414
"github.com/oasisprotocol/oasis-core/go/common/node"
15-
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
15+
consensusAPI "github.com/oasisprotocol/oasis-core/go/consensus/api"
1616
"github.com/oasisprotocol/oasis-core/go/consensus/api/transaction"
1717
)
1818

@@ -23,28 +23,28 @@ var TestSigner = memorySigner.NewTestSigner("oasis-core epochtime mock key seed"
2323

2424
// BeaconImplementationTests exercises the basic functionality of a
2525
// beacon backend.
26-
func BeaconImplementationTests(t *testing.T, backend consensus.Backend) {
26+
func BeaconImplementationTests(t *testing.T, consensus consensusAPI.Service) {
2727
require := require.New(t)
2828

29-
timeSource := backend.Beacon()
29+
timeSource := consensus.Beacon()
3030

31-
beacon, err := timeSource.GetBeacon(context.Background(), consensus.HeightLatest)
31+
beacon, err := timeSource.GetBeacon(context.Background(), consensusAPI.HeightLatest)
3232
require.NoError(err, "GetBeacon")
3333
require.Len(beacon, api.BeaconSize, "GetBeacon - length")
3434

35-
_ = MustAdvanceEpoch(t, backend)
35+
_ = MustAdvanceEpoch(t, consensus)
3636

37-
newBeacon, err := timeSource.GetBeacon(context.Background(), consensus.HeightLatest)
37+
newBeacon, err := timeSource.GetBeacon(context.Background(), consensusAPI.HeightLatest)
3838
require.NoError(err, "GetBeacon")
3939
require.Len(newBeacon, api.BeaconSize, "GetBeacon - length")
4040
require.NotEqual(beacon, newBeacon, "After epoch transition, new beacon should be generated.")
4141

42-
latestEpoch, err := timeSource.GetEpoch(context.Background(), consensus.HeightLatest)
42+
latestEpoch, err := timeSource.GetEpoch(context.Background(), consensusAPI.HeightLatest)
4343
require.NoError(err, "GetEpoch")
4444

4545
// Querying epoch for a non-existing height should fail.
4646
_, err = timeSource.GetEpoch(context.Background(), 100000000000)
47-
require.ErrorIs(err, consensus.ErrVersionNotFound, "GetEpoch should fail for non-existing height")
47+
require.ErrorIs(err, consensusAPI.ErrVersionNotFound, "GetEpoch should fail for non-existing height")
4848

4949
var lastHeight int64
5050
for epoch := api.EpochTime(0); epoch <= latestEpoch; epoch++ {
@@ -57,16 +57,16 @@ func BeaconImplementationTests(t *testing.T, backend consensus.Backend) {
5757

5858
// EpochtimeSetableImplementationTest exercises the basic functionality of
5959
// a setable (mock) epochtime backend.
60-
func EpochtimeSetableImplementationTest(t *testing.T, backend consensus.Backend) {
60+
func EpochtimeSetableImplementationTest(t *testing.T, consensus consensusAPI.Service) {
6161
require := require.New(t)
6262

63-
timeSource := backend.Beacon()
63+
timeSource := consensus.Beacon()
6464

65-
parameters, err := timeSource.ConsensusParameters(context.Background(), consensus.HeightLatest)
65+
parameters, err := timeSource.ConsensusParameters(context.Background(), consensusAPI.HeightLatest)
6666
require.NoError(err, "ConsensusParameters")
6767
require.True(parameters.DebugMockBackend, "expected debug backend")
6868

69-
epoch, err := timeSource.GetEpoch(context.Background(), consensus.HeightLatest)
69+
epoch, err := timeSource.GetEpoch(context.Background(), consensusAPI.HeightLatest)
7070
require.NoError(err, "GetEpoch")
7171

7272
var e api.EpochTime
@@ -92,7 +92,7 @@ func EpochtimeSetableImplementationTest(t *testing.T, backend consensus.Backend)
9292
}
9393

9494
epoch++
95-
err = SetEpoch(context.Background(), epoch, backend)
95+
err = SetEpoch(context.Background(), epoch, consensus)
9696
require.NoError(err, "SetEpoch")
9797

9898
select {
@@ -109,28 +109,28 @@ func EpochtimeSetableImplementationTest(t *testing.T, backend consensus.Backend)
109109
t.Fatalf("failed to receive latest epoch after transition")
110110
}
111111

112-
e, err = timeSource.GetEpoch(context.Background(), consensus.HeightLatest)
112+
e, err = timeSource.GetEpoch(context.Background(), consensusAPI.HeightLatest)
113113
require.NoError(err, "GetEpoch after set")
114114
require.Equal(epoch, e, "GetEpoch after set, epoch")
115115
}
116116

117117
// MustAdvanceEpoch advances the epoch and returns the new epoch.
118-
func MustAdvanceEpoch(t *testing.T, backend consensus.Backend) api.EpochTime {
118+
func MustAdvanceEpoch(t *testing.T, consensus consensusAPI.Service) api.EpochTime {
119119
require := require.New(t)
120120

121-
timeSource := backend.Beacon()
121+
timeSource := consensus.Beacon()
122122

123123
ctx, cancel := context.WithTimeout(context.Background(), recvTimeout)
124124
defer cancel()
125125

126-
epoch, err := timeSource.GetEpoch(ctx, consensus.HeightLatest)
126+
epoch, err := timeSource.GetEpoch(ctx, consensusAPI.HeightLatest)
127127
require.NoError(err, "GetEpoch")
128128

129129
// While using a timeout here would be nice, the correct timeout value
130130
// depends on the block interval and all the various internal timekeeping
131131
// periods so it's not easy to set one.
132132
epoch++
133-
err = SetEpoch(context.Background(), epoch, backend)
133+
err = SetEpoch(context.Background(), epoch, consensus)
134134
require.NoError(err, "SetEpoch")
135135

136136
return epoch
@@ -141,16 +141,16 @@ func MustAdvanceEpoch(t *testing.T, backend consensus.Backend) api.EpochTime {
141141
// Between each epoch increment the method ensures that the consensus validator is re-registered
142142
// so that epochs are not advanced too fast, which could cause a consensus error due to no
143143
// validators being registered for the epoch.
144-
func MustAdvanceEpochMulti(t *testing.T, backend consensus.Backend, increment uint64) api.EpochTime {
144+
func MustAdvanceEpochMulti(t *testing.T, consensus consensusAPI.Service, increment uint64) api.EpochTime {
145145
require := require.New(t)
146146

147-
timeSource := backend.Beacon()
148-
registry := backend.Registry()
147+
timeSource := consensus.Beacon()
148+
registry := consensus.Registry()
149149

150150
ctx, cancel := context.WithTimeout(context.Background(), recvTimeout)
151151
defer cancel()
152152

153-
epoch, err := timeSource.GetEpoch(ctx, consensus.HeightLatest)
153+
epoch, err := timeSource.GetEpoch(ctx, consensusAPI.HeightLatest)
154154
require.NoError(err, "GetEpoch")
155155

156156
// While using a timeout here would be nice, the correct timeout value
@@ -167,7 +167,7 @@ func MustAdvanceEpochMulti(t *testing.T, backend consensus.Backend, increment ui
167167
// While using a timeout here would be nice, the correct timeout value
168168
// depends on the block interval and all the various internal timekeeping
169169
// periods so it's not easy to set one.
170-
err = SetEpoch(context.Background(), epoch, backend)
170+
err = SetEpoch(context.Background(), epoch, consensus)
171171
require.NoError(err, "SetEpoch")
172172

173173
// Ensure validator re-registers before transitioning to next epoch.
@@ -194,15 +194,15 @@ func MustAdvanceEpochMulti(t *testing.T, backend consensus.Backend, increment ui
194194
}
195195

196196
// SetEpoch sets the current epoch.
197-
func SetEpoch(ctx context.Context, epoch api.EpochTime, backend consensus.Backend) error {
198-
ch, sub, err := backend.Beacon().WatchEpochs(ctx)
197+
func SetEpoch(ctx context.Context, epoch api.EpochTime, consensus consensusAPI.Service) error {
198+
ch, sub, err := consensus.Beacon().WatchEpochs(ctx)
199199
if err != nil {
200200
return fmt.Errorf("watch epochs failed: %w", err)
201201
}
202202
defer sub.Close()
203203

204204
tx := transaction.NewTransaction(0, nil, api.MethodSetEpoch, epoch)
205-
if err := consensus.SignAndSubmitTx(ctx, backend, TestSigner, tx); err != nil {
205+
if err := consensusAPI.SignAndSubmitTx(ctx, consensus, TestSigner, tx); err != nil {
206206
return fmt.Errorf("set epoch failed: %w", err)
207207
}
208208

go/consensus/api/api.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ func (m FeatureMask) Has(f FeatureMask) bool {
109109
return m&f != 0
110110
}
111111

112-
// ClientBackend is a consensus interface used by clients that connect to the local full node.
113-
type ClientBackend interface {
112+
// Backend is a consensus interface used by clients that connect to the local full node.
113+
type Backend interface {
114114
// SubmitTx submits a signed consensus transaction and waits for the transaction to be included
115115
// in a block. Use SubmitTxNoWait if you only need to broadcast the transaction.
116116
SubmitTx(ctx context.Context, tx *transaction.SignedTransaction) error
@@ -193,30 +193,36 @@ type ClientBackend interface {
193193

194194
// GetNextBlockState returns the state of the next block being voted on by validators.
195195
GetNextBlockState(ctx context.Context) (*NextBlockState, error)
196+
}
196197

198+
// Services are consensus services.
199+
type Services interface {
197200
// Beacon returns the beacon backend.
198201
Beacon() beacon.Backend
199202

203+
// Core returns the consensus core backend.
204+
Core() Backend
205+
206+
// Governance returns the governance backend.
207+
Governance() governance.Backend
208+
209+
// KeyManager returns the keymanager backend.
210+
KeyManager() keymanager.Backend
211+
200212
// Registry returns the registry backend.
201213
Registry() registry.Backend
202214

203-
// Staking returns the staking backend.
204-
Staking() staking.Backend
215+
// RootHash returns the roothash backend.
216+
RootHash() roothash.Backend
205217

206218
// Scheduler returns the scheduler backend.
207219
Scheduler() scheduler.Backend
208220

209-
// Governance returns the governance backend.
210-
Governance() governance.Backend
211-
212-
// RootHash returns the roothash backend.
213-
RootHash() roothash.Backend
221+
// Staking returns the staking backend.
222+
Staking() staking.Backend
214223

215224
// Vault returns the vault backend.
216225
Vault() vault.Backend
217-
218-
// KeyManager returns the keymanager backend.
219-
KeyManager() keymanager.Backend
220226
}
221227

222228
// Block is a consensus block.
@@ -371,10 +377,10 @@ type P2PStatus struct {
371377
Peers []string `json:"peers"`
372378
}
373379

374-
// Backend is an interface that a consensus backend must provide.
375-
type Backend interface {
380+
// Service is an interface that a consensus backend service must provide.
381+
type Service interface {
376382
service.BackgroundService
377-
ClientBackend
383+
Services
378384

379385
// SupportedFeatures returns the features supported by this consensus backend.
380386
SupportedFeatures() FeatureMask

0 commit comments

Comments
 (0)