From d3aa3a25e454eab45ed13f4c83d18748c7d2c015 Mon Sep 17 00:00:00 2001 From: Elad Gildnur Date: Tue, 17 Sep 2024 11:41:54 +0300 Subject: [PATCH 1/5] Suppress warning log from parser to debug on successful hash parsing --- protocol/parser/parser.go | 95 ++++++++++++++++++++++++++++----------- utils/lavalog.go | 24 ++++++---- 2 files changed, 83 insertions(+), 36 deletions(-) diff --git a/protocol/parser/parser.go b/protocol/parser/parser.go index 19cff573f5..511bcbb4b5 100644 --- a/protocol/parser/parser.go +++ b/protocol/parser/parser.go @@ -12,6 +12,7 @@ import ( sdkerrors "cosmossdk.io/errors" "github.com/lavanet/lava/v3/utils" + "github.com/lavanet/lava/v3/utils/lavaslices" pairingtypes "github.com/lavanet/lava/v3/x/pairing/types" spectypes "github.com/lavanet/lava/v3/x/spec/types" ) @@ -34,6 +35,17 @@ type RPCInput interface { GetID() json.RawMessage } +type ParserOptions struct { + WarnLogOnFailedParsing bool +} + +func (po *ParserOptions) shouldWarnLogOnFailedParsing() bool { + if po == nil { + return true + } + return po.WarnLogOnFailedParsing +} + func ParseDefaultBlockParameter(block string) (int64, error) { switch block { case "latest": @@ -131,9 +143,15 @@ func ParseBlockFromParams(rpcInput RPCInput, blockParser spectypes.BlockParser, parsedBlockInfo = NewParsedInput() } + var parserOptions *ParserOptions + _, err := parsedBlockInfo.GetBlockHashes() + if err == nil { + // found a hash, no need to log warning later + parserOptions = &ParserOptions{WarnLogOnFailedParsing: false} + } parsedBlockInfo.parsedBlock = func() int64 { // first we try to parse the value with the block parser - result, err := parse(rpcInput, blockParser, PARSE_PARAMS) + result, err := parse(rpcInput, blockParser, PARSE_PARAMS, parserOptions) if err != nil || result == nil { utils.LavaFormatDebug("ParseBlockFromParams - parse failed", utils.LogAttr("error", err), @@ -176,7 +194,7 @@ func ParseBlockFromParams(rpcInput RPCInput, blockParser spectypes.BlockParser, // This returns the parsed response without decoding func ParseFromReply(rpcInput RPCInput, blockParser spectypes.BlockParser) (string, error) { - result, err := parse(rpcInput, blockParser, PARSE_RESULT) + result, err := parse(rpcInput, blockParser, PARSE_RESULT, nil) if err != nil || result == nil { utils.LavaFormatDebug("ParseBlockFromParams - parse failed", utils.LogAttr("error", err), @@ -220,7 +238,7 @@ func ParseFromReplyAndDecode(rpcInput RPCInput, resultParser spectypes.BlockPars return parseResponseByEncoding([]byte(response), resultParser.Encoding) } -func parse(rpcInput RPCInput, blockParser spectypes.BlockParser, dataSource int) ([]interface{}, error) { +func parse(rpcInput RPCInput, blockParser spectypes.BlockParser, dataSource int, parserOptions *ParserOptions) ([]interface{}, error) { var retval []interface{} var err error @@ -228,12 +246,14 @@ func parse(rpcInput RPCInput, blockParser spectypes.BlockParser, dataSource int) case spectypes.PARSER_FUNC_EMPTY: return nil, nil case spectypes.PARSER_FUNC_PARSE_BY_ARG: - retval, err = parseByArg(rpcInput, blockParser.ParserArg, dataSource) + retval, err = parseByArg(rpcInput, blockParser.ParserArg, dataSource, parserOptions) case spectypes.PARSER_FUNC_PARSE_CANONICAL: - retval, err = parseCanonical(rpcInput, blockParser.ParserArg, dataSource) + retval, err = parseCanonical(rpcInput, blockParser.ParserArg, dataSource, parserOptions) case spectypes.PARSER_FUNC_PARSE_DICTIONARY: + // currently, parseDictionary does not log warnings. If it ever will, we need to pass parserOptions retval, err = parseDictionary(rpcInput, blockParser.ParserArg, dataSource) case spectypes.PARSER_FUNC_PARSE_DICTIONARY_OR_ORDERED: + // currently, parseDictionaryOrOrdered does not log warnings. If it ever will, we need to pass parserOptions retval, err = parseDictionaryOrOrdered(rpcInput, blockParser.ParserArg, dataSource) case spectypes.PARSER_FUNC_DEFAULT: retval = parseDefault(blockParser.ParserArg) @@ -510,21 +530,28 @@ func blockInterfaceToString(block interface{}) string { } } -func parseByArg(rpcInput RPCInput, input []string, dataSource int) ([]interface{}, error) { +func parseByArg(rpcInput RPCInput, input []string, dataSource int, parserOptions *ParserOptions) ([]interface{}, error) { + var lavaLogSeverity uint = utils.LAVA_LOG_PRODUCTION + if !parserOptions.shouldWarnLogOnFailedParsing() { + lavaLogSeverity = utils.LAVA_LOG_DEBUG + } + // specified block is one of the direct parameters, input should be one string defining the location of the block if len(input) != 1 { - return nil, utils.LavaFormatProduction("invalid input format, input length", nil, utils.LogAttr("input_len", strconv.Itoa(len(input)))) + utils.LavaFormatLog("invalid input format, input length", nil, lavaslices.Slice(utils.LogAttr("input_len", len(input))), lavaLogSeverity) } + inp := input[0] param_index, err := strconv.ParseUint(inp, 10, 32) if err != nil { - return nil, utils.LavaFormatProduction("invalid input format, input isn't an unsigned index", err, utils.LogAttr("input", inp)) + return nil, utils.LavaFormatLog("invalid input format, input isn't an unsigned index", err, lavaslices.Slice(utils.LogAttr("input", inp)), lavaLogSeverity) } unmarshalledData, err := getDataToParse(rpcInput, dataSource) if err != nil { - return nil, utils.LavaFormatProduction("invalid input format, data is not json", err, utils.LogAttr("data", unmarshalledData)) + return nil, utils.LavaFormatLog("invalid input format, data is not json", err, lavaslices.Slice(utils.LogAttr("data", unmarshalledData)), lavaLogSeverity) } + switch unmarshaledDataTyped := unmarshalledData.(type) { case []interface{}: if uint64(len(unmarshaledDataTyped)) <= param_index { @@ -537,10 +564,13 @@ func parseByArg(rpcInput RPCInput, input []string, dataSource int) ([]interface{ retArr = append(retArr, blockInterfaceToString(block)) return retArr, nil default: - // Parse by arg can be only list as we dont have the name of the height property. - return nil, utils.LavaFormatProduction("Parse type unsupported in parse by arg, only list parameters are currently supported", nil, - utils.LogAttr("params", rpcInput.GetParams()), - utils.LogAttr("request", unmarshaledDataTyped), + // Parse by arg can be only list as we don't have the name of the height property. + return nil, utils.LavaFormatLog("Parse type unsupported in parse by arg, only list parameters are currently supported", nil, + lavaslices.Slice( + utils.LogAttr("params", rpcInput.GetParams()), + utils.LogAttr("request", unmarshaledDataTyped), + ), + lavaLogSeverity, ) } } @@ -556,7 +586,12 @@ func parseByArg(rpcInput RPCInput, input []string, dataSource int) ([]interface{ // } // // should output an interface array with "wanted result" in first index 0 -func parseCanonical(rpcInput RPCInput, input []string, dataSource int) ([]interface{}, error) { +func parseCanonical(rpcInput RPCInput, input []string, dataSource int, parserOptions *ParserOptions) ([]interface{}, error) { + var lavaLogSeverity uint = utils.LAVA_LOG_WARN + if !parserOptions.shouldWarnLogOnFailedParsing() { + lavaLogSeverity = utils.LAVA_LOG_DEBUG + } + unmarshalledData, err := getDataToParse(rpcInput, dataSource) if err != nil { return nil, fmt.Errorf("invalid input format, data is not json: %s, error: %s", unmarshalledData, err) @@ -574,14 +609,17 @@ func parseCanonical(rpcInput RPCInput, input []string, dataSource int) ([]interf } blockContainer := unmarshalledDataTyped[param_index] for _, key := range input[1:] { - // type assertion for blockcontainer + // type assertion for blockContainer if blockContainer, ok := blockContainer.(map[string]interface{}); !ok { - return nil, utils.LavaFormatWarning("invalid parser input format, blockContainer is not map[string]interface{}", ValueNotSetError, - utils.LogAttr("params", rpcInput.GetParams()), - utils.LogAttr("method", rpcInput.GetMethod()), - utils.LogAttr("blockContainer", fmt.Sprintf("%v", blockContainer)), - utils.LogAttr("key", key), - utils.LogAttr("unmarshaledDataTyped", unmarshalledDataTyped), + return nil, utils.LavaFormatLog("invalid parser input format, blockContainer is not map[string]interface{}", ValueNotSetError, + lavaslices.Slice( + utils.LogAttr("params", rpcInput.GetParams()), + utils.LogAttr("method", rpcInput.GetMethod()), + utils.LogAttr("blockContainer", fmt.Sprintf("%v", blockContainer)), + utils.LogAttr("key", key), + utils.LogAttr("unmarshaledDataTyped", unmarshalledDataTyped), + ), + lavaLogSeverity, ) } @@ -589,12 +627,15 @@ func parseCanonical(rpcInput RPCInput, input []string, dataSource int) ([]interf if container, ok := blockContainer.(map[string]interface{})[key]; ok { blockContainer = container } else { - return nil, utils.LavaFormatWarning("invalid parser input format, blockContainer does not have the field searched inside", ValueNotSetError, - utils.LogAttr("params", rpcInput.GetParams()), - utils.LogAttr("method", rpcInput.GetMethod()), - utils.LogAttr("blockContainer", fmt.Sprintf("%v", blockContainer)), - utils.LogAttr("key", key), - utils.LogAttr("unmarshaledDataTyped", unmarshalledDataTyped), + return nil, utils.LavaFormatLog("invalid parser input format, blockContainer does not have the field searched inside", ValueNotSetError, + lavaslices.Slice( + utils.LogAttr("params", rpcInput.GetParams()), + utils.LogAttr("method", rpcInput.GetMethod()), + utils.LogAttr("blockContainer", fmt.Sprintf("%v", blockContainer)), + utils.LogAttr("key", key), + utils.LogAttr("unmarshaledDataTyped", unmarshalledDataTyped), + ), + lavaLogSeverity, ) } } diff --git a/utils/lavalog.go b/utils/lavalog.go index 5e7908d185..aae7e82827 100644 --- a/utils/lavalog.go +++ b/utils/lavalog.go @@ -30,6 +30,7 @@ const ( LAVA_LOG_ERROR LAVA_LOG_FATAL LAVA_LOG_PANIC + LAVA_LOG_PRODUCTION NoColor = true ) @@ -226,6 +227,18 @@ func LavaFormatLog(description string, err error, attributes []Attribute, severi zerologlog.Logger = zerologlog.Output(zerolog.ConsoleWriter{Out: os.Stderr, NoColor: NoColor, TimeFormat: time.Stamp}).Level(defaultGlobalLogLevel) } + // depending on the build flag, this log function will log either a warning or an error. + // the purpose of this function is to fail E2E tests and not allow unexpected behavior to reach main. + // while in production some errors may occur as consumers / providers might set up their processes in the wrong way. + // in test environment we don't expect to have these errors and if they occur we would like to fail the test. + if severity == LAVA_LOG_PRODUCTION { + if ExtendedLogLevel == "production" { + severity = LAVA_LOG_WARN + } else { + severity = LAVA_LOG_ERROR + } + } + var logEvent *zerolog.Event var rollingLoggerEvent *zerolog.Event switch severity { @@ -301,16 +314,9 @@ func LavaFormatFatal(description string, err error, attributes ...Attribute) { LavaFormatLog(description, err, attributes, LAVA_LOG_FATAL) } -// depending on the build flag, this log function will log either a warning or an error. -// the purpose of this function is to fail E2E tests and not allow unexpected behavior to reach main. -// while in production some errors may occur as consumers / providers might set up their processes in the wrong way. -// in test environment we dont expect to have these errors and if they occur we would like to fail the test. +// see documentation in LavaFormatLog function func LavaFormatProduction(description string, err error, attributes ...Attribute) error { - if ExtendedLogLevel == "production" { - return LavaFormatWarning(description, err, attributes...) - } else { - return LavaFormatError(description, err, attributes...) - } + return LavaFormatLog(description, err, attributes, LAVA_LOG_PRODUCTION) } func LavaFormatError(description string, err error, attributes ...Attribute) error { From effb633f002038df0c5d191361cd2205e1d6ca35 Mon Sep 17 00:00:00 2001 From: Elad Gildnur Date: Tue, 24 Sep 2024 17:48:03 +0300 Subject: [PATCH 2/5] CR Fix: Move decision out of parser --- protocol/parser/parser.go | 82 +++++++++------------------------------ 1 file changed, 19 insertions(+), 63 deletions(-) diff --git a/protocol/parser/parser.go b/protocol/parser/parser.go index 511bcbb4b5..42737ea60e 100644 --- a/protocol/parser/parser.go +++ b/protocol/parser/parser.go @@ -35,17 +35,6 @@ type RPCInput interface { GetID() json.RawMessage } -type ParserOptions struct { - WarnLogOnFailedParsing bool -} - -func (po *ParserOptions) shouldWarnLogOnFailedParsing() bool { - if po == nil { - return true - } - return po.WarnLogOnFailedParsing -} - func ParseDefaultBlockParameter(block string) (int64, error) { switch block { case "latest": @@ -143,22 +132,21 @@ func ParseBlockFromParams(rpcInput RPCInput, blockParser spectypes.BlockParser, parsedBlockInfo = NewParsedInput() } - var parserOptions *ParserOptions + parseErrorLogLevel := utils.LAVA_LOG_WARN _, err := parsedBlockInfo.GetBlockHashes() if err == nil { // found a hash, no need to log warning later - parserOptions = &ParserOptions{WarnLogOnFailedParsing: false} + parseErrorLogLevel = utils.LAVA_LOG_DEBUG } parsedBlockInfo.parsedBlock = func() int64 { // first we try to parse the value with the block parser - result, err := parse(rpcInput, blockParser, PARSE_PARAMS, parserOptions) + result, err := parse(rpcInput, blockParser, PARSE_PARAMS) if err != nil || result == nil { - utils.LavaFormatDebug("ParseBlockFromParams - parse failed", - utils.LogAttr("error", err), + utils.LavaFormatLog("ParseBlockFromParams - parse failed", err, lavaslices.Slice( utils.LogAttr("result", result), utils.LogAttr("blockParser", blockParser), utils.LogAttr("rpcInput", rpcInput), - ) + ), uint(parseErrorLogLevel)) return spectypes.NOT_APPLICABLE } @@ -194,7 +182,7 @@ func ParseBlockFromParams(rpcInput RPCInput, blockParser spectypes.BlockParser, // This returns the parsed response without decoding func ParseFromReply(rpcInput RPCInput, blockParser spectypes.BlockParser) (string, error) { - result, err := parse(rpcInput, blockParser, PARSE_RESULT, nil) + result, err := parse(rpcInput, blockParser, PARSE_RESULT) if err != nil || result == nil { utils.LavaFormatDebug("ParseBlockFromParams - parse failed", utils.LogAttr("error", err), @@ -238,7 +226,7 @@ func ParseFromReplyAndDecode(rpcInput RPCInput, resultParser spectypes.BlockPars return parseResponseByEncoding([]byte(response), resultParser.Encoding) } -func parse(rpcInput RPCInput, blockParser spectypes.BlockParser, dataSource int, parserOptions *ParserOptions) ([]interface{}, error) { +func parse(rpcInput RPCInput, blockParser spectypes.BlockParser, dataSource int) ([]interface{}, error) { var retval []interface{} var err error @@ -246,9 +234,9 @@ func parse(rpcInput RPCInput, blockParser spectypes.BlockParser, dataSource int, case spectypes.PARSER_FUNC_EMPTY: return nil, nil case spectypes.PARSER_FUNC_PARSE_BY_ARG: - retval, err = parseByArg(rpcInput, blockParser.ParserArg, dataSource, parserOptions) + retval, err = parseByArg(rpcInput, blockParser.ParserArg, dataSource) case spectypes.PARSER_FUNC_PARSE_CANONICAL: - retval, err = parseCanonical(rpcInput, blockParser.ParserArg, dataSource, parserOptions) + retval, err = parseCanonical(rpcInput, blockParser.ParserArg, dataSource) case spectypes.PARSER_FUNC_PARSE_DICTIONARY: // currently, parseDictionary does not log warnings. If it ever will, we need to pass parserOptions retval, err = parseDictionary(rpcInput, blockParser.ParserArg, dataSource) @@ -530,26 +518,21 @@ func blockInterfaceToString(block interface{}) string { } } -func parseByArg(rpcInput RPCInput, input []string, dataSource int, parserOptions *ParserOptions) ([]interface{}, error) { - var lavaLogSeverity uint = utils.LAVA_LOG_PRODUCTION - if !parserOptions.shouldWarnLogOnFailedParsing() { - lavaLogSeverity = utils.LAVA_LOG_DEBUG - } - +func parseByArg(rpcInput RPCInput, input []string, dataSource int) ([]interface{}, error) { // specified block is one of the direct parameters, input should be one string defining the location of the block if len(input) != 1 { - utils.LavaFormatLog("invalid input format, input length", nil, lavaslices.Slice(utils.LogAttr("input_len", len(input))), lavaLogSeverity) + return nil, fmt.Errorf("invalid input format, input length: %d and needs to be 1", len(input)) } inp := input[0] param_index, err := strconv.ParseUint(inp, 10, 32) if err != nil { - return nil, utils.LavaFormatLog("invalid input format, input isn't an unsigned index", err, lavaslices.Slice(utils.LogAttr("input", inp)), lavaLogSeverity) + return nil, fmt.Errorf("invalid input format, input isn't an unsigned index. input=%s error=%w", inp, err) } unmarshalledData, err := getDataToParse(rpcInput, dataSource) if err != nil { - return nil, utils.LavaFormatLog("invalid input format, data is not json", err, lavaslices.Slice(utils.LogAttr("data", unmarshalledData)), lavaLogSeverity) + return nil, fmt.Errorf("invalid input format, data is not json. data=%s err=%w", unmarshalledData, err) } switch unmarshaledDataTyped := unmarshalledData.(type) { @@ -565,13 +548,7 @@ func parseByArg(rpcInput RPCInput, input []string, dataSource int, parserOptions return retArr, nil default: // Parse by arg can be only list as we don't have the name of the height property. - return nil, utils.LavaFormatLog("Parse type unsupported in parse by arg, only list parameters are currently supported", nil, - lavaslices.Slice( - utils.LogAttr("params", rpcInput.GetParams()), - utils.LogAttr("request", unmarshaledDataTyped), - ), - lavaLogSeverity, - ) + return nil, fmt.Errorf("parse type unsupported in parse by arg, only list parameters are currently supported. param=%s request=%s", rpcInput.GetParams(), unmarshaledDataTyped) } } @@ -586,12 +563,7 @@ func parseByArg(rpcInput RPCInput, input []string, dataSource int, parserOptions // } // // should output an interface array with "wanted result" in first index 0 -func parseCanonical(rpcInput RPCInput, input []string, dataSource int, parserOptions *ParserOptions) ([]interface{}, error) { - var lavaLogSeverity uint = utils.LAVA_LOG_WARN - if !parserOptions.shouldWarnLogOnFailedParsing() { - lavaLogSeverity = utils.LAVA_LOG_DEBUG - } - +func parseCanonical(rpcInput RPCInput, input []string, dataSource int) ([]interface{}, error) { unmarshalledData, err := getDataToParse(rpcInput, dataSource) if err != nil { return nil, fmt.Errorf("invalid input format, data is not json: %s, error: %s", unmarshalledData, err) @@ -611,32 +583,16 @@ func parseCanonical(rpcInput RPCInput, input []string, dataSource int, parserOpt for _, key := range input[1:] { // type assertion for blockContainer if blockContainer, ok := blockContainer.(map[string]interface{}); !ok { - return nil, utils.LavaFormatLog("invalid parser input format, blockContainer is not map[string]interface{}", ValueNotSetError, - lavaslices.Slice( - utils.LogAttr("params", rpcInput.GetParams()), - utils.LogAttr("method", rpcInput.GetMethod()), - utils.LogAttr("blockContainer", fmt.Sprintf("%v", blockContainer)), - utils.LogAttr("key", key), - utils.LogAttr("unmarshaledDataTyped", unmarshalledDataTyped), - ), - lavaLogSeverity, - ) + return nil, fmt.Errorf("invalid parser input format, blockContainer is not map[string]interface{}. "+ + "params=%v method=%v blockContainer=%v key=%s unmarshaledDataTyped=%v err=%w", rpcInput.GetParams(), rpcInput.GetMethod(), blockContainer, key, unmarshalledDataTyped, ValueNotSetError) } // assertion for key if container, ok := blockContainer.(map[string]interface{})[key]; ok { blockContainer = container } else { - return nil, utils.LavaFormatLog("invalid parser input format, blockContainer does not have the field searched inside", ValueNotSetError, - lavaslices.Slice( - utils.LogAttr("params", rpcInput.GetParams()), - utils.LogAttr("method", rpcInput.GetMethod()), - utils.LogAttr("blockContainer", fmt.Sprintf("%v", blockContainer)), - utils.LogAttr("key", key), - utils.LogAttr("unmarshaledDataTyped", unmarshalledDataTyped), - ), - lavaLogSeverity, - ) + return nil, fmt.Errorf("invalid parser input format, blockContainer does not have the field searched inside."+ + "params=%v method=%v blockContainer=%v key=%s unmarshaledDataTyped=%v err=%w", rpcInput.GetParams(), rpcInput.GetMethod(), blockContainer, key, unmarshalledDataTyped, ValueNotSetError) } } retArr := make([]interface{}, 0) From 4778e9914cc55702dff72b85fde9cadd06e4bd85 Mon Sep 17 00:00:00 2001 From: Elad Gildnur Date: Sun, 6 Oct 2024 12:44:36 +0300 Subject: [PATCH 3/5] Wrap the error with ValueNotSetError for the ValueNotSetError.Is(err) --- protocol/parser/parser.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/protocol/parser/parser.go b/protocol/parser/parser.go index 42737ea60e..9ea25f75ba 100644 --- a/protocol/parser/parser.go +++ b/protocol/parser/parser.go @@ -583,16 +583,16 @@ func parseCanonical(rpcInput RPCInput, input []string, dataSource int) ([]interf for _, key := range input[1:] { // type assertion for blockContainer if blockContainer, ok := blockContainer.(map[string]interface{}); !ok { - return nil, fmt.Errorf("invalid parser input format, blockContainer is not map[string]interface{}. "+ - "params=%v method=%v blockContainer=%v key=%s unmarshaledDataTyped=%v err=%w", rpcInput.GetParams(), rpcInput.GetMethod(), blockContainer, key, unmarshalledDataTyped, ValueNotSetError) + return nil, ValueNotSetError.Wrapf("invalid parser input format, blockContainer is not map[string]interface{}. "+ + "params=%v method=%v blockContainer=%v key=%s unmarshaledDataTyped=%v", rpcInput.GetParams(), rpcInput.GetMethod(), blockContainer, key, unmarshalledDataTyped) } // assertion for key if container, ok := blockContainer.(map[string]interface{})[key]; ok { blockContainer = container } else { - return nil, fmt.Errorf("invalid parser input format, blockContainer does not have the field searched inside."+ - "params=%v method=%v blockContainer=%v key=%s unmarshaledDataTyped=%v err=%w", rpcInput.GetParams(), rpcInput.GetMethod(), blockContainer, key, unmarshalledDataTyped, ValueNotSetError) + return nil, ValueNotSetError.Wrapf("invalid parser input format, blockContainer does not have the field searched inside."+ + "params=%v method=%v blockContainer=%v key=%s unmarshaledDataTyped=%v", rpcInput.GetParams(), rpcInput.GetMethod(), blockContainer, key, unmarshalledDataTyped) } } retArr := make([]interface{}, 0) From 444c69e5186323117312eb5cedc2b7e6cf448730 Mon Sep 17 00:00:00 2001 From: Elad Gildnur Date: Tue, 5 Nov 2024 13:54:39 +0200 Subject: [PATCH 4/5] CR Fix --- protocol/parser/parser.go | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/protocol/parser/parser.go b/protocol/parser/parser.go index 341699187f..3fac343c0e 100644 --- a/protocol/parser/parser.go +++ b/protocol/parser/parser.go @@ -156,24 +156,10 @@ func ParseRawBlock(rpcInput RPCInput, parsedInput *ParsedInput, defaultValue str parsedInput.SetBlock(parsedBlock) } -func parseInputWithLegacyBlockParser(rpcInput RPCInput, blockParser spectypes.BlockParser, source int, disableWarningLevelLog bool) (string, error) { - parseErrorLogLevel := utils.LAVA_LOG_WARN - if disableWarningLevelLog { - // found a hash, no need to log warning later - parseErrorLogLevel = utils.LAVA_LOG_DEBUG - } - +func parseInputWithLegacyBlockParser(rpcInput RPCInput, blockParser spectypes.BlockParser, source int) (string, error) { result, err := legacyParse(rpcInput, blockParser, source) if err != nil || result == nil { - return "", utils.LavaFormatLog("blockParsing - parse failed", nil, - lavaslices.Slice( - utils.LogAttr("error", err), - utils.LogAttr("result", result), - utils.LogAttr("blockParser", blockParser), - utils.LogAttr("rpcInput", rpcInput), - ), - uint(parseErrorLogLevel), - ) + return "", fmt.Errorf("blockParsing - parse failed. result=%v error=%w", result, err) } resString, ok := result[spectypes.DEFAULT_PARSED_RESULT_INDEX].(string) @@ -198,7 +184,7 @@ func parseInputWithLegacyBlockParser(rpcInput RPCInput, blockParser spectypes.Bl // Returns: // - A pointer to a ParsedInput struct containing the parsed data. func parseBlock(rpcInput RPCInput, blockParser spectypes.BlockParser, genericParsers []spectypes.GenericParser, source int) *ParsedInput { - parsedBlockInfo, parsedSuccessfully := parseInputWithGenericParsers(rpcInput, genericParsers) + parsedBlockInfo, _ := parseInputWithGenericParsers(rpcInput, genericParsers) if parsedBlockInfo == nil { parsedBlockInfo = NewParsedInput() } else { @@ -209,7 +195,7 @@ func parseBlock(rpcInput RPCInput, blockParser spectypes.BlockParser, genericPar } } - parsedRawBlock, _ := parseInputWithLegacyBlockParser(rpcInput, blockParser, source, parsedSuccessfully) + parsedRawBlock, _ := parseInputWithLegacyBlockParser(rpcInput, blockParser, source) parsedBlockInfo.parsedDataRaw = unquoteString(parsedRawBlock) return parsedBlockInfo } @@ -242,9 +228,21 @@ func unquoteString(str string) string { func ParseBlockHashFromReplyAndDecode(rpcInput RPCInput, resultParser spectypes.BlockParser, genericParsers []spectypes.GenericParser) (string, error) { parsedInput, parsedSuccessfully := parseInputWithGenericParsers(rpcInput, genericParsers) if parsedInput == nil { - parsedBlockHashFromBlockParser, err := parseInputWithLegacyBlockParser(rpcInput, resultParser, PARSE_RESULT, parsedSuccessfully) + parsedBlockHashFromBlockParser, err := parseInputWithLegacyBlockParser(rpcInput, resultParser, PARSE_RESULT) if err != nil { - return "", err + parseErrorLogLevel := utils.LAVA_LOG_WARN + if parsedSuccessfully { + // found a hash, no need to log warning later + parseErrorLogLevel = utils.LAVA_LOG_DEBUG + } + + return "", utils.LavaFormatLog("failed to parse with legacy block parser", err, + lavaslices.Slice( + utils.LogAttr("rpcInput", rpcInput), + utils.LogAttr("resultParser", resultParser), + ), + uint(parseErrorLogLevel), + ) } return parseResponseByEncoding([]byte(parsedBlockHashFromBlockParser), resultParser.Encoding) } From 08d5cdffc003cdf3c979fd203dcd59e976057248 Mon Sep 17 00:00:00 2001 From: Elad Gildnur Date: Tue, 5 Nov 2024 14:05:32 +0200 Subject: [PATCH 5/5] Fix test --- protocol/chainlib/jsonRPC_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/protocol/chainlib/jsonRPC_test.go b/protocol/chainlib/jsonRPC_test.go index 885bf953d2..c3faa98a99 100644 --- a/protocol/chainlib/jsonRPC_test.go +++ b/protocol/chainlib/jsonRPC_test.go @@ -187,9 +187,9 @@ func TestJsonRpcChainProxy(t *testing.T) { require.NoError(t, err) _, err = chainFetcher.FetchBlockHashByNum(ctx, block) - actualErrMsg := "GET_BLOCK_BY_NUM Failed ParseMessageResponse {error:blockParsing - parse failed {error:invalid parser input format," - expectedErrMsg := err.Error()[:len(actualErrMsg)] - require.Equal(t, actualErrMsg, expectedErrMsg, err.Error()) + expectedErrMsg := "GET_BLOCK_BY_NUM Failed ParseMessageResponse {error:failed to parse with legacy block parser ErrMsg: blockParsing -" + actualErrMsg := err.Error()[:len(expectedErrMsg)] + require.Equal(t, expectedErrMsg, actualErrMsg, err.Error()) } func TestAddonAndVerifications(t *testing.T) {