Skip to content

Commit

Permalink
fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
sstanculeanu committed Aug 22, 2023
1 parent 7156dc5 commit c87bf6c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
18 changes: 12 additions & 6 deletions process/scQueryProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (

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

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

path := scQueryServicePath
if len(query.BlockHash) > 0 {
path = fmt.Sprintf("%s%s%s", path, blockHashSuffix, hex.EncodeToString(query.BlockHash))
}
if query.BlockNonce.HasValue {
path = fmt.Sprintf("%s%s%d", path, blockNonceSuffix, query.BlockNonce.Value)
path = fmt.Sprintf("%s%s%d", path, blockNonceParam, 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))
}

httpStatus, err := scQueryProcessor.proc.CallPostRestEndPoint(observer.Address, path, request, response)
Expand Down
13 changes: 10 additions & 3 deletions process/scQueryProcessor_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package process

import (
"encoding/hex"
"errors"
"net/http"
"strconv"
Expand Down Expand Up @@ -141,23 +142,28 @@ func TestSCQueryProcessor_ExecuteQueryWithCoordinates(t *testing.T) {
t.Parallel()

providedNonce := uint64(123)
providedHash := []byte("block hash")
providedBlockInfo := data.BlockInfo{
Nonce: providedNonce,
Hash: "block hash",
Hash: string(providedHash),
RootHash: "block rootHash",
}
providedAddr := "address1"
processor, _ := NewSCQueryProcessor(&mock.ProcessorStub{
ComputeShardIdCalled: func(addressBuff []byte) (u uint32, e error) {
return 0, nil
},
GetObserversCalled: func(shardId uint32) (observers []*data.NodeData, e error) {
return []*data.NodeData{
{Address: "adress1", ShardId: 0},
{Address: providedAddr, ShardId: 0},
}, nil
},
CallPostRestEndPointCalled: func(address string, path string, dataValue interface{}, response interface{}) (int, error) {
require.True(t, strings.Contains(path, "blockNonce"))
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)))

response.(*data.ResponseVmValue).Data.Data = &vm.VMOutputApi{
ReturnData: [][]byte{{42}},
Expand All @@ -176,6 +182,7 @@ func TestSCQueryProcessor_ExecuteQueryWithCoordinates(t *testing.T) {
Value: providedNonce,
HasValue: true,
},
BlockHash: providedHash,
})

require.Nil(t, err)
Expand Down

0 comments on commit c87bf6c

Please sign in to comment.