8000 Load multiple KV values from file by martinkersner · Pull Request #204 · Bisonai/orakl · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Load multiple KV values from file #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions core/src/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [VRF](#vrf)
* [Adapter](#adapter)
* [Aggregator](#aggregator)
* [Key-Value](#key-value)
* [Generate adapter hash](#generate-adapter-hash)
* [Generate aggregator hash](#generate-aggregator-hash)
* [Fetch data from adapter](#fetch-data-with-adapter)
Expand Down Expand Up @@ -226,6 +227,62 @@ yarn cli aggregator remove --id [id]
* Activate aggregator TODO
* Deactivate aggregator TODO

### Key-Value

List all key-value pairs

```shell
yarn kv list
```

List all key-value pairs in `localhost` network

```shell
yarn kv list --chain localhost
```

Display value for given key (`PUBLIC_ADDRESS`) in `localhost` network

```shell
yarn kv list \
--key PUBLIC_ADDRESS \
--chain localhost
```

Insert value (`8888`) for a key (`HEALTH_CHECK_PORT`) ona a `localhost` network

```shell
yarn kv insert \
--key HEALTH_CHECK_PORT \
--value 8888 \
--chain localhost
```

Insert many key-value pairs defined in JSON-formatted file

```shell
yarn kv insertMany \
--file-path path/to/file.json \
--chain localhost
```

Delete key-value pair defined by key (`PUBLIC_ADDRESS`) on a `localhost` network

```shell
10000 yarn kv remove \
--key PUBLIC_ADDRESS \
--chain localhost
```

Update value (`8888`) for a key (`HEALTH_CHECK_PORT`) ona a `localhost` network

```shell
yarn kv udpate \
--key HEALTH_CHECK_PORT \
--value 8888 \
--chain localhost
```


## Generate Adapter Hash

Expand Down
48 changes: 43 additions & 5 deletions core/src/cli/operator/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import {
formatResultInsert,
formatResultRemove
} from './utils'
import { ReadFile } from './types'

export function kvSub(db) {
// kv list [--chain [chain]] [--key [key]]
// kv insert --chain [chain] --key [key] --value [value] [--dryrun]
// kv remove --chain [chain] --key [key] [--dryrun]
// kv update --chain [chain] --key [key] --value [value] [--dryrun]
// kv list [--chain [chain]] [--key [key]]
// kv insert --chain [chain] --key [key] --value [value] [--dryrun]
// kv remove --chain [chain] --key [key] [--dryrun]
// kv update --chain [chain] --key [key] --value [value] [--dryrun]
// kv insertMany --chain [chain] --file-path [file-path] [--dryrun]

const list = command({
name: 'list',
Expand Down Expand Up @@ -45,6 +47,22 @@ export function kvSub(db) {
handler: insertHandler(db)
})

const insertMany = command({
name: 'insertMany',
args: {
data: option({
type: ReadFile,
long: 'file-path'
}),
chain: option({
type: cmdstring,
long: 'chain'
}),
dryrun: dryrunOption
},
handler: insertManyHandler(db)
})

const remove = command({
name: 'remove',
args: {
Expand Down Expand Up @@ -83,7 +101,7 @@ export function kvSub(db) {

return subcommands({
name: 'kv',
cmds: { list, insert, remove, update }
cmds: { list, insert, insertMany, remove, update }
})
}

Expand Down Expand Up @@ -137,6 +155,26 @@ export function insertHandler(db) {
return wrapper
}

export function insertManyHandler(db) {
async function wrapper({ data, chain, dryrun }: { data; chain: string; dryrun?: boolean }) {
const chainId = await chainToId(db, chain)

const values: string[] = []
for (const key in data) {
values.push(`(${chainId}, '${key}', '${data[key]}')`)
}

const query = `INSERT INTO Kv (chainId, key, value) VALUES ${values.join()};`
if (dryrun) {
console.debug(query)
} else {
const result = await db.run(query)
console.log(formatResultInsert(result))
}
}
return wrapper
}

export function removeHandler(db) {
async function wrapper({ key, chain, dryrun }: { key: string; chain: string; dryrun?: boolean }) {
const chainId = await chainToId(db, chain)
Expand Down
19 changes: 18 additions & 1 deletion core/test/cli-kv.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { describe, expect, beforeEach, test } from '@jest/globals'
import { listHandler, insertHandler, removeHandler, updateHandler } from '../src/cli/operator/kv'
import {
listHandler,
insertHandler,
insertManyHandler,
removeHandler,
updateHandler
} from '../src/cli/operator/kv'
import { mkTmpFile } from '../src/utils'
import { openDb } from '../src/cli/operator/utils'

Expand All @@ -8,6 +14,10 @@ describe('CLI KV', function () {
const TMP_DB_FILE = mkTmpFile({ fileName: 'settings.test.sqlite' })
const KV_LOCALHOST = { key: 'someKey', value: 'someValue', chain: 'localhost' }
const KV_BAOBAB = { key: 'someKey', value: 'someValue', chain: 'baobab' }
const KV_MANY_LOCALHOST = {
chain: 'localhost',
data: [{ key1: 'val1' }, { key2: 'val2' }]
}

beforeEach(async () => {
DB = await openDb({ dbFile: TMP_DB_FILE, migrate: true })
Expand Down Expand Up @@ -40,6 +50,13 @@ describe('CLI KV', function () {
expect(kvAfter.length).toEqual(kvBefore.length + 1)
})

test('Should insertMany new Key-Value pairs', async function () {
const kvBefore = await listHandler(DB)({})
await insertManyHandler(DB)(KV_MANY_LOCALHOST)
const kvAfter = await listHandler(DB)({})
expect(kvAfter.length).toEqual(kvBefore.length + KV_MANY_LOCALHOST.data.length)
})

test('Should not allow to insert the same Key-Value pair more than once in the same chain', async function () {
await insertHandler(DB)(KV_LOCALHOST)
await expect(async () => {
Expand Down
30 changes: 28 additions & 2 deletions core/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,20 @@
dependencies:
"@sinonjs/commons" "^1.7.0"

"@slack/types@^1.2.1":
version "1.10.0"
resolved "https://registry.yarnpkg.com/@slack/types/-/types-1.10.0.tgz#cbf7d83e1027f4cbfd13d6b429f120c7fb09127a"
integrity sha512-tA7GG7Tj479vojfV3AoxbckalA48aK6giGjNtgH6ihpLwTyHE3fIgRrvt8TWfLwW8X8dyu7vgmAsGLRG7hWWOg==

"@slack/webhook@^6.1.0":
version "6.1.0"
resolved "https://registry.yarnpkg.com/@slack/webhook/-/webhook-6.1.0.tgz#ac9c8add919f0f5ab9440a24e76b2f83adcc0f2c"
integrity sha512-7AYNISyAjn/lA/VDwZ307K5ft5DojXgBd3DRrGoFN8XxIwIyRALdFhxBiMgAqeJH8eWoktvNwLK24R9hREEqpA==
dependencies:
"@slack/types" "^1.2.1"
"@types/node" ">=12.0.0"
axios "^0.21.4"

"@tootallnate/once@1":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-1.1.2.tgz#ccb91445360179a04e7fe6aff78c00ffc1eeaf82"
Expand Down Expand Up @@ -1149,7 +1163,7 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.11.tgz#d421b6c527a3037f7c84433fd2c4229e016863d3"
integrity sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==

"@types/node@*", "@types/node@^18.11.9":
"@types/node@*", "@types/node@>=12.0.0", "@types/node@^18.11.9":
version "18.11.18"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.11.18.tgz#8dfb97f0da23c2293e554c5a50d61ef134d7697f"
integrity sha512-DHQpWGjyQKSHj3ebjFI/wRKcqQcdR+MoFBygntYOZytCqNfkd2ZC4ARDJ2DQqhjH5p85Nnd3jhUJIXrszFX/JA==
Expand Down Expand Up @@ -1410,6 +1424,13 @@ asynckit@^0.4.0:
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==

axios@^0.21.4:
version "0.21.4"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.4.tgz#c67b90dc0568e5c1cf2b0b858c43ba28e2eda575"
integrity sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==
dependencies:
follow-redirects "^1.14.0"

axios@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.2.2.tgz#72681724c6e6a43a9fea860fc558127dbe32f9f1"
Expand Down Expand Up @@ -1740,6 +1761,11 @@ console-control-strings@^1.0.0, console-control-strings@^1.1.0:
resolved "https://registry.yarnpkg.com/console-control-strings/-/console-control-strings-1.1.0.tgz#3d7cf4464db6446ea644bf4b39507f9851008e8e"
integrity sha512-ty/fTekppD2fIwRvnZAVdeOiGd1c7YXEixbgJTNzqcxJWKQnjJ/V1bNEEE6hygpM3WjwHFUVK6HTjWSzV4a8sQ==

console-hook@^0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/console-hook/-/console-hook-0.0.2.tgz#7d7c5fddbb978b50b45ace7e0da2b4403d28b8ba"
integrity sha512-IYSn+z4+5mkEkTpHSLCNXDSsh5+3mlD6XMC0++YMGylOhVYR7vU2qDOLgEEe57LmNcp7MMgL68jb6ruNf6UgxQ==

convert-source-map@^1.6.0, convert-source-map@^1.7.0:
version "1.9.0"
resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.9.0.tgz#7faae62353fb4213366d0ca98358d22e8368b05f"
Expand Down Expand Up @@ -2227,7 +2253,7 @@ flatted@^3.1.0:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.7.tgz#609f39207cb614b89d0765b477cb2d437fbf9787"
integrity sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==

follow-redirects@^1.15.0:
follow-redirects@^1.14.0, follow-redirects@^1.15.0:
version "1.15.2"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==
Expand Down
27 changes: 27 additions & 0 deletions docs/v0.1-operator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,33 @@ yarn cli kv insert --chain ${chain} --key LOCAL_AGGREGATOR --value MEDIAN
yarn cli kv insert --chain ${chain} --key LISTENER_DELAY --value 500
```

### Loading settings from file

Node settings as described in previous section can be also predefined in JSON file and set all with single command.

Below you can see the contents of a JSON file (`settings.json`) containing the same settings as in the section above.

```
{
"PROVIDER_URL": "https://api.baobab.klaytn.net:8651",
"HEALTH_CHECK_PORT": "8888",
"REDIS_HOST": "localhost",
"REDIS_PORT": "6379",
"PRIVATE_KEY": "0x...",
"PUBLIC_KEY": "0x...",
"LOCAL_AGGREGATOR": "MEDIAN",
"LISTENER_DELAY": "500"
}
```

To load all settings with CLI at once, run the code below.

```shell
yarn cli kv insertMany \
--file-path settings.json \
--chain ${chain}
```

## Guides

* [Verifiable Random Function (VRF)](vrf.md)
Expand Down
0