Skip to content

Commit

Permalink
Merge pull request #17 from rarimo/fix/csca-root-op
Browse files Browse the repository at this point in the history
Must update params on Merkle root change instead of treap root
  • Loading branch information
olegfomenko authored Apr 30, 2024
2 parents 9219c4e + 3d30858 commit 9828462
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions x/cscalist/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

0 comments on commit 9828462

Please sign in to comment.