-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (122 loc) · 3.54 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"cosmossdk.io/log"
"cosmossdk.io/store"
"cosmossdk.io/store/metrics"
"cosmossdk.io/store/rootmulti"
dbm "github.com/cosmos/cosmos-db"
)
type ModuleHash struct {
StoreName string `json:"store_name"`
Hash string `json:"hash"`
}
func getModuleHashes(dbPath string) ([]ModuleHash, error) {
dbDir := filepath.Dir(dbPath)
db, err := dbm.NewDB("application", dbm.GoLevelDBBackend, dbDir)
if err != nil {
return nil, err
}
multistoreRaw := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
multistore := multistoreRaw.(*rootmulti.Store)
version := multistore.LatestVersion()
commitInfo, err := multistore.GetCommitInfo(version)
if err != nil {
return nil, err
}
var moduleHashes []ModuleHash
for _, storeInfo := range commitInfo.StoreInfos {
moduleHashes = append(moduleHashes, ModuleHash{
StoreName: storeInfo.Name,
Hash: fmt.Sprintf("%x", storeInfo.GetHash()),
})
}
// Sort the moduleHashes slice alphabetically by StoreName
sort.Slice(moduleHashes, func(i, j int) bool {
return moduleHashes[i].StoreName < moduleHashes[j].StoreName
})
return moduleHashes, nil
}
func saveHashesToFile(moduleHashes []ModuleHash, fileName string) error {
jsonOutput, err := json.MarshalIndent(moduleHashes, "", " ")
if err != nil {
return err
}
err = os.WriteFile(fileName, jsonOutput, 0644)
if err != nil {
return err
}
fmt.Printf("Hashes saved to %s\n", fileName)
fmt.Println(string(jsonOutput))
return nil
}
func main() {
if len(os.Args) != 3 {
panic("please provide exactly 2 paths to application.db")
}
dbPath1 := os.Args[1]
dbPath2 := os.Args[2]
moduleHashes1, err := getModuleHashes(dbPath1)
if err != nil {
panic(err)
}
moduleHashes2, err := getModuleHashes(dbPath2)
if err != nil {
panic(err)
}
// Create the hashes directory in $PWD if it doesn't exist
outputDir := filepath.Join(".", "hashes")
if _, err := os.Stat(outputDir); os.IsNotExist(err) {
err = os.MkdirAll(outputDir, os.ModePerm)
if err != nil {
panic(err)
}
} else {
fmt.Println("Directory hashes already exists. Overwriting files...")
}
// Save the hashes in node-1-hashes.json and node-2-hashes.json
file1 := filepath.Join(outputDir, "node-1-hashes.json")
file2 := filepath.Join(outputDir, "node-2-hashes.json")
err = saveHashesToFile(moduleHashes1, file1)
if err != nil {
panic(err)
}
err = saveHashesToFile(moduleHashes2, file2)
if err != nil {
panic(err)
}
// Compare the module hashes between the two databases
differingModules := []string{}
for i, hash1 := range moduleHashes1 {
hash2 := moduleHashes2[i]
if hash1.StoreName == hash2.StoreName && hash1.Hash != hash2.Hash {
fmt.Printf("Differing module: %s\n", hash1.StoreName)
fmt.Printf("DB1 Hash: %s\n", hash1.Hash)
fmt.Printf("DB2 Hash: %s\n", hash2.Hash)
differingModules = append(differingModules, hash1.StoreName)
}
}
// Export differing modules as an array in a new file
if len(differingModules) > 0 {
envFilePath := filepath.Join(outputDir, "modules.env")
envFile, err := os.Create(envFilePath)
if err != nil {
panic(err)
}
defer envFile.Close()
// Format the differing modules as a bash array
envVarValue := fmt.Sprintf("DIFFERING_MODULES=(%s)\n", strings.Join(differingModules, " "))
_, err = envFile.WriteString(envVarValue)
if err != nil {
panic(err)
}
fmt.Printf("Differing modules exported to %s as an array: %s\n", envFilePath, envVarValue)
} else {
fmt.Println("No differing modules found.")
}
}