Skip to content

Commit 97af66c

Browse files
authored
Distributed nonce pool (#196)
* DB-level changes to support distributed nonce pools * Initial LocalNoncePool implementation * Remove deprecated random nonce selection logic * Setup local nonce pool in gRPC transaction service * Setup local nonce pool in sequencer service * Add validation utility to LocalNoncePool for configuration enforcement * Add nonce and nonce pool method tracing * Add more edge case tests
1 parent 0fb66db commit 97af66c

File tree

24 files changed

+1646
-792
lines changed

24 files changed

+1646
-792
lines changed

go.mod

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,9 @@ require (
3131
github.com/stretchr/testify v1.8.4
3232
github.com/vence722/base122-go v0.0.2
3333
github.com/ybbus/jsonrpc v2.1.2+incompatible
34-
go.etcd.io/etcd/api/v3 v3.5.13
35-
go.etcd.io/etcd/client/v3 v3.5.13
36-
go.uber.org/zap v1.17.0
3734
golang.org/x/crypto v0.32.0
38-
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842
3935
golang.org/x/net v0.34.0
4036
golang.org/x/text v0.21.0
41-
golang.org/x/time v0.5.0
4237
google.golang.org/grpc v1.71.1
4338
google.golang.org/protobuf v1.36.6
4439
)
@@ -50,8 +45,6 @@ require (
5045
github.com/bits-and-blooms/bitset v1.2.0 // indirect
5146
github.com/cenkalti/backoff/v4 v4.1.0 // indirect
5247
github.com/containerd/continuity v0.0.0-20190827140505-75bee3e2ccb6 // indirect
53-
github.com/coreos/go-semver v0.3.0 // indirect
54-
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
5548
github.com/davecgh/go-spew v1.1.1 // indirect
5649
github.com/docker/cli v20.10.7+incompatible // indirect
5750
github.com/docker/docker v20.10.7+incompatible // indirect
@@ -90,11 +83,7 @@ require (
9083
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
9184
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
9285
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
93-
go.etcd.io/etcd/client/pkg/v3 v3.5.13 // indirect
94-
go.uber.org/atomic v1.7.0 // indirect
95-
go.uber.org/multierr v1.6.0 // indirect
9686
golang.org/x/sys v0.29.0 // indirect
97-
google.golang.org/genproto/googleapis/api v0.0.0-20250106144421-5f5ef82da422 // indirect
9887
google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f // indirect
9988
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
10089
gopkg.in/ini.v1 v1.51.0 // indirect

go.sum

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,9 @@ github.com/containerd/continuity v0.0.0-20190827140505-75bee3e2ccb6 h1:NmTXa/uVn
8686
github.com/containerd/continuity v0.0.0-20190827140505-75bee3e2ccb6/go.mod h1:GL3xCUCBDV3CZiTSEKksMWbLE66hEyuu9qyDOOqM47Y=
8787
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
8888
github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
89-
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
9089
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
9190
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
9291
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
93-
github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI=
94-
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
9592
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
9693
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
9794
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
@@ -141,7 +138,6 @@ github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG
141138
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
142139
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
143140
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
144-
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
145141
github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
146142
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
147143
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
@@ -467,12 +463,6 @@ github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
467463
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
468464
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
469465
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
470-
go.etcd.io/etcd/api/v3 v3.5.13 h1:8WXU2/NBge6AUF1K1gOexB6e07NgsN1hXK0rSTtgSp4=
471-
go.etcd.io/etcd/api/v3 v3.5.13/go.mod h1:gBqlqkcMMZMVTMm4NDZloEVJzxQOQIls8splbqBDa0c=
472-
go.etcd.io/etcd/client/pkg/v3 v3.5.13 h1:RVZSAnWWWiI5IrYAXjQorajncORbS0zI48LQlE2kQWg=
473-
go.etcd.io/etcd/client/pkg/v3 v3.5.13/go.mod h1:XxHT4u1qU12E2+po+UVPrEeL94Um6zL58ppuJWXSAB8=
474-
go.etcd.io/etcd/client/v3 v3.5.13 h1:o0fHTNJLeO0MyVbc7I3fsCf6nrOqn5d+diSarKnB2js=
475-
go.etcd.io/etcd/client/v3 v3.5.13/go.mod h1:cqiAeY8b5DEEcpxvgWKsbLIWNM/8Wy2xJSDMtioMcoI=
476466
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
477467
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
478468
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
@@ -495,19 +485,13 @@ go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
495485
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
496486
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
497487
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
498-
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
499-
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
500488
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
501489
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
502490
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
503-
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
504-
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
505491
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
506492
go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
507493
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
508494
go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM=
509-
go.uber.org/zap v1.17.0 h1:MTjgFu6ZLKvY6Pvaqk97GlxNBuMpV4Hy/3P6tRGlI2U=
510-
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
511495
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
512496
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
513497
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@@ -536,8 +520,6 @@ golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u0
536520
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
537521
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
538522
golang.org/x/exp v0.0.0-20200331195152-e8c3332aa8e5/go.mod h1:4M0jN8W1tt0AVLNr8HDosyJCDCDuyL9N9+3m7wDWgKw=
539-
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM=
540-
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
541523
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
542524
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
543525
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
@@ -685,8 +667,6 @@ golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
685667
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
686668
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
687669
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
688-
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
689-
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
690670
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
691671
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
692672
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
@@ -815,8 +795,6 @@ google.golang.org/genproto v0.0.0-20201210142538-e3217bee35cc/go.mod h1:FWY/as6D
815795
google.golang.org/genproto v0.0.0-20201214200347-8c77b98c765d/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
816796
google.golang.org/genproto v0.0.0-20210108203827-ffc7fda8c3d7/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
817797
google.golang.org/genproto v0.0.0-20210226172003-ab064af71705/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
818-
google.golang.org/genproto/googleapis/api v0.0.0-20250106144421-5f5ef82da422 h1:GVIKPyP/kLIyVOgOnTwFOrvQaQUzOzGMCxgFUOEmm24=
819-
google.golang.org/genproto/googleapis/api v0.0.0-20250106144421-5f5ef82da422/go.mod h1:b6h1vNKhxaSoEI+5jc3PJUCustfli/mRab7295pY7rw=
820798
google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f h1:OxYkA3wjPsZyBylwymxSHa7ViiW1Sml4ToBrncvFehI=
821799
google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f/go.mod h1:+2Yz8+CLJbIfL9z73EW45avw8Lmge3xVElCP9zEKi50=
822800
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
@@ -863,12 +841,10 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl
863841
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
864842
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
865843
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
866-
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
867844
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
868845
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
869846
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
870847
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
871-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
872848
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
873849
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
874850
gotest.tools/v3 v3.0.2 h1:kG1BFyqVHuQoVQiR1bWGnfz/fmHvvuiSPIV7rvl360E=

pkg/code/async/nonce/metrics.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func (p *service) metricsGaugeWorker(ctx context.Context) error {
3131
nonce.StateAvailable,
3232
nonce.StateReserved,
3333
nonce.StateInvalid,
34+
nonce.StateClaimed,
3435
} {
3536
count, err := p.data.GetNonceCountByStateAndPurpose(ctx, nonce.EnvironmentCvm, common.CodeVmAccount.PublicKey().ToBase58(), state, nonce.PurposeClientTransaction)
3637
if err != nil {

pkg/code/async/nonce/pool.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,31 +95,36 @@ func (p *service) handle(ctx context.Context, record *nonce.Record) error {
9595
blockhash yet.
9696
9797
StateAvailable:
98-
Available to be used by a payment intent, subscription, or
99-
other nonce-related transaction/instruction.
98+
Available to be by a fulfillment for a virtual instruction
99+
or transaction.
100100
101101
StateReserved:
102-
Reserved by a payment intent, subscription, or other
103-
nonce-related transaction/instruction.
102+
Reserved by a by a fulfillment for a virtual instruction
103+
or transaction.
104104
105105
StateInvalid:
106106
The nonce account is invalid (e.g. insufficient funds, etc).
107107
108+
StateClaimed:
109+
The nonce is claimed by a process for future use (identified
110+
by a node ID)
111+
108112
Transitions:
109113
StateUnknown
110114
-> StateInvalid
111115
-> StateReleased
112116
StateReleased
113117
-> StateAvailable
114118
StateAvailable
115-
-> [externally] StateReserved (nonce used in a new transaction)
119+
-> [externally] StateClaimed (nonce claimed by a process for future use)
120+
StateClaimed
121+
-> [externally] StateAvailable (nonce released by the process that claimed it)
122+
-> [externally] StateReserved (nonce reserved for future use in a virtual instruction or transaction)
116123
StateReserved
117-
-> [externally] StateReleased (nonce used in a submitted transaction)
118-
-> [externally] StateAvailable (nonce will never be submitted in the transaction - eg. it became revoked)
124+
-> [externally] StateReleased (nonce used in a submitted virtual instruction or transaction)
125+
-> [externally] StateAvailable (nonce will never be submitted in the virtual instruction or transaction - eg. it became revoked)
119126
*/
120127

121-
// todo: distributed lock on the nonce
122-
123128
switch record.State {
124129
case nonce.StateUnknown:
125130
return p.handleUnknown(ctx, record)
@@ -131,6 +136,8 @@ func (p *service) handle(ctx context.Context, record *nonce.Record) error {
131136
return p.handleReserved(ctx, record)
132137
case nonce.StateInvalid:
133138
return p.handleInvalid(ctx, record)
139+
case nonce.StateClaimed:
140+
return p.handleClaimed(ctx, record)
134141
default:
135142
return nil
136143
}
@@ -259,3 +266,8 @@ func (p *service) handleInvalid(ctx context.Context, record *nonce.Record) error
259266
// as is for further investigation.
260267
return nil
261268
}
269+
270+
func (p *service) handleClaimed(ctx context.Context, record *nonce.Record) error {
271+
// Nothing to do here
272+
return nil
273+
}

pkg/code/async/sequencer/fulfillment_handler.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type FulfillmentHandler interface {
4646
// to the blockchain. This is an optimization for the nonce pool. Implementations
4747
// should not modify the provided fulfillment record or selected nonce, but rather
4848
// use relevant fields to make the corresponding transaction.
49-
MakeOnDemandTransaction(ctx context.Context, fulfillmentRecord *fulfillment.Record, selectedNonce *transaction_util.SelectedNonce) (*solana.Transaction, error)
49+
MakeOnDemandTransaction(ctx context.Context, fulfillmentRecord *fulfillment.Record, selectedNonce *transaction_util.Nonce) (*solana.Transaction, error)
5050

5151
// OnSuccess is a callback function executed on a finalized transaction.
5252
OnSuccess(ctx context.Context, fulfillmentRecord *fulfillment.Record, txnRecord *transaction.Record) error
@@ -123,7 +123,7 @@ func (h *InitializeLockedTimelockAccountFulfillmentHandler) SupportsOnDemandTran
123123
return true
124124
}
125125

126-
func (h *InitializeLockedTimelockAccountFulfillmentHandler) MakeOnDemandTransaction(ctx context.Context, fulfillmentRecord *fulfillment.Record, selectedNonce *transaction_util.SelectedNonce) (*solana.Transaction, error) {
126+
func (h *InitializeLockedTimelockAccountFulfillmentHandler) MakeOnDemandTransaction(ctx context.Context, fulfillmentRecord *fulfillment.Record, selectedNonce *transaction_util.Nonce) (*solana.Transaction, error) {
127127
if fulfillmentRecord.FulfillmentType != fulfillment.InitializeLockedTimelockAccount {
128128
return nil, errors.New("invalid fulfillment type")
129129
}
@@ -268,7 +268,7 @@ func (h *NoPrivacyTransferWithAuthorityFulfillmentHandler) SupportsOnDemandTrans
268268
return true
269269
}
270270

271-
func (h *NoPrivacyTransferWithAuthorityFulfillmentHandler) MakeOnDemandTransaction(ctx context.Context, fulfillmentRecord *fulfillment.Record, selectedNonce *transaction_util.SelectedNonce) (*solana.Transaction, error) {
271+
func (h *NoPrivacyTransferWithAuthorityFulfillmentHandler) MakeOnDemandTransaction(ctx context.Context, fulfillmentRecord *fulfillment.Record, selectedNonce *transaction_util.Nonce) (*solana.Transaction, error) {
272272
virtualSignatureBytes, err := base58.Decode(*fulfillmentRecord.VirtualSignature)
273273
if err != nil {
274274
return nil, err
@@ -444,7 +444,7 @@ func (h *NoPrivacyWithdrawFulfillmentHandler) SupportsOnDemandTransactions() boo
444444
return true
445445
}
446446

447-
func (h *NoPrivacyWithdrawFulfillmentHandler) MakeOnDemandTransaction(ctx context.Context, fulfillmentRecord *fulfillment.Record, selectedNonce *transaction_util.SelectedNonce) (*solana.Transaction, error) {
447+
func (h *NoPrivacyWithdrawFulfillmentHandler) MakeOnDemandTransaction(ctx context.Context, fulfillmentRecord *fulfillment.Record, selectedNonce *transaction_util.Nonce) (*solana.Transaction, error) {
448448
virtualSignatureBytes, err := base58.Decode(*fulfillmentRecord.VirtualSignature)
449449
if err != nil {
450450
return nil, err
@@ -634,7 +634,7 @@ func (h *CloseEmptyTimelockAccountFulfillmentHandler) SupportsOnDemandTransactio
634634
return true
635635
}
636636

637-
func (h *CloseEmptyTimelockAccountFulfillmentHandler) MakeOnDemandTransaction(ctx context.Context, fulfillmentRecord *fulfillment.Record, selectedNonce *transaction_util.SelectedNonce) (*solana.Transaction, error) {
637+
func (h *CloseEmptyTimelockAccountFulfillmentHandler) MakeOnDemandTransaction(ctx context.Context, fulfillmentRecord *fulfillment.Record, selectedNonce *transaction_util.Nonce) (*solana.Transaction, error) {
638638
if fulfillmentRecord.FulfillmentType != fulfillment.CloseEmptyTimelockAccount {
639639
return nil, errors.New("invalid fulfillment type")
640640
}

pkg/code/async/sequencer/service.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"github.com/code-payments/code-server/pkg/code/data/action"
1515
"github.com/code-payments/code-server/pkg/code/data/fulfillment"
1616
"github.com/code-payments/code-server/pkg/code/data/intent"
17+
"github.com/code-payments/code-server/pkg/code/data/nonce"
18+
"github.com/code-payments/code-server/pkg/code/transaction"
1719
)
1820

1921
var (
@@ -28,22 +30,28 @@ type service struct {
2830
data code_data.Provider
2931
scheduler Scheduler
3032
vmIndexerClient indexerpb.IndexerClient
33+
noncePool *transaction.LocalNoncePool
3134
fulfillmentHandlersByType map[fulfillment.Type]FulfillmentHandler
3235
actionHandlersByType map[action.Type]ActionHandler
3336
intentHandlersByType map[intent.Type]IntentHandler
3437
}
3538

36-
func New(data code_data.Provider, scheduler Scheduler, vmIndexerClient indexerpb.IndexerClient, configProvider ConfigProvider) async.Service {
39+
func New(data code_data.Provider, scheduler Scheduler, vmIndexerClient indexerpb.IndexerClient, noncePool *transaction.LocalNoncePool, configProvider ConfigProvider) (async.Service, error) {
40+
if err := noncePool.Validate(nonce.EnvironmentSolana, nonce.EnvironmentInstanceSolanaMainnet, nonce.PurposeOnDemandTransaction); err != nil {
41+
return nil, err
42+
}
43+
3744
return &service{
3845
log: logrus.StandardLogger().WithField("service", "sequencer"),
3946
conf: configProvider(),
4047
data: data,
4148
scheduler: scheduler,
4249
vmIndexerClient: vmIndexerClient,
50+
noncePool: noncePool, // todo: validate configuration
4351
fulfillmentHandlersByType: getFulfillmentHandlers(data, vmIndexerClient),
4452
actionHandlersByType: getActionHandlers(data),
4553
intentHandlersByType: getIntentHandlers(data),
46-
}
54+
}, nil
4755
}
4856

4957
func (p *service) Start(ctx context.Context, interval time.Duration) error {

pkg/code/async/sequencer/testutil.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import (
44
"context"
55
"errors"
66

7-
"github.com/code-payments/code-server/pkg/solana"
8-
"github.com/code-payments/code-server/pkg/solana/memo"
97
"github.com/code-payments/code-server/pkg/code/common"
108
"github.com/code-payments/code-server/pkg/code/data/fulfillment"
119
"github.com/code-payments/code-server/pkg/code/data/transaction"
1210
transaction_util "github.com/code-payments/code-server/pkg/code/transaction"
11+
"github.com/code-payments/code-server/pkg/solana"
12+
"github.com/code-payments/code-server/pkg/solana/memo"
1313
)
1414

1515
type mockScheduler struct {
@@ -42,7 +42,7 @@ func (h *mockFulfillmentHandler) SupportsOnDemandTransactions() bool {
4242
return h.supportsOnDemandTxnCreation
4343
}
4444

45-
func (h *mockFulfillmentHandler) MakeOnDemandTransaction(ctx context.Context, fulfillmentRecord *fulfillment.Record, selectedNonce *transaction_util.SelectedNonce) (*solana.Transaction, error) {
45+
func (h *mockFulfillmentHandler) MakeOnDemandTransaction(ctx context.Context, fulfillmentRecord *fulfillment.Record, selectedNonce *transaction_util.Nonce) (*solana.Transaction, error) {
4646
if !h.supportsOnDemandTxnCreation {
4747
return nil, errors.New("not supported")
4848
}

0 commit comments

Comments
 (0)