Skip to content

Commit

Permalink
fix: add unique constraints to utxt_id and request_id
Browse files Browse the repository at this point in the history
  • Loading branch information
devbv committed Apr 30, 2024
1 parent bcb0b9b commit 11d45a8
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 47 deletions.
6 changes: 3 additions & 3 deletions x/settlement/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
6 changes: 3 additions & 3 deletions x/settlement/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion x/settlement/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
20 changes: 9 additions & 11 deletions x/settlement/keeper/utxr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
}

Expand Down
58 changes: 29 additions & 29 deletions x/settlement/keeper/utxr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{
Expand All @@ -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",
Expand All @@ -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{
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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() {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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",
Expand All @@ -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)
}
5 changes: 5 additions & 0 deletions x/settlement/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
UTXRPrefix = []byte{0x00}
UTXRRequestIdPrefix = []byte{0x01}
TenantPrefix = []byte{0x02}
LastUtxrIdPrefix = []byte{0x03}
)

func KeyPrefix(p string) []byte {
Expand All @@ -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)...)
}

0 comments on commit 11d45a8

Please sign in to comment.