Skip to content

Commit d5629b9

Browse files
committed
feat(prt): add PRT service boilerplate
1 parent 31c6e0f commit d5629b9

File tree

9 files changed

+539
-22
lines changed

9 files changed

+539
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ machine-snapshot/**
1414
/cartesi-rollups-validator
1515
/cartesi-rollups-claimer
1616
/cartesi-rollups-jsonrpc-api
17+
/cartesi-rollups-prt
1718
/rollups-contracts
1819
/rollups-prt-contracts
1920
/applications

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ DOCKER_PLATFORM=--platform $(BUILD_PLATFORM)
5959
endif
6060

6161
# Go artifacts
62-
GO_ARTIFACTS := cartesi-rollups-node cartesi-rollups-cli cartesi-rollups-evm-reader cartesi-rollups-advancer cartesi-rollups-validator cartesi-rollups-claimer cartesi-rollups-jsonrpc-api
62+
GO_ARTIFACTS := $(addprefix cartesi-rollups-,node cli evm-reader advancer validator claimer jsonrpc-api prt)
6363

6464
# fixme(vfusco): path on all oses
6565
CGO_CFLAGS:= -I$(PREFIX)/include

cmd/cartesi-rollups-prt/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// (c) Cartesi and individual authors (see AUTHORS)
2+
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)
3+
4+
package main
5+
6+
import (
7+
"os"
8+
9+
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-prt/root"
10+
)
11+
12+
func main() {
13+
err := root.Cmd.Execute()
14+
if err != nil {
15+
os.Exit(1)
16+
}
17+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// (c) Cartesi and individual authors (see AUTHORS)
2+
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)
3+
4+
package root
5+
6+
import (
7+
"context"
8+
9+
"github.com/cartesi/rollups-node/internal/config"
10+
"github.com/cartesi/rollups-node/internal/prt"
11+
"github.com/cartesi/rollups-node/internal/repository/factory"
12+
"github.com/cartesi/rollups-node/internal/version"
13+
"github.com/cartesi/rollups-node/pkg/service"
14+
15+
"github.com/spf13/cobra"
16+
"github.com/spf13/viper"
17+
)
18+
19+
const serviceName = "prt"
20+
21+
var (
22+
logLevel string
23+
logColor bool
24+
databaseConnection string
25+
pollInterval string
26+
maxStartupTime string
27+
telemetryAddress string
28+
cfg *config.ValidatorConfig
29+
)
30+
31+
var Cmd = &cobra.Command{
32+
Use: "cartesi-rollups-" + serviceName,
33+
Short: "Runs cartesi-rollups-" + serviceName,
34+
Long: "Runs cartesi-rollups-" + serviceName + " in standalone mode",
35+
Run: run,
36+
Version: version.BuildVersion,
37+
}
38+
39+
func init() {
40+
Cmd.Flags().StringVar(&telemetryAddress, "telemetry-address", ":10003", "Health check and metrics address and port")
41+
cobra.CheckErr(viper.BindPFlag(config.TELEMETRY_ADDRESS, Cmd.Flags().Lookup("telemetry-address")))
42+
43+
Cmd.Flags().StringVar(&logLevel, "log-level", "info", "Log level: debug, info, warn or error")
44+
cobra.CheckErr(viper.BindPFlag(config.LOG_LEVEL, Cmd.Flags().Lookup("log-level")))
45+
46+
Cmd.Flags().BoolVar(&logColor, "log-color", true, "Tint the logs (colored output)")
47+
cobra.CheckErr(viper.BindPFlag(config.LOG_COLOR, Cmd.Flags().Lookup("log-color")))
48+
49+
Cmd.Flags().StringVar(&databaseConnection, "database-connection", "",
50+
"Database connection string in the URL format\n(eg.: 'postgres://user:password@hostname:port/database') ")
51+
cobra.CheckErr(viper.BindPFlag(config.DATABASE_CONNECTION, Cmd.Flags().Lookup("database-connection")))
52+
53+
Cmd.Flags().StringVar(&pollInterval, "poll-interval", "7", "Poll interval")
54+
cobra.CheckErr(viper.BindPFlag(config.VALIDATOR_POLLING_INTERVAL, Cmd.Flags().Lookup("poll-interval")))
55+
56+
Cmd.Flags().StringVar(&maxStartupTime, "max-startup-time", "15", "Maximum startup time in seconds")
57+
cobra.CheckErr(viper.BindPFlag(config.MAX_STARTUP_TIME, Cmd.Flags().Lookup("max-startup-time")))
58+
59+
// TODO: validate on preRunE
60+
Cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
61+
var err error
62+
cfg, err = config.LoadValidatorConfig()
63+
if err != nil {
64+
return err
65+
}
66+
return nil
67+
}
68+
}
69+
70+
func run(cmd *cobra.Command, args []string) {
71+
ctx, cancel := context.WithTimeout(context.Background(), cfg.MaxStartupTime)
72+
defer cancel()
73+
74+
createInfo := prt.CreateInfo{
75+
CreateInfo: service.CreateInfo{
76+
Name: serviceName,
77+
LogLevel: cfg.LogLevel,
78+
LogColor: cfg.LogColor,
79+
EnableSignalHandling: true,
80+
TelemetryCreate: true,
81+
TelemetryAddress: cfg.TelemetryAddress,
82+
PollInterval: cfg.ValidatorPollingInterval,
83+
},
84+
Config: *cfg,
85+
}
86+
var err error
87+
createInfo.Repository, err = factory.NewRepositoryFromConnectionString(ctx, cfg.DatabaseConnection.String())
88+
cobra.CheckErr(err)
89+
defer createInfo.Repository.Close()
90+
91+
validatorService, err := prt.Create(ctx, &createInfo)
92+
cobra.CheckErr(err)
93+
94+
cobra.CheckErr(validatorService.Serve())
95+
}

internal/config/generate/Config.toml

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ default = "info"
88
go-type = "LogLevel"
99
description = """
1010
One of "debug", "info", "warn", "error"."""
11-
used-by = ["advancer", "claimer", "evmreader", "validator", "jsonrpc", "node"]
11+
used-by = ["advancer", "claimer", "evmreader", "validator", "jsonrpc", "node", "prt"]
1212

1313
[logging.CARTESI_LOG_COLOR]
1414
default = "true"
1515
go-type = "bool"
1616
description = """
1717
If set to true, the node will add colors to its log output."""
18-
used-by = ["advancer", "claimer", "evmreader", "validator", "jsonrpc", "node"]
18+
used-by = ["advancer", "claimer", "evmreader", "validator", "jsonrpc", "node", "prt"]
1919

2020
#
2121
# Features
@@ -33,7 +33,7 @@ default = "true"
3333
go-type = "bool"
3434
description = """
3535
If set to false, the node will not submit claims (reader mode)."""
36-
used-by = ["claimer", "node"]
36+
used-by = ["claimer", "node", "prt"]
3737

3838
[features.CARTESI_FEATURE_INSPECT_ENABLED]
3939
default = "true"
@@ -82,12 +82,19 @@ description = """
8282
How many seconds the node will wait before querying the database for new claims."""
8383
used-by = ["claimer", "node"]
8484

85+
[rollups.CARTESI_PRT_POLLING_INTERVAL]
86+
default = "3"
87+
go-type = "Duration"
88+
description = """
89+
How many seconds the node will wait before trying to finish epochs for all applications."""
90+
used-by = ["prt", "node"]
91+
8592
[rollups.CARTESI_MAX_STARTUP_TIME]
8693
default = "15"
8794
go-type = "Duration"
8895
description = """
8996
How many seconds the node expects services take initializing before aborting."""
90-
used-by = ["advancer", "claimer", "evmreader", "validator", "jsonrpc", "node"]
97+
used-by = ["advancer", "claimer", "evmreader", "validator", "jsonrpc", "node", "prt"]
9198

9299
#
93100
# Blockchain
@@ -97,14 +104,14 @@ used-by = ["advancer", "claimer", "evmreader", "validator", "jsonrpc", "node"]
97104
go-type = "uint64"
98105
description = """
99106
An unique identifier representing a blockchain network."""
100-
used-by = ["evmreader", "claimer", "node"]
107+
used-by = ["evmreader", "claimer", "node", "prt"]
101108

102109
[blockchain.CARTESI_BLOCKCHAIN_HTTP_ENDPOINT]
103110
file = true
104111
go-type = "URL"
105112
description = """
106113
HTTP endpoint for the blockchain RPC provider."""
107-
used-by = ["evmreader", "claimer", "node"]
114+
used-by = ["evmreader", "claimer", "node", "prt"]
108115

109116
[blockchain.CARTESI_BLOCKCHAIN_HTTP_AUTHORIZATION]
110117
file = true
@@ -133,15 +140,15 @@ go-type = "bool"
133140
description = """
134141
If set to true the node will send transactions using the legacy gas fee model
135142
(instead of EIP-1559)."""
136-
used-by = ["claimer", "node", "cli"]
143+
used-by = ["claimer", "node", "cli", "prt"]
137144

138145
[blockchain.CARTESI_BLOCKCHAIN_DEFAULT_BLOCK]
139146
default = "finalized"
140147
go-type = "DefaultBlock"
141148
description = """
142149
The default block to be used by EVM Reader and Claimer when requesting new blocks.
143150
One of 'latest', 'pending', 'safe', 'finalized'"""
144-
used-by = ["evmreader", "claimer", "node"]
151+
used-by = ["evmreader", "claimer", "node", "prt"]
145152

146153
[blockchain.CARTESI_BLOCKCHAIN_SUBSCRIPTION_TIMEOUT]
147154
default = "60"
@@ -155,28 +162,28 @@ default = "4"
155162
go-type = "uint64"
156163
description = """
157164
Maximum number of retry attempts for HTTP blockchain requests after encountering an error."""
158-
used-by = ["evmreader", "claimer", "node"]
165+
used-by = ["evmreader", "claimer", "node", "prt"]
159166

160167
[rollups.CARTESI_BLOCKCHAIN_HTTP_RETRY_MIN_WAIT]
161168
default = "1"
162169
go-type = "Duration"
163170
description = """
164171
Minimum wait time in seconds for the exponential backoff retry policy. This is the initial delay before the first retry for HTTP blockchain requests."""
165-
used-by = ["evmreader", "claimer", "node"]
172+
used-by = ["evmreader", "claimer", "node", "prt"]
166173

167174
[rollups.CARTESI_BLOCKCHAIN_HTTP_RETRY_MAX_WAIT]
168175
default = "60"
169176
go-type = "Duration"
170177
description = """
171178
Maximum wait time in seconds for the exponential backoff retry policy. The delay between retries for HTTP blockchain requests will never exceed this value, regardless of the backoff calculation."""
172-
used-by = ["evmreader", "claimer", "node"]
179+
used-by = ["evmreader", "claimer", "node", "prt"]
173180

174181
[rollups.CARTESI_BLOCKCHAIN_MAX_BLOCK_RANGE]
175182
default = "0"
176183
go-type = "uint64"
177184
description = """
178185
Maximum number of blocks in a single query to the provider. Queries with larger ranges will be broken into multiple smaller queries. Zero for unlimited."""
179-
used-by = ["evmreader", "claimer", "node"]
186+
used-by = ["evmreader", "claimer", "node", "prt"]
180187

181188
#
182189
# Contracts
@@ -226,7 +233,7 @@ default = "/var/lib/cartesi-rollups-node/snapshots"
226233
go-type = "string"
227234
description = """
228235
Path to the directory where the snapshots will be written."""
229-
used-by = ["advancer", "node"]
236+
used-by = ["advancer", "node", "prt"]
230237

231238
#
232239
# Auth
@@ -240,23 +247,23 @@ One of "private_key", "private_key_file", "mnemonic", "mnemonic_file", "aws".
240247
241248
The auth variable for the kind defined here is required. Eg.: CARTESI_AUTH_MNEMONIC"""
242249
omit = true
243-
used-by = ["claimer", "node", "cli"]
250+
used-by = ["claimer", "node", "cli", "prt"]
244251

245252
[auth.CARTESI_AUTH_PRIVATE_KEY]
246253
file = true
247254
go-type = "RedactedString"
248255
description = """
249256
The node will use this private key to sign transactions."""
250257
omit = true
251-
used-by = ["claimer", "node", "cli"]
258+
used-by = ["claimer", "node", "cli", "prt"]
252259

253260
[auth.CARTESI_AUTH_MNEMONIC]
254261
file = true
255262
go-type = "RedactedString"
256263
description = """
257264
The node will use the private key generated from this mnemonic to sign transactions."""
258265
omit = true
259-
used-by = ["claimer", "node", "cli"]
266+
used-by = ["claimer", "node", "cli", "prt"]
260267

261268
[auth.CARTESI_AUTH_MNEMONIC_ACCOUNT_INDEX]
262269
default = "0"
@@ -265,7 +272,7 @@ description = """
265272
When using mnemonics to sign transactions,
266273
the node will use this account index to generate the private key."""
267274
omit = true
268-
used-by = ["claimer", "node", "cli"]
275+
used-by = ["claimer", "node", "cli", "prt"]
269276

270277
[auth.CARTESI_AUTH_AWS_KMS_KEY_ID]
271278
go-type = "RedactedString"
@@ -274,7 +281,7 @@ If set, the node will use the AWS KMS service with this key ID to sign transacti
274281
275282
Must be set alongside `CARTESI_AUTH_AWS_KMS_REGION`."""
276283
omit = true
277-
used-by = ["claimer", "node", "cli"]
284+
used-by = ["claimer", "node", "cli", "prt"]
278285

279286
[auth.CARTESI_AUTH_AWS_KMS_REGION]
280287
go-type = "RedactedString"
@@ -283,7 +290,7 @@ An AWS KMS Region.
283290
284291
Must be set alongside `CARTESI_AUTH_AWS_KMS_KEY_ID`."""
285292
omit = true
286-
used-by = ["claimer", "node", "cli"]
293+
used-by = ["claimer", "node", "cli", "prt"]
287294

288295
#
289296
# Database
@@ -302,7 +309,7 @@ See [this](https://www.postgresql.org/docs/current/libpq-envars.html) for more i
302309
It is also possible to set the endpoint without a password and load it from Postgres' passfile.
303310
See [this](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-PASSFILE)
304311
for more information."""
305-
used-by = ["advancer", "claimer", "evmreader", "validator", "jsonrpc", "node", "cli"]
312+
used-by = ["advancer", "claimer", "evmreader", "validator", "jsonrpc", "node", "cli", "prt"]
306313

307314
#
308315
# Telemetry http address
@@ -312,7 +319,7 @@ used-by = ["advancer", "claimer", "evmreader", "validator", "jsonrpc", "node", "
312319
go-type = "string"
313320
description = """
314321
HTTP address for telemetry service."""
315-
used-by = ["advancer", "claimer", "evmreader", "validator", "jsonrpc", "node", "cli"]
322+
used-by = ["advancer", "claimer", "evmreader", "validator", "jsonrpc", "node", "cli", "prt"]
316323

317324
#
318325
# HTTP

0 commit comments

Comments
 (0)