Skip to content

Commit

Permalink
feat: Refact. NetworkConfig for load from file + util cmd (for json &…
Browse files Browse the repository at this point in the history
… address) (#12)
  • Loading branch information
MissingNO57 authored Oct 2, 2024
1 parent 9291bb4 commit 4414430
Show file tree
Hide file tree
Showing 12 changed files with 678 additions and 170 deletions.
51 changes: 39 additions & 12 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ type App struct {
interfaceRegistry types.InterfaceRegistry

invCheckPeriod uint
cudosPath string

cudosPath string
cudosSha256 string
configPath string
configSha256 string

// keys to access the substores
keys map[string]*sdk.KVStoreKey
Expand Down Expand Up @@ -257,7 +261,7 @@ type App struct {
// NewSimApp returns a reference to an initialized SimApp.
func New(
logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool,
skipUpgradeHeights map[int64]bool, homePath string, invCheckPeriod uint, cudosPath string, encodingConfig appparams.EncodingConfig, enabledProposals []wasm.ProposalType,
skipUpgradeHeights map[int64]bool, homePath string, invCheckPeriod uint, cudosPath string, configPath string, cudosSha256 string, configSha256 string, encodingConfig appparams.EncodingConfig, enabledProposals []wasm.ProposalType,
appOpts servertypes.AppOptions, wasmOpts []wasm.Option, baseAppOptions ...func(*baseapp.BaseApp),
) *App {

Expand Down Expand Up @@ -287,6 +291,9 @@ func New(
interfaceRegistry: interfaceRegistry,
invCheckPeriod: invCheckPeriod,
cudosPath: cudosPath,
configPath: configPath,
cudosSha256: cudosSha256,
configSha256: configSha256,
keys: keys,
tkeys: tkeys,
memKeys: memKeys,
Expand Down Expand Up @@ -371,7 +378,7 @@ func New(

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

Expand Down Expand Up @@ -720,6 +727,25 @@ func (app *App) GetSubspace(moduleName string) paramstypes.Subspace {
return subspace
}

func getNetworkInfo(app *App, ctx sdk.Context) (*NetworkConfig, error) {
// Load network config from file if given
var networkInfo *NetworkConfig
var err error
if app.configPath != "" {
networkInfo, err = LoadNetworkConfigFromFile(app.configPath, &app.configSha256)
if err != nil {
return nil, err
}
// Config file not given, config from hardcoded map
} else if info, ok := NetworkInfos[ctx.ChainID()]; ok {
networkInfo = &info
} else {
return nil, fmt.Errorf("network info not found for chain id: %s", ctx.ChainID())
}

return networkInfo, nil
}

func (app *App) RegisterUpgradeHandlers(cfg module.Configurator) {
app.UpgradeKeeper.SetUpgradeHandler("v0.11.3", func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
return app.mm.RunMigrations(ctx, cfg, fromVM)
Expand All @@ -733,37 +759,38 @@ func (app *App) RegisterUpgradeHandlers(cfg module.Configurator) {
return nil, fmt.Errorf("cudos path not set")
}

networkInfo, ok := NetworkInfos[ctx.ChainID()]
if !ok {
return nil, fmt.Errorf("network info not found for chain id: " + ctx.ChainID())
networkInfo, err := getNetworkInfo(app, ctx)
if err != nil {
return nil, err
}

err := app.DeleteContractStates(ctx, &networkInfo, manifest)
err = app.DeleteContractStates(ctx, networkInfo, manifest)
if err != nil {
return nil, err
}

err = app.UpgradeContractAdmins(ctx, &networkInfo, manifest)
err = app.UpgradeContractAdmins(ctx, networkInfo, manifest)
if err != nil {
return nil, err
}

err = app.ChangeContractLabels(ctx, &networkInfo, manifest)
err = app.ChangeContractLabels(ctx, networkInfo, manifest)
if err != nil {
return nil, err
}

err = app.ChangeContractVersions(ctx, &networkInfo, manifest)
err = app.ChangeContractVersions(ctx, networkInfo, manifest)
if err != nil {
return nil, err
}

err = app.ProcessReconciliation(ctx, &networkInfo, manifest)
err = app.ProcessReconciliation(ctx, networkInfo, manifest)
if err != nil {
return nil, err
}

err = CudosMergeUpgradeHandler(app, ctx, networkInfo.CudosMerge, manifest)
cudosConfig := NewCudosMergeConfig(networkInfo.CudosMerge)
err = CudosMergeUpgradeHandler(app, ctx, cudosConfig, manifest)
if err != nil {
return nil, err
}
Expand Down
39 changes: 22 additions & 17 deletions app/ordered_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import (
)

type OrderedMap[K comparable, V any] struct {
keys []K
values map[K]V
sorted bool
keys []K
values map[K]V
isSorted bool
}

type Pair[K comparable, V any] struct {
key K
value V
Key K
Value V
}

// NewOrderedMapWithKeys creates an OrderedMap with the keys set to true
// NewOrderedSet creates an OrderedMap with the keys set to true
func NewOrderedSet[K comparable](keys []K) *OrderedMap[K, bool] {
newMap := NewOrderedMap[K, bool]()

Expand All @@ -28,38 +28,38 @@ func NewOrderedSet[K comparable](keys []K) *OrderedMap[K, bool] {
return newMap
}

// NewOrderedMapWithKeys creates an OrderedMap with the keys set to true
// NewOrderedMapFromPairs creates an OrderedMap from the keys-value pairs
func NewOrderedMapFromPairs[K comparable, V any](pairs []Pair[K, V]) *OrderedMap[K, V] {
newMap := NewOrderedMap[K, V]()

for _, pair := range pairs {
newMap.Set(pair.key, pair.value)
newMap.Set(pair.Key, pair.Value)
}

return newMap
}

// NewOrderedMap creates a new OrderedMap instance
// NewOrderedMap creates a new OrderedMap empty instance
func NewOrderedMap[K comparable, V any]() *OrderedMap[K, V] {
keys := []K{}
sorted := false
return &OrderedMap[K, V]{
keys: keys,
values: make(map[K]V),
sorted: sorted,
keys: keys,
values: make(map[K]V),
isSorted: sorted,
}
}

// Set adds a key-value pair to the map
func (om *OrderedMap[K, V]) Set(key K, value V) {
if _, exists := om.values[key]; !exists {
om.keys = append(om.keys, key)
om.sorted = false
om.isSorted = false
}
om.values[key] = value
}

// Set adds a key-value pair to the map - it must not exist before
// SetNew adds a key-value pair to the map - it must not exist before
func (om *OrderedMap[K, V]) SetNew(key K, value V) {
if om.Has(key) {
log.Panicf("key %v already exist", key)
Expand All @@ -73,7 +73,7 @@ func (om *OrderedMap[K, V]) Get(key K) (V, bool) {
return value, exists
}

// Get retrieves the value associated with the key, panics otherwise
// MustGet retrieves the value associated with the key, panics otherwise
func (om *OrderedMap[K, V]) MustGet(key K) V {
value, exists := om.Get(key)
if !exists {
Expand Down Expand Up @@ -115,13 +115,18 @@ func (om *OrderedMap[K, V]) PrintOrdered() {

// SortKeys sorts the keys in ascending order
func (om *OrderedMap[K, V]) SortKeys(lessFunc func(i, j K) bool) {
if om.sorted {
if om.isSorted {
return
}
sort.Slice(om.keys, func(i, j int) bool {
return lessFunc(om.keys[i], om.keys[j])
})
om.sorted = true
om.isSorted = true
}

// IsSorted returns true *IF* order in the map is *still* sorted after calling `SortKeys(...)` method.
func (om *OrderedMap[K, V]) IsSorted() bool {
return om.isSorted
}

func sortUint64Keys[V any](orderedMap *OrderedMap[uint64, V]) {
Expand Down
16 changes: 10 additions & 6 deletions app/upgrade_0_11_4_reconciliation.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ func prefixStringWithLength(val string) []byte {
return buffer.Bytes()
}

func readInputReconciliationData(csvData []byte) [][]string {
func readInputReconciliationData(csvData []byte) *[][]string {
r := csv.NewReader(bytes.NewReader(csvData))
records, err := r.ReadAll()
if err != nil {
panic(fmt.Sprintf("error reading reconciliation data: %v", err))
}
return records
return &records
}

func (app *App) ChangeContractLabel(ctx types.Context, contractAddr *string, newLabel *string, manifest *UpgradeManifest) error {
Expand Down Expand Up @@ -123,8 +123,8 @@ func (app *App) ChangeContractVersion(ctx types.Context, contractAddr *string, n
origVersion = &val
}

if newVersion.cw2version != nil {
newVersionStoreValue, err := json.Marshal(*newVersion.cw2version)
if newVersion.CW2version != nil {
newVersionStoreValue, err := json.Marshal(*newVersion.CW2version)
if err != nil {
return err
}
Expand All @@ -136,7 +136,7 @@ func (app *App) ChangeContractVersion(ctx types.Context, contractAddr *string, n
manifestVersionUpdate := ContractVersionUpdate{
Address: *contractAddr,
From: origVersion,
To: newVersion.cw2version,
To: newVersion.CW2version,
}

if manifest.Contracts == nil {
Expand Down Expand Up @@ -181,7 +181,11 @@ func (app *App) ChangeContractVersions(ctx types.Context, networkInfo *NetworkCo
func (app *App) ProcessReconciliation(ctx types.Context, networkInfo *NetworkConfig, manifest *UpgradeManifest) error {
records := networkInfo.ReconciliationInfo.InputCSVRecords

err := app.WithdrawReconciliationBalances(ctx, networkInfo, records, manifest)
if records == nil {
return nil
}

err := app.WithdrawReconciliationBalances(ctx, networkInfo, *records, manifest)
if err != nil {
return fmt.Errorf("error withdrawing reconciliation balances: %v", err)
}
Expand Down
Loading

0 comments on commit 4414430

Please sign in to comment.