Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: PRT - Result generic parser #1711

Merged
merged 23 commits into from
Oct 9, 2024
Merged
Changes from 7 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
49 changes: 32 additions & 17 deletions protocol/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,31 +98,34 @@ func filterGenericParsersByType(genericParsers []spectypes.GenericParser, filter
}

func parseInputWithGenericParsers(rpcInput RPCInput, genericParsers []spectypes.GenericParser) (*ParsedInput, bool) {
parsedSuccessfully := false
managedToParseRawBlock := false
if len(genericParsers) == 0 {
return nil, parsedSuccessfully
return nil, managedToParseRawBlock
}

genericParserResult, genericParserErr := ParseWithGenericParsers(rpcInput, filterGenericParsersByType(genericParsers, getParserTypeMap(PARSE_PARAMS)))
if genericParserErr != nil {
return nil, parsedSuccessfully
return nil, managedToParseRawBlock
}

parsed := NewParsedInput()
parsedBlock := genericParserResult.GetRawParsedData()
if parsedBlock != "" {
parsedSuccessfully = true
parsed.parsedDataRaw = parsedBlock
rawParsedData := genericParserResult.GetRawParsedData()
if rawParsedData != "" {
managedToParseRawBlock = true
parsed.parsedDataRaw = rawParsedData
}

parsedBlockHashes, err := genericParserResult.GetBlockHashes()
omerlavanet marked this conversation as resolved.
Show resolved Hide resolved
if err == nil {
parsed.parsedHashes = parsedBlockHashes
}

return parsed, parsedSuccessfully
return parsed, managedToParseRawBlock
}

// ParseRawBlock attempts to parse a block from rpcInput and store it in parsedInput.
// If parsing fails or rawBlock is empty, it uses defaultValue if provided.
// If parsing the defaultValue also fails, it sets the block to NOT_APPLICABLE.
func ParseRawBlock(rpcInput RPCInput, parsedInput *ParsedInput, defaultValue string) {
omerlavanet marked this conversation as resolved.
Show resolved Hide resolved
rawBlock := parsedInput.GetRawParsedData()
var parsedBlock int64
Expand Down Expand Up @@ -170,9 +173,22 @@ func parseInputWithLegacyBlockParser(rpcInput RPCInput, blockParser spectypes.Bl
return resString, nil
}

// parseBlock processes the given RPC input using either generic parsers or a legacy block parser.
// It first attempts to parse the input with the provided generic parsers. If successful, it returns
// the parsed information after unquoting the raw parsed data. If the generic parsing fails, it falls
// back to using a legacy block parser.
//
// Parameters:
// - rpcInput: The input data to be parsed.
// - blockParser: The legacy block parser to use if generic parsing fails.
// - genericParsers: A slice of generic parsers to attempt first.
// - source: An integer representing the source of the input: either PARSE_PARAMS or PARSE_RESULT.
//
// Returns:
// - A pointer to a ParsedInput struct containing the parsed data.
func parseBlock(rpcInput RPCInput, blockParser spectypes.BlockParser, genericParsers []spectypes.GenericParser, source int) *ParsedInput {
omerlavanet marked this conversation as resolved.
Show resolved Hide resolved
parsedBlockInfo, parsedSuccessfully := parseInputWithGenericParsers(rpcInput, genericParsers)
if parsedSuccessfully {
parsedBlockInfo, managedToParseRawBlock := parseInputWithGenericParsers(rpcInput, genericParsers)
if managedToParseRawBlock {
parsedBlockInfo.parsedDataRaw = unquoteString(parsedBlockInfo.parsedDataRaw)
return parsedBlockInfo
}
Expand All @@ -185,10 +201,6 @@ func parseBlock(rpcInput RPCInput, blockParser spectypes.BlockParser, genericPar
return parsedBlockInfo
}

func ParseRawBlockFromParams(rpcInput RPCInput, blockParser spectypes.BlockParser, genericParsers []spectypes.GenericParser) *ParsedInput {
return parseBlock(rpcInput, blockParser, genericParsers, PARSE_PARAMS)
}

func ParseBlockFromParams(rpcInput RPCInput, blockParser spectypes.BlockParser, genericParsers []spectypes.GenericParser) *ParsedInput {
parsedInput := parseBlock(rpcInput, blockParser, genericParsers, PARSE_PARAMS)
ParseRawBlock(rpcInput, parsedInput, blockParser.DefaultValue)
Expand Down Expand Up @@ -275,9 +287,11 @@ type ParsedInput struct {
parsedHashes []string
}

const RAW_NOT_APPLICABLE = "-1"

func NewParsedInput() *ParsedInput {
return &ParsedInput{
omerlavanet marked this conversation as resolved.
Show resolved Hide resolved
parsedDataRaw: "",
parsedDataRaw: RAW_NOT_APPLICABLE,
parsedHashes: make([]string, 0),
}
}
Expand All @@ -303,8 +317,9 @@ func (p *ParsedInput) GetBlockHashes() ([]string, error) {

func getMapForParse(rpcInput RPCInput) map[string]interface{} {
var result map[string]interface{}
if rpcInput.GetResult() != nil {
json.Unmarshal(rpcInput.GetResult(), &result)
rpcInputResult := rpcInput.GetResult()
if rpcInputResult != nil {
json.Unmarshal(rpcInputResult, &result)
}
return map[string]interface{}{"params": rpcInput.GetParams(), "result": result}
}
Expand Down
Loading