|
| 1 | +// Package kvrepair applies reviewed key/value repairs to module stores as hard |
| 2 | +// fork handlers. Each repair is a JSON file under repairs/ that names one chain, |
| 3 | +// one height, and the entries to write or delete at that height. |
| 4 | +package kvrepair |
| 5 | + |
| 6 | +import ( |
| 7 | + "embed" |
| 8 | + "fmt" |
| 9 | + "io/fs" |
| 10 | + "path" |
| 11 | + |
| 12 | + "github.com/sei-protocol/seilog" |
| 13 | + |
| 14 | + "github.com/sei-protocol/sei-chain/app/upgrades" |
| 15 | + sdk "github.com/sei-protocol/sei-chain/sei-cosmos/types" |
| 16 | + "github.com/sei-protocol/sei-chain/sei-db/common/kvrepair" |
| 17 | +) |
| 18 | + |
| 19 | +var logger = seilog.NewLogger("app", "upgrades", "kvrepair") |
| 20 | + |
| 21 | +// repairsDir holds the repair files compiled into the binary. |
| 22 | +const repairsDir = "repairs" |
| 23 | + |
| 24 | +//go:embed all:repairs |
| 25 | +var embeddedRepairs embed.FS |
| 26 | + |
| 27 | +// Register validates every embedded repair against the store keys and |
| 28 | +// registers one handler for each. It panics on an invalid repair file. |
| 29 | +func Register(m *upgrades.HardForkManager, keys map[string]*sdk.KVStoreKey) { |
| 30 | + repairs, err := Load(embeddedRepairs, keys) |
| 31 | + if err != nil { |
| 32 | + panic(fmt.Errorf("kvrepair: %w", err)) |
| 33 | + } |
| 34 | + for _, r := range repairs { |
| 35 | + m.RegisterHandler(NewHandler(r, keys)) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// Load reads and validates every .json file under repairs/ in fsys. It also |
| 40 | +// refuses a repair name used twice, and a key that two files repair at the |
| 41 | +// same height. |
| 42 | +func Load(fsys fs.FS, keys map[string]*sdk.KVStoreKey) ([]kvrepair.Repair, error) { |
| 43 | + files, err := fs.ReadDir(fsys, repairsDir) |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + storeExists := func(name string) bool { |
| 48 | + _, ok := keys[name] |
| 49 | + return ok |
| 50 | + } |
| 51 | + var repairs []kvrepair.Repair |
| 52 | + names := map[string]string{} |
| 53 | + targets := map[string]string{} |
| 54 | + for _, file := range files { |
| 55 | + if file.IsDir() || path.Ext(file.Name()) != ".json" { |
| 56 | + continue |
| 57 | + } |
| 58 | + filePath := path.Join(repairsDir, file.Name()) |
| 59 | + data, err := fs.ReadFile(fsys, filePath) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + r, err := kvrepair.Parse(data) |
| 64 | + if err != nil { |
| 65 | + return nil, fmt.Errorf("%s: %w", filePath, err) |
| 66 | + } |
| 67 | + if err := r.Validate(storeExists); err != nil { |
| 68 | + return nil, fmt.Errorf("%s: %w", filePath, err) |
| 69 | + } |
| 70 | + if other, ok := names[r.Name]; ok { |
| 71 | + return nil, fmt.Errorf("%s: repair name %q already used by %s", filePath, r.Name, other) |
| 72 | + } |
| 73 | + names[r.Name] = filePath |
| 74 | + for _, e := range r.Entries { |
| 75 | + target := fmt.Sprintf("%s/%d/%s/%x", r.ChainID, r.TargetRepairHeight, e.Store, []byte(e.Key)) |
| 76 | + if other, ok := targets[target]; ok { |
| 77 | + return nil, fmt.Errorf("%s: store %s key %x at height %d is also repaired by %s", |
| 78 | + filePath, e.Store, []byte(e.Key), r.TargetRepairHeight, other) |
| 79 | + } |
| 80 | + targets[target] = filePath |
| 81 | + } |
| 82 | + repairs = append(repairs, r) |
| 83 | + } |
| 84 | + return repairs, nil |
| 85 | +} |
| 86 | + |
| 87 | +// Handler implements upgrades.HardForkHandler for one Repair. |
| 88 | +type Handler struct { |
| 89 | + repair kvrepair.Repair |
| 90 | + keys map[string]*sdk.KVStoreKey |
| 91 | +} |
| 92 | + |
| 93 | +// NewHandler returns the hard fork handler that applies r. |
| 94 | +func NewHandler(r kvrepair.Repair, keys map[string]*sdk.KVStoreKey) upgrades.HardForkHandler { |
| 95 | + return Handler{repair: r, keys: keys} |
| 96 | +} |
| 97 | + |
| 98 | +func (h Handler) GetName() string { return "kvrepair-" + h.repair.Name } |
| 99 | + |
| 100 | +func (h Handler) GetTargetChainID() string { return h.repair.ChainID } |
| 101 | + |
| 102 | +func (h Handler) GetTargetHeight() int64 { return h.repair.TargetRepairHeight } |
| 103 | + |
| 104 | +// ExecuteHandler writes every entry and reads each one back. It returns an |
| 105 | +// error when a key that does not hold its new value holds something other than |
| 106 | +// its old value, or when a read-back does not match the write. Values compare |
| 107 | +// with kvrepair.ValuesEqual. |
| 108 | +func (h Handler) ExecuteHandler(ctx sdk.Context) error { |
| 109 | + var repaired, alreadyCorrect int |
| 110 | + for i, e := range h.repair.Entries { |
| 111 | + store := ctx.KVStore(h.keys[e.Store]) |
| 112 | + current := store.Get(e.Key) |
| 113 | + if holdsTarget(e, current) { |
| 114 | + alreadyCorrect++ |
| 115 | + } else if err := checkOld(e, current); err != nil { |
| 116 | + return fmt.Errorf("kvrepair %s: entry %d (store %s key %x): %w", |
| 117 | + h.repair.Name, i, e.Store, []byte(e.Key), err) |
| 118 | + } else { |
| 119 | + repaired++ |
| 120 | + } |
| 121 | + // Entries that already hold their target are written too, so that a |
| 122 | + // correct node and a repaired node commit the same changeset. |
| 123 | + if e.New == nil { |
| 124 | + store.Delete(e.Key) |
| 125 | + } else { |
| 126 | + store.Set(e.Key, *e.New) |
| 127 | + } |
| 128 | + if got := store.Get(e.Key); !holdsTarget(e, got) { |
| 129 | + return fmt.Errorf("kvrepair %s: entry %d (store %s key %x): read back %x after the write", |
| 130 | + h.repair.Name, i, e.Store, []byte(e.Key), got) |
| 131 | + } |
| 132 | + } |
| 133 | + logger.Info("applied kv repair", |
| 134 | + "repair", h.repair.Name, |
| 135 | + "height", ctx.BlockHeight(), |
| 136 | + "repaired", repaired, |
| 137 | + "already_correct", alreadyCorrect, |
| 138 | + ) |
| 139 | + return nil |
| 140 | +} |
| 141 | + |
| 142 | +// holdsTarget reports whether current reads the same as the entry's new value. |
| 143 | +func holdsTarget(e kvrepair.Entry, current []byte) bool { |
| 144 | + // Not bytes.Equal: a FlatKV account row returns a zero nonce for an account |
| 145 | + // whose nonce was deleted while another field remains, where memiavl returns |
| 146 | + // nil. |
| 147 | + return kvrepair.ValuesEqual(e.Store, e.Key, current, optional(e.New)) |
| 148 | +} |
| 149 | + |
| 150 | +func checkOld(e kvrepair.Entry, current []byte) error { |
| 151 | + switch { |
| 152 | + case e.OldAbsent && !kvrepair.ReadsAsAbsent(e.Store, e.Key, current): |
| 153 | + return fmt.Errorf("expected the key to be absent, found %x", current) |
| 154 | + case e.Old != nil && !kvrepair.ValuesEqual(e.Store, e.Key, current, *e.Old): |
| 155 | + return fmt.Errorf("expected old value %x, found %x", []byte(*e.Old), current) |
| 156 | + } |
| 157 | + return nil |
| 158 | +} |
| 159 | + |
| 160 | +// optional returns the bytes of v, or nil when v is nil. |
| 161 | +func optional(v *kvrepair.HexBytes) []byte { |
| 162 | + if v == nil { |
| 163 | + return nil |
| 164 | + } |
| 165 | + return *v |
| 166 | +} |
0 commit comments