Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Cudos menifest block height and community pool balance refactor #385

Merged
merged 5 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func New(

app.GovKeeper = *govKeeper.SetHooks(
govtypes.NewMultiGovHooks(
// register the governance hooks
// register the governance hooks
),
)

Expand Down Expand Up @@ -758,9 +758,6 @@ func getNetworkInfo(app *App, ctx *sdk.Context, manifest *UpgradeManifest, expec
return nil, fmt.Errorf("network info not found for chain id: %s", ctx.ChainID())
}

manifest.MergeSourceChainID = expectedChainIdOfMergeSourceGenesis
manifest.DestinationChainID = ctx.ChainID()

return networkInfo, nil
}

Expand All @@ -779,7 +776,7 @@ func LoadAndParseMergeSourceInputFiles(app *App, ctx *sdk.Context, manifest *Upg

cudosConfig := NewCudosMergeConfig(networkInfo.CudosMerge)

genesisData, err := parseGenesisData(*cudosJsonData, cudosConfig, manifest)
genesisData, err := parseGenesisData(*cudosJsonData, cudosGenDoc, cudosConfig, manifest)
if err != nil {
return nil, nil, fmt.Errorf("failed to parse genesis data: %w", err)
}
Expand All @@ -801,6 +798,12 @@ func (app *App) RegisterUpgradeHandlers(cfg module.Configurator) {
return nil, fmt.Errorf("cudos merge: %w", err)
}

manifest.DestinationChainBlockHeight = cudosGenesisData.blockHeight
manifest.DestinationChainID = cudosGenesisData.chainId

manifest.SourceChainBlockHeight = ctx.BlockHeight()
manifest.MergeSourceChainID = ctx.ChainID()

MissingNO57 marked this conversation as resolved.
Show resolved Hide resolved
err = app.DeleteContractStates(ctx, networkInfo, manifest)
if err != nil {
return nil, err
Expand Down
83 changes: 51 additions & 32 deletions app/upgrade_cudos.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
Expand Down Expand Up @@ -126,6 +125,8 @@ const (

type GenesisData struct {
totalSupply sdk.Coins
blockHeight int64
chainId string

accounts *OrderedMap[string, *AccountInfo]
contracts *OrderedMap[string, *ContractInfo]
Expand Down Expand Up @@ -233,11 +234,6 @@ func CudosMergeUpgradeHandler(app *App, ctx sdk.Context, cudosCfg *CudosMergeCon
return fmt.Errorf("cudos merge: failed process delegations: %w", err)
}

err = fundCommunityPool(ctx, app, genesisData, cudosCfg, manifest)
if err != nil {
return fmt.Errorf("cudos merge: failed to fund community pool: %w", err)
}

err = verifySupply(genesisData, cudosCfg, manifest)
if err != nil {
return fmt.Errorf("cudos merge: failed to verify supply: %w", err)
Expand All @@ -246,14 +242,16 @@ func CudosMergeUpgradeHandler(app *App, ctx sdk.Context, cudosCfg *CudosMergeCon
return nil
}

func parseGenesisData(jsonData map[string]interface{}, cudosCfg *CudosMergeConfig, manifest *UpgradeManifest) (*GenesisData, error) {
func parseGenesisData(jsonData map[string]interface{}, genDoc *tmtypes.GenesisDoc, cudosCfg *CudosMergeConfig, manifest *UpgradeManifest) (*GenesisData, error) {
genesisData := GenesisData{}

totalSupply, err := parseGenesisTotalSupply(jsonData)
if err != nil {
return nil, fmt.Errorf("failed to get total supply: %w", err)
}
genesisData.totalSupply = totalSupply
genesisData.blockHeight = genDoc.InitialHeight
genesisData.chainId = genDoc.ChainID

genesisData.contracts, err = parseGenesisWasmContracts(jsonData)
if err != nil {
Expand Down Expand Up @@ -881,39 +879,30 @@ func createDelegation(ctx sdk.Context, app *App, originalValidator string, newDe
}

func fundCommunityPool(ctx sdk.Context, app *App, genesisData *GenesisData, cudosCfg *CudosMergeConfig, manifest *UpgradeManifest) error {
MissingNO57 marked this conversation as resolved.
Show resolved Hide resolved
// Fund community pool
// Get addresses and amounts
cudosCommunityPoolFundsAccount := genesisData.accounts.MustGet(cudosCfg.config.RemainingDistributionBalanceAddr)
communityPoolBalance, _ := genesisData.distributionInfo.feePool.communityPool.TruncateDecimal()
convertedCommunityPoolBalance, err := convertBalance(communityPoolBalance, cudosCfg)
if err != nil {
return err
}

communityPoolSourceAccountRawAddress := genesisData.accounts.MustGet(cudosCfg.config.RemainingDistributionBalanceAddr).rawAddress

if cudosCfg.config.CommunityPoolBalanceDestAddr == "" {
// Move balance to community pool if destination address is not set

err = app.DistrKeeper.FundCommunityPool(ctx, convertedCommunityPoolBalance, communityPoolSourceAccountRawAddress)
if err != nil {
return err
}

registerBalanceMovement(cudosCfg.config.RemainingDistributionBalanceAddr, distrtypes.ModuleName, communityPoolBalance, convertedCommunityPoolBalance, "community_pool_balance", manifest)

} else {
// Move balance to given account

destAccRawAddr, err := convertAddressToRaw(cudosCfg.config.CommunityPoolBalanceDestAddr, cudosCfg)
if err != nil {
return err
}
// Mint balance to distribution leftover address
err = migrateToAccount(ctx, app, minttypes.ModuleName, cudosCommunityPoolFundsAccount.rawAddress, communityPoolBalance, convertedCommunityPoolBalance, "community_pool_balance", manifest)
if err != nil {
return err
}

err = app.BankKeeper.SendCoins(ctx, communityPoolSourceAccountRawAddress, destAccRawAddr, convertedCommunityPoolBalance)
if err != nil {
return err
}
registerBalanceMovement(cudosCfg.config.RemainingDistributionBalanceAddr, cudosCfg.config.CommunityPoolBalanceDestAddr, communityPoolBalance, convertedCommunityPoolBalance, "community_pool_balance", manifest)
// Move balance to destination chain community pool
err = app.DistrKeeper.FundCommunityPool(ctx, convertedCommunityPoolBalance, cudosCommunityPoolFundsAccount.rawAddress)
if err != nil {
return err
}
MissingNO57 marked this conversation as resolved.
Show resolved Hide resolved

// Subtract balance from genesis balances
err = removeGenesisBalance(genesisData, cudosCfg.config.RemainingDistributionBalanceAddr, communityPoolBalance, "community_pool_balance", manifest)
if err != nil {
return err
}

return nil
Expand Down Expand Up @@ -1596,6 +1585,26 @@ func createGenesisBalance(genesisData *GenesisData, toAddress string, amount sdk
return nil
}

func removeGenesisBalance(genesisData *GenesisData, address string, amount sdk.Coins, memo string, manifest *UpgradeManifest) error {
// Check if fromAddress exists
if _, ok := genesisData.accounts.Get(address); !ok {
return fmt.Errorf("address %s does not exist in genesis balances", address)
}

if acc := genesisData.accounts.MustGet(address); acc.migrated {
return fmt.Errorf("genesis account %s already migrated", address)
}

genesisAccount := genesisData.accounts.MustGet(address)
genesisAccount.balance = genesisAccount.balance.Sub(amount)

genesisData.accounts.Set(address, genesisAccount)

registerManifestBalanceMovement(address, "", amount, memo, manifest)

return nil
}

func GetAddressByName(genesisAccounts *OrderedMap[string, *AccountInfo], name string) (string, error) {

for _, accAddress := range genesisAccounts.Keys() {
Expand Down Expand Up @@ -1798,6 +1807,16 @@ func MigrateGenesisAccounts(genesisData *GenesisData, ctx sdk.Context, app *App,
return err
}

if cudosCfg.config.CommunityPoolBalanceDestAddr == "" {
// Move balance to community pool if destination address is not set

err := fundCommunityPool(ctx, app, genesisData, cudosCfg, manifest)
if err != nil {
return fmt.Errorf("failed to fund community pool: %w", err)
}
}
// Else accounts were moved in cash before minting

// Mint the rest of the supply
for _, genesisAccountAddress := range genesisData.accounts.Keys() {
genesisAccount := genesisData.accounts.MustGet(genesisAccountAddress)
Expand Down
13 changes: 11 additions & 2 deletions app/upgrade_cudos_distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,20 @@ func withdrawGenesisDistributionRewards(app *App, genesisData *GenesisData, cudo
return err
}

// Withdraw remaining balance
// Withdraw Community pool balance to address if defined
communityBalance, _ := genesisData.distributionInfo.feePool.communityPool.TruncateDecimal()
distributionModuleAccount := genesisData.accounts.MustGet(genesisData.distributionInfo.distributionModuleAccountAddress)

communityBalance, _ := genesisData.distributionInfo.feePool.communityPool.TruncateDecimal()
remainingBalance := distributionModuleAccount.balance.Sub(communityBalance)
// If community pool destination balance is set we move community pool tokens there.
if cudosCfg.config.CommunityPoolBalanceDestAddr != "" {
err := moveGenesisBalance(genesisData, genesisData.distributionInfo.distributionModuleAccountAddress, cudosCfg.config.CommunityPoolBalanceDestAddr, communityBalance, "community_pool_balance", manifest, cudosCfg)
if err != nil {
return fmt.Errorf("failed to move community pool balance %w", err)
}
}

// Withdraw remaining balance
app.Logger().Info("cudos merge: remaining dist balance", "amount", remainingBalance.String())

// TODO: Write to manifest?
Expand Down
31 changes: 17 additions & 14 deletions app/upgrade_v_11_4_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@ const manifestFilenameBase = "upgrade_manifest.json"

type UpgradeManifest struct {
// Following 2 hash data members are intentionally without `omitempty` parameter in `json:...` decorator
GenesisFileSha256 string `json:"genesis_file_sha256"`
NetworkConfigFileSha256 string `json:"network_config_file_sha256"`
MergeSourceChainID string `json:"merge_source_chain_id"`
DestinationChainID string `json:"destination_chain_id"`
Reconciliation *UpgradeReconciliation `json:"reconciliation,omitempty"`
Contracts *Contracts `json:"contracts,omitempty"`
IBC *UpgradeIBCTransfers `json:"ibc,omitempty"`
Migration *UpgradeMigation `json:"migration,omitempty"`
MoveGenesisBalance *UpgradeMoveGenesisBalance `json:"move_genesis_balance,omitempty"`
Delegate *UpgradeDelegate `json:"delegate,omitempty"`
MoveMintedBalance *UpgradeMoveMintedBalance `json:"move_minted_balance,omitempty"`
VestingCollision *UpgradeVestingCollision `json:"vesting_collision,omitempty"`
MoveDelegations *UpgradeMoveDelegations `json:"move_delegation,omitempty"`
CreatedAccounts *UpgradeCreatedAccounts `json:"created_accounts,omitempty"`
GenesisFileSha256 string `json:"genesis_file_sha256"`
NetworkConfigFileSha256 string `json:"network_config_file_sha256"`
MergeSourceChainID string `json:"merge_source_chain_id"`
DestinationChainID string `json:"destination_chain_id"`
SourceChainBlockHeight int64 `json:"source_chain_block_height"`
DestinationChainBlockHeight int64 `json:"destination_chain_block_height"`

MissingNO57 marked this conversation as resolved.
Show resolved Hide resolved
Reconciliation *UpgradeReconciliation `json:"reconciliation,omitempty"`
Contracts *Contracts `json:"contracts,omitempty"`
IBC *UpgradeIBCTransfers `json:"ibc,omitempty"`
Migration *UpgradeMigation `json:"migration,omitempty"`
MoveGenesisBalance *UpgradeMoveGenesisBalance `json:"move_genesis_balance,omitempty"`
Delegate *UpgradeDelegate `json:"delegate,omitempty"`
MoveMintedBalance *UpgradeMoveMintedBalance `json:"move_minted_balance,omitempty"`
VestingCollision *UpgradeVestingCollision `json:"vesting_collision,omitempty"`
MoveDelegations *UpgradeMoveDelegations `json:"move_delegation,omitempty"`
CreatedAccounts *UpgradeCreatedAccounts `json:"created_accounts,omitempty"`
}

func NewUpgradeManifest() *UpgradeManifest {
Expand Down
16 changes: 8 additions & 8 deletions app/upgrade_v_11_4_network_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ var NetworkInfos = map[string]NetworkConfig{
RemainingDistributionBalanceAddr: "cudos1nj49l56x7sss5hqyvfmctxr3mq64whg273g3x5",
ContractDestinationFallbackAddr: "cudos1nj49l56x7sss5hqyvfmctxr3mq64whg273g3x5",

CommissionFetchAddr: "fetch15p3rl5aavw9rtu86tna5lgxfkz67zzr6ed4yhw", // Fetch ecosystem wallet
ExtraSupplyFetchAddr: "fetch1wp8fl6fl4je40cfh2reeyj6cvucve9s6antdav",
VestingCollisionDestAddr: "cudos1nj49l56x7sss5hqyvfmctxr3mq64whg273g3x5",
CommunityPoolBalanceDestAddr: "cudos1dslwarknhfsw3pfjzxxf5mn28q3ewfectw0gta",
CommissionFetchAddr: "fetch15p3rl5aavw9rtu86tna5lgxfkz67zzr6ed4yhw", // Fetch ecosystem wallet
ExtraSupplyFetchAddr: "fetch1wp8fl6fl4je40cfh2reeyj6cvucve9s6antdav",
VestingCollisionDestAddr: "cudos1nj49l56x7sss5hqyvfmctxr3mq64whg273g3x5",
//CommunityPoolBalanceDestAddr: "cudos1dslwarknhfsw3pfjzxxf5mn28q3ewfectw0gta",

NewAddrPrefix: "fetch",
OldAddrPrefix: "cudos",
Expand Down Expand Up @@ -314,11 +314,11 @@ type CudosMergeConfigJSON struct {
RemainingGravityBalanceAddr string `json:"remaining_gravity_balance_addr"` // Cudos address
RemainingDistributionBalanceAddr string `json:"remaining_distribution_balance_addr"` // Cudos address
ContractDestinationFallbackAddr string `json:"contract_destination_fallback_addr"` // Cudos address
CommunityPoolBalanceDestAddr string `json:"community_pool_balance_dest_addr"` // Cudos address, funds are moved to destination chain community pool if not set

CommissionFetchAddr string `json:"commission_fetch_addr"` // Fetch address for commission
ExtraSupplyFetchAddr string `json:"extra_supply_fetch_addr"` // Fetch address for extra supply
VestingCollisionDestAddr string `json:"vesting_collision_dest_addr"` // This gets converted to raw address, so it can be fetch or cudos address
CommunityPoolBalanceDestAddr string `json:"community_pool_balance_dest_addr"` // This gets converted to raw address, so it can be fetch or cudos address
CommissionFetchAddr string `json:"commission_fetch_addr"` // Fetch address for commission
ExtraSupplyFetchAddr string `json:"extra_supply_fetch_addr"` // Fetch address for extra supply
VestingCollisionDestAddr string `json:"vesting_collision_dest_addr"` // This gets converted to raw address, so it can be fetch or cudos address

NewAddrPrefix string `json:"new_addr_prefix"`
OldAddrPrefix string `json:"old_addr_prefix"`
Expand Down
Loading