|
| 1 | +import Debug from "debug"; |
| 2 | +import { getConfig } from "../common/config"; |
| 3 | +import { Chain } from "../common/chain"; |
| 4 | +import { PlatformAddress } from "codechain-primitives/lib"; |
| 5 | +import { CreateClientDatagram } from "../common/datagram/createClient"; |
| 6 | +import { strict as assert } from "assert"; |
| 7 | + |
| 8 | +require("dotenv").config(); |
| 9 | + |
| 10 | +const debug = Debug("scenario:main"); |
| 11 | + |
1 | 12 | async function main() { |
2 | | - console.log("Not implemented"); |
| 13 | + const config = getConfig(); |
| 14 | + const chainA = new Chain({ |
| 15 | + server: config.chainA.rpcURL, |
| 16 | + networkId: config.chainA.networkId, |
| 17 | + faucetAddress: PlatformAddress.fromString(config.chainA.faucetAddress), |
| 18 | + counterpartyIdentifiers: { |
| 19 | + client: config.chainA.counterpartyClientId, |
| 20 | + connection: config.chainA.counterpartyConnectionId, |
| 21 | + channel: config.chainA.counterpartyChannelId |
| 22 | + }, |
| 23 | + keystorePath: config.chainA.keystorePath |
| 24 | + }); |
| 25 | + const chainB = new Chain({ |
| 26 | + server: config.chainB.rpcURL, |
| 27 | + networkId: config.chainB.networkId, |
| 28 | + faucetAddress: PlatformAddress.fromString(config.chainB.faucetAddress), |
| 29 | + counterpartyIdentifiers: { |
| 30 | + client: config.chainB.counterpartyClientId, |
| 31 | + connection: config.chainB.counterpartyConnectionId, |
| 32 | + channel: config.chainB.counterpartyChannelId |
| 33 | + }, |
| 34 | + keystorePath: config.chainB.keystorePath |
| 35 | + }); |
| 36 | + |
| 37 | + await createLightClient({ chain: chainA, counterpartyChain: chainB }); |
| 38 | + await createLightClient({ chain: chainB, counterpartyChain: chainA }); |
3 | 39 | } |
4 | 40 |
|
5 | 41 | main().catch(console.error); |
| 42 | + |
| 43 | +async function createLightClient({ |
| 44 | + chain, |
| 45 | + counterpartyChain |
| 46 | +}: { |
| 47 | + chain: Chain; |
| 48 | + counterpartyChain: Chain; |
| 49 | +}) { |
| 50 | + debug("Create light client"); |
| 51 | + const counterpartyBlockNumber = await counterpartyChain.latestHeight(); |
| 52 | + const blockNumber = await chain.latestHeight(); |
| 53 | + debug(`height is ${counterpartyBlockNumber}`); |
| 54 | + const counterpartyRawHeader = await counterpartyChain.queryChainHeader( |
| 55 | + counterpartyBlockNumber |
| 56 | + ); |
| 57 | + debug(`rawHeader is ${counterpartyRawHeader}`); |
| 58 | + |
| 59 | + assert(counterpartyRawHeader, "headet should not empty"); |
| 60 | + assert.notStrictEqual( |
| 61 | + counterpartyRawHeader!.substr(0, 2), |
| 62 | + "0x", |
| 63 | + "should not start with 0x" |
| 64 | + ); |
| 65 | + |
| 66 | + debug(`Get queryClient`); |
| 67 | + const clientStateBefore = await chain.queryClient(blockNumber); |
| 68 | + assert.notEqual(clientStateBefore, null, "querying on the best block"); |
| 69 | + assert.equal(clientStateBefore!.data, null, "client is not initialized"); |
| 70 | + |
| 71 | + const createClient = new CreateClientDatagram({ |
| 72 | + id: chain.counterpartyIdentifiers.client, |
| 73 | + kind: 0, |
| 74 | + consensusState: Buffer.alloc(0), |
| 75 | + data: Buffer.from(counterpartyRawHeader!, "hex") |
| 76 | + }); |
| 77 | + |
| 78 | + debug(`Submit datagram`); |
| 79 | + await chain.submitDatagram(createClient); |
| 80 | + |
| 81 | + const clientStateAfter = await chain.queryClient(); |
| 82 | + assert.notEqual(clientStateAfter, null, "querying on the best block"); |
| 83 | + assert.notEqual(clientStateAfter!.data, null, "client is initialized"); |
| 84 | + debug(`Create client is ${JSON.stringify(clientStateAfter)}`); |
| 85 | +} |
0 commit comments