Skip to content

Commit

Permalink
feat: initial reconciliation transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathansumner committed Apr 17, 2024
1 parent 7f4f616 commit fa37d46
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 2 deletions.
157 changes: 155 additions & 2 deletions cmd/fetchd/cmd/genasiupgrade.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
package cmd

import (
"encoding/csv"
"encoding/json"
"fmt"
"github.com/btcsuite/btcutil/bech32"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
"github.com/spf13/cobra"
"github.com/tendermint/tendermint/types"
"log"
"os"
)

const (
ReconcilliationDataPath = "reconciliation_data.csv"
ReconciliationAccAddress = "fetch1rhrlzsx9z865dqen8t4v47r99dw6y4va4uph0x" /* "asi1rhrlzsx9z865dqen8t4v47r99dw6y4vaw76rd9" */

flagNewDescription = "new-description"
Bech32Chars = "023456789acdefghjklmnpqrstuvwxyz"
AddrDataLength = 32
Expand Down Expand Up @@ -54,13 +66,18 @@ func ASIGenesisUpgradeCmd(defaultNodeHome string) *cobra.Command {

genFile := config.GenesisFile()

_, genDoc, err := genutiltypes.GenesisStateFromGenFile(genFile)
appState, genDoc, err := genutiltypes.GenesisStateFromGenFile(genFile)
if err != nil {
return fmt.Errorf("failed to unmarshal genesis state: %w", err)
}

// replace chain-id
ASIGenesisUpgradeReplaceChainID(genDoc)

if err = ASIGenesisUpgradeWithdrawReconciliationBalances(clientCtx.Codec, &appState); err != nil {
return fmt.Errorf("failed to withdraw reconciliation balances: %w", err)
}

return genutil.ExportGenesisFile(genDoc, genFile)
},
}
Expand All @@ -85,4 +102,140 @@ func ASIGenesisUpgradeReplaceAddresses() {}

func ASIGenesisUpgradeWithdrawIBCChannelsBalances() {}

func ASIGenesisUpgradeWithdrawReconciliationBalances() {}
func getGenesisAccountsMap(authGenState *authtypes.GenesisState) (map[string]*authtypes.GenesisAccount, *authtypes.GenesisAccounts, error) {
accountMap := make(map[string]*authtypes.GenesisAccount)

accounts, err := authtypes.UnpackAccounts(authGenState.Accounts)
if err != nil {
return nil, nil, fmt.Errorf("failed to unpack accounts from authtypes.GenState: %w", err)
}

for _, account := range accounts {
// ensure account is valid
err := account.Validate()
if err != nil {
return nil, nil, err
}

accountMap[account.GetAddress().String()] = &account
}

return accountMap, &accounts, nil
}

func getGenesisBalancesMap(bankGenState *banktypes.GenesisState) (*map[string]int, error) {
balanceMap := make(map[string]int)

for i, balance := range bankGenState.Balances {
balanceMap[balance.Address] = i
}

return &balanceMap, nil
}

func ASIGenesisUpgradeWithdrawReconciliationBalances(cdc codec.Codec, appState *map[string]json.RawMessage) error {
bankGenState := banktypes.GetGenesisStateFromAppState(cdc, *appState)
balances := bankGenState.Balances
balanceMap, err := getGenesisBalancesMap(bankGenState)
if err != nil {
return err
}

authGenState := authtypes.GetGenesisStateFromAppState(cdc, *appState)
accountMap, _, err := getGenesisAccountsMap(&authGenState)
if err != nil {
return err
}

file, err := os.Open(ReconcilliationDataPath)
if err != nil {
log.Fatalf("Error opening reconciliation data: %s", err)
}
defer file.Close()

r := csv.NewReader(file)
items, err := r.ReadAll()
if err != nil {
log.Fatalf("Error reading reconciliation data: %s", err)
}

//reconciliationAccount, ok := accountMap[ReconciliationAccAddress]
//if !ok {
// return fmt.Errorf("no genesis match for reconciliation address: %s", ReconciliationAccAddress)
//}

reconciliationBalanceIdx, ok := (*balanceMap)[ReconciliationAccAddress]
if !ok {
return fmt.Errorf("no genesis match for reconciliation address: %s", ReconciliationAccAddress)
}

for _, row := range items {
addr := row[2]

//_ = row[3] balance from CSV

// TODO: use this conversion when merging with address replacement branch
//addr, err := convertAddressToASI(oldAddr, AccAddressPrefix)
//if err != nil {
// return fmt.Errorf("failed to convert address: %w", err)
//}
//if _, err = sdk.AccAddressFromBech32(addr); err != nil {
// return fmt.Errorf("converted address is invalid: %w", err)
//}

acc, ok := accountMap[addr]
if !ok {
return fmt.Errorf("no genesis match for reconciliation address: %s", addr)
}

balanceIdx, ok := (*balanceMap)[addr]
if !ok {
continue
}

accBalance := balances[balanceIdx]

// check if the reconciliation address is still dormant and contains funds
if (*acc).GetSequence() != 0 && !accBalance.Coins.IsAllPositive() {
fmt.Println("Reconciliation address is not dormant or has no funds, skipping withdrawal")
continue
}

// withdraw funds from the reconciliation address
balances[reconciliationBalanceIdx].Coins = balances[reconciliationBalanceIdx].Coins.Add(accBalance.Coins...)

// zero out the other account's balance
balances[balanceIdx].Coins = sdk.NewCoins()
}

// update the bank genesis state
(*bankGenState).Balances = balances

bankGenStateBytes, err := cdc.MarshalJSON(bankGenState)
if err != nil {
return fmt.Errorf("failed to marshal auth genesis state: %w", err)
}

(*appState)[banktypes.ModuleName] = bankGenStateBytes

return nil
}

func convertAddressToASI(addr string, addressPrefix string) (string, error) {
_, decodedAddrData, err := bech32.Decode(addr)
if err != nil {
return "", err
}

newAddress, err := bech32.Encode(NewAddrPrefix+addressPrefix, decodedAddrData)
if err != nil {
return "", err
}

err = sdk.VerifyAddressFormat(decodedAddrData)
if err != nil {
return "", err
}

return newAddress, nil
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ require (
github.com/tendermint/tm-db v0.6.7
)

require github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce

require (
filippo.io/edwards25519 v1.0.0-beta.2 // indirect
github.com/99designs/keyring v1.1.6 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ github.com/btcsuite/btcutil v0.0.0-20190207003914-4c204d697803/go.mod h1:+5NJ2+q
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
github.com/btcsuite/btcutil v1.0.2/go.mod h1:j9HUFwoQRsZL3V4n+qG+CUnEGHOarIxfC3Le2Yhbcts=
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce h1:YtWJF7RHm2pYCvA5t0RPmAaLUhREsKuKd+SLhxFbFeQ=
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce/go.mod h1:0DVlHczLPewLcPGEIeUEzfOJhqGPQ0mJJRDBtD307+o=
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg=
github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVaaLLH7j4eDXPRvw78tMflu7Ie2bzYOH4Y8rRKBY=
github.com/btcsuite/goleveldb v1.0.0/go.mod h1:QiK9vBlgftBg6rWQIj6wFzbPfRjiykIEhBH4obrXJ/I=
Expand Down

0 comments on commit fa37d46

Please sign in to comment.