Skip to content

Commit 0f8b16f

Browse files
author
abstrct
committed
Updated allocation transactions
1 parent 9f83daf commit 0f8b16f

5 files changed

Lines changed: 48 additions & 38 deletions

x/structs/keeper/allocation_cache.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ func (cache *AllocationCache) GetObjectLoad(objectId string) (uint64) {
9797
return cache.CC.GetGridAttribute(objectLoadAttributeId)
9898
}
9999

100+
func (cache *AllocationCache) SetController(address string) () {
101+
if !cache.Loaded {
102+
cache.LoadAllocation()
103+
}
104+
cache.Allocation.Controller = address
105+
cache.Changed = true
106+
}
107+
100108
func (cache *AllocationCache) SetAllocationSourceObjectId(sourceObjectId string) (bool) {
101109
if ! cache.Loaded {
102110
if ! cache.LoadAllocation() {

x/structs/keeper/msg_server_allocation_create.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,40 @@ func (k msgServer) AllocationCreate(goCtx context.Context, msg *types.MsgAllocat
2020
msg.Controller = msg.Creator
2121
}
2222

23-
allocation := types.CreateAllocationStub(msg.AllocationType, msg.SourceObjectId, msg.Creator, msg.Controller)
24-
25-
player, playerFound := k.GetPlayerFromIndex(ctx, k.GetPlayerIndexFromAddress(ctx, msg.Creator))
26-
if (!playerFound) {
23+
player, playerErr := cc.GetPlayerByAddress(msg.Creator)
24+
if playerErr != nil {
2725
return &types.MsgAllocationCreateResponse{}, types.NewPlayerRequiredError(msg.Creator, "allocation_create")
2826
}
2927

30-
sourceObjectPermissionId := GetObjectPermissionIDBytes(msg.SourceObjectId, player.Id)
28+
sourceObjectPermissionId := GetObjectPermissionIDBytes(msg.SourceObjectId, player.GetPlayerId())
3129
addressPermissionId := GetAddressPermissionIDBytes(msg.Creator)
3230

3331
// Ignore the one case where it's a player creating an allocation on themselves.
3432
// Surely that doesn't need a lookup.
35-
if (player.Id != msg.SourceObjectId) {
33+
if (player.GetPlayerId() != msg.SourceObjectId) {
3634
// check that the player has permissions
37-
if (!k.PermissionHasOneOf(ctx, sourceObjectPermissionId, types.PermissionAssets)) {
38-
return &types.MsgAllocationCreateResponse{}, types.NewPermissionError("player", player.Id, "allocation", msg.SourceObjectId, uint64(types.PermissionAssets), "allocation_create")
35+
if (!cc.PermissionHasOneOf(sourceObjectPermissionId, types.PermissionAssets)) {
36+
return &types.MsgAllocationCreateResponse{}, types.NewPermissionError("player", player.GetPlayerId(), "allocation", msg.SourceObjectId, uint64(types.PermissionAssets), "allocation_create")
3937
}
4038
}
4139

4240

4341
// check that the account has energy management permissions
44-
if (!k.PermissionHasOneOf(ctx, addressPermissionId, types.Permission(types.PermissionAssets))) {
42+
if (!cc.PermissionHasOneOf(addressPermissionId, types.Permission(types.PermissionAssets))) {
4543
return &types.MsgAllocationCreateResponse{}, types.NewPermissionError("address", msg.Creator, "", "", uint64(types.PermissionAssets), "energy_management")
4644
}
4745

48-
_ = cc
49-
50-
allocation, _ , err := k.AppendAllocation(ctx, allocation, msg.Power)
46+
allocation, err := cc.NewAllocation(
47+
msg.AllocationType,
48+
msg.SourceObjectId,
49+
"",
50+
msg.Creator,
51+
msg.Controller,
52+
msg.Power,
53+
)
5154

5255
return &types.MsgAllocationCreateResponse{
53-
AllocationId: allocation.Id,
56+
AllocationId: allocation.GetAllocationId(),
5457
}, err
5558

5659
}

x/structs/keeper/msg_server_allocation_delete.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func (k msgServer) AllocationDelete(goCtx context.Context, msg *types.MsgAllocat
1515
// indexer for UI requirements
1616
k.AddressEmitActivity(ctx, msg.Creator)
1717

18-
allocation, allocationFound := k.GetAllocation(ctx, msg.AllocationId)
18+
allocation, allocationFound := cc.GetAllocation(msg.AllocationId)
1919
if (!allocationFound) {
2020
return &types.MsgAllocationDeleteResponse{}, types.NewObjectNotFoundError("allocation", msg.AllocationId)
2121
}
@@ -25,29 +25,29 @@ func (k msgServer) AllocationDelete(goCtx context.Context, msg *types.MsgAllocat
2525
return &types.MsgAllocationDeleteResponse{}, types.NewPlayerRequiredError(msg.Creator, "allocation_delete")
2626
}
2727

28-
sourceObjectPermissionId := GetObjectPermissionIDBytes(allocation.SourceObjectId, player.PlayerId)
28+
sourceObjectPermissionId := GetObjectPermissionIDBytes(allocation.GetAllocation().SourceObjectId, player.GetPlayerId())
2929
addressPermissionId := GetAddressPermissionIDBytes(msg.Creator)
3030

3131
// Ignore the one case where it's a player creating an allocation on themselves.
3232
// Surely that doesn't need a lookup.
33-
if (player.PlayerId != allocation.SourceObjectId) {
33+
if (player.GetPlayerId() != allocation.GetAllocation().SourceObjectId) {
3434
// check that the player has permissions
35-
if (!k.PermissionHasOneOf(ctx, sourceObjectPermissionId, types.PermissionAssets)) {
36-
return &types.MsgAllocationDeleteResponse{}, types.NewPermissionError("player", player.PlayerId, "allocation", allocation.SourceObjectId, uint64(types.PermissionAssets), "allocation_delete")
35+
if (!cc.PermissionHasOneOf(sourceObjectPermissionId, types.PermissionAssets)) {
36+
return &types.MsgAllocationDeleteResponse{}, types.NewPermissionError("player", player.GetPlayerId(), "allocation", allocation.GetAllocation().SourceObjectId, uint64(types.PermissionAssets), "allocation_delete")
3737
}
3838
}
3939

4040
// check that the account has energy management permissions
41-
if (!k.PermissionHasOneOf(ctx, addressPermissionId, types.Permission(types.PermissionAssets))) {
41+
if (!cc.PermissionHasOneOf(addressPermissionId, types.Permission(types.PermissionAssets))) {
4242
return &types.MsgAllocationDeleteResponse{}, types.NewPermissionError("address", msg.Creator, "", "", uint64(types.PermissionAssets), "energy_management")
4343
}
4444

4545

46-
if (allocation.Type != types.AllocationType_dynamic) {
47-
return &types.MsgAllocationDeleteResponse{}, types.NewAllocationError(allocation.SourceObjectId, "immutable_type").WithFieldChange("type", allocation.Type.String(), "dynamic")
46+
if (allocation.GetAllocation().Type != types.AllocationType_dynamic) {
47+
return &types.MsgAllocationDeleteResponse{}, types.NewAllocationError(allocation.GetAllocation().SourceObjectId, "immutable_type").WithFieldChange("type", allocation.GetAllocation().Type.String(), "dynamic")
4848
}
4949

50-
k.DestroyAllocation(ctx, msg.AllocationId)
50+
allocation.Destroy()
5151

5252
return &types.MsgAllocationDeleteResponse{
5353
AllocationId: msg.AllocationId,

x/structs/keeper/msg_server_allocation_transfer.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,28 @@ func (k msgServer) AllocationTransfer(goCtx context.Context, msg *types.MsgAlloc
1010
ctx := sdk.UnwrapSDKContext(goCtx)
1111
cc := k.NewCurrentContext(ctx)
1212
defer cc.CommitAll()
13-
_ = cc
1413

1514
// Add an Active Address record to the
1615
// indexer for UI requirements
1716
k.AddressEmitActivity(ctx, msg.Creator)
1817

1918
// Check permissions on the substation
2019

21-
allocation, allocationFound := k.GetAllocation(ctx, msg.AllocationId)
20+
allocation, allocationFound := cc.GetAllocation(msg.AllocationId)
2221
if (!allocationFound) {
2322
return &types.MsgAllocationTransferResponse{}, types.NewObjectNotFoundError("allocation", msg.AllocationId)
2423
}
2524

26-
if (allocation.Controller != msg.Creator) {
25+
// TODO Allow for other addresses from a player to control it too
26+
if (allocation.GetAllocation().Controller != msg.Creator) {
2727
return &types.MsgAllocationTransferResponse{}, types.NewPermissionError("address", msg.Creator, "allocation", msg.AllocationId, uint64(types.PermissionAssets), "allocation_transfer")
2828
}
2929

30-
if (allocation.DestinationId != "") {
31-
return &types.MsgAllocationTransferResponse{}, types.NewAllocationError(msg.AllocationId, "connected").WithDestination(allocation.DestinationId)
30+
if (allocation.GetAllocation().DestinationId != "") {
31+
return &types.MsgAllocationTransferResponse{}, types.NewAllocationError(msg.AllocationId, "connected").WithDestination(allocation.GetAllocation().DestinationId)
3232
}
3333

34-
allocation.Controller = msg.Controller
35-
allocation, _ = k.SetAllocationOnly(ctx, allocation)
34+
allocation.SetController(msg.Controller)
3635

3736
return &types.MsgAllocationTransferResponse{
3837
AllocationId: msg.AllocationId,

x/structs/keeper/msg_server_allocation_update.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func (k msgServer) AllocationUpdate(goCtx context.Context, msg *types.MsgAllocat
1515
// indexer for UI requirements
1616
k.AddressEmitActivity(ctx, msg.Creator)
1717

18-
allocation, allocationFound := k.GetAllocation(ctx, msg.AllocationId)
18+
allocation, allocationFound := cc.GetAllocation(msg.AllocationId)
1919
if (!allocationFound) {
2020
return &types.MsgAllocationUpdateResponse{}, types.NewObjectNotFoundError("allocation", msg.AllocationId)
2121
}
@@ -25,33 +25,33 @@ func (k msgServer) AllocationUpdate(goCtx context.Context, msg *types.MsgAllocat
2525
return &types.MsgAllocationUpdateResponse{}, types.NewPlayerRequiredError(msg.Creator, "allocation_update")
2626
}
2727

28-
sourceObjectPermissionId := GetObjectPermissionIDBytes(allocation.SourceObjectId, player.PlayerId)
28+
sourceObjectPermissionId := GetObjectPermissionIDBytes(allocation.GetAllocation().SourceObjectId, player.GetPlayerId())
2929
addressPermissionId := GetAddressPermissionIDBytes(msg.Creator)
3030

3131
// Ignore the one case where it's a player creating an allocation on themselves.
3232
// Surely that doesn't need a lookup.
33-
if (player.PlayerId != allocation.SourceObjectId) {
33+
if (player.GetPlayerId() != allocation.GetAllocation().SourceObjectId) {
3434
// check that the player has permissions
35-
if (!k.PermissionHasOneOf(ctx, sourceObjectPermissionId, types.PermissionAssets)) {
36-
return &types.MsgAllocationUpdateResponse{}, types.NewPermissionError("player", player.PlayerId, "allocation", allocation.SourceObjectId, uint64(types.PermissionAssets), "allocation_update")
35+
if (!cc.PermissionHasOneOf(sourceObjectPermissionId, types.PermissionAssets)) {
36+
return &types.MsgAllocationUpdateResponse{}, types.NewPermissionError("player", player.GetPlayerId(), "allocation", allocation.GetAllocation().SourceObjectId, uint64(types.PermissionAssets), "allocation_update")
3737
}
3838
}
3939

4040
// check that the account has energy management permissions
41-
if (!k.PermissionHasOneOf(ctx, addressPermissionId, types.Permission(types.PermissionAssets))) {
41+
if (!cc.PermissionHasOneOf(addressPermissionId, types.Permission(types.PermissionAssets))) {
4242
return &types.MsgAllocationUpdateResponse{}, types.NewPermissionError("address", msg.Creator, "", "", uint64(types.PermissionAssets), "energy_management")
4343
}
4444

4545

46-
if (allocation.Type != types.AllocationType_dynamic) {
47-
return &types.MsgAllocationUpdateResponse{}, types.NewAllocationError(allocation.SourceObjectId, "immutable_type").WithFieldChange("type", allocation.Type.String(), "dynamic")
46+
if (allocation.GetAllocation().Type != types.AllocationType_dynamic) {
47+
return &types.MsgAllocationUpdateResponse{}, types.NewAllocationError(allocation.GetAllocation().SourceObjectId, "immutable_type").WithFieldChange("type", allocation.GetAllocation().Type.String(), "dynamic")
4848
}
4949

5050
if (msg.Power == 0) {
5151
return &types.MsgAllocationUpdateResponse{}, types.NewParameterValidationError("power", 0, "below_minimum").WithRange(1, 0)
5252
}
5353

54-
allocation, _, err = k.SetAllocation(ctx, allocation, msg.Power)
54+
allocation.SetDynamicPower(msg.Power)
5555

5656
return &types.MsgAllocationUpdateResponse{
5757
AllocationId: msg.AllocationId,

0 commit comments

Comments
 (0)