Skip to content

Commit

Permalink
Fix: temp handleBatchRequest modification to allow headers
Browse files Browse the repository at this point in the history
  • Loading branch information
obasekiosa committed Aug 7, 2024
1 parent ef54d71 commit 62e82af
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"errors"
"fmt"
"io"
"maps"
"net/http"
"reflect"
"strconv"
"strings"
"sync"
"time"
Expand All @@ -27,6 +29,10 @@ const (
InternalError = -32603 // Internal JSON-RPC error.
)

const (
ExecutionStepsHeaderUint64 string = "X-Cairo-Steps"
)

var (
ErrInvalidID = errors.New("id should be a string or an integer")

Expand Down Expand Up @@ -392,11 +398,23 @@ func (s *Server) handleBatchRequest(ctx context.Context, batchReq []json.RawMess

result, err := json.Marshal(responses)

if len(headers) == 0 {
return result, http.Header{}, err
}
header := mergeHeaders(headers)

return result, headers[0], err // todo: fix batch request aggregate header
return result, header, err // todo: fix batch request aggregate header
}

func mergeHeaders(headers []http.Header) http.Header {
var totalExec uint64
finalHeaders := http.Header{}
for _, header := range headers {
val, err := strconv.ParseUint(header.Get(ExecutionStepsHeaderUint64), 10, 64)
if err == nil {
totalExec += val
}
maps.Copy(finalHeaders, header)
}
finalHeaders.Set(ExecutionStepsHeaderUint64, strconv.FormatUint(totalExec, 10))
return finalHeaders
}

func isBatch(reader *bufio.Reader) bool {
Expand Down

0 comments on commit 62e82af

Please sign in to comment.