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

Commit

Permalink
feat: add setup for storage node
Browse files Browse the repository at this point in the history
  • Loading branch information
imotai committed Jun 20, 2023
1 parent b9b119e commit b339147
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 43 deletions.
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
40 changes: 9 additions & 31 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,35 +64,13 @@ export function createClient(
} as Client
}

/**
*
* Config the rollup
*
* ```ts
* // 10 min
* const rollupInterval = 10 * 60 * 1000
* // 10 M
* const minRollupSize = 10 * 1024 * 1024
* const result = configRollup(client, rollupInterval, minRollupSize)
* ```
*
* @param client - the client of db3 network
* @param rollupInterval - the interval of rollup
* @param minRollupSize - the min data size of rollup
*
* @returns the response
*
**/
export async function configRollup(
export async function setupStorageNode(
client: Client,
rollupInterval: number,
minRollupSize: number
network: string,
rollupInterval: string,
minRollupSize: string
) {
const response = await client.provider.configRollup(
rollupInterval.toString(),
minRollupSize.toString()
)
return response
return await client.provider.setup(network, rollupInterval, minRollupSize)
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export {
createFromPrivateKey,
createRandomAccount,
signTypedData,
createFromExternal,
} from './account/db3_account'

export type { DB3Account } from './account/types'
Expand All @@ -35,7 +36,8 @@ export {
scanGcRecords,
scanRollupRecords,
getStorageNodeStatus,
configRollup,
getIndexNodeStatus,
setupStorageNode,
} from './client/client_v2'

export {
Expand Down
36 changes: 30 additions & 6 deletions src/provider/storage_provider_v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
GetCollectionOfDatabase,
ScanGcRecordRequest,
GetSystemStatusRequest,
SetupRollupRequest,
SetupRequest,
} from '../proto/db3_storage'
import { fromHEX, toHEX } from '../crypto/crypto_utils'
import { DB3Account } from '../account/types'
Expand Down Expand Up @@ -163,12 +163,36 @@ export class StorageProviderV2 {
return response
}

async configRollup(rollupInterval: string, minRollupSize: string) {
const request: SetupRollupRequest = {
rollupInterval: rollupInterval,
minRollupSize: minRollupSize,
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.setupRollup(request)
const { response } = await this.client.setup(request)
return response
}

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
}
2 changes: 1 addition & 1 deletion tests/client_v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('test db3.js client module', () => {
const minRollupSize = 10 * 1024 * 1024
console.log(await getStorageNodeStatus(client))
console.log(await getIndexNodeStatus(client))
console.log(await configRollup(client, rollupInterval, minRollupSize))
// console.log(await configRollup(client, rollupInterval, minRollupSize))
})

test('test query document', async () => {
Expand Down
15 changes: 15 additions & 0 deletions tests/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ describe('test db3.js provider module', () => {
localStorage.clear()
})

test('provider setup test', async () => {
const privateKey =
'0xca6ade874391db77a7a66e542f800ba4f89f249cb7c785371eeacbaa3ef74cfd'
const db3_account = createFromPrivateKey(privateKey)
expect(db3_account.address).toBe(
'0xBbE29f26dc7ADEFEf6592FA34a2EFa037585087C'
)
const provider = new StorageProviderV2(
'http://127.0.0.1:26619',
db3_account
)
const response = await provider.setup('1111', '1000000', '11000000')
console.log(response)
})

test('provider send mutation test', async () => {
const privateKey =
'0xad689d9b7751da07b0fb39c5091672cbfe50f59131db015f8a0e76c9790a6fcc'
Expand Down

0 comments on commit b339147

Please sign in to comment.