Skip to content

Commit

Permalink
made hex unmarshalling more flexible (allow array encoded hashes as w…
Browse files Browse the repository at this point in the history
…ell - nimbus does that...)
  • Loading branch information
pk910 committed Aug 23, 2023
1 parent 1a920d1 commit 7766ee2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
3 changes: 2 additions & 1 deletion indexer/cacheLogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,9 @@ func (cache *indexerCache) processOrphanedBlocks(processedEpoch int64) error {
}
cache.cacheMutex.RUnlock()

logger.Infof("processing %v non-canonical blocks (epoch <= %v)", len(cachedBlocks), processedEpoch)
logger.Infof("processing %v non-canonical blocks (epoch <= %v, lowest slot: %v)", len(cachedBlocks), processedEpoch, cache.lowestSlot)
if len(cachedBlocks) == 0 {
cache.resetLowestSlot()
return nil
}

Expand Down
39 changes: 39 additions & 0 deletions rpctypes/basetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rpctypes

import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"strconv"
Expand All @@ -22,6 +23,44 @@ func (s *BytesHexStr) UnmarshalText(b []byte) error {
return nil
}

func (s *BytesHexStr) UnmarshalJSON(b []byte) error {
if s == nil {
return fmt.Errorf("cannot unmarshal bytes into nil")
}
var bytes []byte
var tmpStr string
if err := json.Unmarshal(b, &tmpStr); err == nil {
if len(tmpStr) >= 2 && tmpStr[0] == '0' && tmpStr[1] == 'x' {
tmpStr = tmpStr[2:]
}
bytes = make([]byte, len(tmpStr)/2)
hex.Decode(bytes, []byte(tmpStr))
} else {
err := json.Unmarshal(b, &bytes)
if err != nil {
var tmpStrArr []string
err = json.Unmarshal(b, &tmpStrArr)
if err == nil {
bytes = make([]byte, len(tmpStrArr))
for idx, str := range tmpStrArr {
n, e := strconv.ParseUint(str, 0, 64)
if e != nil {
err = e
break
}
bytes[idx] = uint8(n)
}
}
}
if err != nil {
fmt.Printf("err: %v\n", err)
return err
}
}
*s = bytes
return nil
}

func (s *BytesHexStr) MarshalJSON() ([]byte, error) {
if s == nil {
return nil, nil
Expand Down

0 comments on commit 7766ee2

Please sign in to comment.