Skip to content

Commit

Permalink
feat(claimer): Make claim submission optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
mpolitzer committed Nov 13, 2024
1 parent 37c213c commit dd49f70
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 8 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ env:
@echo export CARTESI_POSTGRES_ENDPOINT="postgres://postgres:password@localhost:5432/rollupsdb?sslmode=disable"
@echo export CARTESI_TEST_POSTGRES_ENDPOINT="postgres://test_user:password@localhost:5432/test_rollupsdb?sslmode=disable"
@echo export CARTESI_TEST_MACHINE_IMAGES_PATH=\"$(CARTESI_TEST_MACHINE_IMAGES_PATH)\"
@echo export CARTESI_FEATURE_CLAIMER_SUBMISSION_ENABLED=true
@echo export PATH=$(CURDIR):$$PATH

# =============================================================================
Expand Down
9 changes: 8 additions & 1 deletion cmd/cartesi-rollups-claimer/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var (
TelemetryAddress: ":8081",
Impl: &claimerService,
},
EnableSubmission: true,
}
)

Expand All @@ -38,7 +39,6 @@ var Cmd = &cobra.Command{

func init() {
c := config.FromEnv()
createInfo.Auth = c.Auth
createInfo.BlockchainHttpEndpoint = c.BlockchainHttpEndpoint
createInfo.PostgresEndpoint = c.PostgresEndpoint
createInfo.PollInterval = c.ClaimerPollingInterval
Expand All @@ -48,6 +48,10 @@ func init() {
slog.LevelWarn: "warn",
slog.LevelError: "error",
}[c.LogLevel]
createInfo.EnableSubmission = c.FeatureClaimerSubmissionEnabled
if createInfo.EnableSubmission {
createInfo.Auth = c.Auth
}

Cmd.Flags().StringVar(&createInfo.TelemetryAddress,
"telemetry-address", createInfo.TelemetryAddress,
Expand All @@ -61,6 +65,9 @@ func init() {
Cmd.Flags().StringVar(&createInfo.LogLevel,
"log-level", createInfo.LogLevel,
"log level: debug, info, warn, error.")
Cmd.Flags().BoolVar(&createInfo.EnableSubmission,
"enable-submission", createInfo.EnableSubmission,
"enable submission in addition to verification.")
}

func run(cmd *cobra.Command, args []string) {
Expand Down
10 changes: 10 additions & 0 deletions cmd/cartesi-rollups-node/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,25 @@ var (
Long: "Runs the Cartesi Rollups Node as a single process",
RunE: run,
}
enableSubmissionOverride *bool
)

func init() {
enableSubmissionOverride = Cmd.Flags().Bool(
"enable-submission", true,
"enable submission in addition to verification.")
}

func run(cmd *cobra.Command, args []string) error {
startTime := time.Now()

ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

config := config.FromEnv()
if enableSubmissionOverride != nil {
config.FeatureClaimerSubmissionEnabled = *enableSubmissionOverride
}

// setup log
startup.ConfigLogs(config.LogLevel, config.LogPrettyEnabled)
Expand Down
16 changes: 10 additions & 6 deletions internal/claimer/claimer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type CreateInfo struct {

PostgresEndpoint Redacted[string]
DBConn *Database

EnableSubmission bool
}

type claimKey struct {
Expand All @@ -38,10 +40,11 @@ type claimKey struct {
type Service struct {
service.Service

DBConn *Database
EthConn *ethclient.Client
Signer *bind.TransactOpts
ClaimsInFlight map[claimKey]Hash // -> txHash
submissionEnabled bool
DBConn *Database
EthConn *ethclient.Client
Signer *bind.TransactOpts
ClaimsInFlight map[claimKey]Hash // -> txHash
}

func Create(ci CreateInfo, s *Service) error {
Expand All @@ -52,6 +55,7 @@ func Create(ci CreateInfo, s *Service) error {
return err
}

s.submissionEnabled = ci.EnableSubmission
if s.EthConn == nil {
if ci.EthConn == nil {
ci.EthConn, err = ethclient.Dial(ci.BlockchainHttpEndpoint.Value)
Expand All @@ -76,7 +80,7 @@ func Create(ci CreateInfo, s *Service) error {
s.ClaimsInFlight = map[claimKey]Hash{}
}

if s.Signer == nil {
if s.Signer == nil && s.submissionEnabled {
if ci.Signer == nil {
ci.Signer, err = CreateSignerFromAuth(ci.Auth, s.Context, s.EthConn)
if err != nil {
Expand Down Expand Up @@ -234,7 +238,7 @@ func (s *Service) submitClaimsAndUpdateDatabase(se SideEffects) error {
}

// submit if not found in the logs (fetch from hash again, can be stale)
if claim, ok := computedClaimsMap[key]; ok {
if claim, ok := computedClaimsMap[key]; ok && s.submissionEnabled {
s.Logger.Info("Submitting claim to blockchain",
"app", claim.AppContractAddress,
"claim", claim.Hash,
Expand Down
40 changes: 40 additions & 0 deletions internal/claimer/claimer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func newServiceMock() *ServiceMock {
Service: service.Service{
Logger: slog.Default(),
},
submissionEnabled: true,
},
}
}
Expand Down Expand Up @@ -162,6 +163,45 @@ func TestSubmitNewClaim(t *testing.T) {
m.AssertNumberOfCalls(t, "updateEpochWithSubmittedClaim", 0)
}

// Got a claim, don't submit.
func TestSubmitNewClaimDisabled(t *testing.T) {
m := newServiceMock()
m.submissionEnabled = false

newClaimHash := HexToHash("0x01")
newClaimTxHash := HexToHash("0x10")
newClaim := ComputedClaim{
Hash: newClaimHash,
}
m.ClaimsInFlight = map[claimKey]Hash{}
m.On("selectComputedClaims").Return([]ComputedClaim{
newClaim,
}, nil)
m.On("submitClaimToBlockchain", nil, nil, &newClaim).
Return(newClaimTxHash, nil)

itMock := &ClaimSubmissionIteratorMock{}
itMock.On("Next").Return(false)
itMock.On("Error").Return(nil)

m.On("enumerateSubmitClaimEventsSince").
Return(itMock, &iconsensus.IConsensus{}, nil)
m.On("pollTransaction", newClaimTxHash).
Return(false, &types.Receipt{}, nil)
assert.Equal(t, len(m.ClaimsInFlight), 0)

err := m.submitClaimsAndUpdateDatabase(m)
assert.Nil(t, err)

assert.Equal(t, len(m.ClaimsInFlight), 0)
m.AssertNumberOfCalls(t, "enumerateSubmitClaimEventsSince", 1)
m.AssertNumberOfCalls(t, "pollTransaction", 0)
m.AssertNumberOfCalls(t, "selectComputedClaims", 1)
m.AssertNumberOfCalls(t, "submitClaimToBlockchain", 0)
m.AssertNumberOfCalls(t, "updateEpochWithSubmittedClaim", 0)
}


// Query the blockchain for the submitClaim transaction, it may not be ready yet
func TestClaimInFlightNotReadyDoesNothing(t *testing.T) {
m := newServiceMock()
Expand Down
4 changes: 3 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type NodeConfig struct {
HttpAddress string
HttpPort int
FeatureClaimerEnabled bool
FeatureClaimerSubmissionEnabled bool
FeatureMachineHashCheckEnabled bool
Auth Auth
AdvancerPollingInterval Duration
Expand Down Expand Up @@ -92,8 +93,9 @@ func FromEnv() NodeConfig {
config.HttpAddress = GetHttpAddress()
config.HttpPort = GetHttpPort()
config.FeatureClaimerEnabled = GetFeatureClaimerEnabled()
config.FeatureClaimerSubmissionEnabled = GetFeatureClaimerSubmissionEnabled()
config.FeatureMachineHashCheckEnabled = GetFeatureMachineHashCheckEnabled()
if config.FeatureClaimerEnabled {
if config.FeatureClaimerEnabled && config.FeatureClaimerSubmissionEnabled {
config.Auth = authFromEnv()
}
config.AdvancerPollingInterval = GetAdvancerPollingInterval()
Expand Down
6 changes: 6 additions & 0 deletions internal/config/generate/Config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ go-type = "bool"
description = """
If set to false, the node will not make claims."""

[features.CARTESI_FEATURE_CLAIMER_SUBMISSION_ENABLED]
default = "true"
go-type = "bool"
description = """
If set to false, the node will not submit claims."""

[features.CARTESI_FEATURE_MACHINE_HASH_CHECK_ENABLED]
default = "true"
go-type = "bool"
Expand Down
12 changes: 12 additions & 0 deletions internal/config/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dd49f70

Please sign in to comment.