From 3d30858df94e0c0c5d511182d8ab1b76b9849b3f Mon Sep 17 00:00:00 2001 From: violog <51th.apprent1ce.f0rce@gmail.com> Date: Mon, 29 Apr 2024 18:47:11 +0300 Subject: [PATCH] Must update params on Merkle root change instead of treap root --- x/cscalist/keeper/proposal.go | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/x/cscalist/keeper/proposal.go b/x/cscalist/keeper/proposal.go index f0e244d8..c89f2b25 100644 --- a/x/cscalist/keeper/proposal.go +++ b/x/cscalist/keeper/proposal.go @@ -8,8 +8,11 @@ import ( ) func (k Keeper) EditCSCAListProposal(ctx sdk.Context, proposal *types.EditCSCAListProposal) error { - tree := Treap{k} - root := k.GetRootKey(ctx) + var ( + tree = Treap{k} + oldRootKey = k.GetRootKey(ctx) + oldRoot, _ = k.GetNode(ctx, oldRootKey) + ) for _, leaf := range proposal.ToRemove { tree.Remove(ctx, leaf) @@ -22,31 +25,34 @@ func (k Keeper) EditCSCAListProposal(ctx sdk.Context, proposal *types.EditCSCALi tree.Insert(ctx, leaf) } - if root != k.GetRootKey(ctx) { - params := k.GetParams(ctx) - params.RootUpdated = true - params.UpdatedAtBlock = uint64(ctx.BlockHeight()) - k.SetParams(ctx, params) - } - + k.updateParamsOnNeed(ctx, oldRoot) return nil } func (k Keeper) ReplaceCSCAListProposal(ctx sdk.Context, proposal *types.ReplaceCSCAListProposal) error { - tree := Treap{k} - root := k.GetRootKey(ctx) + var ( + tree = Treap{k} + oldRootKey = k.GetRootKey(ctx) + oldRoot, _ = k.GetNode(ctx, oldRootKey) + ) k.RemoveTree(ctx) // safe while no errors are expected, otherwise think about a backup for _, leaf := range proposal.Leaves { tree.Insert(ctx, leaf) } - if root != k.GetRootKey(ctx) { + k.updateParamsOnNeed(ctx, oldRoot) + return nil +} + +func (k Keeper) updateParamsOnNeed(ctx sdk.Context, oldRoot types.Node) { + newRootKey := k.GetRootKey(ctx) + newRoot, _ := k.GetNode(ctx, newRootKey) + + if oldRoot.Hash != newRoot.Hash { params := k.GetParams(ctx) params.RootUpdated = true params.UpdatedAtBlock = uint64(ctx.BlockHeight()) k.SetParams(ctx, params) } - - return nil }