Skip to content
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
3 changes: 1 addition & 2 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
branches: [main]
env:
IROHA_GIT: https://github.com/hyperledger-iroha/iroha.git
IROHA_REV: daa2d50fbe288aed1231c6f45c853698435477d6
IROHA_REV: c1d8b5e2fbffabba13e448a66f9b5dd86875cf18 # 2.0.0-rc.2 wip
jobs:
prep-crypto-wasm:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -62,7 +62,6 @@ jobs:
if: steps.prep-cache.outputs.cache-hit != 'true'
run: |
deno task prep:iroha --git $IROHA_GIT --git-rev $IROHA_REV
deno task prep:iroha:build

check-only:
if: github.event_name == 'pull_request'
Expand Down
9 changes: 9 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 94 additions & 24 deletions etc/__snapshots__/codegen_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ snapshot[`generate prototypes > prototypes snapshot 1`] = `
export type QueryCompatibleSelectors = {
FindDomains: 'domain' | 'domain-id' | 'domain-id-name' | 'domain-metadata' | 'domain-metadata-key'
FindAccounts: 'account' | 'account-id' | 'account-id-domain' | 'account-id-domain-name' | 'account-id-signatory' | 'account-metadata' | 'account-metadata-key'
FindAssets: 'asset' | 'asset-id' | 'asset-id-account' | 'asset-id-account-domain' | 'asset-id-account-domain-name' | 'asset-id-account-signatory' | 'asset-id-definition' | 'asset-id-definition-domain' | 'asset-id-definition-domain-name' | 'asset-id-definition-name' | 'asset-value' | 'asset-value-numeric' | 'asset-value-store' | 'asset-value-store-key'
FindAssets: 'asset' | 'asset-id' | 'asset-id-account' | 'asset-id-account-domain' | 'asset-id-account-domain-name' | 'asset-id-account-signatory' | 'asset-id-definition' | 'asset-id-definition-domain' | 'asset-id-definition-domain-name' | 'asset-id-definition-name' | 'asset-value'
FindAssetsDefinitions: 'asset-definition' | 'asset-definition-id' | 'asset-definition-id-domain' | 'asset-definition-id-domain-name' | 'asset-definition-id-name' | 'asset-definition-metadata' | 'asset-definition-metadata-key'
FindNfts: 'nft' | 'nft-id' | 'nft-id-domain' | 'nft-id-domain-name' | 'nft-id-name' | 'nft-metadata' | 'nft-metadata-key' | 'nft-account-id' | 'nft-account-id-domain' | 'nft-account-id-domain-name' | 'nft-account-id-signatory'
FindRoles: 'role' | 'role-id' | 'role-id-name'
FindRoleIds: 'role-id' | 'role-id-name'
FindPermissionsByAccountId: 'permission'
Expand Down Expand Up @@ -44,17 +45,25 @@ export type SelectorIdToOutput = {
'asset-id-definition-domain': lib.DomainId
'asset-id-definition-domain-name': lib.Name
'asset-id-definition-name': lib.Name
'asset-value': lib.AssetValue
'asset-value-numeric': lib.Numeric
'asset-value-store': lib.Metadata
'asset-value-store-key': lib.Json
'asset-value': lib.Numeric
'asset-definition': lib.AssetDefinition
'asset-definition-id': lib.AssetDefinitionId
'asset-definition-id-domain': lib.DomainId
'asset-definition-id-domain-name': lib.Name
'asset-definition-id-name': lib.Name
'asset-definition-metadata': lib.Metadata
'asset-definition-metadata-key': lib.Json
'nft': lib.Nft
'nft-id': lib.NftId
'nft-id-domain': lib.DomainId
'nft-id-domain-name': lib.Name
'nft-id-name': lib.Name
'nft-metadata': lib.Metadata
'nft-metadata-key': lib.Json
'nft-account-id': lib.AccountId
'nft-account-id-domain': lib.DomainId
'nft-account-id-domain-name': lib.Name
'nft-account-id-signatory': lib.PublicKey
'role': lib.Role
'role-id': lib.RoleId
'role-id-name': lib.Name
Expand Down Expand Up @@ -151,15 +160,6 @@ export type QuerySelectors = {
}
value: {
__selector: 'asset-value',
numeric: {
__selector: 'asset-value-numeric',
}
store: {
__selector: 'asset-value-store',
key(key: lib.Name): {
__selector: 'asset-value-store-key',
}
}
}
}
FindAssetsDefinitions: {
Expand All @@ -183,6 +183,39 @@ export type QuerySelectors = {
}
}
}
FindNfts: {
__selector: 'nft',
id: {
__selector: 'nft-id',
domain: {
__selector: 'nft-id-domain',
name: {
__selector: 'nft-id-domain-name',
}
}
name: {
__selector: 'nft-id-name',
}
}
metadata: {
__selector: 'nft-metadata',
key(key: lib.Name): {
__selector: 'nft-metadata-key',
}
}
accountId: {
__selector: 'nft-account-id',
domain: {
__selector: 'nft-account-id-domain',
name: {
__selector: 'nft-account-id-domain-name',
}
}
signatory: {
__selector: 'nft-account-id-signatory',
}
}
}
FindRoles: {
__selector: 'role',
id: {
Expand Down Expand Up @@ -378,16 +411,7 @@ export type QueryPredicates = {
}
}
}
value: {
isNumeric: () => lib.AssetProjectionPredicate
isStore: () => lib.AssetProjectionPredicate
numeric: never
store: {
key: (key: lib.Name) => {
equals: (value: lib.Json) => lib.AssetProjectionPredicate
}
}
}
value: never
}
FindAssetsDefinitions: {
id: {
Expand All @@ -414,6 +438,46 @@ export type QueryPredicates = {
}
}
}
FindNfts: {
id: {
equals: (value: lib.NftId) => lib.NftProjectionPredicate
domain: {
equals: (value: lib.DomainId) => lib.NftProjectionPredicate
name: {
equals: (value: lib.String) => lib.NftProjectionPredicate
contains: (value: lib.String) => lib.NftProjectionPredicate
startsWith: (value: lib.String) => lib.NftProjectionPredicate
endsWith: (value: lib.String) => lib.NftProjectionPredicate
}
}
name: {
equals: (value: lib.String) => lib.NftProjectionPredicate
contains: (value: lib.String) => lib.NftProjectionPredicate
startsWith: (value: lib.String) => lib.NftProjectionPredicate
endsWith: (value: lib.String) => lib.NftProjectionPredicate
}
}
metadata: {
key: (key: lib.Name) => {
equals: (value: lib.Json) => lib.NftProjectionPredicate
}
}
accountId: {
equals: (value: lib.AccountId) => lib.NftProjectionPredicate
domain: {
equals: (value: lib.DomainId) => lib.NftProjectionPredicate
name: {
equals: (value: lib.String) => lib.NftProjectionPredicate
contains: (value: lib.String) => lib.NftProjectionPredicate
startsWith: (value: lib.String) => lib.NftProjectionPredicate
endsWith: (value: lib.String) => lib.NftProjectionPredicate
}
}
signatory: {
equals: (value: lib.PublicKey) => lib.NftProjectionPredicate
}
}
}
FindRoles: {
id: {
equals: (value: lib.RoleId) => lib.RoleProjectionPredicate
Expand Down Expand Up @@ -527,6 +591,7 @@ export type QueryPredicates = {
}
}
FindBlocks: {
isEmpty: () => lib.SignedBlockProjectionPredicate
header: {
hash: {
equals: (value: lib.Hash) => lib.SignedBlockProjectionPredicate
Expand Down Expand Up @@ -568,6 +633,11 @@ export class FindAPI {
return new client.QueryBuilder(this._executor, 'FindAssetsDefinitions', params)
}

/** Convenience method for \`FindNfts\` query, a variant of {@linkcode types.QueryBox} enum. */
public nfts(params?: core.QueryBuilderParams): client.QueryBuilder<'FindNfts'> {
return new client.QueryBuilder(this._executor, 'FindNfts', params)
}

/** Convenience method for \`FindRoles\` query, a variant of {@linkcode types.QueryBox} enum. */
public roles(params?: core.QueryBuilderParams): client.QueryBuilder<'FindRoles'> {
return new client.QueryBuilder(this._executor, 'FindRoles', params)
Expand Down
29 changes: 29 additions & 0 deletions etc/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,14 @@ export type LibType =
| 'Bool'
| 'Timestamp'
| 'Duration'
| 'DurationCompact'
| 'Name'
| 'CompoundPredicate'
| 'DomainId'
| `AccountId`
| `AssetDefinitionId`
| `AssetId`
| 'NftId'
| 'Algorithm'
| 'Signature'
| 'Hash'
Expand Down Expand Up @@ -250,6 +252,7 @@ export class Resolver {
'DomainId',
'AccountId',
'AssetId',
'NftId',
'AssetDefinitionId',
'Compact',
'Algorithm',
Expand Down Expand Up @@ -279,6 +282,21 @@ export class Resolver {
}),
}),
)
.with(
{ refStr: 'Uptime', schema: { Tuple: ['Compact<u64>', 'u32'] } },
({ refStr }) => ({
t: 'local',
id: refStr,
// TODO: merge with duration? change Status in schema?
emit: () => ({
t: 'struct',
fields: [{ name: 'secs', type: { t: 'lib', id: 'Compact' } }, {
name: 'nanos',
type: { t: 'lib', id: 'U32' },
}],
}),
}),
)
.with(
{
ref: { id: P.union('Register', 'Unregister'), items: [P._] },
Expand Down Expand Up @@ -721,7 +739,18 @@ export class Resolver {
params: [{ t: 'lib', id: 'NonZero', params: [{ t: 'lib', id: rewriteWith }] }],
}),
)
.with(
[
'Duration',
{ t: 'lib', id: 'Compact' },
],
() => ({
t: 'lib',
id: 'DurationCompact',
}),
)
.otherwise(() => {
console.debug(this.resolve(x.type))
throw new Error(`Unexpected type of a field with _ms suffix: ${x.type}`)
})
return { name: x.name.slice(0, -3), type }
Expand Down
13 changes: 6 additions & 7 deletions etc/iroha-build-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ const resolvePrepIroha = (...paths: string[]) => {
return path.resolve(dirname, '../prep/iroha', ...paths)
}

export type Binary = 'irohad' | 'kagami' | 'iroha_codec'
export type Binary = 'irohad' | 'kagami'

export const BIN_PATHS: Record<Binary, string> = {
irohad: resolvePrepIroha('irohad'),
kagami: resolvePrepIroha('kagami'),
iroha_codec: resolvePrepIroha('iroha_codec'),
}

export const EXECUTOR_WASM_PATH: string = resolvePrepIroha('executor.wasm')
Expand All @@ -36,14 +35,14 @@ for (const filePath of [...Object.values(BIN_PATHS), EXECUTOR_WASM_PATH]) {
}
}

export async function irohaCodecToScale(
export async function kagamiCodecToScale(
type: keyof typeof SCHEMA,
json: JsonValue,
): Promise<Uint8Array> {
const input = JSON.stringify(json, undefined, 2)

return new Promise((resolve, reject) => {
const child = spawn(BIN_PATHS.iroha_codec, ['json-to-scale', '--type', type], {
const child = spawn(BIN_PATHS.kagami, ['codec', 'json-to-scale', '--type', type], {
stdio: ['pipe', 'pipe', 'inherit'],
})

Expand All @@ -54,7 +53,7 @@ export async function irohaCodecToScale(
})

child.on('close', (code) => {
if (code !== 0) reject(new Error('non-zero exit code of iroha_codec'))
if (code !== 0) reject(new Error('non-zero exit code of kagami'))
resolve(Uint8Array.from(Buffer.concat(chunks)))
})

Expand All @@ -63,12 +62,12 @@ export async function irohaCodecToScale(
})
}

export async function irohaCodecToJson(
export async function kagamiCodecToJson(
type: keyof typeof SCHEMA,
scale: Uint8Array,
): Promise<JsonValue> {
return new Promise<JsonValue>((resolve, reject) => {
const child = spawn(BIN_PATHS.iroha_codec, ['scale-to-json', '--type', type], {
const child = spawn(BIN_PATHS.kagami, ['codec', 'scale-to-json', '--type', type], {
stdio: ['pipe', 'pipe', 'inherit'],
})

Expand Down
28 changes: 1 addition & 27 deletions etc/task-codegen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { Schema } from '@iroha/core/data-model/schema'
import SCHEMA from '@iroha/core/data-model/schema-json'
import { generateClientFindAPI, generateDataModel, generatePrototypes, Resolver } from './codegen.ts'
import { expect } from '@std/expect'
import * as colors from '@std/fmt/colors'
import { parseArgs } from 'jsr:@std/cli/parse-args'
import { assertEquals } from '@std/assert/equals'
Expand Down Expand Up @@ -41,31 +39,7 @@ async function writeAll(entries: { file: string; code: () => string }[]) {
}
}

/**
* There are not included into the schema for some reason, but are useful to generate code for.
*/
const EXTENSION: Schema = {
Status: {
Struct: [
{ name: 'peers', type: 'Compact<u128>' },
{ name: 'blocks', type: 'Compact<u128>' },
{ name: 'txs_accepted', type: 'Compact<u128>' },
{ name: 'txs_rejected', type: 'Compact<u128>' },
{ name: 'uptime', type: 'Uptime' },
{ name: 'view_changes', type: 'Compact<u128>' },
{ name: 'queue_size', type: 'Compact<u128>' },
],
},
Uptime: {
Struct: [
{ name: 'secs', type: 'Compact<u128>' },
{ name: 'nanos', type: 'u32' },
],
},
}
expect(Object.keys(SCHEMA)).not.toContain(Object.keys(EXTENSION))

const resolver = new Resolver({ ...SCHEMA, ...EXTENSION })
const resolver = new Resolver(SCHEMA)

console.time('codegen')
await writeAll([
Expand Down
Loading