Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions apps/randomness/monitor-randomness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { z } from "zod"

const RANDOMNESS_CONTRACT_ABI = abis.Random
const RANDOMNESS_CONTRACT_ADDRESS = deployment.Random

const DRAND_SERVICE_URL = "https://api.drand.sh/v2/beacons/evmnet"

const client = createPublicClient({
Expand Down
1 change: 1 addition & 0 deletions contracts/deployments/anvil/random/abis.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"name": "drandPK0",
"inputs": [],
"outputs": [

{
"name": "",
"type": "uint256",
Expand Down
1 change: 1 addition & 0 deletions contracts/src/randomness/Drand.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ contract Drand {
mstore(add(0x20, hashedRoundBytes), hashedRound)
}
uint256[2] memory message = BLS.hashToPoint(DST, hashedRoundBytes);

// NB: Always check that the signature is a valid signature (a valid G1 point on the curve)!
if (!BLS.isValidSignature(signature)) {
revert InvalidSignature([DRAND_PK_0, DRAND_PK_1, DRAND_PK_2, DRAND_PK_3], message, signature);
Expand Down
7 changes: 7 additions & 0 deletions packages/txm/lib/BlockMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ export class BlockMonitor {

constructor(_transactionManager: TransactionManager) {
this.txmgr = _transactionManager
}

async start() {
this.txmgr.viemClient.watchBlocks({
onBlock: this.onNewBlock.bind(this),
...(this.txmgr.transportProtocol === "http"
? {
pollingInterval: this.txmgr.pollingInterval,
}
: {}),
})
}

Expand Down
19 changes: 17 additions & 2 deletions packages/txm/lib/TransactionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ export type TransactionManagerConfig = {
* Defaults to false.
*/
allowDebug?: boolean

/**
* Specifies the polling interval in milliseconds.
* Defaults to 1/2 of the block time.
*/
pollingInterval?: number
}
/** The private key of the account used for signing transactions. */
privateKey: Hex
Expand Down Expand Up @@ -159,6 +165,8 @@ export class TransactionManager {
public readonly rpcAllowDebug: boolean
public readonly blockTime: bigint
public readonly finalizedTransactionPurgeTime: number
public readonly pollingInterval: number
public readonly transportProtocol: "http" | "websocket"

constructor(_config: TransactionManagerConfig) {
this.collectors = []
Expand All @@ -169,12 +177,14 @@ export class TransactionManager {
throw protocol.error
}

this.transportProtocol = protocol.value

const retries = _config.rpc.retries || 2
const retryDelay = _config.rpc.retryDelay || 50
const timeout = _config.rpc.timeout || 500

let transport: ViemTransport
if (protocol.value === "http") {
if (this.transportProtocol === "http") {
transport = viemHttpTransport(_config.rpc.url, {
timeout,
retryCount: retries,
Expand Down Expand Up @@ -247,6 +257,8 @@ export class TransactionManager {
this.rpcAllowDebug = _config.rpc.allowDebug || false
this.blockTime = _config.blockTime || 2n
this.finalizedTransactionPurgeTime = _config.finalizedTransactionPurgeTime || 2 * 60 * 1000

this.pollingInterval = _config.rpc.pollingInterval || (Number(this.blockTime) * 1000) / 2
}

/**
Expand Down Expand Up @@ -309,7 +321,10 @@ export class TransactionManager {
throw new Error(errorMessage)
}

// Await the completion of the gas price oracle startup before marking the TransactionManager as started
// Wait for the gas price oracle to initialize before starting the block monitor,
// which emits 'NewBlock' events as the TXM heartbeat.
await priceOraclePromise

await this.blockMonitor.start()
}
}