Skip to content

Commit

Permalink
Add placeholder EventBus methods (#3)
Browse files Browse the repository at this point in the history
* Add methods to PrivValidator interface

* Add placeholder methods to EventBus

* Add methods to Store interface
  • Loading branch information
prathamesh0 authored Aug 31, 2023
1 parent eb2a7af commit 965284f
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 13 deletions.
6 changes: 6 additions & 0 deletions packages/cometbft/src/abci/types/types.pb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// TODO: Generate

// Event allows application developers to attach additional information to
// ResponseBeginBlock, ResponseEndBlock, ResponseCheckTx and ResponseDeliverTx.
// Later, transactions may be queried using these events.
export class Event {}
15 changes: 15 additions & 0 deletions packages/cometbft/src/libs/pubsub/pubsub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// TODO: Implement using libp2p pubsub

// Query defines an interface for a query to be used for subscribing. A query
// matches against a map of events. Each key in this map is a composite of the
// even type and an attribute key (e.g. "{eventType}.{eventAttrKey}") and the
// values are the event values that are contained under that relationship. This
// allows event types to repeat themselves with the same set of keys and
// different values.
// TODO: Implement
export interface Query {}

// Server allows clients to subscribe/unsubscribe for messages, publishing
// messages with or without events, and manages internal state.
// TODO: Implement
export class Server {}
19 changes: 10 additions & 9 deletions packages/cometbft/src/libs/service/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ export interface Service {
setLogger(logger: Logger): void
}

export interface BaseServiceParams {
logger?: Logger
name?: string
started?: number
stopped?: number
_quit?: ReadWriteChannel<void>
impl?: Service
}

/*
Classical-inheritance-style service declarations. Services can be started, then
stopped, then optionally restarted.
Expand Down Expand Up @@ -83,7 +92,6 @@ Typical usage:
// stop subroutines, etc.
}
*/

// TODO: Implement
export class BaseService {
logger?: Logger;
Expand All @@ -95,14 +103,7 @@ export class BaseService {
// The "subclass" of BaseService
private impl?: Service;

constructor (params: {
logger?: Logger
name?: string
started?: number
stopped?: number
_quit?: ReadWriteChannel<void>
impl?: Service
}) {
constructor (params: BaseServiceParams) {
Object.assign(this, params);
}

Expand Down
6 changes: 6 additions & 0 deletions packages/cometbft/src/proto/tendermint/state/types.pb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// TODO: Generate

// ABCIResponses retains the responses
// of the various ABCI calls during block processing.
// It is persisted to disk for each height before calling Commit.
export class ABCIResponses {}
7 changes: 7 additions & 0 deletions packages/cometbft/src/proto/tendermint/types/types.pb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// TODO: Generate

// Vote represents a prevote, precommit, or commit vote from validators for
// consensus.
export class Vote {}

export class Proposal {}
11 changes: 11 additions & 0 deletions packages/cometbft/src/state/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// -----------------------------------------------------------------------------

// State is a short description of the latest committed block of the consensus protocol.
// It keeps all information necessary to validate new blocks,
// including the last validator set and the consensus params.
// All fields are exposed so the struct can be easily serialized,
// but none of them should be mutated directly.
// Instead, use state.Copy() or state.NextState(...).
// NOTE: not goroutine-safe.
// TODO: Implement
export class State {}
47 changes: 46 additions & 1 deletion packages/cometbft/src/state/store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,51 @@
import { ABCIResponses } from '../proto/tendermint/state/types.pb';
import { GenesisDoc } from '../types/genesis';
import { ConsensusParams } from '../types/params';
import { ValidatorSet } from '../types/validator-set';
import { State } from './state';

// Store defines the state store interface
//
// It is used to retrieve current state and save and load ABCI responses,
// validators and consensus parameters
// TODO: Implement
export interface Store {}
export interface Store {
// LoadFromDBOrGenesisFile loads the most recent state.
// If the chain is new it will use the genesis file from the provided genesis file path as the current state.
// TODO: Can throw an error
loadFromDBOrGenesisFile(f: string): State
// LoadFromDBOrGenesisDoc loads the most recent state.
// If the chain is new it will use the genesis doc as the current state.
// TODO: Can throw an error
loadFromDBOrGenesisDoc(g: GenesisDoc): State
// Load loads the current state of the blockchain
// TODO: Can throw an error
load(): State
// LoadValidators loads the validator set at a given height
// TODO: Can throw an error
loadValidators(h: bigint): ValidatorSet
// LoadABCIResponses loads the abciResponse for a given height
// TODO: Can throw an error
loadABCIResponses(h: bigint): ABCIResponses
// LoadLastABCIResponse loads the last abciResponse for a given height
// TODO: Can throw an error
loadLastABCIResponse(h: bigint): ABCIResponses
// LoadConsensusParams loads the consensus params for a given height
// TODO: Can throw an error
loadConsensusParams(h: bigint): ConsensusParams
// Save overwrites the previous state with the updated one
// TODO: Can throw an error
save(s: State): void
// SaveABCIResponses saves ABCIResponses for a given height
// TODO: Can throw an error
saveABCIResponses(h: bigint, r: ABCIResponses): void
// Bootstrap is used for bootstrapping state when not starting from a initial height.
// TODO: Can throw an error
bootstrap(s: State): void
// PruneStates takes the height from which to start prning and which height stop at
// TODO: Can throw an error
pruneStates(start: bigint, stop: bigint): void
// Close closes the connection with the database
// TODO: Can throw an error
close(): void
}
177 changes: 175 additions & 2 deletions packages/cometbft/src/types/event-bus.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,183 @@
import { BaseService } from '../libs/service/service';
import { BaseService, BaseServiceParams } from '../libs/service/service';
import * as cmtpubsub from '../libs/pubsub/pubsub';
import { Logger } from '../libs/log/logger';
import {
EventDataCompleteProposal,
EventDataNewBlock,
EventDataNewBlockHeader,
EventDataNewEvidence,
EventDataNewRound,
EventDataRoundState,
EventDataTx,
EventDataValidatorSetUpdates,
EventDataVote,
TMEventData
} from './events';
import * as types from '../abci/types/types.pb';

const defaultCapacity = 0;

// TODO: Implement
export interface EventBusSubscriber {}

// TODO: Implement
export interface Subscription {}

interface EventBusParams extends BaseServiceParams {
pubsub?: cmtpubsub.Server;
}

// EventBus is a common bus for all events going through the system. All calls
// are proxied to underlying pubsub server. All events must be published using
// EventBus to ensure correct data types.
// TODO: Implement
export class EventBus extends BaseService {}
export class EventBus extends BaseService {
pubsub?: cmtpubsub.Server;

constructor (params: EventBusParams) {
super(params);
Object.assign(this, params);
}

// NewEventBus returns a new event bus.
static newEventBus (): EventBus {
return EventBus.newEventBusWithBufferCapacity(defaultCapacity);
}

// NewEventBusWithBufferCapacity returns a new event bus with the given buffer capacity.
static newEventBusWithBufferCapacity (cap: number): EventBus {
return {} as EventBus;
}

// TODO: Implement
setLogger (l: Logger) {}

// TODO: Implement
// TODO: Can throw an error
onStart (): void {}

// TODO: Implement
onStop (): void {}

// TODO: Implement
numClients (): number {
return 0;
}

// TODO: Implement
numClientSubscriptions (clientID: string): number {
return 0;
}

// TODO: Implement
// TODO: Can throw an error
subscribe (
// TODO: Implement Context
ctx: any,
subscriber: string,
query: cmtpubsub.Query,
...outCapacity: number[]
): Subscription {
return {} as Subscription;
}

// This method can be used for a local consensus explorer and synchronous
// testing. Do not use for for public facing / untrusted subscriptions!
// TODO: Implement
// TODO: Can throw an error
subscribeUnbuffered (
ctx: any,
subscriber: string,
query: cmtpubsub.Query
): Subscription {
return {} as Subscription;
}

// TODO: Implement
// TODO: Can throw an error
unsubscribe (ctx: any, subscriber: string, query: cmtpubsub.Query): void {}

// TODO: Implement
// TODO: Can throw an error
unsubscribeAll (ctx: any, subscriber: string): void {}

// TODO: Implement
// TODO: Can throw an error
publish (eventType: string, eventData: TMEventData): void {}

// validateAndStringifyEvents takes a slice of event objects and creates a
// map of stringified events where each key is composed of the event
// type and each of the event's attributes keys in the form of
// "{event.Type}.{attribute.Key}" and the value is each attribute's value.
// TODO: Implement
private validateAndStringifyEvents (events: types.Event[], logger: Logger): Map<string, string[]> {
return new Map();
}

// TODO: Implement
// TODO: Can throw an error
publishEventNewBlock (data: EventDataNewBlock): void {}

// TODO: Implement
// TODO: Can throw an error
publishEventNewBlockHeader (data: EventDataNewBlockHeader): void {}

// TODO: Implement
// TODO: Can throw an error
PublishEventNewEvidence (evidence: EventDataNewEvidence): void {}

// TODO: Implement
// TODO: Can throw an error
publishEventVote (data: EventDataVote): void {}

// TODO: Implement
// TODO: Can throw an error
publishEventValidBlock (data: EventDataRoundState): void {}

// PublishEventTx publishes tx event with events from Result. Note it will add
// predefined keys (EventTypeKey, TxHashKey). Existing events with the same keys
// will be overwritten.
// TODO: Implement
// TODO: Can throw an error
publishEventTx (data: EventDataTx): void {}

// TODO: Implement
// TODO: Can throw an error
publishEventNewRoundStep (data: EventDataRoundState): void {}

// TODO: Implement
// TODO: Can throw an error
publishEventTimeoutPropose (data: EventDataRoundState): void {}

// TODO: Implement
// TODO: Can throw an error
publishEventTimeoutWait (data: EventDataRoundState): void {}

// TODO: Implement
// TODO: Can throw an error
publishEventNewRound (data: EventDataNewRound): void {}

// TODO: Implement
// TODO: Can throw an error
publishEventCompleteProposal (data: EventDataCompleteProposal): void {}

// TODO: Implement
// TODO: Can throw an error
publishEventPolka (data: EventDataRoundState): void {}

// TODO: Implement
// TODO: Can throw an error
publishEventUnlock (data: EventDataRoundState): void {}

// TODO: Implement
// TODO: Can throw an error
publishEventRelock (data: EventDataRoundState): void {}

// TODO: Implement
// TODO: Can throw an error
publishEventLock (data: EventDataRoundState): void {}

// TODO: Implement
// TODO: Can throw an error
publishEventValidatorSetUpdates (data: EventDataValidatorSetUpdates): void {}
}
42 changes: 42 additions & 0 deletions packages/cometbft/src/types/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// TMEventData implements events.EventData.
export interface TMEventData {
// empty interface
}

// Most event messages are basic types (a block, a transaction)
// but some (an input to a call tx or a receive) are more exotic

// TODO: Implement
export interface EventDataNewBlock {}

// TODO: Implement
export interface EventDataNewBlockHeader {}

// TODO: Implement
export interface EventDataNewEvidence {}

// All txs fire EventDataTx
// TODO: Implement
export interface EventDataTx {}

// NOTE: This goes into the replay WAL
// TODO: Implement
export interface EventDataRoundState {}

// TODO: Implement
export interface ValidatorInfo {}

// TODO: Implement
export interface EventDataNewRound {}

// TODO: Implement
export interface EventDataCompleteProposal {}

// TODO: Implement
export interface EventDataVote {}

// TODO: Implement
export type EventDataString = string

// TODO: Implement
export interface EventDataValidatorSetUpdates {}
13 changes: 12 additions & 1 deletion packages/cometbft/src/types/priv-validator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import * as crypto from '../crypto/crypto';
import * as cmtprototypes from '../proto/tendermint/types/types.pb';

// PrivValidator defines the functionality of a local CometBFT validator
// that signs votes and proposals, and never double signs.
// TODO: Implement
export interface PrivValidator {}
export interface PrivValidator {
// TODO: Can throw an error
getPubKey (): crypto.PubKey;

// TODO: Can throw an error
signVote (chainID: string, vote: cmtprototypes.Vote): void
// TODO: Can throw an error
signProposal (chainID: string, proposal: cmtprototypes.Proposal): void
}

// TODO: Implement
export class PrivValidatorsByAddress {
Expand Down
Loading

0 comments on commit 965284f

Please sign in to comment.