Gravity-Devs / liquidity

Apache License 2.0
9 stars 13 forks source link

Balance incorrect when querying address related with pool's activity. #17

Open yj0x0x opened 2 years ago

yj0x0x commented 2 years ago

Summary of Bug

Account cosmos1tx68a8k9yz54z06qfve9l2zxvgsz4ka3hr8962 activities in the block height 8695158 has balance incorrect. Transaction hash is the 002D9BEECFD46E4B35339D47166B7D0CE6F357DFA54AF3EBD1854ACC33A4D4CFB0 which is endblocker transaction. This activity possibly get through Rosetta API return. Returns are like below

   {
                "transaction_identifier": {
                    "hash": "002D9BEECFD46E4B35339D47166B7D0CE6F357DFA54AF3EBD1854ACC33A4D4CFB0"
                },
                "operations": [
                    {
                        "operation_identifier": {
                            "index": 0
                        },
                        "type": "coin_received",
                        "status": "Success",
                        "account": {
                            "address": "cosmos1tx68a8k9yz54z06qfve9l2zxvgsz4ka3hr8962"
                        },
                        "amount": {
                            "value": "24",
                            "currency": {
                                "symbol": "pool32DD066BE949E5FDCC7DC09EBB67C7301D0CA957C2EF56A39B37430165447DAC",
                                "decimals": 0
                            }
                        }
                    },
                    {
                        "operation_identifier": {
                            "index": 1
                        },
                        "type": "coin_spent",
                        "status": "Success",
                        "account": {
                            "address": "cosmos1tx68a8k9yz54z06qfve9l2zxvgsz4ka3hr8962"
                        },
                        "amount": {
                            "value": "-356604",
                            "currency": {
                                "symbol": "ibc/2181AAB0218EAC24BC9F86BD1364FBBFA3E6E3FCC25E88E3E68C15DC6E752D86",
                                "decimals": 0
                            }
                        }
                    },
                    {
                        "operation_identifier": {
                            "index": 2
                        },
                        "type": "coin_spent",
                        "status": "Success",
                        "account": {
                            "address": "cosmos1tx68a8k9yz54z06qfve9l2zxvgsz4ka3hr8962"
                        },
                        "amount": {
                            "value": "-24770",
                            "currency": {
                                "symbol": "uatom",
                                "decimals": 0
                            }
                        }
                    },
                    {
                        "operation_identifier": {
                            "index": 3
                        },
                        "type": "coin_spent",
                        "status": "Success",
                        "account": {
                            "address": "cosmos1tx68a8k9yz54z06qfve9l2zxvgsz4ka3hr8962"
                        },
                        "amount": {
                            "value": "-11160745",
                            "currency": {
                                "symbol": "ibc/2181AAB0218EAC24BC9F86BD1364FBBFA3E6E3FCC25E88E3E68C15DC6E752D86",
                                "decimals": 0
                            }
                        }
                    },
                    {
                        "operation_identifier": {
                            "index": 4
                        },
                        "type": "coin_spent",
                        "status": "Success",
                        "account": {
                            "address": "cosmos1tx68a8k9yz54z06qfve9l2zxvgsz4ka3hr8962"
                        },
                        "amount": {
                            "value": "-775230",
                            "currency": {
                                "symbol": "uatom",
                                "decimals": 0
                            }
                        }
                    },
                    {
                        "operation_identifier": {
                            "index": 5
                        },
                        "type": "coin_spent",
                        "status": "Success",
                        "account": {
                            "address": "cosmos1tx68a8k9yz54z06qfve9l2zxvgsz4ka3hr8962"
                        },
                        "amount": {
                            "value": "-24",
                            "currency": {
                                "symbol": "pool32DD066BE949E5FDCC7DC09EBB67C7301D0CA957C2EF56A39B37430165447DAC",
                                "decimals": 0
                            }
                        }
                    },
                    {
                        "operation_identifier": {
                            "index": 6
                        },
                        "type": "coin_received",
                        "status": "Success",
                        "account": {
                            "address": "cosmos13s0lj78gujy89vn9780gq6qn4v937ywjasyw2q"
                        },
                        "amount": {
                            "value": "356604",
                            "currency": {
                                "symbol": "ibc/2181AAB0218EAC24BC9F86BD1364FBBFA3E6E3FCC25E88E3E68C15DC6E752D86",
                                "decimals": 0
                            }
                        }
                    },
                    {
                        "operation_identifier": {
                            "index": 7
                        },
                        "type": "coin_received",
                        "status": "Success",
                        "account": {
                            "address": "cosmos13s0lj78gujy89vn9780gq6qn4v937ywjasyw2q"
                        },
                        "amount": {
                            "value": "24770",
                            "currency": {
                                "symbol": "uatom",
                                "decimals": 0
                            }
                        }
                    },
                    {
                        "operation_identifier": {
                            "index": 8
                        },
                        "type": "coin_received",
                        "status": "Success",
                        "account": {
                            "address": "cosmos1xtwsv6lff8jlmnracz0tke78xqwse22h4sez9u"
                        },
                        "amount": {
                            "value": "11160745",
                            "currency": {
                                "symbol": "ibc/2181AAB0218EAC24BC9F86BD1364FBBFA3E6E3FCC25E88E3E68C15DC6E752D86",
                                "decimals": 0
                            }
                        }
                    },
                    {
                        "operation_identifier": {
                            "index": 9
                        },
                        "type": "coin_received",
                        "status": "Success",
                        "account": {
                            "address": "cosmos1xtwsv6lff8jlmnracz0tke78xqwse22h4sez9u"
                        },
                        "amount": {
                            "value": "775230",
                            "currency": {
                                "symbol": "uatom",
                                "decimals": 0
                            }
                        }
                    },
                    {
                        "operation_identifier": {
                            "index": 10
                        },
                        "type": "coin_received",
                        "status": "Success",
                        "account": {
                            "address": "cosmos13s0lj78gujy89vn9780gq6qn4v937ywjasyw2q"
                        },
                        "amount": {
                            "value": "24",
                            "currency": {
                                "symbol": "pool32DD066BE949E5FDCC7DC09EBB67C7301D0CA957C2EF56A39B37430165447DAC",
                                "decimals": 0
                            }
                        }
                    }
                ]
            }

operation below is weird that because address cosmos1tx68a8k9yz54z06qfve9l2zxvgsz4ka3hr8962 didn't have any balance for assets on chain.

 {
                        "operation_identifier": {
                            "index": 2
                        },
                        "type": "coin_spent",
                        "status": "Success",
                        "account": {
                            "address": "cosmos1tx68a8k9yz54z06qfve9l2zxvgsz4ka3hr8962"
                        },
                        "amount": {
                            "value": "-24770",
                            "currency": {
                                "symbol": "uatom",
                                "decimals": 0
                            }
                        }
                    },

I have traced this address asset through bulletin rest api like below.

 curl -X GET -H "Content-Type: application/json" -H "x-cosmos-block-height: 8695158" http://localhost:1317/cosmos/bank/v1beta1/balances/cosmos1tx68a8k9yz54z06qfve9l2zxvgsz4ka3hr8962

 result ; 

 {
    "balances": [],
    "pagination": {
        "next_key": null,
        "total": "0"
    }
}

 curl -X GET -H "Content-Type: application/json" -H "x-cosmos-block-height: 8695157" http://localhost:1317/cosmos/bank/v1beta1/balances/cosmos1tx68a8k9yz54z06qfve9l2zxvgsz4ka3hr8962

 {
    "balances": [],
    "pagination": {
        "next_key": null,
        "total": "0"
    }
}

There were no balance about this address but there were activity . I also compare through api.cosmos.network/cosmos/bank/v1beta1/balances/. Result also same.

Is there some issue about block data or something?

Version

Gaia v6.0.4 Liquidity module version is v1.4.6

Steps to Reproduce

You can query cosmos node with Rosetta API turn on and like below.

curl -X POST  -H "Content-type: application/json" http://localhost:8080/block -d '{"network_identifier": {"blockchain": "app","network": "network"},"block_identifier": {"index": 8695158}}'

For Admin Use

yj0x0x commented 2 years ago

Why I think that this activity related with pool's activity;

When you wee the endblocker transaction operation through the Rosetta API, Below coin_spent operation's received address is cosmos13s0lj78gujy89vn9780gq6qn4v937ywjasyw2q. Which is pool address when you could see below.

link : https://www.mintscan.io/cosmos/account/cosmos13s0lj78gujy89vn9780gq6qn4v937ywjasyw2q

coin_spent operation.

{
                        "operation_identifier": {
                            "index": 2
                        },
                        "type": "coin_spent",
                        "status": "Success",
                        "account": {
                            "address": "cosmos1tx68a8k9yz54z06qfve9l2zxvgsz4ka3hr8962"
                        },
                        "amount": {
                            "value": "-24770",
                            "currency": {
                                "symbol": "uatom",
                                "decimals": 0
                            }
                        }
                    },

coin_receive operation

                    {
                        "operation_identifier": {
                            "index": 7
                        },
                        "type": "coin_received",
                        "status": "Success",
                        "account": {
                            "address": "cosmos13s0lj78gujy89vn9780gq6qn4v937ywjasyw2q"
                        },
                        "amount": {
                            "value": "24770",
                            "currency": {
                                "symbol": "uatom",
                                "decimals": 0
                            }
                        }
                    },

I have also checked liquidity module's endblocker function.

// ExecutePoolBatches executes the accumulated msgs in the batch.
// The order is (1)swap, (2)deposit, (3)withdraw.
func (k Keeper) ExecutePoolBatches(ctx sdk.Context) {
    params := k.GetParams(ctx)
    logger := k.Logger(ctx)

    k.IterateAllPoolBatches(ctx, func(poolBatch types.PoolBatch) bool {
        if !poolBatch.Executed && ctx.BlockHeight()%int64(params.UnitBatchHeight) == 0 {
            executedMsgCount, err := k.SwapExecution(ctx, poolBatch)
            if err != nil {
                panic(err)
            }

            k.IterateAllPoolBatchDepositMsgStates(ctx, poolBatch, func(batchMsg types.DepositMsgState) bool {
                if batchMsg.Executed || batchMsg.ToBeDeleted || batchMsg.Succeeded {
                    return false
                }
                executedMsgCount++
                if err := k.ExecuteDeposit(ctx, batchMsg, poolBatch); err != nil {
                    logger.Error("deposit failed",
                        "poolID", poolBatch.PoolId,
                        "batchIndex", poolBatch.Index,
                        "msgIndex", batchMsg.MsgIndex,
                        "depositor", batchMsg.Msg.GetDepositor(),
                        "error", err)
                    if err := k.RefundDeposit(ctx, batchMsg, poolBatch); err != nil {
                        panic(err)
                    }
                }
                return false
            })

            k.IterateAllPoolBatchWithdrawMsgStates(ctx, poolBatch, func(batchMsg types.WithdrawMsgState) bool {
                if batchMsg.Executed || batchMsg.ToBeDeleted || batchMsg.Succeeded {
                    return false
                }
                executedMsgCount++
                if err := k.ExecuteWithdrawal(ctx, batchMsg, poolBatch); err != nil {
                    logger.Error("withdraw failed",
                        "poolID", poolBatch.PoolId,
                        "batchIndex", poolBatch.Index,
                        "msgIndex", batchMsg.MsgIndex,
                        "withdrawer", batchMsg.Msg.GetWithdrawer(),
                        "error", err)
                    if err := k.RefundWithdrawal(ctx, batchMsg, poolBatch); err != nil {
                        panic(err)
                    }
                }
                return false
            })

            // Mark the batch as executed when any msgs were executed.
            if executedMsgCount > 0 {
                poolBatch.Executed = true
                k.SetPoolBatch(ctx, poolBatch)
            }
        }
        return false
    })
}

These function I think check about account's validation. But reported endblocker tx ,002D9BEECFD46E4B35339D47166B7D0CE6F357DFA54AF3EBD1854ACC33A4D4CFB0, has activities without account's balance validation.

yj0x0x commented 2 years ago

Could you please check this balance incorrection?

dongsam commented 2 years ago

Hi @yj0x0x , I can't find tx 002D9BEECFD46E4B35339D47166B7D0CE6F357DFA54AF3EBD1854ACC33A4D4CFB0, could you please let me know the exact tx hash? and there is no liquidity tx on the block 8695158