Skip to content
This repository has been archived by the owner on Jul 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #84 from dbpunk-labs/feat/get_system_status
Browse files Browse the repository at this point in the history
feat: add rollup config
  • Loading branch information
imotai authored Jun 21, 2023
2 parents c59651a + b339147 commit 7421445
Show file tree
Hide file tree
Showing 13 changed files with 294 additions and 61 deletions.
55 changes: 55 additions & 0 deletions src/abi/metastore_abi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// metastore_abi.ts
// Copyright (C) 2023 db3.network Author imotai <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

export const db3MetaStoreContractConfig = {
address: '0xb9709cE5E749b80978182db1bEdfb8c7340039A9',
abi: [
{
inputs: [
{
internalType: 'uint64',
name: 'networkId',
type: 'uint64',
},
{
internalType: 'string',
name: 'rollupNodeUrl',
type: 'string',
},
{
internalType: 'address',
name: 'rollupNodeAddress',
type: 'address',
},
{
internalType: 'string[]',
name: 'indexNodeUrls',
type: 'string[]',
},
{
internalType: 'address[]',
name: 'indexNodeAddresses',
type: 'address[]',
},
],
name: 'registerNetwork',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
},
],
} as const
22 changes: 19 additions & 3 deletions src/account/db3_account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type {
} from 'viem'
import type { DB3Account } from './types'
import * as secp from '@noble/secp256k1'
import { createWalletClient, http } from 'viem'
import { createWalletClient, http, Chain, custom } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
import { toHEX } from '../crypto/crypto_utils'
Expand All @@ -39,7 +39,7 @@ import { toHEX } from '../crypto/crypto_utils'
* @returns the instance of {@link DB3ACCOUNT}
*
**/
export function createFromPrivateKey(privateKey: Hex): DB3Account {
export function createFromPrivateKey(privateKey: Hex) {
const account = privateKeyToAccount(privateKey)
const client = createWalletClient({
account,
Expand All @@ -63,11 +63,27 @@ export function createFromPrivateKey(privateKey: Hex): DB3Account {
* @returns the instance of {@link DB3ACCOUNT}
*
**/
export function createRandomAccount(): DB3Account {
export function createRandomAccount() {
const rawKey = '0x' + toHEX(secp.utils.randomPrivateKey())
return createFromPrivateKey(rawKey as Hex)
}

export async function createFromExternal(chain: Chain) {
const [account] = await window.ethereum.request({
method: 'eth_requestAccounts',
})
const client = createWalletClient({
account,
chain,
transport: custom(window.ethereum),
})
const [address] = await client.getAddresses()
return {
client,
address,
} as DB3Account
}

/**
* Signs typed data and calculates an Ethereum-specific signature in [https://eips.ethereum.org/EIPS/eip-712](https://eips.ethereum.org/EIPS/eip-712): `sign(keccak256("\x19\x01" ‖ domainSeparator ‖ hashStruct(message)))`
*
Expand Down
51 changes: 47 additions & 4 deletions src/client/client_v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ import { BSON } from 'db3-bson'
*
**/
export function createClient(
rollup_node_url: string,
index_node_url: string,
rollupNodeUrl: string,
indexNodeUrl: string,
account: DB3Account
) {
const provider = new StorageProviderV2(rollup_node_url, account)
const indexer = new IndexerProvider(index_node_url)
const provider = new StorageProviderV2(rollupNodeUrl, account)
const indexer = new IndexerProvider(indexNodeUrl)
return {
provider,
indexer,
Expand All @@ -64,6 +64,49 @@ export function createClient(
} as Client
}

export async function setupStorageNode(
client: Client,
network: string,
rollupInterval: string,
minRollupSize: string
) {
return await client.provider.setup(network, rollupInterval, minRollupSize)
}

/**
*
* Get the system status of storage node
*
* ```ts
* const status = getStorageNodeStatus(client)
* ```
*
* @param client - the client of db3 network
* @returns the storage system status
*
**/
export async function getStorageNodeStatus(client: Client) {
const response = await client.provider.getSystemStatus()
return response
}

/**
*
* Get the system status of index node
*
* ```ts
* const status = getIndexNodeStatus(client)
* ```
*
* @param client - the client of db3 network
* @returns the Index system status
*
**/
export async function getIndexNodeStatus(client: Client) {
const response = await client.indexer.getSystemStatus()
return response
}

/**
*
* Get the mutation content by the id
Expand Down
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ export {
createFromPrivateKey,
createRandomAccount,
signTypedData,
createFromExternal,
} from './account/db3_account'

export type { DB3Account } from './account/types'

export type { Client } from './client/types'

export { addDoc, updateDoc, deleteDoc, queryDoc } from './store/document_v2'

export {
Expand All @@ -30,6 +35,9 @@ export {
scanMutationHeaders,
scanGcRecords,
scanRollupRecords,
getStorageNodeStatus,
getIndexNodeStatus,
setupStorageNode,
} from './client/client_v2'

export {
Expand All @@ -40,3 +48,4 @@ export {
} from './store/database_v2'

export { Index, IndexType } from './proto/db3_database_v2'
export { db3MetaStoreContractConfig } from './abi/metastore_abi'
7 changes: 6 additions & 1 deletion src/provider/indexer_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
GrpcWebOptions,
} from '@protobuf-ts/grpcweb-transport'
import { IndexerNodeClient } from '../proto/db3_indexer.client'
import { RunQueryRequest } from '../proto/db3_indexer'
import { RunQueryRequest, GetSystemStatusRequest } from '../proto/db3_indexer'
import { Query } from '../proto/db3_database_v2'

export class IndexerProvider {
Expand All @@ -46,4 +46,9 @@ export class IndexerProvider {
const { response } = await this.client.runQuery(request)
return response
}
async getSystemStatus() {
const request: GetSystemStatusRequest = {}
const { response } = await this.client.getSystemStatus(request)
return response
}
}
41 changes: 41 additions & 0 deletions src/provider/storage_provider_v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import {
GetDatabaseOfOwnerRequest,
GetCollectionOfDatabase,
ScanGcRecordRequest,
GetSystemStatusRequest,
SetupRequest,
} from '../proto/db3_storage'
import { fromHEX, toHEX } from '../crypto/crypto_utils'
import { DB3Account } from '../account/types'
Expand Down Expand Up @@ -161,6 +163,45 @@ export class StorageProviderV2 {
return response
}

async setup(
network: string,
rollupInterval: string,
minRollupSize: string
) {
const message = {
types: {
EIP712Domain: [],
Message: [
{ name: 'rollupInterval', type: 'string' },
{ name: 'minRollupSize', type: 'string' },
{ name: 'network', type: 'string' },
],
},
domain: {},
primaryType: 'Message',
message: {
rollupInterval,
minRollupSize,
network,
},
}
const signature = await signTypedData(this.account, message)
const msgParams = JSON.stringify(message)
const payload = new TextEncoder().encode(msgParams)
const request: SetupRequest = {
payload,
signature,
}
const { response } = await this.client.setup(request)
return response
}

async getSystemStatus() {
const request: GetSystemStatusRequest = {}
const { response } = await this.client.getSystemStatus(request)
return response
}

parseMutationBody(body: MutationBody) {
const typedMsg = new TextDecoder().decode(body.payload)
const typedData = JSON.parse(typedMsg)
Expand Down
20 changes: 20 additions & 0 deletions src/react-app-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// react-app-env.d.ts
// Copyright (C) 2023 db3.network Author imotai <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

interface Window {
ethereum: any
}
3 changes: 1 addition & 2 deletions src/store/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,5 @@ export class DB3Store {
return undefined
}

async getDatabase() {
}
async getDatabase() {}
}
7 changes: 6 additions & 1 deletion src/store/document_v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,12 @@ export async function addDoc(col: Collection, doc: DocumentData) {
)
if (response.code == 0 && response.items.length > 0) {
col.db.client.nonce += 1
return [response.id, response.block, response.order, response.items[0].value]
return [
response.id,
response.block,
response.order,
response.items[0].value,
]
} else {
throw new Error('fail to create collection')
}
Expand Down
2 changes: 0 additions & 2 deletions tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,4 @@ describe('test db3.js client module', () => {
)
const [dbId, txId] = await client.createDatabase()
})


})
Loading

0 comments on commit 7421445

Please sign in to comment.