diff --git a/x/settlement/genesis_test.go b/x/settlement/genesis_test.go index de28bbb3..eb3faaba 100644 --- a/x/settlement/genesis_test.go +++ b/x/settlement/genesis_test.go @@ -87,15 +87,15 @@ func (suite *GenesisTestSuite) TestGenesis_ExportGenesis() { }, Tenants: []types.Tenant{ { - Id: 1, + Id: 0, Admins: []string{sample.AccAddress()}, PayoutPeriod: 100, }, }, Utxrs: []types.UTXRWithTenantAndId{ { - Id: 1, - TenantId: 1, + Id: 0, + TenantId: 0, Utxr: types.UTXR{ RequestId: "request-0", Recipients: types.SingleRecipients(sdk.MustAccAddressFromBech32(suite.creator)), diff --git a/x/settlement/keeper/grpc_query_test.go b/x/settlement/keeper/grpc_query_test.go index 50583811..97380b9d 100644 --- a/x/settlement/keeper/grpc_query_test.go +++ b/x/settlement/keeper/grpc_query_test.go @@ -18,7 +18,7 @@ func (suite *SettlementTestSuite) TestKeeper_Params() { func (suite *SettlementTestSuite) TestKeeper_UTXR() { // Create UTXRs - for i := 1; i < 4; i++ { + for i := 0; i < 3; i++ { utxrId, err := s.keeper.CreateUTXR(s.ctx, uint64(1), &types.UTXR{ RequestId: fmt.Sprintf("request-%d", i), Recipients: types.SingleRecipients(s.creator), @@ -40,7 +40,7 @@ func (suite *SettlementTestSuite) TestKeeper_UTXR() { suite.Commit() // Get UTXRs - for i := 1; i < 4; i++ { + for i := 0; i < 3; i++ { res, err := s.queryClient.UTXR(s.ctx, &types.QueryUTXRRRequest{ TenantId: uint64(1), RequestId: fmt.Sprintf("request-%d", i), @@ -59,7 +59,7 @@ func (suite *SettlementTestSuite) TestKeeper_UTXR() { func (suite *SettlementTestSuite) TestKeeper_UTXRs() { // Create UTXRs - for i := 1; i < 4; i++ { + for i := 0; i < 3; i++ { utxrId, err := s.keeper.CreateUTXR(s.ctx, 1, &types.UTXR{ RequestId: fmt.Sprintf("request-%d", i), Recipients: types.SingleRecipients(s.creator), diff --git a/x/settlement/keeper/msg_server_test.go b/x/settlement/keeper/msg_server_test.go index e4e9b933..f95280ba 100644 --- a/x/settlement/keeper/msg_server_test.go +++ b/x/settlement/keeper/msg_server_test.go @@ -166,7 +166,7 @@ func (suite *SettlementTestSuite) TestMsgServer_Record_IDInOrder() { utxrs := suite.keeper.GetAllUTXRWithTenantAndID(suite.ctx) for i, utxr := range utxrs { - suite.EqualValues(utxr.Id, i+1) + suite.EqualValues(utxr.Id, i) } } diff --git a/x/settlement/keeper/utxr.go b/x/settlement/keeper/utxr.go index afe1f513..e87c7a6c 100644 --- a/x/settlement/keeper/utxr.go +++ b/x/settlement/keeper/utxr.go @@ -25,18 +25,17 @@ func (k SettlementKeeper) HasUTXRByRequestId(ctx sdk.Context, tenantId uint64, r } // GetLargestUTXRId returns the latest UTXR from the store by its tenantId -func (k SettlementKeeper) GetLargestUTXRId(ctx sdk.Context, tenantId uint64) uint64 { +func (k SettlementKeeper) GenerateUtxrId(ctx sdk.Context, tenantId uint64) uint64 { + var utxrId uint64 store := ctx.KVStore(k.storeKey) - utxrTenantStore := prefix.NewStore(store, types.UTXRStoreByTenantKey(tenantId)) - iterator := utxrTenantStore.ReverseIterator(nil, nil) - defer iterator.Close() - - if !iterator.Valid() { - // if there is no UTXR, return 0 - return 0 + bz := store.Get(types.LastUtxrIdStoreKey(tenantId)) + if bz != nil { + utxrId = sdk.BigEndianToUint64(bz) + utxrId++ } - return sdk.BigEndianToUint64(iterator.Key()) + store.Set(types.LastUtxrIdStoreKey(tenantId), sdk.Uint64ToBigEndian(utxrId)) + return utxrId } // CreateUTXR creates a new UTXR in the store @@ -56,7 +55,7 @@ func (k SettlementKeeper) CreateUTXR(ctx sdk.Context, tenantId uint64, utxr *typ } } - utxrId := k.GetLargestUTXRId(ctx, tenantId) + 1 + utxrId := k.GenerateUtxrId(ctx, tenantId) store := ctx.KVStore(k.storeKey) bz := k.cdc.MustMarshal(utxr) @@ -78,7 +77,6 @@ func (k SettlementKeeper) deleteUTXR(ctx sdk.Context, tenantId, utxrId uint64) e k.cdc.MustUnmarshal(bz, &utxr) store.Delete(types.UTXRStoreKey(tenantId, utxrId)) - store.Delete(types.UTXRStoreByRequestIdKey(tenantId, utxr.RequestId)) return nil } diff --git a/x/settlement/keeper/utxr_test.go b/x/settlement/keeper/utxr_test.go index 9e251eb1..1b3462bf 100644 --- a/x/settlement/keeper/utxr_test.go +++ b/x/settlement/keeper/utxr_test.go @@ -15,7 +15,7 @@ func (suite *SettlementTestSuite) TestKeeper_HasUTXRByRequestId() { CreatedAt: uint64(100), }) suite.NoError(err) - suite.Equal(uint64(1), utxrId) + suite.Equal(uint64(0), utxrId) has := suite.keeper.HasUTXRByRequestId(suite.ctx, 0, "request-1") suite.True(has) @@ -24,8 +24,8 @@ func (suite *SettlementTestSuite) TestKeeper_HasUTXRByRequestId() { suite.False(has) } -func (suite *SettlementTestSuite) TestKeeper_GetLatestUTXR() { - id := suite.keeper.GetLargestUTXRId(suite.ctx, 0) +func (suite *SettlementTestSuite) TestKeeper_GenerateUtxrId() { + id := suite.keeper.GenerateUtxrId(suite.ctx, 0) suite.Equal(uint64(0), id) utxrId, err := suite.keeper.CreateUTXR(suite.ctx, 0, &types.UTXR{ @@ -40,8 +40,8 @@ func (suite *SettlementTestSuite) TestKeeper_GetLatestUTXR() { has := suite.keeper.HasUTXRByRequestId(suite.ctx, 0, "request-1") suite.True(has) - id = suite.keeper.GetLargestUTXRId(suite.ctx, 0) - suite.Equal(uint64(1), id) + id = suite.keeper.GenerateUtxrId(suite.ctx, 0) + suite.Equal(uint64(2), id) utxrId, err = suite.keeper.CreateUTXR(suite.ctx, 0, &types.UTXR{ RequestId: "request-2", @@ -50,30 +50,30 @@ func (suite *SettlementTestSuite) TestKeeper_GetLatestUTXR() { CreatedAt: uint64(100), }) suite.NoError(err) - suite.Equal(uint64(2), utxrId) + suite.Equal(uint64(3), utxrId) has = suite.keeper.HasUTXRByRequestId(suite.ctx, 0, "request-2") suite.True(has) - id = suite.keeper.GetLargestUTXRId(suite.ctx, 0) - suite.Equal(uint64(2), id) + id = suite.keeper.GenerateUtxrId(suite.ctx, 0) + suite.Equal(uint64(4), id) deletedUtxrId, err := suite.keeper.DeleteUTXRByRequestId(suite.ctx, 0, "request-2") suite.NoError(err) - suite.Equal(uint64(2), deletedUtxrId) + suite.Equal(uint64(3), deletedUtxrId) has = suite.keeper.HasUTXRByRequestId(suite.ctx, 0, "request-2") suite.False(has) - id = suite.keeper.GetLargestUTXRId(suite.ctx, 0) - suite.Equal(uint64(1), id) + id = suite.keeper.GenerateUtxrId(suite.ctx, 0) + suite.Equal(uint64(5), id) } -func (suite *SettlementTestSuite) TestKeeper_GetLatestUTXR_MultipleTenants() { - t0Id := suite.keeper.GetLargestUTXRId(suite.ctx, 0) +func (suite *SettlementTestSuite) TestKeeper_GenerateUtxrId_MultipleTenants() { + t0Id := suite.keeper.GenerateUtxrId(suite.ctx, 0) suite.Equal(uint64(0), t0Id) - t1Id := suite.keeper.GetLargestUTXRId(suite.ctx, 1) + t1Id := suite.keeper.GenerateUtxrId(suite.ctx, 1) suite.Equal(uint64(0), t1Id) utxrId, err := suite.keeper.CreateUTXR(suite.ctx, 0, &types.UTXR{ @@ -88,8 +88,8 @@ func (suite *SettlementTestSuite) TestKeeper_GetLatestUTXR_MultipleTenants() { has := suite.keeper.HasUTXRByRequestId(suite.ctx, 0, "request-1") suite.True(has) - t0Id = suite.keeper.GetLargestUTXRId(suite.ctx, 0) - suite.Equal(uint64(1), t0Id) + t0Id = suite.keeper.GenerateUtxrId(suite.ctx, 0) + suite.Equal(uint64(2), t0Id) utxrId, err = suite.keeper.CreateUTXR(suite.ctx, 1, &types.UTXR{ RequestId: "request-1", @@ -103,8 +103,8 @@ func (suite *SettlementTestSuite) TestKeeper_GetLatestUTXR_MultipleTenants() { has = suite.keeper.HasUTXRByRequestId(suite.ctx, 1, "request-1") suite.True(has) - t1Id = suite.keeper.GetLargestUTXRId(suite.ctx, 1) - suite.Equal(uint64(1), t1Id) + t1Id = suite.keeper.GenerateUtxrId(suite.ctx, 1) + suite.Equal(uint64(2), t1Id) utxrId, err = suite.keeper.CreateUTXR(suite.ctx, 0, &types.UTXR{ RequestId: "request-2", @@ -113,16 +113,16 @@ func (suite *SettlementTestSuite) TestKeeper_GetLatestUTXR_MultipleTenants() { CreatedAt: uint64(100), }) suite.NoError(err) - suite.Equal(uint64(2), utxrId) + suite.Equal(uint64(3), utxrId) has = suite.keeper.HasUTXRByRequestId(suite.ctx, 0, "request-2") suite.True(has) - t0Id = suite.keeper.GetLargestUTXRId(suite.ctx, 0) - suite.Equal(uint64(2), t0Id) + t0Id = suite.keeper.GenerateUtxrId(suite.ctx, 0) + suite.Equal(uint64(4), t0Id) - t1Id = suite.keeper.GetLargestUTXRId(suite.ctx, 1) - suite.Equal(uint64(1), t1Id) + t1Id = suite.keeper.GenerateUtxrId(suite.ctx, 1) + suite.Equal(uint64(3), t1Id) } func (suite *SettlementTestSuite) TestKeeper_CreateUTXR() { @@ -133,7 +133,7 @@ func (suite *SettlementTestSuite) TestKeeper_CreateUTXR() { CreatedAt: uint64(100), }) suite.NoError(err) - suite.Equal(uint64(1), utxrId) + suite.Equal(uint64(0), utxrId) has := suite.keeper.HasUTXRByRequestId(suite.ctx, 0, "request-1") suite.True(has) @@ -172,7 +172,7 @@ func (suite *SettlementTestSuite) TestKeeper_DeleteUTXRByRequestId() { utxrId, err := suite.keeper.DeleteUTXRByRequestId(suite.ctx, 0, "request-1") suite.NoError(err) - suite.Equal(uint64(1), utxrId) + suite.Equal(uint64(0), utxrId) has := suite.keeper.HasUTXRByRequestId(suite.ctx, 0, "request-1") suite.False(has) @@ -186,7 +186,7 @@ func (suite *SettlementTestSuite) TestKeeper_GetAllUTXRWithTenantAndID() { CreatedAt: uint64(100), }) suite.NoError(err) - suite.Equal(uint64(1), utxrId) + suite.Equal(uint64(0), utxrId) utxrId, err = suite.keeper.CreateUTXR(suite.ctx, 0, &types.UTXR{ RequestId: "request-2", @@ -195,12 +195,12 @@ func (suite *SettlementTestSuite) TestKeeper_GetAllUTXRWithTenantAndID() { CreatedAt: uint64(100), }) suite.NoError(err) - suite.Equal(uint64(2), utxrId) + suite.Equal(uint64(1), utxrId) utxrs := suite.keeper.GetAllUTXRWithTenantAndID(suite.ctx) suite.Equal(2, len(utxrs)) suite.Equal("request-1", utxrs[0].Utxr.RequestId) - suite.Equal(uint64(1), utxrs[0].Id) + suite.Equal(uint64(0), utxrs[0].Id) suite.Equal("request-2", utxrs[1].Utxr.RequestId) - suite.Equal(uint64(2), utxrs[1].Id) + suite.Equal(uint64(1), utxrs[1].Id) } diff --git a/x/settlement/types/keys.go b/x/settlement/types/keys.go index e842608e..7f215813 100644 --- a/x/settlement/types/keys.go +++ b/x/settlement/types/keys.go @@ -31,6 +31,7 @@ var ( UTXRPrefix = []byte{0x00} UTXRRequestIdPrefix = []byte{0x01} TenantPrefix = []byte{0x02} + LastUtxrIdPrefix = []byte{0x03} ) func KeyPrefix(p string) []byte { @@ -53,3 +54,7 @@ func UTXRStoreByRequestIdKey(tenantId uint64, requestId string) []byte { func TenantStoreKey(tenantId uint64) []byte { return append(TenantPrefix, sdk.Uint64ToBigEndian(tenantId)...) } + +func LastUtxrIdStoreKey(tenantId uint64) []byte { + return append(LastUtxrIdPrefix, sdk.Uint64ToBigEndian(tenantId)...) +}