Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #222 from irisnet/develop
Browse files Browse the repository at this point in the history
R4R:merge master from develop
  • Loading branch information
zhangjinbiao746 authored Jul 15, 2020
2 parents 5b0d719 + 2a5d16e commit e300fd5
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 295 deletions.
2 changes: 1 addition & 1 deletion conf/server/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

var (
BlockChainMonitorUrl = []string{"tcp://35.201.147.145:30657"}
BlockChainMonitorUrl = []string{"http://192.168.150.32:26657"}

WorkerNumCreateTask = 1
WorkerNumExecuteTask = 60
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ module github.com/irisnet/irishub-sync

require (
github.com/go-kit/kit v0.9.0
github.com/irisnet/irishub v0.16.0-rc0
github.com/irisnet/irishub v0.16.0
github.com/jolestar/go-commons-pool v2.0.0+incompatible
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v1.2.1
github.com/robfig/cron v1.2.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ github.com/irisnet/iavl v0.12.3 h1:AYqI1q/EVAOCWznYx4FtEZIq8x9yPBs9ZA/Rk3zsTDo=
github.com/irisnet/iavl v0.12.3/go.mod h1:qofGh9236iFgVez+gPk7bC+ef5wCw7aIMvAVBK3lT84=
github.com/irisnet/irishub v0.16.0-rc0 h1:A+8FOKOZDVeCzUVaiP0HFMiHs0uYpkW3nlvmEPZxGdk=
github.com/irisnet/irishub v0.16.0-rc0/go.mod h1:qE/9fLjRS6yUquLtfWmj2P66yPvTI6d8qCaF4BF7TNc=
github.com/irisnet/irishub v0.16.0 h1:Xuvynxz2TsrTsBym3HAcbleogqR4GXP+NUxW25JJcmk=
github.com/irisnet/irishub v0.16.0/go.mod h1:S6Y1vyjutENBFt7mS8UTh6L+91TpenYtmfu0KtND9I8=
github.com/irisnet/tendermint v0.32.0 h1:+NFxoUzVq67LLnAtM8hIkeqTRji3hJOUip+T3/ZHU6A=
github.com/irisnet/tendermint v0.32.0/go.mod h1:36RAXv6V/Ar3H6ofEtmtxU2u80tYRdQh1z51gxoG9Nk=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
Expand Down
99 changes: 26 additions & 73 deletions service/handler/account.go
Original file line number Diff line number Diff line change
@@ -1,90 +1,43 @@
package handler

import (
"github.com/irisnet/irishub-sync/logger"
"github.com/irisnet/irishub-sync/store"
"github.com/irisnet/irishub-sync/store/document"
"github.com/irisnet/irishub-sync/util/constant"
"github.com/irisnet/irishub-sync/logger"
"github.com/irisnet/irishub-sync/types/msg"
"github.com/irisnet/irishub-sync/util/helper"
"encoding/json"
)

// TODO: sync only save account address, let app to update balance, delegation, unbondingDelegation info
func SaveOrUpdateAccountBalanceInfo(accounts []string, height, timestamp int64) {
var (
accountModel document.Account
)
if len(accounts) == 0 {
//when new address not found or the tx is not success,this address will not be collected
func saveNewAccount(tx *document.CommonTx) {
var accountModel document.Account
if document.TxStatusSuccess != tx.Status {
return
}

for _, v := range accounts {
coins, accountNumber := helper.QueryAccountInfo(v)
coinIris := getCoinIrisFromCoins(coins)

if err := accountModel.UpsertBalanceInfo(v, coinIris, accountNumber, height, timestamp); err != nil {
logger.Error("update account balance info fail", logger.Int64("height", height),
logger.String("address", v), logger.String("err", err.Error()))
switch tx.Type {
case constant.TxTypeTransfer:
accountModel.Address = tx.To
case constant.TxTypeAddTrustee:
if len(tx.Msgs) > 0 {
msgData := msg.DocTxMsgAddTrustee{}
if err := json.Unmarshal([]byte(helper.ToJson(tx.Msgs[0].Msg)), &msgData); err == nil {
accountModel.Address = msgData.Address
}
}
}
}

func SaveOrUpdateAccountDelegationInfo(docTx document.CommonTx) {
var (
delegator string
accountModel document.Account
)
switch docTx.Type {
case constant.TxTypeStakeDelegate, constant.TxTypeStakeBeginUnbonding, constant.TxTypeBeginRedelegate,
constant.TxTypeStakeCreateValidator:
delegator = docTx.From
}
if delegator == "" {
return
}
delegations := helper.GetDelegations(delegator)
delegation := store.Coin{
Denom: constant.IrisAttoUnit,
Amount: helper.CalculateDelegatorDelegationTokens(delegations),
}

if err := accountModel.UpsertDelegationInfo(delegator, delegation, docTx.Height, docTx.Time.Unix()); err != nil {
logger.Error("update account delegation info fail", logger.Int64("height", docTx.Height),
logger.String("address", delegator), logger.String("err", err.Error()))
case constant.TxTypeSetWithdrawAddress:
if len(tx.Msgs) > 0 {
msgData := msg.DocTxMsgSetWithdrawAddress{}
if err := json.Unmarshal([]byte(helper.ToJson(tx.Msgs[0].Msg)), &msgData); err == nil {
accountModel.Address = msgData.WithdrawAddr
}
}
}
}

func SaveOrUpdateAccountUnbondingDelegationInfo(accounts []string, height, timestamp int64) {
var (
accountModel document.Account
)

if len(accounts) == 0 {
if accountModel.Address == "" {
return
}
for _, v := range accounts {
unbondingDelegations := helper.GetUnbondingDelegations(v)
unbondingDelegation := store.Coin{
Denom: constant.IrisAttoUnit,
Amount: helper.CalculateDelegatorUnbondingDelegationTokens(unbondingDelegations),
}

if err := accountModel.UpsertUnbondingDelegationInfo(v, unbondingDelegation, height, timestamp); err != nil {
logger.Error("update account unbonding delegation info fail", logger.Int64("height", height),
logger.String("address", v), logger.String("err", err.Error()))
}
}
}

func getCoinIrisFromCoins(coins store.Coins) store.Coin {
if len(coins) > 0 {
for _, v := range coins {
if v.Denom == constant.IrisAttoUnit {
return store.Coin{
Denom: v.Denom,
Amount: v.Amount,
}
}
}
if err := accountModel.SaveAddress(accountModel.Address); err != nil {
logger.Warn("Save new account address failed", logger.String("err", err.Error()))
}
return store.Coin{}
}
12 changes: 1 addition & 11 deletions service/handler/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ const (
unDelegationSubject = "Undelegation"
)

func ParseBlock(meta *types.BlockMeta, block *types.Block, validators []*types.Validator,
accsBalanceNeedUpdatedByParseTxs []string) document.Block {
func ParseBlock(meta *types.BlockMeta, block *types.Block, validators []*types.Validator) document.Block {
cdc := types.GetCodec()

hexFunc := func(bytes []byte) string {
Expand Down Expand Up @@ -139,15 +138,6 @@ func ParseBlock(meta *types.BlockMeta, block *types.Block, validators []*types.V
}
}

// save or update account balance info and unbonding delegation info by parse block coin flow
accsBalanceNeedUpdated, accsUnbondingDelegationNeedUpdated := getAccountsFromCoinFlow(
docBlock.Result.EndBlock.Tags, docBlock.Height)

accsBalanceNeedUpdated = helper.DistinctStringSlice(append(accsBalanceNeedUpdated, accsBalanceNeedUpdatedByParseTxs...))
SaveOrUpdateAccountBalanceInfo(accsBalanceNeedUpdated, docBlock.Height, docBlock.Time.Unix())

SaveOrUpdateAccountUnbondingDelegationInfo(accsUnbondingDelegationNeedUpdated, docBlock.Height, docBlock.Time.Unix())

return docBlock
}

Expand Down
2 changes: 1 addition & 1 deletion service/handler/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestParseBlock(t *testing.T) {
} else {
validators = valRes.Validators
}
blockDoc := ParseBlock(res.BlockMeta, res.Block, validators, nil)
blockDoc := ParseBlock(res.BlockMeta, res.Block, validators)

resBytes, _ := json.MarshalIndent(blockDoc, "", "\t")
t.Log(string(resBytes))
Expand Down
27 changes: 5 additions & 22 deletions service/handler/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,15 @@ import (
"github.com/irisnet/irishub-sync/store"
"github.com/irisnet/irishub-sync/store/document"
"github.com/irisnet/irishub-sync/types"
"github.com/irisnet/irishub-sync/util/constant"
"github.com/irisnet/irishub-sync/util/helper"
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2/txn"
"strings"
)

func HandleTx(block *types.Block) ([]string, error) {
func HandleTx(block *types.Block) (error) {
var (
batch []txn.Op
accsBalanceNeedUpdated []string
)
getAccsBalanceNeedUpdated := func(addr string) {
if strings.HasPrefix(addr, types.Bech32AccountAddrPrefix) {
accsBalanceNeedUpdated = append(accsBalanceNeedUpdated, addr)
}
}

for _, txByte := range block.Txs {
tx := helper.ParseTx(txByte, block)
Expand All @@ -37,25 +29,16 @@ func HandleTx(block *types.Block) ([]string, error) {
handleProposal(tx)
//handleTokenFlow(blockWithTags, tx, &batch)

// save or update account delegations info and unbonding delegation info
SaveOrUpdateAccountDelegationInfo(tx)
switch tx.Type {
case constant.TxTypeStakeBeginUnbonding:
accounts := []string{tx.From}
SaveOrUpdateAccountUnbondingDelegationInfo(accounts, tx.Height, tx.Time.Unix())
}

// get accounts which balance need updated by parse tx
getAccsBalanceNeedUpdated(tx.From)
getAccsBalanceNeedUpdated(tx.To)
// save new account address
saveNewAccount(&tx)
}

if len(batch) > 0 {
err := store.Txn(batch)
if err != nil {
return accsBalanceNeedUpdated, err
return err
}
}

return accsBalanceNeedUpdated, nil
return nil
}
5 changes: 2 additions & 3 deletions service/task/task_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ func parseBlock(b int64, client *helper.Client) (document.Block, error) {
}
}

accsBalanceNeedUpdatedByParseTxs, err := handler.HandleTx(block.Block)
if err != nil {
if err := handler.HandleTx(block.Block); err != nil {
return blockDoc, err
}

Expand All @@ -313,7 +312,7 @@ func parseBlock(b int64, client *helper.Client) (document.Block, error) {
validators = res.Validators
}

return handler.ParseBlock(block.BlockMeta, block.Block, validators, accsBalanceNeedUpdatedByParseTxs), nil
return handler.ParseBlock(block.BlockMeta, block.Block, validators), nil
}

// assert task worker unchanged
Expand Down
2 changes: 1 addition & 1 deletion service/task/task_execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func Test_parseBlock(t *testing.T) {
name: "test parse block",
args: args{
client: client,
b: 1588,
b: 4379192,
},
},
}
Expand Down
Loading

0 comments on commit e300fd5

Please sign in to comment.