import { Block , BlockHeader , BlockBody , Consensus , ConsensusMessage , Transaction , ValidatorSet } from '@uniqys/blockchain' ; import { BufferWriter , BufferReader , Serializable , UInt8 , UInt32 , UInt64 , List } from '@uniqys/serialize' ; import { Hash } from '@uniqys/signature' ; export type EventMessage = Message . Hello | Message . NewTransaction | Message . NewBlock | Message . NewBlockHeight | Message . NewConsensusMessage ; export type RequestMessage = Message . GetConsentedHeader | Message . GetHeaders | Message . GetBodies ; export type ResponseMessage = Message . ConsentedHeader | Message . Headers | Message . Bodies ; export type Message = EventMessage | RequestMessage | ResponseMessage ; export namespace Message { export enum Type { Event , Request , Response } export class Hello implements Serializable { public static readonly code = 0x00 ; public readonly code = Hello . code ; public readonly type = Type . Event ; constructor ( public readonly height , public readonly genesis ) { } public static deserialize ( reader ) { const height = UInt64 . deserialize ( reader ) ; const genesis = Hash . deserialize ( reader ) ; return new Hello ( height , genesis ) ; } public serialize ( writer ) { UInt64 . serialize ( this . height , writer ) ; this . genesis . serialize ( writer ) ; } } export class NewTransaction implements Serializable { public static readonly code = 0x10 ; public readonly code = NewTransaction . code ; public readonly type = Type . Event ; constructor ( public readonly transaction ) { } public static deserialize ( reader ) { const transaction = Transaction . deserialize ( reader ) ; return new NewTransaction ( transaction ) ; } public serialize ( writer ) { this . transaction . serialize ( writer ) ; } } export class NewBlock { public static readonly code = 0x11 ; public readonly code = NewBlock . code ; public readonly type = Type . Event ; constructor ( public readonly block , public readonly consensus ) { } public static deserialize ( reader ) { const block = Block . deserialize ( reader ) ; const consensus = Consensus . deserialize ( reader ) ; return new NewBlock ( block , consensus ) ; } public serialize ( writer ) { this . block . serialize ( writer ) ; this . consensus . serialize ( writer ) ; } } export class NewBlockHeight { public static readonly code = 0x12 ; public readonly code = NewBlockHeight . code ; public readonly type = Type . Event ; constructor ( public readonly height ) { } public static deserialize ( reader ) { const height = UInt64 . deserialize ( reader ) ; return new NewBlockHeight ( height ) ; } public serialize ( writer ) { UInt64 . serialize ( this . height , writer ) ; } } export class NewConsensusMessage implements Serializable { public static readonly code = 0x13 ; public readonly code = NewConsensusMessage . code ; public readonly type = Type . Event ; constructor ( public readonly message ) { } public static deserialize ( reader ) { const message = ConsensusMessage . deserialize ( reader ) ; return new NewConsensusMessage ( message ) ; } public serialize ( writer ) { this . message . serialize ( writer ) ; } } export class GetConsentedHeader { public static readonly code = 0x20 ; public readonly code = GetConsentedHeader . code ; public readonly type = Type . Request ; constructor ( public readonly height ) { } public static deserialize ( reader ) { const height = UInt64 . deserialize ( reader ) ; return new GetConsentedHeader ( height ) ; } public serialize ( writer ) { UInt64 . serialize ( this . height , writer ) ; } } export class ConsentedHeader { public static readonly code = 0x30 ; public readonly code = ConsentedHeader . code ; public readonly type = Type . Response ; constructor ( public readonly header , public readonly consensus , public readonly validatorSet ) { } public static deserialize ( reader ) { const header = BlockHeader . deserialize ( reader ) ; const consensus = Consensus . deserialize ( reader ) ; const validatorSet = ValidatorSet . deserialize ( reader ) ; return new ConsentedHeader ( header , consensus , validatorSet ) ; } public serialize ( writer ) { this . header . serialize ( writer ) ; this . consensus . serialize ( writer ) ; this . validatorSet . serialize ( writer ) ; } } export class GetHeaders { public static readonly code = 0x21 ; public readonly code = GetHeaders . code ; public readonly type = Type . Request ; constructor ( public readonly from , public readonly count ) { } public static deserialize ( reader ) { const from = UInt64 . deserialize ( reader ) ; const count = UInt32 . deserialize ( reader ) ; return new GetHeaders ( from , count ) ; } public serialize ( writer ) { UInt64 . serialize ( this . from , writer ) ; UInt32 . serialize ( this . count , writer ) ; } } export class Headers { public static readonly code = 0x31 ; public readonly code = Headers . code ; public readonly type = Type . Response ; constructor ( public readonly headers ) { } public static deserialize ( reader ) { const headers = List . deserialize ( BlockHeader . deserialize ) ( reader ) ; return new Headers ( headers ) ; } public serialize ( writer ) { List . serialize < BlockHeader > ( ( b , w ) => b . serialize ( w ) ) ( this . headers , writer ) ; } } export class GetBodies { public static readonly code = 0x22 ; public readonly code = GetBodies . code ; public readonly type = Type . Request ; constructor ( public readonly from , public readonly count ) { } public static deserialize ( reader ) { const from = UInt64 . deserialize ( reader ) ; const count = UInt32 . deserialize ( reader ) ; return new GetBodies ( from , count ) ; } public serialize ( writer ) { UInt64 . serialize ( this . from , writer ) ; UInt32 . serialize ( this . count , writer ) ; } } export class Bodies { public static readonly code = 0x32 ; public readonly code = Bodies . code ; public readonly type = Type . Response ; constructor ( public readonly bodies ) { } public static deserialize ( reader ) { const bodies = List . deserialize ( BlockBody . deserialize ) ( reader ) ; return new Bodies ( bodies ) ; } public serialize ( writer ) { List . serialize < BlockBody > ( ( b , w ) => b . serialize ( w ) ) ( this . bodies , writer ) ; } } export function serialize ( msg , writer ) { UInt8 . serialize ( msg . code , writer ) ; msg . serialize ( writer ) ; } export function deserialize ( reader ) { const code = UInt8 . deserialize ( reader ) ; switch ( code ) { case Hello . code : return Hello . deserialize ( reader ) ; case NewTransaction . code : return NewTransaction . deserialize ( reader ) ; case NewBlock . code : return NewBlock . deserialize ( reader ) ; case NewBlockHeight . code : return NewBlockHeight . deserialize ( reader ) ; case NewConsensusMessage . code : return NewConsensusMessage . deserialize ( reader ) ; case GetConsentedHeader . code : return GetConsentedHeader . deserialize ( reader ) ; case ConsentedHeader . code : return ConsentedHeader . deserialize ( reader ) ; case GetHeaders . code : return GetHeaders . deserialize ( reader ) ; case Headers . code : return Headers . deserialize ( reader ) ; case GetBodies . code : return GetBodies . deserialize ( reader ) ; case Bodies . code : return Bodies . deserialize ( reader ) ; default : throw new Error ( 'Unknown message code' ) ; } } }