/
processor.go
63 lines (52 loc) · 1.86 KB
/
processor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package core
import (
"fmt"
"github.com/DefiantLabs/cosmos-indexer/config"
"github.com/DefiantLabs/cosmos-indexer/db/models"
ctypes "github.com/cometbft/cometbft/rpc/core/types"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
)
type BlockProcessingFailure int
const (
NodeMissingBlockTxs BlockProcessingFailure = iota
BlockQueryError
UnprocessableTxError
OsmosisNodeRewardLookupError
OsmosisNodeRewardIndexError
NodeMissingHistoryForBlock
FailedBlockEventHandling
)
type FailedBlockHandler func(height int64, code BlockProcessingFailure, err error)
// Process RPC Block data into the model object used by the application.
func ProcessBlock(blockData *ctypes.ResultBlock, blockResultsData *ctypes.ResultBlockResults, chainID uint) (models.Block, error) {
block := models.Block{
Height: blockData.Block.Height,
ChainID: chainID,
}
propAddressFromHex, err := sdkTypes.ConsAddressFromHex(blockData.Block.ProposerAddress.String())
if err != nil {
return block, err
}
block.ProposerConsAddress = models.Address{Address: propAddressFromHex.String()}
block.TimeStamp = blockData.Block.Time
return block, nil
}
// Log error to stdout. Not much else we can do to handle right now.
func HandleFailedBlock(height int64, code BlockProcessingFailure, err error) {
reason := "{unknown error}"
switch code {
case NodeMissingBlockTxs:
reason = "node has no TX history for block"
case BlockQueryError:
reason = "failed to query block result for block"
case OsmosisNodeRewardLookupError:
reason = "Failed Osmosis rewards lookup for block"
case OsmosisNodeRewardIndexError:
reason = "Failed Osmosis rewards indexing for block"
case NodeMissingHistoryForBlock:
reason = "Node has no TX history for block"
case FailedBlockEventHandling:
reason = "Failed to process block event"
}
config.Log.Error(fmt.Sprintf("Block %v failed. Reason: %v", height, reason), err)
}