Skip to content

Commit 444f094

Browse files
committed
refactor(x/core): refactor data request indexing
- Fixes a bug where status transition from committing to tallying was not considered as a possibility. - Committing, revealing, and tallying are combined into a single collection with different prefixes. - DataRequestStatus enum type now follows proto3 requirements. - Invalid status transition is now identified as an error in UpdateDataRequestIndexing. - DataRequest's TimeoutHeight is set to -1 if it is not in timeout queue.
1 parent 1fafad9 commit 444f094

File tree

12 files changed

+261
-232
lines changed

12 files changed

+261
-232
lines changed

interchaintest/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/sedaprotocol/seda-chain/interchaintest
22

3-
go 1.23.5
3+
go 1.24.6
44

55
replace (
66
github.com/ChainSafe/go-schnorrkel => github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d

proto/sedachain/core/v1/core.proto

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package sedachain.core.v1;
44
import "gogoproto/gogo.proto";
55
import "amino/amino.proto";
66
import "cosmos_proto/cosmos.proto";
7-
import "cosmos/base/v1beta1/coin.proto";
87

98
option go_package = "github.com/sedaprotocol/seda-chain/x/core/types";
109

@@ -84,7 +83,8 @@ message DataRequest {
8483
(gogoproto.nullable) = false,
8584
(amino.dont_omitempty) = true
8685
];
87-
// Timeout height of the data request
86+
// Timeout height of the data request (-1 if not set, i.e., under tallying
87+
// status)
8888
int64 timeout_height = 21;
8989
// Status of the data request
9090
DataRequestStatus status = 22;
@@ -94,9 +94,16 @@ message DataRequest {
9494
enum DataRequestStatus {
9595
option (gogoproto.goproto_enum_prefix) = false;
9696

97-
DATA_REQUEST_COMMITTING = 0;
98-
DATA_REQUEST_REVEALING = 1;
99-
DATA_REQUEST_TALLYING = 2;
97+
// Zero value as required by proto3.
98+
DATA_REQUEST_STATUS_UNSPECIFIED = 0;
99+
// Data request has been posted and is collecting commits.
100+
DATA_REQUEST_STATUS_COMMITTING = 1;
101+
// The number of commits has reached the replication factor, and the data
102+
// request is now collecting reveals.
103+
DATA_REQUEST_STATUS_REVEALING = 2;
104+
// The number of reveals has reached the replication factor, and the data
105+
// request is now ready to be tallied.
106+
DATA_REQUEST_STATUS_TALLYING = 3;
100107
}
101108

102109
// RevealBody is the content of a reveal.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package keeper
2+
3+
import (
4+
"cosmossdk.io/collections"
5+
6+
sdk "github.com/cosmos/cosmos-sdk/types"
7+
8+
"github.com/sedaprotocol/seda-chain/x/core/types"
9+
)
10+
11+
func (k Keeper) GetTallyingDataRequestIDs(ctx sdk.Context) ([]string, error) {
12+
rng := collections.NewPrefixedPairRange[types.DataRequestStatus, types.DataRequestIndex](types.DATA_REQUEST_STATUS_TALLYING)
13+
14+
iter, err := k.dataRequestIndexing.Iterate(ctx, rng)
15+
if err != nil {
16+
return nil, err
17+
}
18+
defer iter.Close()
19+
20+
var ids []string
21+
for ; iter.Valid(); iter.Next() {
22+
key, err := iter.Key()
23+
if err != nil {
24+
return nil, err
25+
}
26+
ids = append(ids, key.K2().DrID())
27+
}
28+
return ids, nil
29+
}
30+
31+
func (k Keeper) UpdateDataRequestIndexing(ctx sdk.Context, index types.DataRequestIndex, currentStatus, newStatus types.DataRequestStatus) error {
32+
// Check the logic of the status transition, which follows:
33+
// Unspecified (addition) -> Committing -> Revealing -> Tallying -> Unspecified (removal)
34+
// except that in case of timeout, Committing -> Tallying is also possible.
35+
switch currentStatus {
36+
case types.DATA_REQUEST_STATUS_UNSPECIFIED:
37+
if newStatus != types.DATA_REQUEST_STATUS_COMMITTING {
38+
return types.ErrInvalidStatusTransition.Wrapf("%s -> %s", currentStatus, newStatus)
39+
}
40+
41+
case types.DATA_REQUEST_STATUS_COMMITTING:
42+
if newStatus != types.DATA_REQUEST_STATUS_REVEALING &&
43+
newStatus != types.DATA_REQUEST_STATUS_TALLYING {
44+
return types.ErrInvalidStatusTransition.Wrapf("%s -> %s", currentStatus, newStatus)
45+
}
46+
47+
case types.DATA_REQUEST_STATUS_REVEALING:
48+
if newStatus != types.DATA_REQUEST_STATUS_TALLYING {
49+
return types.ErrInvalidStatusTransition.Wrapf("%s -> %s", currentStatus, newStatus)
50+
}
51+
52+
case types.DATA_REQUEST_STATUS_TALLYING:
53+
if newStatus != types.DATA_REQUEST_STATUS_UNSPECIFIED {
54+
return types.ErrInvalidStatusTransition.Wrapf("%s -> %s", currentStatus, newStatus)
55+
}
56+
}
57+
58+
if currentStatus != types.DATA_REQUEST_STATUS_UNSPECIFIED {
59+
exists, err := k.dataRequestIndexing.Has(ctx, collections.Join(currentStatus, index))
60+
if err != nil {
61+
return err
62+
}
63+
if !exists {
64+
return types.ErrDataRequestNotFoundInIndex.Wrapf("data request ID %s, status %s", index.DrID(), currentStatus)
65+
}
66+
err = k.dataRequestIndexing.Remove(ctx, collections.Join(currentStatus, index))
67+
if err != nil {
68+
return err
69+
}
70+
}
71+
72+
if newStatus != types.DATA_REQUEST_STATUS_UNSPECIFIED {
73+
err := k.dataRequestIndexing.Set(ctx, collections.Join(newStatus, index))
74+
if err != nil {
75+
return err
76+
}
77+
}
78+
return nil
79+
}

x/core/keeper/dr_indexing.go

Lines changed: 0 additions & 65 deletions
This file was deleted.

x/core/keeper/endblock.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,12 @@ func (k Keeper) ProcessTallies(ctx sdk.Context) error {
113113
return err
114114
}
115115

116-
err = k.RemoveFromTallying(ctx, dr.Index())
116+
err = k.UpdateDataRequestIndexing(ctx, dr.Index(), dr.Status, types.DATA_REQUEST_STATUS_UNSPECIFIED)
117117
if err != nil {
118118
return err
119119
}
120+
dr.Status = types.DATA_REQUEST_STATUS_UNSPECIFIED
121+
120122
err = k.RemoveRevealBodies(ctx, dr.Id)
121123
if err != nil {
122124
return err

x/core/keeper/keeper.go

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
88

99
"cosmossdk.io/collections"
10-
collcodec "cosmossdk.io/collections/codec"
10+
collcdc "cosmossdk.io/collections/codec"
1111
storetypes "cosmossdk.io/core/store"
1212
"cosmossdk.io/log"
1313

@@ -40,12 +40,8 @@ type Keeper struct {
4040
dataRequests collections.Map[string, types.DataRequest]
4141
// revealBodies is a map of data request IDs and executor public keys to reveal bodies.
4242
revealBodies collections.Map[collections.Pair[string, string], types.RevealBody]
43-
// committing is a set of data request indices that are being committed.
44-
committing collections.KeySet[types.DataRequestIndex]
45-
// revealing is a set of data request indices that are being revealed.
46-
revealing collections.KeySet[types.DataRequestIndex]
47-
// tallying is a set of data request indices that are being tallied.
48-
tallying collections.KeySet[types.DataRequestIndex]
43+
// dataRequestIndexing is a set of data request indices under different statuses.
44+
dataRequestIndexing collections.KeySet[collections.Pair[types.DataRequestStatus, types.DataRequestIndex]]
4945
// timeoutQueue is a queue of data request IDs and their timeout heights.
5046
timeoutQueue collections.KeySet[collections.Pair[int64, string]]
5147

@@ -69,23 +65,21 @@ func NewKeeper(
6965
sb := collections.NewSchemaBuilder(storeService)
7066

7167
k := Keeper{
72-
wasmStorageKeeper: wsk,
73-
batchingKeeper: batk,
74-
dataProxyKeeper: dpk,
75-
stakingKeeper: sk,
76-
bankKeeper: bank,
77-
wasmKeeper: wk,
78-
wasmViewKeeper: wvk,
79-
authority: authority,
80-
allowlist: collections.NewKeySet(sb, types.AllowlistKey, "allowlist", collections.StringKey),
81-
stakers: collections.NewMap(sb, types.StakersKeyPrefix, "stakers", collections.StringKey, codec.CollValue[types.Staker](cdc)),
82-
dataRequests: collections.NewMap(sb, types.DataRequestsKeyPrefix, "data_requests", collections.StringKey, codec.CollValue[types.DataRequest](cdc)),
83-
revealBodies: collections.NewMap(sb, types.RevealBodiesKeyPrefix, "reveals", collections.PairKeyCodec(collections.StringKey, collections.StringKey), codec.CollValue[types.RevealBody](cdc)),
84-
committing: collections.NewKeySet(sb, types.CommittingKeyPrefix, "committing", collcodec.NewBytesKey[types.DataRequestIndex]()),
85-
revealing: collections.NewKeySet(sb, types.RevealingKeyPrefix, "revealing", collcodec.NewBytesKey[types.DataRequestIndex]()),
86-
tallying: collections.NewKeySet(sb, types.TallyingKeyPrefix, "tallying", collcodec.NewBytesKey[types.DataRequestIndex]()),
87-
timeoutQueue: collections.NewKeySet(sb, types.TimeoutQueueKeyPrefix, "timeout_queue", collections.PairKeyCodec(collections.Int64Key, collections.StringKey)),
88-
params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
68+
wasmStorageKeeper: wsk,
69+
batchingKeeper: batk,
70+
dataProxyKeeper: dpk,
71+
stakingKeeper: sk,
72+
bankKeeper: bank,
73+
wasmKeeper: wk,
74+
wasmViewKeeper: wvk,
75+
authority: authority,
76+
allowlist: collections.NewKeySet(sb, types.AllowlistKey, "allowlist", collections.StringKey),
77+
stakers: collections.NewMap(sb, types.StakersKeyPrefix, "stakers", collections.StringKey, codec.CollValue[types.Staker](cdc)),
78+
dataRequests: collections.NewMap(sb, types.DataRequestsKeyPrefix, "data_requests", collections.StringKey, codec.CollValue[types.DataRequest](cdc)),
79+
revealBodies: collections.NewMap(sb, types.RevealBodiesKeyPrefix, "reveals", collections.PairKeyCodec(collections.StringKey, collections.StringKey), codec.CollValue[types.RevealBody](cdc)),
80+
dataRequestIndexing: collections.NewKeySet(sb, types.DrIndexingKeyPrefix, "data_request_indexing", collections.PairKeyCodec(collcdc.NewInt32Key[types.DataRequestStatus](), collcdc.NewBytesKey[types.DataRequestIndex]())),
81+
timeoutQueue: collections.NewKeySet(sb, types.TimeoutQueueKeyPrefix, "timeout_queue", collections.PairKeyCodec(collections.Int64Key, collections.StringKey)),
82+
params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
8983
}
9084

9185
schema, err := sb.Build()

x/core/keeper/msg_server_dr.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,22 @@ func (m msgServer) PostDataRequest(goCtx context.Context, msg *types.MsgPostData
9292
Poster: msg.Sender,
9393
Escrow: msg.Funds.Amount,
9494
TimeoutHeight: ctx.BlockHeight() + int64(drConfig.CommitTimeoutInBlocks),
95-
Status: types.DATA_REQUEST_COMMITTING,
9695
// Commits: make(map[string][]byte), // Dropped by proto anyways
9796
// Reveals: make(map[string]bool), // Dropped by proto anyways
9897
}
9998

100-
err = m.SetDataRequest(ctx, dr)
99+
err = m.UpdateDataRequestIndexing(ctx, dr.Index(), dr.Status, types.DATA_REQUEST_STATUS_COMMITTING)
101100
if err != nil {
102101
return nil, err
103102
}
104-
err = m.AddToCommitting(ctx, dr.Index())
103+
dr.Status = types.DATA_REQUEST_STATUS_COMMITTING
104+
105+
err = m.AddToTimeoutQueue(ctx, drID, dr.TimeoutHeight)
105106
if err != nil {
106107
return nil, err
107108
}
108-
err = m.AddToTimeoutQueue(ctx, drID, dr.TimeoutHeight)
109+
110+
err = m.SetDataRequest(ctx, dr)
109111
if err != nil {
110112
return nil, err
111113
}
@@ -130,7 +132,7 @@ func (m msgServer) Commit(goCtx context.Context, msg *types.MsgCommit) (*types.M
130132
}
131133

132134
// Verify the data request status.
133-
if dr.Status != types.DATA_REQUEST_COMMITTING {
135+
if dr.Status != types.DATA_REQUEST_STATUS_COMMITTING {
134136
return nil, types.ErrNotCommitting
135137
}
136138
if _, ok := dr.Commits[msg.PublicKey]; ok {
@@ -175,19 +177,18 @@ func (m msgServer) Commit(goCtx context.Context, msg *types.MsgCommit) (*types.M
175177
dr.AddCommit(msg.PublicKey, commit)
176178

177179
if len(dr.Commits) >= int(dr.ReplicationFactor) {
178-
dr.Status = types.DATA_REQUEST_REVEALING
179-
180180
newTimeoutHeight := dr.TimeoutHeight + int64(params.DataRequestConfig.RevealTimeoutInBlocks)
181181
err = m.UpdateDataRequestTimeout(ctx, msg.DrId, dr.TimeoutHeight, newTimeoutHeight)
182182
if err != nil {
183183
return nil, err
184184
}
185185
dr.TimeoutHeight = newTimeoutHeight
186186

187-
err = m.CommittingToRevealing(ctx, dr.Index())
187+
err = m.UpdateDataRequestIndexing(ctx, dr.Index(), dr.Status, types.DATA_REQUEST_STATUS_REVEALING)
188188
if err != nil {
189189
return nil, err
190190
}
191+
dr.Status = types.DATA_REQUEST_STATUS_REVEALING
191192
}
192193

193194
err = m.SetDataRequest(ctx, dr)
@@ -209,7 +210,7 @@ func (m msgServer) Reveal(goCtx context.Context, msg *types.MsgReveal) (*types.M
209210
if err != nil {
210211
return nil, err
211212
}
212-
if dr.Status != types.DATA_REQUEST_REVEALING {
213+
if dr.Status != types.DATA_REQUEST_STATUS_REVEALING {
213214
return nil, types.ErrRevealNotStarted
214215
}
215216
if dr.TimeoutHeight <= ctx.BlockHeight() {
@@ -271,17 +272,16 @@ func (m msgServer) Reveal(goCtx context.Context, msg *types.MsgReveal) (*types.M
271272

272273
revealsCount := dr.MarkAsRevealed(msg.PublicKey)
273274
if revealsCount >= int(dr.ReplicationFactor) {
274-
dr.Status = types.DATA_REQUEST_TALLYING
275-
276275
err = m.RemoveFromTimeoutQueue(ctx, dr.Id, dr.TimeoutHeight)
277276
if err != nil {
278277
return nil, err
279278
}
280279

281-
err = m.RevealingToTallying(ctx, dr.Index())
280+
err = m.UpdateDataRequestIndexing(ctx, dr.Index(), dr.Status, types.DATA_REQUEST_STATUS_TALLYING)
282281
if err != nil {
283282
return nil, err
284283
}
284+
dr.Status = types.DATA_REQUEST_STATUS_TALLYING
285285
}
286286

287287
err = m.SetRevealBody(ctx, msg.PublicKey, *msg.RevealBody)

x/core/keeper/timeout_queue.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ func (k Keeper) ExpireDataRequests(ctx sdk.Context) error {
6262
return err
6363
}
6464

65-
dr.Status = types.DATA_REQUEST_TALLYING
66-
67-
err = k.RevealingToTallying(ctx, dr.Index())
65+
err = k.UpdateDataRequestIndexing(ctx, dr.Index(), dr.Status, types.DATA_REQUEST_STATUS_TALLYING)
6866
if err != nil {
6967
return err
7068
}
69+
dr.Status = types.DATA_REQUEST_STATUS_TALLYING
70+
dr.TimeoutHeight = -1
71+
7172
err = k.SetDataRequest(ctx, dr)
7273
if err != nil {
7374
return err

0 commit comments

Comments
 (0)