diff --git a/x/machine/keeper/trust_anchor.go b/x/machine/keeper/trust_anchor.go index 55ce039..260255d 100644 --- a/x/machine/keeper/trust_anchor.go +++ b/x/machine/keeper/trust_anchor.go @@ -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 +} diff --git a/x/machine/keeper/trust_anchor_test.go b/x/machine/keeper/trust_anchor_test.go index 6962bba..9a82b1f 100644 --- a/x/machine/keeper/trust_anchor_test.go +++ b/x/machine/keeper/trust_anchor_test.go @@ -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) +} diff --git a/x/machine/types/keys.go b/x/machine/types/keys.go index a6860ab..30fd9c6 100644 --- a/x/machine/types/keys.go +++ b/x/machine/types/keys.go @@ -28,6 +28,8 @@ const ( LiquidAssetKey = "Machine/LiquidAsset/" ParamsKey = "Machine/Params" + + ActivatedTACounterKey = "ActivatedTACounter" ) func KeyPrefix(p string) []byte {