Skip to content

Commit 1f9a7b4

Browse files
author
Jeff Yanta
committed
Add validation utility to LocalNoncePool for configuration enforcement
1 parent f10fca5 commit 1f9a7b4

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

pkg/code/async/sequencer/service.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,8 @@ type service struct {
3737
}
3838

3939
func New(data code_data.Provider, scheduler Scheduler, vmIndexerClient indexerpb.IndexerClient, noncePool *transaction.LocalNoncePool, configProvider ConfigProvider) (async.Service, error) {
40-
noncePoolEnv, noncePoolEnvInstance, noncePoolType := noncePool.GetConfiguration()
41-
if noncePoolEnv != nonce.EnvironmentSolana {
42-
return nil, errors.Errorf("nonce pool environment must be %s", nonce.EnvironmentSolana)
43-
}
44-
if noncePoolEnvInstance != nonce.EnvironmentInstanceSolanaMainnet {
45-
return nil, errors.Errorf("nonce pool environment instance must be %s", nonce.EnvironmentInstanceSolanaMainnet)
46-
}
47-
if noncePoolType != nonce.PurposeOnDemandTransaction {
48-
return nil, errors.Errorf("nonce pool type must be %s", nonce.PurposeOnDemandTransaction)
40+
if err := noncePool.Validate(nonce.EnvironmentSolana, nonce.EnvironmentInstanceSolanaMainnet, nonce.PurposeOnDemandTransaction); err != nil {
41+
return nil, err
4942
}
5043

5144
return &service{

pkg/code/server/transaction/server.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"sync"
66

7-
"github.com/pkg/errors"
87
"github.com/sirupsen/logrus"
98

109
transactionpb "github.com/code-payments/code-protobuf-api/generated/go/transaction/v2"
@@ -67,15 +66,8 @@ func NewTransactionServer(
6766

6867
stripedLockParallelization := uint(conf.stripedLockParallelization.Get(ctx))
6968

70-
noncePoolEnv, noncePoolEnvInstance, noncePoolType := noncePool.GetConfiguration()
71-
if noncePoolEnv != nonce.EnvironmentCvm {
72-
return nil, errors.Errorf("nonce pool environment must be %s", nonce.EnvironmentCvm)
73-
}
74-
if noncePoolEnvInstance != common.CodeVmAccount.PublicKey().ToBase58() {
75-
return nil, errors.Errorf("nonce pool environment instance must be %s", common.CodeVmAccount.PublicKey().ToBase58())
76-
}
77-
if noncePoolType != nonce.PurposeClientTransaction {
78-
return nil, errors.Errorf("nonce pool type must be %s", nonce.PurposeClientTransaction)
69+
if err := noncePool.Validate(nonce.EnvironmentCvm, common.CodeVmAccount.PublicKey().ToBase58(), nonce.PurposeClientTransaction); err != nil {
70+
return nil, err
7971
}
8072

8173
s := &transactionServer{

pkg/code/transaction/nonce_pool.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,21 @@ func (np *LocalNoncePool) GetNonce(ctx context.Context) (*Nonce, error) {
323323
return n, nil
324324
}
325325

326-
func (np *LocalNoncePool) GetConfiguration() (nonce.Environment, string, nonce.Purpose) {
327-
return np.env, np.envInstance, np.poolType
326+
func (np *LocalNoncePool) Validate(
327+
env nonce.Environment,
328+
envInstance string,
329+
poolType nonce.Purpose,
330+
) error {
331+
if np.env != env {
332+
return errors.Errorf("nonce pool environment must be %s", env)
333+
}
334+
if np.envInstance != envInstance {
335+
return errors.Errorf("nonce pool environment instance must be %s", envInstance)
336+
}
337+
if np.poolType != poolType {
338+
return errors.Errorf("nonce pool type must be %s", poolType)
339+
}
340+
return nil
328341
}
329342

330343
func (np *LocalNoncePool) Close() error {

0 commit comments

Comments
 (0)