From 5f84d6575b92a57862af60dba3342017a4368158 Mon Sep 17 00:00:00 2001 From: Peter Bukva Date: Thu, 27 Jun 2024 17:06:24 +0100 Subject: [PATCH] Implementing contract version update --- app/app.go | 5 +++ app/upgrade_0_11_4.go | 66 ++++++++++++++++++++++++++++ app/upgrade_v_11_4_manifest.go | 13 ++++-- app/upgrade_v_11_4_network_config.go | 20 +++++++-- 4 files changed, 98 insertions(+), 6 deletions(-) diff --git a/app/app.go b/app/app.go index d393d38c..2154bcc0 100644 --- a/app/app.go +++ b/app/app.go @@ -752,6 +752,11 @@ func (app *App) RegisterUpgradeHandlers(cfg module.Configurator) { return nil, err } + err = app.ChangeContractVersions(ctx, &networkInfo, manifest) + if err != nil { + return nil, err + } + // Save the manifest err = app.SaveManifest(manifest, plan.Name) if err != nil { diff --git a/app/upgrade_0_11_4.go b/app/upgrade_0_11_4.go index 13a23547..3722c8ac 100644 --- a/app/upgrade_0_11_4.go +++ b/app/upgrade_0_11_4.go @@ -22,6 +22,7 @@ var reconciliationData []byte var reconciliationDataTestnet []byte var ( + cw20ContractInfoKey = []byte("contract_info") reconciliationTotalBalanceKey = []byte("total_balance") reconciliationNOutstandingAddressesKey = []byte("n_outstanding_addresses") reconciliationStateKey = []byte("state") @@ -90,6 +91,53 @@ func (app *App) ChangeContractLabel(ctx types.Context, contractAddr *string, new return nil } +func (app *App) ChangeContractVersion(ctx types.Context, contractAddr *string, newVersion *ContractVersion, manifest *UpgradeManifest) error { + if contractAddr == nil { + return nil + } + + _, _, prefixStore, err := app.getContractData(ctx, *contractAddr) + if err != nil { + return err + } + + wasPresent := prefixStore.Has(cw20ContractInfoKey) + + var origVersion *ContractVersion + if wasPresent { + storeVal := prefixStore.Get(cw20ContractInfoKey) + var val ContractVersion + if err := json.Unmarshal(storeVal, val); err != nil { + return err + } + origVersion = &val + } + + //if origVersion == newVersion || (origVersion != nil && newVersion != nil && *origVersion == *newVersion) { + // return nil + //} + + if newVersion != nil { + newVersionStoreValue, err := json.Marshal(*newVersion) + if err != nil { + return err + } + prefixStore.Set(cw20ContractInfoKey, newVersionStoreValue) + } else if wasPresent { + prefixStore.Delete(cw20ContractInfoKey) + } + + manifestVersionUpdate := ContractVersionUpdate{ + Address: *contractAddr, + From: origVersion, + To: newVersion, + } + + manifest.Contracts.VersionUpdated = append(manifest.Contracts.VersionUpdated, manifestVersionUpdate) + + return nil +} + func (app *App) ChangeContractLabels(ctx types.Context, networkInfo *NetworkConfig, manifest *UpgradeManifest) error { contracts := []struct{ addr, newLabel *string }{ {addr: &networkInfo.Contracts.Reconciliation.Addr, newLabel: networkInfo.Contracts.Reconciliation.NewLabel}, @@ -104,6 +152,24 @@ func (app *App) ChangeContractLabels(ctx types.Context, networkInfo *NetworkConf return nil } +func (app *App) ChangeContractVersions(ctx types.Context, networkInfo *NetworkConfig, manifest *UpgradeManifest) error { + contracts := []struct { + addr *string + newVersion *ContractVersion + }{ + {addr: &networkInfo.Contracts.Reconciliation.Addr, newVersion: networkInfo.Contracts.Reconciliation.NewContractVersion}, + } + for _, contract := range contracts { + + err := app.ChangeContractVersion(ctx, contract.addr, contract.newVersion, manifest) + if err != nil { + return err + } + } + + return nil +} + func (app *App) ProcessReconciliation(ctx types.Context, networkInfo *NetworkConfig, manifest *UpgradeManifest) error { records := networkInfo.ReconciliationInfo.InputCSVRecords diff --git a/app/upgrade_v_11_4_manifest.go b/app/upgrade_v_11_4_manifest.go index 5b2e0def..b8060d6e 100644 --- a/app/upgrade_v_11_4_manifest.go +++ b/app/upgrade_v_11_4_manifest.go @@ -22,9 +22,10 @@ func NewUpgradeManifest() *UpgradeManifest { } type Contracts struct { - StateCleaned []string `json:"contracts_state_cleaned,omitempty"` - AdminUpdated []ContractValueUpdate `json:"contracts_admin_updated,omitempty"` - LabelUpdated []ContractValueUpdate `json:"contracts_label_updated,omitempty"` + StateCleaned []string `json:"contracts_state_cleaned,omitempty"` + AdminUpdated []ContractValueUpdate `json:"contracts_admin_updated,omitempty"` + LabelUpdated []ContractValueUpdate `json:"contracts_label_updated,omitempty"` + VersionUpdated []ContractVersionUpdate `json:"version_updated,omitempty"` } type ContractValueUpdate struct { @@ -33,6 +34,12 @@ type ContractValueUpdate struct { To string `json:"to"` } +type ContractVersionUpdate struct { + Address string `json:"address"` + From *ContractVersion `json:"from,omitempty"` + To *ContractVersion `json:"to"` +} + type ValueUpdate struct { From string `json:"from"` To string `json:"to"` diff --git a/app/upgrade_v_11_4_network_config.go b/app/upgrade_v_11_4_network_config.go index d9fef524..5de6b3a8 100644 --- a/app/upgrade_v_11_4_network_config.go +++ b/app/upgrade_v_11_4_network_config.go @@ -11,6 +11,10 @@ var NetworkInfos = map[string]NetworkConfig{ Addr: "fetch1tynmzk68pq6kzawqffrqdhquq475gw9ccmlf9gk24mxjjy6ugl3q70aeyd", NewAdmin: getStringPtr("fetch15p3rl5aavw9rtu86tna5lgxfkz67zzr6ed4yhw"), NewLabel: getStringPtr("reconciliation-contract"), + NewContractVersion: &ContractVersion{ + Contract: "contract-fetch-asi-reconciliation-test", + Version: "1.0.0-rc0", + }, }, Almanac: &Almanac{ ProdAddr: "fetch1mezzhfj7qgveewzwzdk6lz5sae4dunpmmsjr9u7z0tpmdsae8zmquq3y0y", @@ -33,6 +37,10 @@ var NetworkInfos = map[string]NetworkConfig{ Contracts: &ContractSet{ Reconciliation: &Reconciliation{ Addr: "fetch1g5ur2wc5xnlc7sw9wd895lw7mmxz04r5syj3s6ew8md6pvwuweqqavkgt0", + NewContractVersion: &ContractVersion{ + Contract: "contract-fetch-asi-reconciliation", + Version: "1.0.0", + }, }, Almanac: &Almanac{ ProdAddr: "fetch1tjagw8g8nn4cwuw00cf0m5tl4l6wfw9c0ue507fhx9e3yrsck8zs0l3q4w", @@ -68,10 +76,16 @@ type TokenBridge struct { NewAdmin *string } +type ContractVersion struct { + Contract string `json:"contract"` + Version string `json:"version"` +} + type Reconciliation struct { - Addr string - NewAdmin *string - NewLabel *string + Addr string + NewAdmin *string + NewLabel *string + NewContractVersion *ContractVersion } type Almanac struct {