Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(claimer): Make claim submission optional. #608

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 18 additions & 0 deletions cmd/cartesi-rollups-node/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,32 @@ var (
Long: "Runs the Cartesi Rollups Node as a single process",
RunE: run,
}
enableClaimSubmissionOverride bool
)

func init() {
Cmd.Flags().BoolVar(&enableClaimSubmissionOverride,
"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()

// hack: change the environment variable according to the command line flag
// such that auth only gets loaded when claimer submission is enabled.
if cmd.Flags().Lookup("enable-submission").Changed {
var value string
if enableClaimSubmissionOverride {
value = "1"
} else {
value = "0"
}
os.Setenv("CARTESI_FEATURE_CLAIMER_SUBMISSION_ENABLED", value)
}
config := config.FromEnv()

// setup log
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.

Loading