Skip to content

Commit

Permalink
fix: Blame index update (#1264)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kevinssgh authored Oct 11, 2023
1 parent ed35305 commit a1d7be0
Show file tree
Hide file tree
Showing 15 changed files with 848 additions and 130 deletions.
3 changes: 2 additions & 1 deletion cmd/zetaclientd/keygen_tss.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ func keygenTss(cfg *config.Config, tss *mc.TSS, keygenLogger zerolog.Logger) err
if err != nil {
return err
}
zetaHash, err := tss.CoreBridge.PostBlameData(&res.Blame, common.ZetaChain().ChainId, digest)
index := fmt.Sprintf("keygen-%s-%d", digest, keyGen.BlockNumber)
zetaHash, err := tss.CoreBridge.PostBlameData(&res.Blame, common.ZetaChain().ChainId, index)
if err != nil {
keygenLogger.Error().Err(err).Msg("error sending blame data to core")
return err
Expand Down
34 changes: 34 additions & 0 deletions docs/openapi/openapi.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27295,6 +27295,32 @@ paths:
type: string
tags:
- Query
/zeta-chain/observer/blame_by_chain_and_nonce/{chain_id}/{nonce}:
get:
summary: Queries a list of VoterByIdentifier items.
operationId: Query_BlamesByChainAndNonce
responses:
"200":
description: A successful response.
schema:
$ref: '#/definitions/observerQueryBlameByChainAndNonceResponse'
default:
description: An unexpected error response.
schema:
$ref: '#/definitions/googlerpcStatus'
parameters:
- name: chain_id
in: path
required: true
type: string
format: int64
- name: nonce
in: path
required: true
type: string
format: int64
tags:
- Query
/zeta-chain/observer/blame_by_identifier/{blame_identifier}:
get:
summary: Queries a list of VoterByIdentifier items.
Expand Down Expand Up @@ -51119,6 +51145,14 @@ definitions:
$ref: '#/definitions/observerObservationType'
ballot_status:
$ref: '#/definitions/observerBallotStatus'
observerQueryBlameByChainAndNonceResponse:
type: object
properties:
blame_info:
type: array
items:
type: object
$ref: '#/definitions/observerBlame'
observerQueryBlameByIdentifierResponse:
type: object
properties:
Expand Down
14 changes: 14 additions & 0 deletions proto/observer/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ service Query {
option (google.api.http).get = "/zeta-chain/observer/get_all_blame_records";
}

// Queries a list of VoterByIdentifier items.
rpc BlamesByChainAndNonce(QueryBlameByChainAndNonceRequest) returns (QueryBlameByChainAndNonceResponse) {
option (google.api.http).get = "/zeta-chain/observer/blame_by_chain_and_nonce/{chain_id}/{nonce}";
}

rpc GetAllBlockHeaders(QueryAllBlockHeaderRequest) returns (QueryAllBlockHeaderResponse) {
option (google.api.http).get = "/zeta-chain/observer/get_all_block_headers";
}
Expand Down Expand Up @@ -211,6 +216,15 @@ message QueryAllBlameRecordsResponse {
repeated Blame blame_info = 1;
}

message QueryBlameByChainAndNonceRequest {
int64 chain_id = 1;
int64 nonce = 2;
}

message QueryBlameByChainAndNonceResponse {
repeated Blame blame_info = 1;
}

message QueryAllBlockHeaderRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}
Expand Down
1 change: 1 addition & 0 deletions x/observer/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func GetQueryCmd(_ string) *cobra.Command {
CmdShowObserverCount(),
CmdBlameByIdentifier(),
CmdGetAllBlameRecords(),
CmdGetBlameByChainAndNonce(),
)

return cmd
Expand Down
45 changes: 45 additions & 0 deletions x/observer/client/cli/query_blame.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cli

import (
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -69,3 +71,46 @@ func CmdGetAllBlameRecords() *cobra.Command {

return cmd
}

func CmdGetBlameByChainAndNonce() *cobra.Command {
cmd := &cobra.Command{
Use: "list-blame-by-msg [chainId] [nonce]",
Short: "Query AllBlameRecords",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
chainID := args[0]
nonce := args[1]

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

chain, err := strconv.ParseInt(chainID, 10, 64)
if err != nil {
return err
}
nonceInt, err := strconv.ParseInt(nonce, 10, 64)
if err != nil {
return err
}
params := &types.QueryBlameByChainAndNonceRequest{
ChainId: chain,
Nonce: nonceInt,
}

res, err := queryClient.BlamesByChainAndNonce(cmd.Context(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
29 changes: 29 additions & 0 deletions x/observer/keeper/blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ func (k Keeper) GetAllBlame(ctx sdk.Context) (BlameRecords []*types.Blame, found
return
}

func (k Keeper) GetBlamesByChainAndNonce(ctx sdk.Context, chainID int64, nonce int64) (BlameRecords []*types.Blame, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.BlameKey))
blamePrefix := types.GetBlamePrefix(chainID, nonce)
iterator := sdk.KVStorePrefixIterator(store, []byte(blamePrefix))
defer iterator.Close()
found = false
for ; iterator.Valid(); iterator.Next() {
var val types.Blame
k.cdc.MustUnmarshal(iterator.Value(), &val)
BlameRecords = append(BlameRecords, &val)
found = true
}
return
}

// Query

func (k Keeper) BlameByIdentifier(goCtx context.Context, request *types.QueryBlameByIdentifierRequest) (*types.QueryBlameByIdentifierResponse, error) {
Expand Down Expand Up @@ -71,3 +86,17 @@ func (k Keeper) GetAllBlameRecords(goCtx context.Context, request *types.QueryAl
BlameInfo: blameRecords,
}, nil
}

func (k Keeper) BlamesByChainAndNonce(goCtx context.Context, request *types.QueryBlameByChainAndNonceRequest) (*types.QueryBlameByChainAndNonceResponse, error) {
if request == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
blameRecords, found := k.GetBlamesByChainAndNonce(ctx, request.ChainId, request.Nonce)
if !found {
return nil, status.Error(codes.NotFound, "blame info not found")
}
return &types.QueryBlameByChainAndNonceResponse{
BlameInfo: blameRecords,
}, nil
}
47 changes: 47 additions & 0 deletions x/observer/keeper/blame_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package keeper

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/x/observer/types"
)

func TestKeeper_BlameByIdentifier(t *testing.T) {
keeper, ctx := SetupKeeper(t)
var chainId int64 = 97
var nonce uint64 = 101
digest := "85f5e10431f69bc2a14046a13aabaefc660103b6de7a84f75c4b96181d03f0b5"

index := types.GetBlameIndex(chainId, nonce, digest, 123)

keeper.SetBlame(ctx, &types.Blame{
Index: index,
FailureReason: "failed to join party",
Nodes: nil,
})

blameRecords, found := keeper.GetBlame(ctx, index)
require.True(t, found)
require.Equal(t, index, blameRecords.Index)
}

func TestKeeper_BlameByChainAndNonce(t *testing.T) {
keeper, ctx := SetupKeeper(t)
var chainId int64 = 97
var nonce uint64 = 101
digest := "85f5e10431f69bc2a14046a13aabaefc660103b6de7a84f75c4b96181d03f0b5"

index := types.GetBlameIndex(chainId, nonce, digest, 123)

keeper.SetBlame(ctx, &types.Blame{
Index: index,
FailureReason: "failed to join party",
Nodes: nil,
})

blameRecords, found := keeper.GetBlamesByChainAndNonce(ctx, chainId, int64(nonce))
require.True(t, found)
require.Equal(t, 1, len(blameRecords))
require.Equal(t, index, blameRecords[0].Index)
}
8 changes: 8 additions & 0 deletions x/observer/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,11 @@ const (

BallotListKey = "BallotList-value-"
)

func GetBlameIndex(chainID int64, nonce uint64, digest string, height uint64) string {
return fmt.Sprintf("%d-%d-%s-%d", chainID, nonce, digest, height)
}

func GetBlamePrefix(chainID int64, nonce int64) string {
return fmt.Sprintf("%d-%d", chainID, nonce)
}
Loading

0 comments on commit a1d7be0

Please sign in to comment.