forked from hyperledger-cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
453 additions
and
366 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
278 changes: 278 additions & 0 deletions
278
...-ledger-connector-ethereum/src/test/typescript/integration/geth-monitoring-blocks.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,278 @@ | ||
/** | ||
* Tests for monitoring endpoints | ||
*/ | ||
|
||
////////////////////////////////// | ||
// Constants | ||
////////////////////////////////// | ||
|
||
const testLogLevel: LogLevelDesc = "debug"; | ||
const containerImageName = "ghcr.io/hyperledger/cacti-geth-all-in-one"; | ||
const containerImageVersion = "2023-07-27-2a8c48ed6"; | ||
const asyncTestTimeout = 1000 * 60 * 2; // 2 minutes | ||
|
||
import "jest-extended"; | ||
import express from "express"; | ||
import bodyParser from "body-parser"; | ||
import http from "http"; | ||
import { v4 as uuidV4 } from "uuid"; | ||
import { Server as SocketIoServer } from "socket.io"; | ||
import Web3 from "web3"; | ||
import type { Subscription } from "rxjs"; | ||
|
||
import { | ||
LogLevelDesc, | ||
Servers, | ||
Logger, | ||
LoggerProvider, | ||
} from "@hyperledger/cactus-common"; | ||
import { PluginRegistry } from "@hyperledger/cactus-core"; | ||
import { Configuration, Constants } from "@hyperledger/cactus-core-api"; | ||
import { pruneDockerAllIfGithubAction } from "@hyperledger/cactus-test-tooling"; | ||
import { GethTestLedger } from "@hyperledger/cactus-test-geth-ledger"; | ||
|
||
import { | ||
PluginLedgerConnectorEthereum, | ||
EthereumApiClient, | ||
WatchBlocksV1Progress, | ||
Web3BlockHeader, | ||
} from "../../../main/typescript/public-api"; | ||
|
||
const log: Logger = LoggerProvider.getOrCreate({ | ||
label: "geth-monitoring-blocks.test", | ||
level: testLogLevel, | ||
}); | ||
|
||
async function testWatchBlock( | ||
apiClient: EthereumApiClient, | ||
getBlockData: boolean, | ||
count = 1, | ||
) { | ||
let subscription: Subscription | undefined = undefined; | ||
const blocksReceived: WatchBlocksV1Progress[] = []; | ||
|
||
// Wait for blocks | ||
await new Promise<boolean>((resolve, reject) => { | ||
const watchObservable = apiClient.watchBlocksV1({ | ||
getBlockData, | ||
}); | ||
|
||
subscription = watchObservable.subscribe({ | ||
next(event) { | ||
blocksReceived.push(event); | ||
log.debug( | ||
"Received event:", | ||
JSON.stringify(event), | ||
"count:", | ||
blocksReceived.length, | ||
); | ||
if (blocksReceived.length >= count) { | ||
subscription?.unsubscribe(); | ||
resolve(true); | ||
} | ||
}, | ||
error(err) { | ||
log.error("watchBlocksV1() error:", err); | ||
subscription?.unsubscribe(); | ||
reject(err); | ||
}, | ||
}); | ||
}); | ||
|
||
return blocksReceived; | ||
} | ||
|
||
function assertBlockHeader(header?: Web3BlockHeader) { | ||
if (!header) { | ||
throw new Error("Header is missing!"); | ||
} | ||
|
||
// Check if defined and with expected type | ||
// Ignore nullable / undefine-able fields | ||
expect(typeof header.parentHash).toEqual("string"); | ||
expect(typeof header.sha3Uncles).toEqual("string"); | ||
expect(typeof header.miner).toEqual("string"); | ||
expect(typeof header.number).toEqual("string"); | ||
expect(typeof header.gasLimit).toEqual("string"); | ||
expect(typeof header.gasUsed).toEqual("string"); | ||
expect(typeof header.difficulty).toEqual("string"); | ||
} | ||
|
||
describe("Ethereum monitoring endpoints tests", () => { | ||
let ledger: GethTestLedger; | ||
let rpcApiWsHost: string; | ||
let rpcApiHttpHost: string; | ||
|
||
////////////////////////////////// | ||
// Setup ledger | ||
////////////////////////////////// | ||
|
||
beforeAll(async () => { | ||
const pruning = pruneDockerAllIfGithubAction({ logLevel: testLogLevel }); | ||
await expect(pruning).resolves.toBeTruthy(); | ||
|
||
ledger = new GethTestLedger({ | ||
containerImageName, | ||
containerImageVersion, | ||
}); | ||
await ledger.start(); | ||
rpcApiHttpHost = await ledger.getRpcApiHttpHost(); | ||
rpcApiWsHost = await ledger.getRpcApiWebSocketHost(); | ||
}); | ||
|
||
afterAll(async () => { | ||
if (ledger) { | ||
log.info("Closing ethereum ledger"); | ||
await ledger.stop(); | ||
await ledger.destroy(); | ||
} | ||
|
||
const pruning = pruneDockerAllIfGithubAction({ logLevel: testLogLevel }); | ||
await expect(pruning).resolves.toBeTruthy(); | ||
}); | ||
|
||
////////////////////////////////// | ||
// Monitoring with WebSocket node | ||
////////////////////////////////// | ||
|
||
describe("Monitoring ethereum blocks with WebSocket node connection tests", () => { | ||
let web3: InstanceType<typeof Web3>; | ||
let apiClient: EthereumApiClient; | ||
let connector: PluginLedgerConnectorEthereum; | ||
const expressApp = express(); | ||
expressApp.use(bodyParser.json({ limit: "250mb" })); | ||
const server = http.createServer(expressApp); | ||
const wsApi = new SocketIoServer(server, { | ||
path: Constants.SocketIoConnectionPathV1, | ||
}); | ||
|
||
beforeAll(async () => { | ||
const addressInfo = await Servers.listen({ | ||
hostname: "127.0.0.1", | ||
port: 0, | ||
server, | ||
}); | ||
apiClient = new EthereumApiClient( | ||
new Configuration({ | ||
basePath: `http://${addressInfo.address}:${addressInfo.port}`, | ||
}), | ||
); | ||
|
||
connector = new PluginLedgerConnectorEthereum({ | ||
instanceId: uuidV4(), | ||
rpcApiWsHost, | ||
logLevel: testLogLevel, | ||
pluginRegistry: new PluginRegistry({ plugins: [] }), | ||
}); | ||
await connector.getOrCreateWebServices(); | ||
await connector.registerWebServices(expressApp, wsApi); | ||
}); | ||
|
||
afterAll(async () => { | ||
if (server) { | ||
log.info("Shutdown connector servers"); | ||
await Servers.shutdown(server); | ||
} | ||
|
||
if (connector) { | ||
log.info("Shutdown connector"); | ||
await connector.shutdown(); | ||
} | ||
}); | ||
|
||
test( | ||
"Monitor new blocks headers on Ethereum", | ||
async () => { | ||
const ledgerEvents = await testWatchBlock(apiClient, false); | ||
expect(ledgerEvents).toBeTruthy(); | ||
expect(ledgerEvents.length).toEqual(1); | ||
const ledgerEvent = ledgerEvents[0]; | ||
// blockData should not be present if called with empty options | ||
expect(ledgerEvent.blockData).toBeUndefined(); | ||
expect(ledgerEvent.blockHeader).toBeTruthy(); | ||
|
||
// check some fields | ||
assertBlockHeader(ledgerEvent.blockHeader); | ||
}, | ||
asyncTestTimeout, | ||
); | ||
|
||
test( | ||
"Monitor new blocks data on Ethereum", | ||
async () => { | ||
const ledgerEvents = await testWatchBlock(apiClient, true); | ||
expect(ledgerEvents).toBeTruthy(); | ||
expect(ledgerEvents.length).toEqual(1); | ||
const ledgerEvent = ledgerEvents[0]; | ||
// blockHeader should not be present if called with getBlockData option | ||
expect(ledgerEvent.blockHeader).toBeFalsy(); | ||
expect(ledgerEvent.blockData).toBeTruthy(); | ||
|
||
// check some fields | ||
assertBlockHeader(ledgerEvent.blockData as unknown as Web3BlockHeader); // remove as unknown | ||
expect(typeof ledgerEvent.blockData?.size).toEqual("string"); | ||
expect(typeof ledgerEvent.blockData?.totalDifficulty).toEqual("string"); | ||
expect(typeof ledgerEvent.blockData?.uncles).toEqual("object"); | ||
}, | ||
asyncTestTimeout, | ||
); | ||
}); | ||
|
||
////////////////////////////////// | ||
// Monitoring with WebSocket node | ||
////////////////////////////////// | ||
|
||
describe("Monitoring ethereum blocks with HTTP node connection tests", () => { | ||
let web3: InstanceType<typeof Web3>; | ||
let apiClient: EthereumApiClient; | ||
let connector: PluginLedgerConnectorEthereum; | ||
const expressApp = express(); | ||
expressApp.use(bodyParser.json({ limit: "250mb" })); | ||
const server = http.createServer(expressApp); | ||
const wsApi = new SocketIoServer(server, { | ||
path: Constants.SocketIoConnectionPathV1, | ||
}); | ||
|
||
beforeAll(async () => { | ||
const addressInfo = await Servers.listen({ | ||
hostname: "127.0.0.1", | ||
port: 0, | ||
server, | ||
}); | ||
apiClient = new EthereumApiClient( | ||
new Configuration({ | ||
basePath: `http://${addressInfo.address}:${addressInfo.port}`, | ||
}), | ||
); | ||
|
||
connector = new PluginLedgerConnectorEthereum({ | ||
instanceId: uuidV4(), | ||
rpcApiHttpHost, | ||
logLevel: testLogLevel, | ||
pluginRegistry: new PluginRegistry({ plugins: [] }), | ||
}); | ||
await connector.getOrCreateWebServices(); | ||
await connector.registerWebServices(expressApp, wsApi); | ||
}); | ||
|
||
afterAll(async () => { | ||
if (server) { | ||
log.info("Shutdown connector servers"); | ||
await Servers.shutdown(server); | ||
} | ||
|
||
if (connector) { | ||
log.info("Shutdown connector"); | ||
await connector.shutdown(); | ||
} | ||
}); | ||
|
||
test( | ||
"Monitor new blocks headers on Ethereum", | ||
async () => { | ||
expect(true).toBeTruthy(); | ||
}, | ||
asyncTestTimeout, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.