Skip to content

Commit a1d7be0

Browse files
authored
fix: Blame index update (#1264)
* initial commit * added queries and unit tests * added cli * fix parse error * fix parse error 2 * fix lint and test errors * ran make generate * update index for keygen * refactor query name * refactor key calculation * refactor lib name
1 parent ed35305 commit a1d7be0

File tree

15 files changed

+848
-130
lines changed

15 files changed

+848
-130
lines changed

cmd/zetaclientd/keygen_tss.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ func keygenTss(cfg *config.Config, tss *mc.TSS, keygenLogger zerolog.Logger) err
134134
if err != nil {
135135
return err
136136
}
137-
zetaHash, err := tss.CoreBridge.PostBlameData(&res.Blame, common.ZetaChain().ChainId, digest)
137+
index := fmt.Sprintf("keygen-%s-%d", digest, keyGen.BlockNumber)
138+
zetaHash, err := tss.CoreBridge.PostBlameData(&res.Blame, common.ZetaChain().ChainId, index)
138139
if err != nil {
139140
keygenLogger.Error().Err(err).Msg("error sending blame data to core")
140141
return err

docs/openapi/openapi.swagger.yaml

+34
Original file line numberDiff line numberDiff line change
@@ -27295,6 +27295,32 @@ paths:
2729527295
type: string
2729627296
tags:
2729727297
- Query
27298+
/zeta-chain/observer/blame_by_chain_and_nonce/{chain_id}/{nonce}:
27299+
get:
27300+
summary: Queries a list of VoterByIdentifier items.
27301+
operationId: Query_BlamesByChainAndNonce
27302+
responses:
27303+
"200":
27304+
description: A successful response.
27305+
schema:
27306+
$ref: '#/definitions/observerQueryBlameByChainAndNonceResponse'
27307+
default:
27308+
description: An unexpected error response.
27309+
schema:
27310+
$ref: '#/definitions/googlerpcStatus'
27311+
parameters:
27312+
- name: chain_id
27313+
in: path
27314+
required: true
27315+
type: string
27316+
format: int64
27317+
- name: nonce
27318+
in: path
27319+
required: true
27320+
type: string
27321+
format: int64
27322+
tags:
27323+
- Query
2729827324
/zeta-chain/observer/blame_by_identifier/{blame_identifier}:
2729927325
get:
2730027326
summary: Queries a list of VoterByIdentifier items.
@@ -51119,6 +51145,14 @@ definitions:
5111951145
$ref: '#/definitions/observerObservationType'
5112051146
ballot_status:
5112151147
$ref: '#/definitions/observerBallotStatus'
51148+
observerQueryBlameByChainAndNonceResponse:
51149+
type: object
51150+
properties:
51151+
blame_info:
51152+
type: array
51153+
items:
51154+
type: object
51155+
$ref: '#/definitions/observerBlame'
5112251156
observerQueryBlameByIdentifierResponse:
5112351157
type: object
5112451158
properties:

proto/observer/query.proto

+14
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ service Query {
8080
option (google.api.http).get = "/zeta-chain/observer/get_all_blame_records";
8181
}
8282

83+
// Queries a list of VoterByIdentifier items.
84+
rpc BlamesByChainAndNonce(QueryBlameByChainAndNonceRequest) returns (QueryBlameByChainAndNonceResponse) {
85+
option (google.api.http).get = "/zeta-chain/observer/blame_by_chain_and_nonce/{chain_id}/{nonce}";
86+
}
87+
8388
rpc GetAllBlockHeaders(QueryAllBlockHeaderRequest) returns (QueryAllBlockHeaderResponse) {
8489
option (google.api.http).get = "/zeta-chain/observer/get_all_block_headers";
8590
}
@@ -211,6 +216,15 @@ message QueryAllBlameRecordsResponse {
211216
repeated Blame blame_info = 1;
212217
}
213218

219+
message QueryBlameByChainAndNonceRequest {
220+
int64 chain_id = 1;
221+
int64 nonce = 2;
222+
}
223+
224+
message QueryBlameByChainAndNonceResponse {
225+
repeated Blame blame_info = 1;
226+
}
227+
214228
message QueryAllBlockHeaderRequest {
215229
cosmos.base.query.v1beta1.PageRequest pagination = 1;
216230
}

x/observer/client/cli/query.go

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func GetQueryCmd(_ string) *cobra.Command {
3939
CmdShowObserverCount(),
4040
CmdBlameByIdentifier(),
4141
CmdGetAllBlameRecords(),
42+
CmdGetBlameByChainAndNonce(),
4243
)
4344

4445
return cmd

x/observer/client/cli/query_blame.go

+45
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cli
22

33
import (
4+
"strconv"
5+
46
"github.com/cosmos/cosmos-sdk/client"
57
"github.com/cosmos/cosmos-sdk/client/flags"
68
"github.com/spf13/cobra"
@@ -69,3 +71,46 @@ func CmdGetAllBlameRecords() *cobra.Command {
6971

7072
return cmd
7173
}
74+
75+
func CmdGetBlameByChainAndNonce() *cobra.Command {
76+
cmd := &cobra.Command{
77+
Use: "list-blame-by-msg [chainId] [nonce]",
78+
Short: "Query AllBlameRecords",
79+
Args: cobra.ExactArgs(2),
80+
RunE: func(cmd *cobra.Command, args []string) (err error) {
81+
chainID := args[0]
82+
nonce := args[1]
83+
84+
clientCtx, err := client.GetClientTxContext(cmd)
85+
if err != nil {
86+
return err
87+
}
88+
89+
queryClient := types.NewQueryClient(clientCtx)
90+
91+
chain, err := strconv.ParseInt(chainID, 10, 64)
92+
if err != nil {
93+
return err
94+
}
95+
nonceInt, err := strconv.ParseInt(nonce, 10, 64)
96+
if err != nil {
97+
return err
98+
}
99+
params := &types.QueryBlameByChainAndNonceRequest{
100+
ChainId: chain,
101+
Nonce: nonceInt,
102+
}
103+
104+
res, err := queryClient.BlamesByChainAndNonce(cmd.Context(), params)
105+
if err != nil {
106+
return err
107+
}
108+
109+
return clientCtx.PrintProto(res)
110+
},
111+
}
112+
113+
flags.AddQueryFlagsToCmd(cmd)
114+
115+
return cmd
116+
}

x/observer/keeper/blame.go

+29
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ func (k Keeper) GetAllBlame(ctx sdk.Context) (BlameRecords []*types.Blame, found
4040
return
4141
}
4242

43+
func (k Keeper) GetBlamesByChainAndNonce(ctx sdk.Context, chainID int64, nonce int64) (BlameRecords []*types.Blame, found bool) {
44+
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.BlameKey))
45+
blamePrefix := types.GetBlamePrefix(chainID, nonce)
46+
iterator := sdk.KVStorePrefixIterator(store, []byte(blamePrefix))
47+
defer iterator.Close()
48+
found = false
49+
for ; iterator.Valid(); iterator.Next() {
50+
var val types.Blame
51+
k.cdc.MustUnmarshal(iterator.Value(), &val)
52+
BlameRecords = append(BlameRecords, &val)
53+
found = true
54+
}
55+
return
56+
}
57+
4358
// Query
4459

4560
func (k Keeper) BlameByIdentifier(goCtx context.Context, request *types.QueryBlameByIdentifierRequest) (*types.QueryBlameByIdentifierResponse, error) {
@@ -71,3 +86,17 @@ func (k Keeper) GetAllBlameRecords(goCtx context.Context, request *types.QueryAl
7186
BlameInfo: blameRecords,
7287
}, nil
7388
}
89+
90+
func (k Keeper) BlamesByChainAndNonce(goCtx context.Context, request *types.QueryBlameByChainAndNonceRequest) (*types.QueryBlameByChainAndNonceResponse, error) {
91+
if request == nil {
92+
return nil, status.Error(codes.InvalidArgument, "invalid request")
93+
}
94+
ctx := sdk.UnwrapSDKContext(goCtx)
95+
blameRecords, found := k.GetBlamesByChainAndNonce(ctx, request.ChainId, request.Nonce)
96+
if !found {
97+
return nil, status.Error(codes.NotFound, "blame info not found")
98+
}
99+
return &types.QueryBlameByChainAndNonceResponse{
100+
BlameInfo: blameRecords,
101+
}, nil
102+
}

x/observer/keeper/blame_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package keeper
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
"github.com/zeta-chain/zetacore/x/observer/types"
8+
)
9+
10+
func TestKeeper_BlameByIdentifier(t *testing.T) {
11+
keeper, ctx := SetupKeeper(t)
12+
var chainId int64 = 97
13+
var nonce uint64 = 101
14+
digest := "85f5e10431f69bc2a14046a13aabaefc660103b6de7a84f75c4b96181d03f0b5"
15+
16+
index := types.GetBlameIndex(chainId, nonce, digest, 123)
17+
18+
keeper.SetBlame(ctx, &types.Blame{
19+
Index: index,
20+
FailureReason: "failed to join party",
21+
Nodes: nil,
22+
})
23+
24+
blameRecords, found := keeper.GetBlame(ctx, index)
25+
require.True(t, found)
26+
require.Equal(t, index, blameRecords.Index)
27+
}
28+
29+
func TestKeeper_BlameByChainAndNonce(t *testing.T) {
30+
keeper, ctx := SetupKeeper(t)
31+
var chainId int64 = 97
32+
var nonce uint64 = 101
33+
digest := "85f5e10431f69bc2a14046a13aabaefc660103b6de7a84f75c4b96181d03f0b5"
34+
35+
index := types.GetBlameIndex(chainId, nonce, digest, 123)
36+
37+
keeper.SetBlame(ctx, &types.Blame{
38+
Index: index,
39+
FailureReason: "failed to join party",
40+
Nodes: nil,
41+
})
42+
43+
blameRecords, found := keeper.GetBlamesByChainAndNonce(ctx, chainId, int64(nonce))
44+
require.True(t, found)
45+
require.Equal(t, 1, len(blameRecords))
46+
require.Equal(t, index, blameRecords[0].Index)
47+
}

x/observer/types/keys.go

+8
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,11 @@ const (
5050

5151
BallotListKey = "BallotList-value-"
5252
)
53+
54+
func GetBlameIndex(chainID int64, nonce uint64, digest string, height uint64) string {
55+
return fmt.Sprintf("%d-%d-%s-%d", chainID, nonce, digest, height)
56+
}
57+
58+
func GetBlamePrefix(chainID int64, nonce int64) string {
59+
return fmt.Sprintf("%d-%d", chainID, nonce)
60+
}

0 commit comments

Comments
 (0)