Skip to content

Commit

Permalink
Merge pull request #2565 from Sifchain/develop
Browse files Browse the repository at this point in the history
Bump version v0.12.1
  • Loading branch information
intl-man authored Mar 28, 2022
2 parents dfdb014 + 304d928 commit 269cfad
Show file tree
Hide file tree
Showing 62 changed files with 3,309 additions and 918 deletions.
2 changes: 1 addition & 1 deletion app/ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestAdjustGasPriceDecorator_AnteHandle(t *testing.T) {
}
ctx = ctx.WithMinGasPrices(sdk.NewDecCoins(highGasPrice))
dispensationCreateMsg := dispensationtypes.NewMsgCreateDistribution(addrs[0], dispensationtypes.DistributionType_DISTRIBUTION_TYPE_AIRDROP, []banktypes.Output{}, "")
dispensationRunMsg := dispensationtypes.NewMsgRunDistribution(addrs[0].String(), "airdrop", dispensationtypes.DistributionType_DISTRIBUTION_TYPE_AIRDROP)
dispensationRunMsg := dispensationtypes.NewMsgRunDistribution(addrs[0].String(), "airdrop", dispensationtypes.DistributionType_DISTRIBUTION_TYPE_AIRDROP, 10)
otherMsg := banktypes.NewMsgSend(addrs[0], addrs[1], sdk.NewCoins(sdk.NewCoin("rowan", sdk.NewIntFromUint64(100))))
// next doesn't accept err, it is only called if decorator does not return error, it passes ctx to decorator caller
next := func(ctx sdk.Context, tx sdk.Tx, simulate bool) (newCtx sdk.Context, err error) { return ctx, nil }
Expand Down
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ func NewSifApp(
evidencetypes.ModuleName,
stakingtypes.ModuleName,
ibchost.ModuleName,
dispensation.ModuleName,
)
app.mm.SetOrderEndBlockers(
crisistypes.ModuleName,
Expand Down
26 changes: 1 addition & 25 deletions app/setup_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,14 @@ import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
m "github.com/cosmos/cosmos-sdk/types/module"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
ibcconnectiontypes "github.com/cosmos/ibc-go/v2/modules/core/03-connection/types"
)

const releaseVersion = "0.11.0"
const releaseVersion = "0.12.1"

func SetupHandlers(app *SifchainApp) {
app.UpgradeKeeper.SetUpgradeHandler(releaseVersion, func(ctx sdk.Context, plan types.Plan, vm m.VersionMap) (m.VersionMap, error) {
app.Logger().Info("Running upgrade handler for " + releaseVersion)
app.IBCKeeper.ConnectionKeeper.SetParams(ctx, ibcconnectiontypes.DefaultParams())
/*
The exact APR depends on the total Bonded Rowan , and can thus fluctuate a little .
- Inflation Percentage Required = APR * BondRatio
Where
BondRatio = ( Total Bonded Rowan/ Total Supply Rowan)
- Calculations for APR 300 % , assuming the max APR to be 350 and min APR to be 250
- 300% → 41.78
- 350 % → 48.74
- 250 % → 34.81
*/
minter := minttypes.Minter{
Inflation: sdk.MustNewDecFromStr("0.417800000000000000"),
AnnualProvisions: sdk.ZeroDec(),
}
app.MintKeeper.SetMinter(ctx, minter)
params := app.MintKeeper.GetParams(ctx)
params.InflationMax = sdk.MustNewDecFromStr("0.487400000000000000")
params.InflationMin = sdk.MustNewDecFromStr("0.348100000000000000")
app.MintKeeper.SetParams(ctx, params)
return app.mm.RunMigrations(ctx, app.configurator, vm)
})

Expand Down
330 changes: 330 additions & 0 deletions cmd/sifnoded/cmd/ibc-diag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,330 @@
package cmd

import (
"context"
"encoding/hex"
"encoding/json"
"fmt"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/types/query"
chtypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types"
"github.com/spf13/cobra"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"

abcitypes "github.com/tendermint/tendermint/abci/types"
coretypes "github.com/tendermint/tendermint/rpc/core/types"
)

var (
srcNode string
dstNode string
srcChannel string
dstChannel string
transferPort = "transfer"
)

func NewIBCDiagCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "ibc-diag",
Short: "IBC diagnostics commands",
}
cmd.AddCommand(
NewGetStuckTransfersCmd(),
)
return cmd
}

func NewGetStuckTransfersCmd() *cobra.Command {
command := &cobra.Command{
Use: "stuck-txs",
Short: "get stuck IBC transfers",
Long: `get stuck IBC transfers
Example: Getting stuck transfers between sifchain and terra
sifnoded ibc-diag stuck-txs \
--src-node http://rpc.sifchain.finance:80 \
--dst-node http://public-node.terra.dev:26657 \
--src-channel channel-18 \
--dst-channel channel-7
Use the regular IBC commands to find the src and dst channels of a connection
`,
Run: func(cmd *cobra.Command, args []string) {
getStuckTransfers(cmd)
},
}
command.Flags().StringVar(&srcNode, "src-node", srcNode, "rpc endpoint of source node")
command.Flags().StringVar(&dstNode, "dst-node", dstNode, "rpc endpoint of destination node")
command.Flags().StringVar(&srcChannel, "src-channel", dstChannel, "source channel id")
command.Flags().StringVar(&dstChannel, "dst-channel", dstChannel, "destination channel id")
_ = command.MarkFlagRequired("src-node")
_ = command.MarkFlagRequired("dst-node")
_ = command.MarkFlagRequired("src-channel")
_ = command.MarkFlagRequired("dst-channel")
return command
}

func getStuckTransfers(cmd *cobra.Command) {
commitments, err := getCommittedPackets(cmd, srcNode, transferPort, srcChannel)
if err != nil {
panic(err)
}

unreceived, err := getUnreceivedPackets(
cmd,
dstNode,
commitments,
transferPort,
dstChannel)
if err != nil {
panic(err)
}

transfers, err := getTransfers(srcNode, srcChannel, unreceived)
if err != nil {
panic(err)
}

// print results in csv format
fmt.Println("packet_sequence, tx_hash, amount, denom, receiver, sender")
for _, t := range transfers {
fmt.Printf("%s, %s, %s, %s, %s, %s\n",
t.PacketSequence,
t.TxHash,
t.PacketData.Amount,
t.PacketData.Denom,
t.PacketData.Receiver,
t.PacketData.Sender,
)
}
}

// getCommmittedPackets returns the list of packets that were sent on a given
// channel/port but for which there is still a PacketCommitment in the
// underlying DB . A packet that still has a PacketCommitment is a packet whose
// receipt was never acknowledged and which hasn't yet timed out.
func getCommittedPackets(
cmd *cobra.Command,
nodeURI string,
portID string,
channelID string) ([]uint64, error) {

clientCtx, err := getClientContext(cmd, nodeURI)
if err != nil {
return nil, err
}

queryClient := chtypes.NewQueryClient(clientCtx)

packets := []uint64{}

page := uint64(1)
limit := uint64(100)

for {
pageReq := &query.PageRequest{
Offset: (page - 1) * limit,
Limit: limit,
}

req := &chtypes.QueryPacketCommitmentsRequest{
PortId: portID,
ChannelId: channelID,
Pagination: pageReq,
}

res, err := queryClient.PacketCommitments(context.Background(), req)
if err != nil {
return nil, err
}

packetSequences := make([]uint64, len(res.Commitments))
for i, p := range res.Commitments {
packetSequences[i] = p.Sequence
}

packets = append(packets, packetSequences...)

if len(res.Commitments) < int(limit) {
break
} else {
page++
}
}

return packets, nil
}

// getUnreceivedPackets takes a list of packets and returns the subset that
// hasn't been received on the destination channel/port.
func getUnreceivedPackets(
cmd *cobra.Command,
nodeURI string,
committedPackets []uint64,
portID string,
channelID string) ([]uint64, error) {

clientCtx, err := getClientContext(cmd, nodeURI)
if err != nil {
panic(err)
}

queryClient := chtypes.NewQueryClient(clientCtx)

req := &chtypes.QueryUnreceivedPacketsRequest{
PortId: portID,
ChannelId: channelID,
PacketCommitmentSequences: committedPackets,
}

res, err := queryClient.UnreceivedPackets(context.Background(), req)
if err != nil {
return nil, err
}

return res.Sequences, nil
}

// getTransfers takes a list of packet sequences and returns the corresponding
// list of Transfers which contain the actual transfer data.
func getTransfers(nodeURI string, channelID string, packets []uint64) ([]*Transfer, error) {
c, err := rpchttp.New(nodeURI, "/websocket")
if err != nil {
return nil, err
}

transfers := []*Transfer{}
for _, seq := range packets {
ev, err := getTransfer(c, channelID, seq)
if err != nil {
return nil, err
}
transfers = append(transfers, ev)
}

return transfers, nil
}

// getTransfer fetches transfer data corresponding to a given packet.
func getTransfer(client *rpchttp.HTTP, channelID string, packetSequence uint64) (*Transfer, error) {
query := fmt.Sprintf("send_packet.packet_sequence=%d AND send_packet.packet_src_channel='%s'", packetSequence, channelID)

page := 1
perPage := 100
res, err := client.TxSearch(
context.Background(),
query,
false,
&page,
&perPage,
"asc",
)
if err != nil {
return nil, err
}

filter := func(eventType string) bool {
return eventType == "send_packet"
}
filteredEvents := FilterEvents(res.Txs, filter)

if len(filteredEvents) == 0 {
return nil, fmt.Errorf("pruned send_packet (sequence %d)", packetSequence)
}
if len(filteredEvents) > 1 {
return nil, fmt.Errorf("multiple events (%d) for %s", len(filteredEvents), query)
}

ev := filteredEvents[0]
var fungibleTokenPacket FungibleTokenPacketData
err = json.Unmarshal(
[]byte(ev.GetAttribute("packet_data")),
&fungibleTokenPacket,
)
if err != nil {
return nil, err
}

t := &Transfer{
PacketSequence: ev.GetAttribute("packet_sequence"),
TxHash: ev.TxHash,
PacketData: fungibleTokenPacket,
}

return t, nil
}

func getClientContext(cmd *cobra.Command, nodeURI string) (*client.Context, error) {
ctx, err := client.GetClientQueryContext(cmd)
if err != nil {
return nil, err
}

ctx = ctx.WithNodeURI(nodeURI)

srcClient, err := client.NewClientFromNode(nodeURI)
if err != nil {
return nil, err
}

ctx = ctx.WithClient(srcClient)

return &ctx, nil
}

type Transfer struct {
TxHash string
PacketSequence string
PacketData FungibleTokenPacketData
}

type FungibleTokenPacketData struct {
Denom string
Amount string
Sender string
Receiver string
}

func FilterEvents(
txs []*coretypes.ResultTx,
typeFilter func(string) bool,
) []*EventInfo {
infos := []*EventInfo{}
for _, tx := range txs {
txHash := hex.EncodeToString(tx.Tx.Hash())
for _, ev := range tx.TxResult.Events {
if typeFilter(ev.Type) {
attributes := []string{}
for _, attr := range ev.Attributes {
attributes = append(attributes, attr.String())
}
info := &EventInfo{
Type: ev.Type,
TxHash: txHash,
Attributes: attributes,
RealAttributes: ev.Attributes,
}
infos = append(infos, info)
}
}
}
return infos
}

type EventInfo struct {
Type string
TxHash string
Attributes []string
RealAttributes []abcitypes.EventAttribute
}

func (ev *EventInfo) GetAttribute(key string) string {
for _, attr := range ev.RealAttributes {
if string(attr.Key) == key {
return string(attr.Value)
}
}
return ""
}
1 change: 1 addition & 0 deletions cmd/sifnoded/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
SetGenesisOracleAdminCmd(app.DefaultNodeHome),
tmcli.NewCompletionCmd(rootCmd, true),
debug.Cmd(),
NewIBCDiagCmd(),
)
server.AddCommands(rootCmd, app.DefaultNodeHome, newApp, createSimappAndExport, addModuleInitFlags)
// add keybase, auxiliary RPC, query, and tx child commands
Expand Down
Loading

0 comments on commit 269cfad

Please sign in to comment.