Skip to content

Commit

Permalink
Add get status to provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed Aug 23, 2024
1 parent 705f64c commit 4a95fb8
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/events/eventsPoller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class EventPoller extends EventEmitter {
if (this.lastSlot) {
this.eventsFilter.start = nextSlot(this.lastSlot)
}

const events = await this.provider.getEvents(this.eventsFilter)

if (events.length) {
Expand Down
23 changes: 23 additions & 0 deletions src/generated/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,29 @@ export type UnorderedSetOfUnorderedSetOfTransferQEyQHpyLoFgVJgXU = UnorderedSetO
export type UnorderedSetOfEndorsementInfoeYLhCU05 = EndorsementInfo[];
export type UnorderedSetOfGraphIntervalnrFPkOrt = GraphInterval[];
export type UnorderedSetOfOperationInfoMdPofISE = OperationInfo[];



export interface NodeStatus {
config: Config;
connected_nodes: ConnectedNodes;
consensus_stats: ConsensusStats;
current_cycle: NumberSWFW9I8Z;
current_time: NumberYQZ1VVuw;
current_cycle_time: NumberQuWRP8Oa;
next_cycle_time: NumberSDEXjSo6;
last_slot: Slot;
network_stats: NetworkStats;
next_slot: Slot;
node_id: StringOFgZzVe7;
node_ip?: OneOfNullQu0Arl1FStringDoaGddGANsst9HIR;
pool_stats: PoolStats;
version: Version;
execution_stats: ExecutionStats;
chain_id: NumberBte4OVdF;
minimal_fees?: StringTXHumHoA;
}

/**
*
* Node status
Expand Down
2 changes: 2 additions & 0 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ReadSCData,
ReadSCParams,
SignedData,
NodeStatusInfo,
} from './'

/**
Expand Down Expand Up @@ -38,4 +39,5 @@ export type Provider = {
deploySC(params: DeploySCParams): Promise<SmartContract>
getOperationStatus(opId: string): Promise<OperationStatus>
getEvents(filter: EventFilter): Promise<SCEvent[]>
getStatus(): Promise<NodeStatusInfo>
}
66 changes: 65 additions & 1 deletion src/provider/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Args } from '../basicElements'
import { Mas } from '../basicElements/mas'
import { U64 } from '../basicElements/serializers/number/u64'
import { SCEvent } from '../client'
import { SCEvent, Slot } from '../client'

type CallSCCommons = {
parameter?: Args | Uint8Array
Expand Down Expand Up @@ -41,3 +41,67 @@ export type SignedData = {
/** Base58 encoded representation of the signature */
signature: string
}

export type NodeStatusInfo = {
config: Config
connectedNodes: Record<string, unknown>
consensusStats: ConsensusStats
currentCycle: number
currentTime: number
currentCycleTime: number
nextCycleTime: number
lastSlot: Slot
nextSlot: Slot
networkStats: NetworkStats
nodeId: string
nodeIp?: null | string
poolStats: PoolStats
version: Version
executionStats: ExecutionStats
chainId: number
minimalFees?: string
}

export type Config = {
blockReward: string
deltaF0: number
endTimestamp?: number | null
genesisTimestamp: number
maxBlockSize?: number
operationValidityPeriods: number
periodsPerCycle: number
rollPrice: string
t0: number
threadCount: number
}

export type ConnectedNodes = Record<string, unknown>

export type ConsensusStats = {
cliqueCount: number
endTimespan: number
finalBlockCount: number
staleBlockCount: number
startTimespan: number
}

export type NetworkStats = {
activeNodeCount: number
bannedPeerCount: number
inConnectionCount: number
knownPeerCount: number
outConnectionCount: number
}

export type PoolStats = number[]

export type Version = string

export type ExecutionStats = {
timeWindowStart: number
timeWindowEnd: number
finalBlockCount: number
finalExecutedOperationsCount: number
activeCursor: Slot
finalCursor: Slot
}
14 changes: 13 additions & 1 deletion src/provider/web3Provider/web3Provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// a Web3Provider is the combination of a clientAPI and an private key account
import { CallSCParams, DeploySCParams, Provider, SignedData } from '..'
import {
CallSCParams,
DeploySCParams,
NodeStatusInfo,
Provider,
SignedData,
} from '..'
import { SCProvider } from './smartContracts'
import {
Account,
Expand All @@ -26,6 +32,7 @@ import {
getAbsoluteExpirePeriod,
OperationManager,
} from '../../operation/operationManager'
import { formatNodeStatusObject } from '../../utils/formatObjects/nodeStatusInfo'

export class Web3Provider extends SCProvider implements Provider {
static fromRPCUrl(url: string, account: Account): Web3Provider {
Expand Down Expand Up @@ -198,4 +205,9 @@ export class Web3Provider extends SCProvider implements Provider {
public async getEvents(filter: EventFilter): Promise<SCEvent[]> {
return this.client.getEvents(filter)
}

public async getStatus(): Promise<NodeStatusInfo> {

Check warning on line 209 in src/provider/web3Provider/web3Provider.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
const status = await this.client.status()

Check warning on line 210 in src/provider/web3Provider/web3Provider.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
return formatNodeStatusObject(status)

Check warning on line 211 in src/provider/web3Provider/web3Provider.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}
}
1 change: 1 addition & 0 deletions src/utils/formatObjects/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './nodeStatusInfo'
55 changes: 55 additions & 0 deletions src/utils/formatObjects/nodeStatusInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { NodeStatus } from 'src/generated/client'
import { NodeStatusInfo } from 'src/provider'

export function formatNodeStatusObject(status: NodeStatus): NodeStatusInfo {

Check warning on line 4 in src/utils/formatObjects/nodeStatusInfo.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
return {
config: {
blockReward: status.config.block_reward,
deltaF0: status.config.delta_f0,
endTimestamp: status.config.end_timestamp,
genesisTimestamp: status.config.genesis_timestamp,
maxBlockSize: status.config.max_block_size,
operationValidityPeriods: status.config.operation_validity_periods,
periodsPerCycle: status.config.periods_per_cycle,
rollPrice: status.config.roll_price,
t0: status.config.t0,
threadCount: status.config.thread_count,
},
connectedNodes: status.connected_nodes,
consensusStats: {
cliqueCount: status.consensus_stats.clique_count,
endTimespan: status.consensus_stats.end_timespan,
finalBlockCount: status.consensus_stats.final_block_count,
staleBlockCount: status.consensus_stats.stale_block_count,
startTimespan: status.consensus_stats.start_timespan,
},
currentCycle: status.current_cycle,
currentTime: status.current_time,
currentCycleTime: status.current_cycle_time,
nextCycleTime: status.next_cycle_time,
lastSlot: status.last_slot,
nextSlot: status.next_slot,
networkStats: {
activeNodeCount: status.network_stats.active_node_count,
bannedPeerCount: status.network_stats.banned_peer_count,
inConnectionCount: status.network_stats.in_connection_count,
knownPeerCount: status.network_stats.known_peer_count,
outConnectionCount: status.network_stats.out_connection_count,
},
nodeId: status.node_id,
nodeIp: status.node_ip,
poolStats: status.pool_stats,
version: status.version,
executionStats: {
timeWindowStart: status.execution_stats.time_window_start,
timeWindowEnd: status.execution_stats.time_window_end,
finalBlockCount: status.execution_stats.final_block_count,
finalExecutedOperationsCount:
status.execution_stats.final_executed_operations_count,
activeCursor: status.execution_stats.active_cursor,
finalCursor: status.execution_stats.final_cursor,
},
chainId: status.chain_id,
minimalFees: status.minimal_fees,
}

Check warning on line 54 in src/utils/formatObjects/nodeStatusInfo.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './isNode'
export * from './networks'
export * from './events'
export * from './maths'
export * from './formatObjects'
7 changes: 5 additions & 2 deletions test/integration/MRC20.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventPoller } from '../../src'
import { EventPoller, Mas } from '../../src'
import { MRC20 } from '../../src/contracts-wrappers'
import { provider } from './setup'

Expand Down Expand Up @@ -59,7 +59,10 @@ describe('Token wrapper tests', () => {
const amount = 123_000_000n
let operation = await usdcContract.increaseAllowance(
'AU1wN8rn4SkwYSTDF3dHFY4U28KtsqKL1NnEjDZhHnHEy6cEQm53',
amount
amount,
{
coins: Mas.fromString('0.1'),
}
)
await operation.waitSpeculativeExecution()

Expand Down
3 changes: 2 additions & 1 deletion test/integration/events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ describe('SC Event tests', () => {

test('poll transfer event from operationId', async () => {
const amount = 1_000n
const currentSlot = await provider.client.getCurrentSlot()
const { lastSlot: currentSlot } = await provider.getStatus()

const operation = await usdcContract.transfer(
'AU1wN8rn4SkwYSTDF3dHFY4U28KtsqKL1NnEjDZhHnHEy6cEQm53',
amount
Expand Down
21 changes: 21 additions & 0 deletions test/integration/provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,25 @@ describe('Provider tests', () => {
url: 'https://buildnet.massa.net:443/api/v2',
})
})

test('Node Status', async () => {
const status = await provider.getStatus()
expect(status.config).toBeDefined()
expect(status.connectedNodes).toBeDefined()
expect(status.consensusStats).toBeDefined()
expect(status.currentCycle).toBeDefined()
expect(status.currentTime).toBeDefined()
expect(status.currentCycleTime).toBeDefined()
expect(status.nextCycleTime).toBeDefined()
expect(status.lastSlot).toBeDefined()
expect(status.nextSlot).toBeDefined()
expect(status.networkStats).toBeDefined()
expect(status.nodeId).toBeDefined()
expect(status.nodeIp).toBeDefined()
expect(status.poolStats).toBeDefined()
expect(status.version).toBeDefined()
expect(status.executionStats).toBeDefined()
expect(status.chainId).toBeDefined()
expect(status.minimalFees).toBeDefined()
})
})

0 comments on commit 4a95fb8

Please sign in to comment.