feat: add counter for activated TAs

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2024-07-22 01:03:33 +02:00
parent e06fc55630
commit 064e7c1b38
No known key found for this signature in database
GPG Key ID: FA5EE906EB55316A
3 changed files with 30 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package keeper
import (
"encoding/binary"
"errors"
"github.com/planetmint/planetmint-go/util"
@ -16,6 +17,8 @@ func (k Keeper) StoreTrustAnchor(ctx sdk.Context, ta types.TrustAnchor, activate
var appendValue []byte
if activated {
appendValue = []byte{1}
counter := k.GetActivatedTACounter(ctx)
k.setActivatedTACounter(ctx, counter+1)
} else {
appendValue = []byte{0}
}
@ -46,3 +49,20 @@ func (k Keeper) GetTrustAnchor(ctx sdk.Context, pubKey string) (val types.TrustA
}
return val, false, true
}
func (k Keeper) setActivatedTACounter(ctx sdk.Context, counter uint64) {
taCounterStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.TAIndexKey))
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, counter)
taCounterStore.Set([]byte(types.ActivatedTACounterKey), bz)
}
func (k Keeper) GetActivatedTACounter(ctx sdk.Context) (counter uint64) {
taCounterStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.TAIndexKey))
bz := taCounterStore.Get([]byte(types.ActivatedTACounterKey))
if bz == nil {
return 0
}
counter = binary.BigEndian.Uint64(bz)
return
}

View File

@ -69,3 +69,11 @@ func TestUpdateTrustAnchor(t *testing.T) {
assert.True(t, activated)
}
}
func TestActivatedTACounter(t *testing.T) {
t.Parallel()
keeper, ctx := keepertest.MachineKeeper(t)
createNTrustAnchor(t, keeper, ctx, 100)
counter := keeper.GetActivatedTACounter(ctx)
assert.Equal(t, uint64(50), counter)
}

View File

@ -28,6 +28,8 @@ const (
LiquidAssetKey = "Machine/LiquidAsset/"
ParamsKey = "Machine/Params"
ActivatedTACounterKey = "ActivatedTACounter"
)
func KeyPrefix(p string) []byte {