Skip to content

Commit

Permalink
Merge pull request #80 from cosmos-gaminghub/develop
Browse files Browse the repository at this point in the history
Develop > Main
  • Loading branch information
EG-easy authored Mar 1, 2022
2 parents e028982 + 81124c9 commit f24cd58
Show file tree
Hide file tree
Showing 15 changed files with 185 additions and 123 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ DB_POOL_LIMIT=4096
DB_ADDR=127.0.0.1

ADDR_NODE_SERVER="http://198.13.33.206:1317"
RPC_ENPOINT="tcp://45.32.39.253:26657"

COINGECKO_API_ENDPOINT="https://api.coingecko.com/api"
COINGECKO_API_VERSION="v3"
Expand Down
11 changes: 0 additions & 11 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,9 @@ func GetTxs(height int64) (txs types.TxResult, err error) {
return txs, err
}

result := make(map[string][]map[string]map[string]interface{})
json.Unmarshal(resBytes, &result)

if err := json.Unmarshal(resBytes, &txs); err != nil {
logger.Error(fmt.Sprintf("Unmarshal Tx error for height %d", height), logger.String("err", err.Error()))
}

if len(txs.TxResponse) > 0 {
for index := range txs.TxResponse {
mJson, _ := json.Marshal(result["txs"][index]["body"]["messages"])
txs.Txs[index].Body.BodyMessage = string(mJson)
}
}

return txs, nil
}

Expand Down
3 changes: 3 additions & 0 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
KeyAddressPrefix = "ADDRESS_PREFIX"

KeyAddrHubLcd = "ADDR_NODE_SERVER"
KeyHubRPC = "RPC_ENPOINT"
KeyCoin = "DEFAULT_COIN"

KeyCoingeckoEndPoint = "COINGECKO_API_ENDPOINT"
Expand Down Expand Up @@ -61,6 +62,7 @@ func init() {

hubcf := hubConf{
LcdUrl: getEnv(KeyAddrHubLcd, DefaultEnvironment),
RpcUrl: getEnv(KeyHubRPC, DefaultEnvironment),
Coin: getEnv(KeyCoin, DefaultEnvironment),
}
config.Hub = hubcf
Expand All @@ -79,6 +81,7 @@ type Config struct {

type hubConf struct {
LcdUrl string
RpcUrl string
Coin string
}

Expand Down
21 changes: 16 additions & 5 deletions exporter/account_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func SaveAccountTransaction(validators []*schema.Validator, transactions []*schema.Transaction) {

for _, tx := range transactions {
var listAccountAddress = getListAccountAddres(tx.Messages)
var listAccountAddress = getListAccountAddress(tx.Messages)
for _, address := range listAccountAddress {
orm.Save(document.CollectionAccountTransaction, &document.AccountTransaction{
Height: tx.Height,
Expand All @@ -25,17 +25,28 @@ func SaveAccountTransaction(validators []*schema.Validator, transactions []*sche
}
}

func getListAccountAddres(messages string) []string {
func getListAccountAddress(messages string) []string {
var list []string
var addressPrefix = conf.Get().Db.AddresPrefix
var length = len(addressPrefix) + 39 // length off account address
for {
if strings.Contains(messages, addressPrefix) {
index := strings.Index(messages, addressPrefix)
var nextIndex = index + length
if nextIndex > len(messages)-1 {
break
}

address := utils.Convert(addressPrefix, messages[index:index+length])
if address != "" {
list = append(list, address)
if string(messages[index+length]) != "\"" { //skip string if got string like ugame,...
messages = messages[index+1 : len(messages)-1]
continue
}
var messageAddress = messages[index : index+length]
if !(strings.Contains(messageAddress, addressPrefix+"valoper")) { // check if string is gamevaloper
address := utils.Convert(addressPrefix, messageAddress)
if address != "" {
list = append(list, address)
}
}
messages = messages[index+length : len(messages)-1]
} else {
Expand Down
40 changes: 17 additions & 23 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/cosmos-gaminghub/explorer-backend/orm/document"
"github.com/cosmos-gaminghub/explorer-backend/schema"
"github.com/pkg/errors"

rpchttp "github.com/tendermint/tendermint/rpc/client/http"
)

var (
Expand All @@ -22,13 +24,26 @@ var (
Commit = ""
)

var clientHTTP *rpchttp.HTTP

// Exporter wraps the required params to export blockchain
type Exporter struct {
clientHTTP *rpchttp.HTTP
}

// Start starts to synchronize Chain data.
func Start() error {
fmt.Println("Starting Chain Exporter...")

var (
err error
)

clientHTTP, err = rpchttp.New(conf.Get().Hub.RpcUrl, "/websocket")
if err != nil {
fmt.Println(err.Error())
}

go func() {
for {
fmt.Println("start - sync blockchain")
Expand All @@ -53,18 +68,6 @@ func Start() error {
}
}()

go func() {
for {
fmt.Println("start - sync total missed block for validator")
err := syncTotalMissedBlock()
if err != nil {
fmt.Printf("error - sync total missed block for validator: %v\n", err)
}
fmt.Println("finish - sync total missed block for validator")
time.Sleep(3600 * time.Second)
}
}()

for {
select {}
}
Expand Down Expand Up @@ -138,6 +141,8 @@ func process(height int64) error {
}
orm.Save(document.CollectionNmBlock, resultBlock)

SaveMissedBlock(clientHTTP, height, resultBlock)

resultTxs, err := GetTxs(txs, *resultBlock)
if err != nil {
logger.Error("failed to get txs:", logger.String("err", err.Error()))
Expand All @@ -151,8 +156,6 @@ func process(height int64) error {
logger.Error("failed to get validator set:", logger.String("err", err.Error()))
}

SaveMissedBlock(vals, validatorSets, block)

resultValidators, err := GetValidators(vals, validatorSets)
if err != nil {
logger.Error("failed to get validators:", logger.String("err", err.Error()))
Expand Down Expand Up @@ -197,15 +200,6 @@ func syncProposal() error {
return nil
}

func syncTotalMissedBlock() error {
validators, err := document.Validator{}.GetValidatorList()
if err != nil {
return nil
}
saveTotalMissedBlock(validators)
return nil
}

func processProposal(proposal schema.Proposal) error {
depositResult, _ := client.GetProposalDeposits(proposal.ProposalId)
if len(depositResult.Deposits) > 0 {
Expand Down
54 changes: 54 additions & 0 deletions exporter/missed_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package exporter

import (
"bytes"
"context"
"fmt"
"strconv"

"github.com/cosmos-gaminghub/explorer-backend/logger"
"github.com/cosmos-gaminghub/explorer-backend/orm"
"github.com/cosmos-gaminghub/explorer-backend/schema"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
)

// SaveMissedBlock by height
func SaveMissedBlock(clientHTTP *rpchttp.HTTP, height int64, block *schema.Block) {
result, err := clientHTTP.BlockResults(context.Background(), &height)
if err != nil {
fmt.Println(err.Error())
}

if result != nil {
for _, event := range result.BeginBlockEvents {
if event.Type == "liveness" {
var consensusAddress, eventHeight string
var insertHeight int64
for _, v := range event.GetAttributes() {

var ak = bytes.NewBuffer(v.GetKey()).String()
if ak == "address" {
consensusAddress = bytes.NewBuffer(v.GetValue()).String()
}

if ak == "height" {
eventHeight = bytes.NewBuffer(v.GetValue()).String()
insertHeight, _ = strconv.ParseInt(eventHeight, 10, 64)
if err != nil {
logger.Error(fmt.Sprintf("[Missed block] failed to parse string %s to int64", eventHeight))
}
}
if insertHeight > 0 {
b := schema.NewMissedBlock(schema.MissedBlock{
Height: insertHeight,
ConsensusAddress: consensusAddress,
Timestamp: block.Timestamp,
})
orm.Save("missed_block", b)
}
}
}
}
}

}
4 changes: 3 additions & 1 deletion exporter/transaction.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package exporter

import (
"encoding/json"
"strconv"

types "github.com/cosmos-gaminghub/explorer-backend/lcd"
Expand All @@ -18,6 +19,7 @@ func GetTxs(txs types.TxResult, block schema.Block) (transactions []*schema.Tran
height, _ := strconv.ParseInt(tx.Height, 10, 64)
gasWanted, _ := strconv.ParseInt(tx.GasWanted, 10, 64)
gasUsed, _ := strconv.ParseInt(tx.GasUsed, 10, 64)
messages, _ := json.Marshal(txs.Txs[index].Body.BodyMessage)

t := schema.NewTransaction(schema.Transaction{
Height: height,
Expand All @@ -30,7 +32,7 @@ func GetTxs(txs types.TxResult, block schema.Block) (transactions []*schema.Tran
Logs: tx.Logs,
Fee: txs.Txs[index].AuthInfo.FeeInfo,
Signatures: txs.Txs[index].Signatures,
Messages: txs.Txs[index].Body.BodyMessage,
Messages: string(messages),
RawLog: tx.RawLog,
})
transactions = append(transactions, t)
Expand Down
14 changes: 0 additions & 14 deletions exporter/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package exporter

import (
"encoding/base64"
"strconv"

"github.com/cosmos-gaminghub/explorer-backend/client"
"github.com/cosmos-gaminghub/explorer-backend/conf"
Expand Down Expand Up @@ -57,16 +56,3 @@ func SaveValidator(validator schema.Validator) (interface{}, error) {

return orm.Upsert(document.CollectionNmValidator, selector, validator)
}

func saveTotalMissedBlock(validators []document.Validator) {
for _, validator := range validators {
var missedBlockCount int64
if validator.ConsensusAddress != "" {
valSigningInfo, err := client.GetValSigningInfo(validator.ConsensusAddress)
if err == nil {
missedBlockCount, _ = strconv.ParseInt(valSigningInfo.Info.MissedBlocksCount, 10, 64)
}
}
orm.Update(document.CollectionNmValidator, bson.M{"consensus_address": validator.ConsensusAddress}, bson.M{"$set": bson.M{"total_missed_block": missedBlockCount}})
}
}
30 changes: 0 additions & 30 deletions exporter/validator_set.go

This file was deleted.

25 changes: 25 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/robfig/cron v1.2.0
github.com/stretchr/testify v1.7.0
github.com/tendermint/tendermint v0.34.14
go.uber.org/zap v1.17.0
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a
golang.org/x/net v0.0.0-20210903162142-ad29c8ab022f
Expand All @@ -16,11 +17,35 @@ require (
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd v0.22.0-beta // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/enigmampc/btcutil v1.0.3-0.20200723161021-e2fb6adb2a25 // indirect
github.com/go-kit/kit v0.10.0 // indirect
github.com/go-logfmt/logfmt v0.5.0 // indirect
github.com/gogo/protobuf v1.3.3 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/gtank/merlin v0.1.1 // indirect
github.com/libp2p/go-buffer-pool v0.0.2 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643 // indirect
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.11.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.29.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 // indirect
github.com/sasha-s/go-deadlock v0.2.1-0.20190427202633-1595213edefa // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/sys v0.0.0-20210903071746-97244b99971b // indirect
golang.org/x/text v0.3.6 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/grpc v1.40.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Expand Down
Loading

0 comments on commit f24cd58

Please sign in to comment.