Skip to content
Open
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
61 changes: 36 additions & 25 deletions internal/jsonrpc/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ func handleListEpochs(s *Service, w http.ResponseWriter, r *http.Request, req RP
return
}

if len(epochs) == 0 && !s.applicationExists(w, r, req, params.Application) {
writeRPCError(w, req.ID, JSONRPC_RESOURCE_NOT_FOUND, "Application not found", nil)
if len(epochs) == 0 && s.applicationAbsentOrError(w, r, req, params.Application) {
return
}
if epochs == nil {
Expand Down Expand Up @@ -282,21 +281,6 @@ func handleListEpochs(s *Service, w http.ResponseWriter, r *http.Request, req RP
writeRPCResult(w, req.ID, result)
}

func (s *Service) applicationExists(
w http.ResponseWriter,
r *http.Request,
req RPCRequest,
validatedNameOrAddress string,
) bool {
app, err := s.repository.GetApplication(r.Context(), validatedNameOrAddress)
if err != nil {
s.Logger.Error("Unable to retrieve application from repository", "err", err)
writeRPCError(w, req.ID, JSONRPC_INTERNAL_ERROR, "Internal server error", nil)
return false
}
return app != nil
}

func parseIndex(indexString string, field string) (uint64, error) {
if len(indexString) < 3 || (!strings.HasPrefix(indexString, "0x") && !strings.HasPrefix(indexString, "0X")) {
return 0, fmt.Errorf("invalid %s: expected hex encoded value", field)
Expand Down Expand Up @@ -438,8 +422,7 @@ func handleListInputs(s *Service, w http.ResponseWriter, r *http.Request, req RP
writeRPCError(w, req.ID, JSONRPC_INTERNAL_ERROR, "Internal server error", nil)
return
}
if len(inputs) == 0 && !s.applicationExists(w, r, req, params.Application) {
writeRPCError(w, req.ID, JSONRPC_RESOURCE_NOT_FOUND, "Application not found", nil)
if len(inputs) == 0 && s.applicationAbsentOrError(w, r, req, params.Application) {
return
}

Expand Down Expand Up @@ -654,8 +637,7 @@ func handleListOutputs(s *Service, w http.ResponseWriter, r *http.Request, req R
resultOutputs = append(resultOutputs, decoded)
}

if len(resultOutputs) == 0 && !s.applicationExists(w, r, req, params.Application) {
writeRPCError(w, req.ID, JSONRPC_RESOURCE_NOT_FOUND, "Application not found", nil)
if len(resultOutputs) == 0 && s.applicationAbsentOrError(w, r, req, params.Application) {
return
}

Expand Down Expand Up @@ -782,8 +764,7 @@ func handleListReports(s *Service, w http.ResponseWriter, r *http.Request, req R
return
}

if len(reports) == 0 && !s.applicationExists(w, r, req, params.Application) {
writeRPCError(w, req.ID, JSONRPC_RESOURCE_NOT_FOUND, "Application not found", nil)
if len(reports) == 0 && s.applicationAbsentOrError(w, r, req, params.Application) {
return
}
if reports == nil {
Expand Down Expand Up @@ -923,6 +904,9 @@ func handleListTournaments(s *Service, w http.ResponseWriter, r *http.Request, r
writeRPCError(w, req.ID, JSONRPC_INTERNAL_ERROR, "Internal server error", nil)
return
}
if len(tournaments) == 0 && s.applicationAbsentOrError(w, r, req, params.Application) {
return
}
if tournaments == nil {
tournaments = []*model.Tournament{}
}
Expand Down Expand Up @@ -1043,6 +1027,9 @@ func handleListCommitments(s *Service, w http.ResponseWriter, r *http.Request, r
writeRPCError(w, req.ID, JSONRPC_INTERNAL_ERROR, "Internal server error", nil)
return
}
if len(commitments) == 0 && s.applicationAbsentOrError(w, r, req, params.Application) {
return
}
if commitments == nil {
commitments = []*model.Commitment{}
}
Expand Down Expand Up @@ -1096,7 +1083,7 @@ func handleGetCommitment(s *Service, w http.ResponseWriter, r *http.Request, req
return
}

if _, err := hex.DecodeString(params.Commitment); err != nil {
if _, err := hex.DecodeString(params.Commitment); (len(params.Commitment) == 0) || (err != nil) {
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message will display "Invalid commitment hex: " when the commitment parameter is empty, because hex.DecodeString("") succeeds (returns nil error). Consider checking for empty string first and providing a more specific error message, or use a validation function similar to config.ToHashFromString which requires the 0x prefix and proper hex encoding.

Suggested change
if _, err := hex.DecodeString(params.Commitment); (len(params.Commitment) == 0) || (err != nil) {
if len(params.Commitment) == 0 {
writeRPCError(w, req.ID, JSONRPC_INVALID_PARAMS, "Commitment must not be empty", nil)
return
}
if _, err := hex.DecodeString(params.Commitment); err != nil {

Copilot uses AI. Check for mistakes.
writeRPCError(w, req.ID, JSONRPC_INVALID_PARAMS, fmt.Sprintf("Invalid commitment hex: %v", err), nil)
return
}
Expand Down Expand Up @@ -1173,6 +1160,9 @@ func handleListMatches(s *Service, w http.ResponseWriter, r *http.Request, req R
writeRPCError(w, req.ID, JSONRPC_INTERNAL_ERROR, "Internal server error", nil)
return
}
if len(matches) == 0 && s.applicationAbsentOrError(w, r, req, params.Application) {
return
}
if matches == nil {
matches = []*model.Match{}
}
Expand Down Expand Up @@ -1303,6 +1293,9 @@ func handleListMatchAdvances(s *Service, w http.ResponseWriter, r *http.Request,
writeRPCError(w, req.ID, JSONRPC_INTERNAL_ERROR, "Internal server error", nil)
return
}
if len(matchAdvances) == 0 && s.applicationAbsentOrError(w, r, req, params.Application) {
return
}
if matchAdvances == nil {
matchAdvances = []*model.MatchAdvanced{}
}
Expand Down Expand Up @@ -1367,7 +1360,7 @@ func handleGetMatchAdvanced(s *Service, w http.ResponseWriter, r *http.Request,
}

matchAdvanced, err := s.repository.GetMatchAdvanced(r.Context(), params.Application, epochIndex,
params.TournamentAddress, params.IDHash, params.Parent)
params.TournamentAddress, params.IDHash, params.Parent[2:]) // TODO: use parsed value
if err != nil {
s.Logger.Error("Unable to retrieve match advanced from repository", "err", err)
writeRPCError(w, req.ID, JSONRPC_INTERNAL_ERROR, "Internal server error", nil)
Expand Down Expand Up @@ -1418,3 +1411,21 @@ func handleGetNodeVersion(_ *Service, w http.ResponseWriter, _ *http.Request, re

writeRPCResult(w, req.ID, result)
}

func (s *Service) applicationAbsentOrError(
w http.ResponseWriter,
r *http.Request,
req RPCRequest,
validatedNameOrAddress string,
) bool {
app, err := s.repository.GetApplication(r.Context(), validatedNameOrAddress)
if err != nil {
s.Logger.Error("Unable to retrieve application from repository", "err", err)
writeRPCError(w, req.ID, JSONRPC_INTERNAL_ERROR, "Internal server error", nil)
return true
} else if app == nil {
writeRPCError(w, req.ID, JSONRPC_RESOURCE_NOT_FOUND, "Application not found", nil)
return true
}
return false
}
Loading
Loading