Skip to content

Commit

Permalink
feat: added placeholder for parsers (lavanet#1449)
Browse files Browse the repository at this point in the history
* added placeholder for parsers

* lint
  • Loading branch information
omerlavanet authored May 31, 2024
1 parent 604af55 commit d7d7308
Show file tree
Hide file tree
Showing 5 changed files with 764 additions and 100 deletions.
17 changes: 17 additions & 0 deletions proto/lavanet/lava/spec/api_collection.proto
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ message Api {
SpecCategory category = 6 [(gogoproto.nullable) = false];
BlockParser block_parsing = 7 [(gogoproto.nullable) = false];
uint64 timeout_ms = 8;
repeated GenericParser parsers = 9 [(gogoproto.nullable) = false];
}

message ParseDirective {
Expand All @@ -91,6 +92,12 @@ message BlockParser {
string encoding =4; // used to parse byte responses: base64,hex,bech32
}

message GenericParser {
string parse_path = 1;
string value = 2;
PARSER_TYPE parse_type = 3;
}

enum EXTENSION {
NONE = 0;
ARCHIVE =1;
Expand All @@ -106,6 +113,16 @@ enum FUNCTION_TAG {
GET_EARLIEST_BLOCK = 6;
}

enum PARSER_TYPE {
NO_PARSER = 0;
BLOCK_LATEST = 1;
BLOCK_EARLIEST = 2;
RESULT = 3;
EXTENSION_ARG = 4;
IDENTIFIER = 5;
DEFAULT_VALUE = 6;
}

enum PARSER_FUNC{
EMPTY = 0;
PARSE_BY_ARG = 1; //means parameters are ordered and flat expected arguments are: [param index] (example: PARAMS: [<#BlockNum>,"banana"]) args: 0
Expand Down
205 changes: 205 additions & 0 deletions x/spec/keeper/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"testing"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/testutil/common"
keepertest "github.com/lavanet/lava/testutil/keeper"
Expand Down Expand Up @@ -56,6 +57,7 @@ func prepareMockApis(count int, apiDiff string) []*types.Api {
BlockParsing: types.BlockParser{
DefaultValue: apiDiff,
},
ComputeUnits: 20,
}

api.Enabled = true
Expand All @@ -67,6 +69,7 @@ func prepareMockApis(count int, apiDiff string) []*types.Api {
BlockParsing: types.BlockParser{
DefaultValue: apiDiff,
},
ComputeUnits: 10,
}
mockApis[i+count/2] = api
}
Expand Down Expand Up @@ -888,3 +891,205 @@ func removeSetFromSet(set1, set2 []string) []string {

return resultSet
}

func TestParsers(t *testing.T) {
parserBook := []struct {
parsers []types.GenericParser
name string
shouldErr bool
}{
{
parsers: []types.GenericParser{
{
ParsePath: "1",
Value: "",
ParseType: types.PARSER_TYPE_NO_PARSER,
},
},
name: "invalid parsers type",
shouldErr: true,
},
{
parsers: []types.GenericParser{
{
ParsePath: "",
Value: "",
ParseType: types.PARSER_TYPE_BLOCK_LATEST,
},
},
name: "invalid parsers path",
shouldErr: true,
},
{
parsers: []types.GenericParser{
{
ParsePath: "0",
Value: "",
ParseType: types.PARSER_TYPE_BLOCK_LATEST,
},
},
name: "valid block parser",
shouldErr: false,
},
{
parsers: []types.GenericParser{
{
ParsePath: "0",
Value: "",
ParseType: types.PARSER_TYPE_BLOCK_LATEST,
},
{
ParsePath: "1",
Value: "",
ParseType: types.PARSER_TYPE_BLOCK_LATEST,
},
},
name: "valid multiple block parser",
shouldErr: false,
},
{
parsers: []types.GenericParser{
{
ParsePath: "0",
Value: "",
ParseType: types.PARSER_TYPE_BLOCK_LATEST,
},
{
ParsePath: "1",
Value: "",
ParseType: types.PARSER_TYPE_NO_PARSER,
},
},
name: "invalid type multiple block parser",
shouldErr: true,
},
{
parsers: []types.GenericParser{
{
ParsePath: "0",
Value: "",
ParseType: types.PARSER_TYPE_BLOCK_LATEST,
},
{
ParsePath: "",
Value: "",
ParseType: types.PARSER_TYPE_BLOCK_LATEST,
},
},
name: "invalid type multiple block parser",
shouldErr: true,
},
}
for _, parsers := range parserBook {
t.Run(parsers.name, func(t *testing.T) {
ts := newTester(t)

apiCollection := createApiCollection(5, []int{1, 2, 3}, 0, "jsonrpc", "POST", "", nil, "")
api := apiCollection.Apis[0]
api.Parsers = parsers.parsers
tt := struct {
desc string
name string
imports []string
apisCollections []*types.ApiCollection
resultApiCollections int
result []int
totalApis int
ok bool
}{
desc: "",
name: "test",
imports: []string{},
apisCollections: []*types.ApiCollection{apiCollection},
}
sp := types.Spec{
Index: tt.name,
Name: tt.name,
Enabled: true,
ReliabilityThreshold: 0xffffff,
DataReliabilityEnabled: false,
BlockDistanceForFinalizedData: 0,
BlocksInFinalizationProof: 1,
AverageBlockTime: 10,
AllowedBlockLagForQosSync: 1,
BlockLastUpdated: 0,
MinStakeProvider: sdk.Coin{
Denom: "ulava",
Amount: math.NewInt(5000000),
},
ApiCollections: tt.apisCollections,
Shares: 1,
Identity: "",
}
fullspec, err := ts.expandSpec(sp)
require.NoError(t, err)
_, err = fullspec.ValidateSpec(10000)
if parsers.shouldErr {
require.Error(t, err)
} else {
require.NoError(t, err)
require.True(t, len(fullspec.ApiCollections[0].Apis[0].Parsers) > 0)
}
ts.setSpec(sp)
})
}
}

func TestSpecParsing(t *testing.T) {
specJSON := `
{
"proposal": {
"title": "Add Specs: Lava",
"description": "Adding new specification support for relaying Lava data on Lava",
"specs": [
{
"index": "test",
"name": "test",
"enabled": true,
"reliability_threshold": 16777215,
"data_reliability_enabled": false,
"block_distance_for_finalized_data": 0,
"blocks_in_finalization_proof": 1,
"average_block_time": 10,
"allowed_block_lag_for_qos_sync": 1,
"block_last_updated": 0,
"min_stake_provider": {
"denom": "ulava",
"amount": "5000000"
},
"api_collections": [
{
"apis": [
{
"name": "APIName",
"compute_units": 10,
"parsers": [
{
"parse_path": "0",
"value": "",
"parse_type": "BLOCK_LATEST"
}
]
}
]
}
],
"shares": 1,
"identity": ""
}
]
}
}`

var proposal utils.SpecAddProposalJSON
err := json.Unmarshal([]byte(specJSON), &proposal)
require.NoError(t, err)
ts := newTester(t)
for _, spec := range proposal.Proposal.Specs {
fullspec, err := ts.expandSpec(spec)
require.NoError(t, err)
_, err = fullspec.ValidateSpec(10000)
require.NoError(t, err)
ts.setSpec(spec)
}
}
Loading

0 comments on commit d7d7308

Please sign in to comment.