Skip to content

Commit

Permalink
further fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
sstanculeanu committed Aug 22, 2023
1 parent c87bf6c commit 5b2454a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
23 changes: 12 additions & 11 deletions process/scQueryProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/hex"
"fmt"
"net/http"
"net/url"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
Expand All @@ -13,10 +14,8 @@ import (

// scQueryServicePath defines the get values path at which the nodes answer
const scQueryServicePath = "/vm-values/query"
const separator = "&"
const suffix = "?"
const blockNonceParam = suffix + "blockNonce="
const blockHashParam = "blockHash="
const blockNonce = "blockNonce"
const blockHash = "blockHash"

// SCQueryProcessor is able to process smart contract queries
type SCQueryProcessor struct {
Expand Down Expand Up @@ -60,16 +59,18 @@ func (scQueryProcessor *SCQueryProcessor) ExecuteQuery(query *data.SCQuery) (*vm
request := scQueryProcessor.createRequestFromQuery(query)
response := &data.ResponseVmValue{}

path := scQueryServicePath
params := url.Values{}
if query.BlockNonce.HasValue {
path = fmt.Sprintf("%s%s%d", path, blockNonceParam, query.BlockNonce.Value)
params.Add(blockNonce, fmt.Sprintf("%d", query.BlockNonce.Value))
}
if len(query.BlockHash) > 0 {
hashSuffix := suffix
if query.BlockNonce.HasValue {
hashSuffix = separator
}
path = fmt.Sprintf("%s%s%s%s", path, hashSuffix, blockHashParam, hex.EncodeToString(query.BlockHash))
params.Add(blockHash, hex.EncodeToString(query.BlockHash))
}

queryParams := params.Encode()
path := scQueryServicePath
if len(queryParams) > 0 {
path = path + "?" + queryParams
}

httpStatus, err := scQueryProcessor.proc.CallPostRestEndPoint(observer.Address, path, request, response)
Expand Down
10 changes: 3 additions & 7 deletions process/scQueryProcessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package process
import (
"encoding/hex"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"testing"

"github.com/multiversx/mx-chain-core-go/core"
Expand Down Expand Up @@ -159,11 +158,8 @@ func TestSCQueryProcessor_ExecuteQueryWithCoordinates(t *testing.T) {
}, nil
},
CallPostRestEndPointCalled: func(address string, path string, dataValue interface{}, response interface{}) (int, error) {
require.Equal(t, providedAddr, address)
require.True(t, strings.Contains(path, "?blockNonce"))
require.True(t, strings.Contains(path, strconv.FormatUint(providedNonce, 10)))
require.True(t, strings.Contains(path, "&blockHash"))
require.True(t, strings.Contains(path, hex.EncodeToString(providedHash)))
expectedPath := fmt.Sprintf("%s/vm-values/query?blockHash=%s&blockNonce=%d", providedAddr, hex.EncodeToString(providedHash), providedNonce)
require.Equal(t, expectedPath, address+path)

response.(*data.ResponseVmValue).Data.Data = &vm.VMOutputApi{
ReturnData: [][]byte{{42}},
Expand Down

0 comments on commit 5b2454a

Please sign in to comment.