Skip to content

Commit f8c5aed

Browse files
author
abstrct
committed
Sooo close, almost done this refactoring adventure
1 parent 4b0eabe commit f8c5aed

28 files changed

Lines changed: 339 additions & 404 deletions

x/structs/keeper/agreement_cache.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package keeper
22

33
import (
4-
"context"
5-
64
"structs/x/structs/types"
75

86
"cosmossdk.io/math"

x/structs/keeper/allocation.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package keeper
33
import (
44
//"encoding/binary"
55
"context"
6-
"strconv"
76

87
"github.com/cosmos/cosmos-sdk/runtime"
98
"cosmossdk.io/store/prefix"

x/structs/keeper/fleet_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package keeper
22

33
import (
44

5-
"context"
5+
66
//"math"
77

88
sdk "github.com/cosmos/cosmos-sdk/types"

x/structs/keeper/guild_cache.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package keeper
22

33
import (
4-
"context"
54

65
"structs/x/structs/types"
76

x/structs/keeper/guild_membership_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package keeper
22

33
import (
4-
"context"
4+
55

66
"structs/x/structs/types"
77
//sdk "github.com/cosmos/cosmos-sdk/types"

x/structs/keeper/infusion.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (k Keeper) GetInfusionByID(ctx context.Context, infusionId string) (val typ
5656
if len(infusionIdSplit) != 2 {
5757
return types.Infusion{}, false
5858
}
59-
return k.GetInfusion(ctx, infusionIdSplit[0], infusionIdSplit[1])
59+
return k.GetInfusion(ctx, infusionIdSplit[0] + "-" + infusionIdSplit[1], infusionIdSplit[2])
6060
}
6161

6262
// RemoveInfusion removes a infusion from the store
@@ -112,6 +112,24 @@ func (k Keeper) GetAllInfusionsByDestination(ctx context.Context, objectId strin
112112
return
113113
}
114114

115+
116+
// GetAllInfusionsByDestination returns all infusion relating to a struct
117+
func (k Keeper) GetAllInfusionIdsByDestination(ctx context.Context, objectId string) (list []string) {
118+
store := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), InfusionKeyPrefix(objectId))
119+
iterator := storetypes.KVStorePrefixIterator(store, []byte{})
120+
121+
defer iterator.Close()
122+
123+
for ; iterator.Valid(); iterator.Next() {
124+
var val types.Infusion
125+
k.cdc.MustUnmarshal(iterator.Value(), &val)
126+
allocationId := objectId + "-" + val.Address
127+
list = append(list, allocationId)
128+
}
129+
130+
return
131+
}
132+
115133
func (k Keeper) GetInfusionDestructionQueue(ctx context.Context, clear bool) (queue []string) {
116134
infusionDestructionQueueStore := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), types.KeyPrefix(types.InfusionDestructionQueue))
117135
iterator := storetypes.KVStorePrefixIterator(infusionDestructionQueueStore, []byte{})

x/structs/keeper/infusion_cache.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
type InfusionCache struct {
10-
DestinationType types.ObjectType
10+
InfusionId string
1111
DestinationId string
1212
Address string
1313

@@ -44,7 +44,7 @@ func (cache *InfusionCache) Commit() {
4444
func (cache *InfusionCache) IsChanged() bool { return cache.Changed }
4545

4646
func (cache *InfusionCache) ID() string {
47-
return cache.DestinationId + "/" + cache.Address
47+
return cache.InfusionId
4848
}
4949

5050
// =========================================================================
@@ -57,17 +57,6 @@ func (cache *InfusionCache) LoadInfusion() bool {
5757
cache.CC.ctx, cache.DestinationId, cache.Address,
5858
)
5959

60-
if !cache.InfusionLoaded {
61-
cache.Infusion = types.Infusion{
62-
DestinationType: cache.DestinationType,
63-
DestinationId: cache.DestinationId,
64-
Address: cache.Address,
65-
PlayerId: cache.GetOwnerId(),
66-
Commission: math.LegacyZeroDec(),
67-
}
68-
cache.InfusionLoaded = true
69-
}
70-
7160
return cache.InfusionLoaded
7261
}
7362

@@ -82,6 +71,15 @@ func (cache *InfusionCache) GetOwnerId() string {
8271
return cache.Infusion.PlayerId
8372
}
8473

74+
func (cache *InfusionCache) CheckInfusion() error {
75+
if !cache.InfusionLoaded {
76+
if !cache.LoadInfusion() {
77+
return types.NewObjectNotFoundError("infusion", cache.InfusionId)
78+
}
79+
}
80+
return nil
81+
}
82+
8583
func (cache *InfusionCache) GetInfusion() types.Infusion { if !cache.InfusionLoaded { cache.LoadInfusion() }; return cache.Infusion }
8684
func (cache *InfusionCache) GetInfusionId() string { if !cache.InfusionLoaded { cache.LoadInfusion() }; return cache.DestinationId + "-" + cache.Address }
8785
func (cache *InfusionCache) GetPower() uint64 { if !cache.InfusionLoaded { cache.LoadInfusion() }; return cache.Infusion.Power }

x/structs/keeper/infusion_context.go

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,46 @@ package keeper
33
import (
44
"structs/x/structs/types"
55
"strings"
6+
"cosmossdk.io/math"
67
)
78

89

9-
func (cc *CurrentContext) GetInfusionById(infusionKey string) (*InfusionCache, bool) {
10+
func (cc *CurrentContext) GetInfusionById(infusionKey string) *InfusionCache {
11+
1012
if cache, exists := cc.infusions[infusionKey]; exists {
11-
return cache, exists
13+
return cache
14+
}
15+
16+
infusionIdSplit := strings.Split(infusionKey, "-")
17+
if len(infusionIdSplit) != 3 {
18+
return &InfusionCache{}
19+
}
20+
21+
destinationId := infusionIdSplit[0] + "-" + infusionIdSplit[1]
22+
address := infusionIdSplit[2]
23+
24+
cc.infusions[infusionKey] = &InfusionCache{
25+
InfusionId: infusionKey,
26+
DestinationId: destinationId,
27+
Address: address,
28+
CC: cc,
29+
DestinationFuelAttributeId: GetGridAttributeIDByObjectId(types.GridAttributeType_fuel, destinationId),
30+
DestinationCapacityAttributeId: GetGridAttributeIDByObjectId(types.GridAttributeType_capacity, destinationId),
1231
}
1332

14-
return &InfusionCache{}, false
33+
return cc.infusions[infusionKey]
1534
}
1635

1736

18-
func (cc *CurrentContext) GetInfusion(destinationType types.ObjectType, destinationId string, address string) *InfusionCache {
19-
infusionKey := destinationId + "/" + address
37+
func (cc *CurrentContext) GetInfusion(destinationId string, address string) *InfusionCache {
38+
infusionKey := destinationId + "-" + address
2039

2140
if cache, exists := cc.infusions[infusionKey]; exists {
2241
return cache
2342
}
2443

2544
cc.infusions[infusionKey] = &InfusionCache{
26-
DestinationType: destinationType,
45+
InfusionId: infusionKey,
2746
DestinationId: destinationId,
2847
Address: address,
2948
CC: cc,
@@ -34,6 +53,34 @@ func (cc *CurrentContext) GetInfusion(destinationType types.ObjectType, destinat
3453
return cc.infusions[infusionKey]
3554
}
3655

56+
func (cc *CurrentContext) GetAllInfusionByDestination(destinationId string) (infusions []*InfusionCache) {
57+
infusionIds := cc.k.GetAllInfusionIdsByDestination(cc.ctx, destinationId)
58+
for _, infusionId := range infusionIds {
59+
infusion := cc.GetInfusionById(infusionId)
60+
infusions = append(infusions, infusion)
61+
}
62+
return
63+
}
64+
65+
func (cc *CurrentContext) UpsertInfusion(destinationType types.ObjectType, destinationId string, address string, playerId string) (*InfusionCache){
66+
infusion := cc.GetInfusion(destinationId, address)
67+
68+
if infusion.CheckInfusion() != nil {
69+
infusion.Infusion = types.Infusion{
70+
DestinationId: destinationId,
71+
DestinationType: destinationType,
72+
Address: address,
73+
PlayerId: playerId,
74+
Commission: math.LegacyZeroDec(),
75+
}
76+
77+
infusion.InfusionLoaded = true
78+
infusion.Changed = true
79+
}
80+
return infusion
81+
}
82+
83+
3784
func (cc *CurrentContext) ProcessInfusionDestructionQueue() {
3885
for {
3986
queue := cc.k.GetInfusionDestructionQueue(cc.ctx, true)
@@ -42,28 +89,18 @@ func (cc *CurrentContext) ProcessInfusionDestructionQueue() {
4289
}
4390

4491
for _, infusionId := range queue {
45-
// infusionId format: "destinationId-address" (e.g. "3-1-cosmos1abc...")
46-
// destinationId itself contains "-", so split on the last dash
47-
lastDash := strings.LastIndex(infusionId, "-")
48-
if lastDash == -1 {
49-
continue
50-
}
51-
destinationId := infusionId[:lastDash]
52-
address := infusionId[lastDash+1:]
53-
54-
infusion, found := cc.k.GetInfusion(cc.ctx, destinationId, address)
55-
if found && infusion.Power == 0 && infusion.Defusing == 0 {
56-
cache := cc.GetInfusion(infusion.DestinationType, destinationId, address)
57-
cache.Destroy()
92+
infusion := cc.GetInfusionById(infusionId)
93+
if (infusion.CheckInfusion() == nil && infusion.GetInfusion().Power == 0 && infusion.GetInfusion().Defusing == 0) {
94+
infusion.Destroy()
5895
}
5996
}
6097
}
6198
}
6299

63100
func (cc *CurrentContext) DestroyAllInfusions(infusionIds []string) {
64101
for _, infusionId := range infusionIds {
65-
infusion, found := cc.GetInfusionById(infusionId)
66-
if found {
102+
infusion := cc.GetInfusionById(infusionId)
103+
if infusion.CheckInfusion() == nil {
67104
infusion.Destroy()
68105
}
69106
}

x/structs/keeper/msg_server_struct_generator_infuse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (k msgServer) StructGeneratorInfuse(goCtx context.Context, msg *types.MsgSt
7878
}
7979
k.bankKeeper.BurnCoins(ctx, types.ModuleName, infusionAmount)
8080

81-
infusion := cc.GetInfusion(types.ObjectType_struct, structure.GetStructId(), callingPlayer.GetPrimaryAddress())
81+
infusion := cc.UpsertInfusion(types.ObjectType_struct, structure.GetStructId(), callingPlayer.GetPrimaryAddress(), callingPlayer.GetPlayerId())
8282

8383
infusion.SetRatio(structure.GetStructType().GeneratingRate)
8484
infusion.SetCommission(math.LegacyZeroDec())

x/structs/keeper/planet_attribute_cache.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package keeper
22

3-
import (
4-
"structs/x/structs/types"
5-
)
63

74
// attributeCache holds a single attribute value with change tracking.
85
type PlanetAttributeCache struct {
@@ -11,6 +8,7 @@ type PlanetAttributeCache struct {
118
Value uint64
129
Loaded bool
1310
Changed bool
11+
Deleted bool
1412
}
1513

1614

0 commit comments

Comments
 (0)