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

style: x/genutil: CollectTxs should move []os.DirEntry close to where it is used and also reuse valAccAddr as key to balancesMap #17814

Closed
1 task done
odeke-em opened this issue Sep 20, 2023 · 0 comments · Fixed by #17816
Labels
Type: Code Hygiene General cleanup and restructuring of code to provide clarity, flexibility, and modularity.

Comments

@odeke-em
Copy link
Collaborator

Is there an existing issue for this?

  • I have searched the existing issues

What happened?

This is a stylistic nit but also makes the code much better

Move []os.DirEntry closest to where it is used

Please see

var fos []os.DirEntry
fos, err = os.ReadDir(genTxsDir)
if err != nil {
return appGenTxs, persistentPeers, err
}
balancesMap := make(map[string]bankexported.GenesisBalance)
genBalIterator.IterateGenesisBalances(
cdc, appState,
func(balance bankexported.GenesisBalance) (stop bool) {
addr := balance.GetAddress()
balancesMap[addr] = balance
return false
},
)
// addresses and IPs (and port) validator server info
var addressesIPs []string
for _, fo := range fos {

and fix it up like this

diff --git a/x/genutil/collect.go b/x/genutil/collect.go
index 422a278b5a..5fc817f47d 100644
--- a/x/genutil/collect.go
+++ b/x/genutil/collect.go
@@ -76,12 +76,6 @@ func CollectTxs(cdc codec.JSONCodec, txJSONDecoder sdk.TxDecoder, moniker, genTx
 		return appGenTxs, persistentPeers, err
 	}
 
-	var fos []os.DirEntry
-	fos, err = os.ReadDir(genTxsDir)
-	if err != nil {
-		return appGenTxs, persistentPeers, err
-	}
-
 	balancesMap := make(map[string]bankexported.GenesisBalance)
 
 	genBalIterator.IterateGenesisBalances(
@@ -93,6 +87,12 @@ func CollectTxs(cdc codec.JSONCodec, txJSONDecoder sdk.TxDecoder, moniker, genTx
 		},
 	)
 
+	var fos []os.DirEntry
+	fos, err = os.ReadDir(genTxsDir)
+	if err != nil {
+		return appGenTxs, persistentPeers, err
+	}
+
 	// addresses and IPs (and port) validator server info
 	var addressesIPs []string
 
@@ -151,7 +151,7 @@ func CollectTxs(cdc codec.JSONCodec, txJSONDecoder sdk.TxDecoder, moniker, genTx
 			return appGenTxs, persistentPeers, fmt.Errorf("account %s balance not in genesis state: %+v", valAccAddr, balancesMap)
 		}
 
-		_, valOk := balancesMap[sdk.AccAddress(valAddr).String()]
+		_, valOk := balancesMap[valAccAddr]
 		if !valOk {
 			_, file, no, ok := runtime.Caller(1)
 			if ok {

Cosmos SDK Version

86ab989

How to reproduce?

No response

@odeke-em odeke-em added the Type: Code Hygiene General cleanup and restructuring of code to provide clarity, flexibility, and modularity. label Sep 20, 2023
odeke-em added a commit that referenced this issue Sep 20, 2023
This change moves []os.DirEntry close to where it is used
and also reuses the already created sdk.AccAddress.String()
[]byte->string (sure it is cached but unnecessary to re-invoke)
as a key to the balancesMap.

Fixes #17814
odeke-em added a commit that referenced this issue Sep 21, 2023
This change reuses the already created sdk.AccAddress.String()
[]byte->string (sure it is cached but unnecessary to re-invoke)
as a key to the balancesMap.

Fixes #17814
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Code Hygiene General cleanup and restructuring of code to provide clarity, flexibility, and modularity.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant