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

Add event attribute decoder configuration #851

Merged
merged 6 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions appinterface/tendermint/blockresulteventattrivutedecoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package tendermint

type BlockResultEventAttributeDecoder interface {
DecodeKey(string) (string, error)
DecodeValue(string) (string, error)
}
2 changes: 1 addition & 1 deletion appinterface/tendermint/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ type Client interface {
Genesis() (*genesis.Genesis, error)
GenesisChunked() (*genesis.Genesis, error)
Block(height int64) (*usecase_model.Block, *usecase_model.RawBlock, error)
BlockResults(height int64) (*usecase_model.BlockResults, error)
BlockResults(height int64, decoder BlockResultEventAttributeDecoder) (*usecase_model.BlockResults, error)
LatestBlockHeight() (int64, error)
}
17 changes: 9 additions & 8 deletions bootstrap/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ type Config struct {
}

type IndexService struct {
Enable bool `yaml:"enable" toml:"enable" xml:"enable" json:"enable,omitempty"`
StartingBlockHeight int64 `yaml:"starting_block_height" toml:"starting_block_height" xml:"starting_block_height" json:"starting_block_height,omitempty"`
Mode string `yaml:"mode" toml:"mode" xml:"mode" json:"mode,omitempty"`
WindowSize int `yaml:"window_size" toml:"window_size" xml:"window_size" json:"window_size,omitempty"`
Projection Projection `yaml:"projection" toml:"projection" xml:"projection" json:"projection"`
CronJob CronJob `yaml:"cron_job" toml:"cron_job" xml:"cron_job" json:"cron_job"`
CosmosVersionEnabledHeight CosmosVersionEnabledHeight `yaml:"cosmos_version_enabled_height" toml:"cosmos_version_enabled_height" xml:"cosmos_version_enabled_height" json:"cosmos_version_enabled_height"`
GithubAPI GithubAPI `yaml:"github_api" toml:"github_api" xml:"github_api" json:"github_api"`
Enable bool `yaml:"enable" toml:"enable" xml:"enable" json:"enable,omitempty"`
StartingBlockHeight int64 `yaml:"starting_block_height" toml:"starting_block_height" xml:"starting_block_height" json:"starting_block_height,omitempty"`
Mode string `yaml:"mode" toml:"mode" xml:"mode" json:"mode,omitempty"`
WindowSize int `yaml:"window_size" toml:"window_size" xml:"window_size" json:"window_size,omitempty"`
Projection Projection `yaml:"projection" toml:"projection" xml:"projection" json:"projection"`
CronJob CronJob `yaml:"cron_job" toml:"cron_job" xml:"cron_job" json:"cron_job"`
CosmosVersionEnabledHeight CosmosVersionEnabledHeight `yaml:"cosmos_version_enabled_height" toml:"cosmos_version_enabled_height" xml:"cosmos_version_enabled_height" json:"cosmos_version_enabled_height"`
GithubAPI GithubAPI `yaml:"github_api" toml:"github_api" xml:"github_api" json:"github_api"`
BlockResultEventAttributeDecodeMethod string `yaml:"block_result_event_attribute_decode_method" toml:"block_result_event_attribute_decode_method" xml:"block_result_event_attribute_decode_method" json:"block_result_event_attribute_decode_method"`
}

type HTTPService struct {
Expand Down
65 changes: 34 additions & 31 deletions bootstrap/indexservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ type IndexService struct {
projections []projection_entity.Projection
cronJobs []projection_entity.CronJob

mode string
accountAddressPrefix string
consNodeAddressPrefix string
bondingDenom string
windowSize int
tendermintHTTPRPCURL string
cosmosAppHTTPRPCURL string
insecureTendermintClient bool
insecureCosmosAppClient bool
strictGenesisParsing bool
startingBlockHeight int64
mode string
accountAddressPrefix string
consNodeAddressPrefix string
bondingDenom string
windowSize int
tendermintHTTPRPCURL string
cosmosAppHTTPRPCURL string
insecureTendermintClient bool
insecureCosmosAppClient bool
strictGenesisParsing bool
startingBlockHeight int64
BlockResultEventAttributeDecodeMethod string

cosmosVersionBlockHeight utils.CosmosVersionBlockHeight

Expand All @@ -57,17 +58,18 @@ func NewIndexService(
projections: projections,
cronJobs: cronJobs,

mode: config.IndexService.Mode,
consNodeAddressPrefix: config.Blockchain.ConNodeAddressPrefix,
accountAddressPrefix: config.Blockchain.AccountAddressPrefix,
bondingDenom: config.Blockchain.BondingDenom,
windowSize: config.IndexService.WindowSize,
tendermintHTTPRPCURL: config.TendermintApp.HTTPRPCUrl,
cosmosAppHTTPRPCURL: config.CosmosApp.HTTPRPCUrl,
insecureTendermintClient: config.TendermintApp.Insecure,
insecureCosmosAppClient: config.CosmosApp.Insecure,
strictGenesisParsing: config.TendermintApp.StrictGenesisParsing,
startingBlockHeight: config.IndexService.StartingBlockHeight,
mode: config.IndexService.Mode,
consNodeAddressPrefix: config.Blockchain.ConNodeAddressPrefix,
accountAddressPrefix: config.Blockchain.AccountAddressPrefix,
bondingDenom: config.Blockchain.BondingDenom,
windowSize: config.IndexService.WindowSize,
tendermintHTTPRPCURL: config.TendermintApp.HTTPRPCUrl,
cosmosAppHTTPRPCURL: config.CosmosApp.HTTPRPCUrl,
insecureTendermintClient: config.TendermintApp.Insecure,
insecureCosmosAppClient: config.CosmosApp.Insecure,
strictGenesisParsing: config.TendermintApp.StrictGenesisParsing,
startingBlockHeight: config.IndexService.StartingBlockHeight,
BlockResultEventAttributeDecodeMethod: config.IndexService.BlockResultEventAttributeDecodeMethod,
cosmosVersionBlockHeight: utils.CosmosVersionBlockHeight{
V0_42_7: utils.ParserBlockHeight(config.IndexService.CosmosVersionEnabledHeight.V0_42_7),
},
Expand Down Expand Up @@ -144,15 +146,16 @@ func (service *IndexService) RunEventStoreMode() error {
Logger: service.logger,
RDbConn: service.rdbConn,
Config: SyncManagerConfig{
WindowSize: service.windowSize,
TendermintRPCUrl: service.tendermintHTTPRPCURL,
CosmosAppHTTPRPCURL: service.cosmosAppHTTPRPCURL,
InsecureTendermintClient: service.insecureTendermintClient,
InsecureCosmosAppClient: service.insecureCosmosAppClient,
StrictGenesisParsing: service.strictGenesisParsing,
AccountAddressPrefix: service.accountAddressPrefix,
StakingDenom: service.bondingDenom,
StartingBlockHeight: service.startingBlockHeight,
WindowSize: service.windowSize,
TendermintRPCUrl: service.tendermintHTTPRPCURL,
CosmosAppHTTPRPCURL: service.cosmosAppHTTPRPCURL,
InsecureTendermintClient: service.insecureTendermintClient,
InsecureCosmosAppClient: service.insecureCosmosAppClient,
StrictGenesisParsing: service.strictGenesisParsing,
AccountAddressPrefix: service.accountAddressPrefix,
StakingDenom: service.bondingDenom,
StartingBlockHeight: service.startingBlockHeight,
BlockResultEventAttributeDecodeMethod: service.BlockResultEventAttributeDecodeMethod,
},
TxDecoder: service.txDecoder,
},
Expand Down
35 changes: 25 additions & 10 deletions bootstrap/syncmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/cenkalti/backoff/v4"
cosmosapp_interface "github.com/crypto-com/chain-indexing/appinterface/cosmosapp"
eventhandler_interface "github.com/crypto-com/chain-indexing/appinterface/eventhandler"
tendermint_interface "github.com/crypto-com/chain-indexing/appinterface/tendermint"
"github.com/crypto-com/chain-indexing/external/txdecoder"
cosmosapp_infrastructure "github.com/crypto-com/chain-indexing/infrastructure/cosmosapp"
"github.com/crypto-com/chain-indexing/usecase/model"
Expand Down Expand Up @@ -54,6 +55,8 @@ type SyncManager struct {
startingBlockHeight int64

txDecoder txdecoder.TxDecoder

eventAttributeDecoder tendermint_interface.BlockResultEventAttributeDecoder
}

type SyncManagerParams struct {
Expand All @@ -65,15 +68,16 @@ type SyncManagerParams struct {
}

type SyncManagerConfig struct {
WindowSize int
TendermintRPCUrl string
CosmosAppHTTPRPCURL string
InsecureTendermintClient bool
InsecureCosmosAppClient bool
StrictGenesisParsing bool
AccountAddressPrefix string
StakingDenom string
StartingBlockHeight int64
WindowSize int
TendermintRPCUrl string
CosmosAppHTTPRPCURL string
InsecureTendermintClient bool
InsecureCosmosAppClient bool
StrictGenesisParsing bool
AccountAddressPrefix string
StakingDenom string
StartingBlockHeight int64
BlockResultEventAttributeDecodeMethod string
}

// NewSyncManager creates a new feed with polling for latest block starts at a specific height
Expand Down Expand Up @@ -106,6 +110,15 @@ func NewSyncManager(
)
}

var eventAttributeDecoder tendermint_interface.BlockResultEventAttributeDecoder

switch params.Config.BlockResultEventAttributeDecodeMethod {
case "base64":
eventAttributeDecoder = &tendermint.Base64BlockResultEventAttributeDecoder{}
default:
eventAttributeDecoder = &tendermint.RawBlockResultEventAttributeDecoder{}
}

return &SyncManager{
rdbConn: params.RDbConn,
tendermintClient: tendermintClient,
Expand All @@ -132,6 +145,8 @@ func NewSyncManager(
startingBlockHeight: params.Config.StartingBlockHeight,

txDecoder: params.TxDecoder,

eventAttributeDecoder: eventAttributeDecoder,
}
}

Expand Down Expand Up @@ -241,7 +256,7 @@ func (manager *SyncManager) syncBlockWorker(blockHeight int64) ([]command_entity
return nil, fmt.Errorf("error requesting chain block at height %d: %v", blockHeight, err)
}

blockResults, err := manager.tendermintClient.BlockResults(blockHeight)
blockResults, err := manager.tendermintClient.BlockResults(blockHeight, manager.eventAttributeDecoder)
if err != nil {
return nil, fmt.Errorf("error requesting chain block_results at height %d: %v", blockHeight, err)
}
Expand Down
1 change: 1 addition & 0 deletions example/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/crypto-com/chain-indexing v1.2.1-0.20220111132225-b2712ba4fe23 h1:Pit1X4i8SXU/pUg60mo3tj/fKM+xYJ5j3TGtBK41D3k=
github.com/crypto-com/chain-indexing v1.2.1-0.20220111132225-b2712ba4fe23/go.mod h1:0KBBR7C2RZDavMpCBKlNtHkMZZ7tyndioZQRWUTD4YI=
github.com/crypto-com/chain-indexing v1.3.0/go.mod h1:0KBBR7C2RZDavMpCBKlNtHkMZZ7tyndioZQRWUTD4YI=
github.com/crypto-org-chain/chain-main/v3 v3.0.0-croeseid h1:YAffq+tYiSqYXgIb11Vc6dvBtgSvRsf2g1wPaMFkQUA=
github.com/crypto-org-chain/chain-main/v3 v3.0.0-croeseid/go.mod h1:92Z70bDbsScrWzIHB496p7nXKydtq5am9rqa0fCpbM8=
github.com/crypto-org-chain/cronos v0.6.0-testnet h1:iFLwra4QMZ6HgIEB1P0vMCa9pnQTeXjwSuYbH4glxqA=
Expand Down
9 changes: 2 additions & 7 deletions infrastructure/httpapi/handlers/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,8 @@ func (handler *Blocks) ListAccountByHeight(ctx *fasthttp.RequestCtx) {
return
}

for i := range rawEventAccounts {
accountStrSlice = append(accountStrSlice, rawEventAccounts[i])
}

for i := range rawTransactionAccounts {
accountStrSlice = append(accountStrSlice, rawTransactionAccounts[i])
}
accountStrSlice = append(accountStrSlice, rawEventAccounts...)
accountStrSlice = append(accountStrSlice, rawTransactionAccounts...)

accountStrSlice = unique(accountStrSlice)
accounts := make([]account, 0)
Expand Down
35 changes: 35 additions & 0 deletions infrastructure/tendermint/blockresulteventattrivutedecoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tendermint

import "encoding/base64"

type RawBlockResultEventAttributeDecoder struct {
}

func (decoder RawBlockResultEventAttributeDecoder) DecodeKey(s string) (string, error) {
return s, nil
}

func (decoder RawBlockResultEventAttributeDecoder) DecodeValue(s string) (string, error) {
return s, nil
}

type Base64BlockResultEventAttributeDecoder struct {
}

func (decoder Base64BlockResultEventAttributeDecoder) DecodeKey(s string) (string, error) {
decoded, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return "", err
}

return string(decoded), nil
}

func (decoder Base64BlockResultEventAttributeDecoder) DecodeValue(s string) (string, error) {
decoded, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return "", err
}

return string(decoded), nil
}
4 changes: 2 additions & 2 deletions infrastructure/tendermint/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (client *HTTPClient) Block(height int64) (*usecase_model.Block, *usecase_mo
return block, rawBlock, nil
}

func (client *HTTPClient) BlockResults(height int64) (*usecase_model.BlockResults, error) {
func (client *HTTPClient) BlockResults(height int64, eventAttributeDecoder tendermint.BlockResultEventAttributeDecoder) (*usecase_model.BlockResults, error) {
var err error

rawRespBody, err := client.request("block_results", queryKV{
Expand All @@ -176,7 +176,7 @@ func (client *HTTPClient) BlockResults(height int64) (*usecase_model.BlockResult
}
defer rawRespBody.Close()

blockResults, err := ParseBlockResultsResp(rawRespBody)
blockResults, err := ParseBlockResultsResp(rawRespBody, eventAttributeDecoder)
if err != nil {
return nil, err
}
Expand Down
Loading