Skip to content

Commit

Permalink
Problem: no efficient batch query for encryption keys
Browse files Browse the repository at this point in the history
Update CHANGELOG.md

Signed-off-by: yihuang <huang@crypto.com>

update swagger

typo
  • Loading branch information
yihuang committed Apr 30, 2024
1 parent 87efbec commit 64cd21e
Show file tree
Hide file tree
Showing 10 changed files with 626 additions and 30 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* (versiondb) [#1379](https://github.com/crypto-org-chain/cronos/pull/1379) Flush versiondb when graceful shutdown, make rocksdb upgrade smooth.
* (store) [#1378](https://github.com/crypto-org-chain/cronos/pull/1378) Upgrade rocksdb to `v8.11.3`.
* (versiondb) [#1387](https://github.com/crypto-org-chain/cronos/pull/1387) Add dedicated config section for versiondb, prepare for sdk 0.50 integration.
* [#1413](https://github.com/crypto-org-chain/cronos/pull/1413) Add custom keyring implementation for e2ee module.
* (e2ee)[#1413](https://github.com/crypto-org-chain/cronos/pull/1413) Add custom keyring implementation for e2ee module.
* (e2ee)[#1415](https://github.com/crypto-org-chain/cronos/pull/1415) Add batch keys query for e2ee module.

### Bug Fixes

Expand Down
69 changes: 67 additions & 2 deletions client/docs/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,6 @@ paths:
properties:
key:
type: string
format: byte
description: KeyResponse is the response type for the Query/Key RPC method.
default:
description: An unexpected error response.
Expand Down Expand Up @@ -927,6 +926,57 @@ paths:
type: string
tags:
- Query
/e2ee/v1/keys:
post:
summary: Keys queries the encryption keys for a batch of addresses
operationId: Keys
responses:
'200':
description: A successful response.
schema:
type: object
properties:
keys:
type: array
items:
type: string
description: KeysResponse is the response type for the Query/Key RPC method.
default:
description: An unexpected error response.
schema:
type: object
properties:
error:
type: string
code:
type: integer
format: int32
message:
type: string
details:
type: array
items:
type: object
properties:
type_url:
type: string
value:
type: string
format: byte
parameters:
- name: body
in: body
required: true
schema:
type: object
properties:
addresses:
type: array
items:
type: string
description: KeysRequest is the request type for the Query/Key RPC method.
tags:
- Query
/ethermint/evm/v1/account/{address}:
get:
summary: Account queries an Ethereum account.
Expand Down Expand Up @@ -45554,8 +45604,23 @@ definitions:
properties:
key:
type: string
format: byte
description: KeyResponse is the response type for the Query/Key RPC method.
e2ee.KeysRequest:
type: object
properties:
addresses:
type: array
items:
type: string
description: KeysRequest is the request type for the Query/Key RPC method.
e2ee.KeysResponse:
type: object
properties:
keys:
type: array
items:
type: string
description: KeysResponse is the response type for the Query/Key RPC method.
ethermint.evm.v1.ChainConfig:
type: object
properties:
Expand Down
12 changes: 12 additions & 0 deletions integration_tests/cosmoscli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,18 @@ def query_e2ee_key(self, address):
)
)["key"]

def query_e2ee_keys(self, *addresses):
return json.loads(
self.raw(
"q",
"e2ee",
"keys",
*addresses,
home=self.data_dir,
output="json",
)
)["keys"]

def register_e2ee_key(self, key, **kwargs):
kwargs.setdefault("gas_prices", DEFAULT_GAS_PRICE)
kwargs.setdefault("gas", DEFAULT_GAS)
Expand Down
7 changes: 6 additions & 1 deletion integration_tests/test_e2ee.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ def test_encrypt_decrypt(cronos):
assert cli.query_e2ee_key(cli.address("validator")) == pubkey0
pubkey1 = cli.keygen(keyring_name="key1")
cli.register_e2ee_key(pubkey1, _from="community")
assert cli.query_e2ee_key(cli.address("community")) == pubkey1

# query in batch
assert cli.query_e2ee_keys(cli.address("validator"), cli.address("community")) == [
pubkey0,
pubkey1,
]

# prepare data file to encrypt
content = "Hello World!"
Expand Down
18 changes: 18 additions & 0 deletions proto/e2ee/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ service Query {
rpc Key(KeyRequest) returns (KeyResponse) {
option (google.api.http).get = "/e2ee/v1/key/{address}";
}
// Keys queries the encryption keys for a batch of addresses
rpc Keys(KeysRequest) returns (KeysResponse) {
option (google.api.http) = {
post: "/e2ee/v1/keys"
body: "*"
};
}
}

// KeyRequest is the request type for the Query/Key RPC method.
Expand All @@ -22,3 +29,14 @@ message KeyRequest {
message KeyResponse {
string key = 1;
}


// KeysRequest is the request type for the Query/Key RPC method.
message KeysRequest {
repeated string addresses = 1;
}

// KeysResponse is the response type for the Query/Key RPC method.
message KeysResponse {
repeated string keys = 1;
}
6 changes: 6 additions & 0 deletions x/e2ee/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
Short: "Query an encryption key by address",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "address"}},
},
{
RpcMethod: "Keys",
Use: "keys [addresses] ...",
Short: "Query a batch of encryption key by addresses",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "addresses", Varargs: true}},
},
},
},
Tx: &autocliv1.ServiceCommandDescriptor{
Expand Down
17 changes: 9 additions & 8 deletions x/e2ee/client/cli/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ func EncryptCommand() *cobra.Command {

// query encryption key from chain state
client := types.NewQueryClient(clientCtx)
rsp, err := client.Keys(clientCtx.CmdContext, &types.KeysRequest{
Addresses: recs,
})
if err != nil {
return err

Check warning on line 47 in x/e2ee/client/cli/encrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/encrypt.go#L47

Added line #L47 was not covered by tests
}

recipients := make([]age.Recipient, len(recs))
for i, rec := range recs {
rsp, err := client.Key(clientCtx.CmdContext, &types.KeyRequest{
Address: rec,
})
if err != nil {
return err
}
recipient, err := age.ParseX25519Recipient(rsp.Key)
for i, key := range rsp.Keys {
recipient, err := age.ParseX25519Recipient(key)
if err != nil {
return err
}
Expand Down
15 changes: 15 additions & 0 deletions x/e2ee/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,18 @@ func (k Keeper) Key(ctx context.Context, req *types.KeyRequest) (*types.KeyRespo
value := sdkCtx.KVStore(k.storeKey).Get(types.KeyPrefix(bz))
return &types.KeyResponse{Key: string(value)}, nil
}

func (k Keeper) Keys(ctx context.Context, requests *types.KeysRequest) (*types.KeysResponse, error) {
store := sdk.UnwrapSDKContext(ctx).KVStore(k.storeKey)
var rsp types.KeysResponse
for _, address := range requests.Addresses {
bz, err := k.addressCodec.StringToBytes(address)
if err != nil {
return nil, err

Check warning on line 95 in x/e2ee/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/keeper/keeper.go#L89-L95

Added lines #L89 - L95 were not covered by tests
}
value := store.Get(types.KeyPrefix(bz))
rsp.Keys = append(rsp.Keys, string(value))

Check warning on line 98 in x/e2ee/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/keeper/keeper.go#L97-L98

Added lines #L97 - L98 were not covered by tests
}

return &rsp, nil

Check warning on line 101 in x/e2ee/keeper/keeper.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/keeper/keeper.go#L101

Added line #L101 was not covered by tests
}
Loading

0 comments on commit 64cd21e

Please sign in to comment.