Skip to content

Commit

Permalink
refactor(sdk): update with SDK's structpb usage (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
zfy0701 authored Dec 30, 2022
1 parent bcb7546 commit 775c39d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 71 deletions.
9 changes: 4 additions & 5 deletions protos/processor.proto
Original file line number Diff line number Diff line change
Expand Up @@ -268,19 +268,18 @@ message RawTransaction {
message Data {
// Every Handler type should have a data type
message EthLog {
bytes data = 1;
google.protobuf.Struct log = 3;
optional bytes transaction = 2;
}
message EthBlock {
bytes data = 1;
google.protobuf.Struct block = 2;
}
message EthTransaction {
bytes data = 1;
optional bytes transaction = 2;
google.protobuf.Struct transaction = 4;
optional bytes transaction_receipt = 3;
}
message EthTrace {
bytes data = 1;
google.protobuf.Struct trace = 4;
optional bytes transaction = 2;
optional bytes transaction_receipt = 3;
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/core/account-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TransferEvent as ERC20TransferEvent } from '../builtin/internal/ERC20'
import { TransferEvent as ERC721TransferEvent } from '../builtin/internal/ERC721'
import { AccountContext } from './context'
import { PromiseOrVoid } from '../promise-or-void'
import { Event, EventFilter } from '@ethersproject/contracts'
import { Event } from '@ethersproject/contracts'
import { BytesLike } from '@ethersproject/bytes'
import { AddressOrTypeEventFilter, EventsHandler } from './base-processor'

Expand Down
83 changes: 34 additions & 49 deletions sdk/src/gen/processor/protos/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,22 +474,21 @@ export interface Data {
}

export interface Data_EthLog {
data: Uint8Array;
log: { [key: string]: any } | undefined;
transaction?: Uint8Array | undefined;
}

export interface Data_EthBlock {
data: Uint8Array;
block: { [key: string]: any } | undefined;
}

export interface Data_EthTransaction {
data: Uint8Array;
transaction?: Uint8Array | undefined;
transaction: { [key: string]: any } | undefined;
transactionReceipt?: Uint8Array | undefined;
}

export interface Data_EthTrace {
data: Uint8Array;
trace: { [key: string]: any } | undefined;
transaction?: Uint8Array | undefined;
transactionReceipt?: Uint8Array | undefined;
}
Expand Down Expand Up @@ -3003,13 +3002,13 @@ export const Data = {
};

function createBaseData_EthLog(): Data_EthLog {
return { data: new Uint8Array(), transaction: undefined };
return { log: undefined, transaction: undefined };
}

export const Data_EthLog = {
encode(message: Data_EthLog, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.data.length !== 0) {
writer.uint32(10).bytes(message.data);
if (message.log !== undefined) {
Struct.encode(Struct.wrap(message.log), writer.uint32(26).fork()).ldelim();
}
if (message.transaction !== undefined) {
writer.uint32(18).bytes(message.transaction);
Expand All @@ -3024,8 +3023,8 @@ export const Data_EthLog = {
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.data = reader.bytes();
case 3:
message.log = Struct.unwrap(Struct.decode(reader, reader.uint32()));
break;
case 2:
message.transaction = reader.bytes();
Expand All @@ -3040,36 +3039,35 @@ export const Data_EthLog = {

fromJSON(object: any): Data_EthLog {
return {
data: isSet(object.data) ? bytesFromBase64(object.data) : new Uint8Array(),
log: isObject(object.log) ? object.log : undefined,
transaction: isSet(object.transaction) ? bytesFromBase64(object.transaction) : undefined,
};
},

toJSON(message: Data_EthLog): unknown {
const obj: any = {};
message.data !== undefined &&
(obj.data = base64FromBytes(message.data !== undefined ? message.data : new Uint8Array()));
message.log !== undefined && (obj.log = message.log);
message.transaction !== undefined &&
(obj.transaction = message.transaction !== undefined ? base64FromBytes(message.transaction) : undefined);
return obj;
},

fromPartial(object: DeepPartial<Data_EthLog>): Data_EthLog {
const message = createBaseData_EthLog();
message.data = object.data ?? new Uint8Array();
message.log = object.log ?? undefined;
message.transaction = object.transaction ?? undefined;
return message;
},
};

function createBaseData_EthBlock(): Data_EthBlock {
return { data: new Uint8Array() };
return { block: undefined };
}

export const Data_EthBlock = {
encode(message: Data_EthBlock, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.data.length !== 0) {
writer.uint32(10).bytes(message.data);
if (message.block !== undefined) {
Struct.encode(Struct.wrap(message.block), writer.uint32(18).fork()).ldelim();
}
return writer;
},
Expand All @@ -3081,8 +3079,8 @@ export const Data_EthBlock = {
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.data = reader.bytes();
case 2:
message.block = Struct.unwrap(Struct.decode(reader, reader.uint32()));
break;
default:
reader.skipType(tag & 7);
Expand All @@ -3093,34 +3091,30 @@ export const Data_EthBlock = {
},

fromJSON(object: any): Data_EthBlock {
return { data: isSet(object.data) ? bytesFromBase64(object.data) : new Uint8Array() };
return { block: isObject(object.block) ? object.block : undefined };
},

toJSON(message: Data_EthBlock): unknown {
const obj: any = {};
message.data !== undefined &&
(obj.data = base64FromBytes(message.data !== undefined ? message.data : new Uint8Array()));
message.block !== undefined && (obj.block = message.block);
return obj;
},

fromPartial(object: DeepPartial<Data_EthBlock>): Data_EthBlock {
const message = createBaseData_EthBlock();
message.data = object.data ?? new Uint8Array();
message.block = object.block ?? undefined;
return message;
},
};

function createBaseData_EthTransaction(): Data_EthTransaction {
return { data: new Uint8Array(), transaction: undefined, transactionReceipt: undefined };
return { transaction: undefined, transactionReceipt: undefined };
}

export const Data_EthTransaction = {
encode(message: Data_EthTransaction, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.data.length !== 0) {
writer.uint32(10).bytes(message.data);
}
if (message.transaction !== undefined) {
writer.uint32(18).bytes(message.transaction);
Struct.encode(Struct.wrap(message.transaction), writer.uint32(34).fork()).ldelim();
}
if (message.transactionReceipt !== undefined) {
writer.uint32(26).bytes(message.transactionReceipt);
Expand All @@ -3135,11 +3129,8 @@ export const Data_EthTransaction = {
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.data = reader.bytes();
break;
case 2:
message.transaction = reader.bytes();
case 4:
message.transaction = Struct.unwrap(Struct.decode(reader, reader.uint32()));
break;
case 3:
message.transactionReceipt = reader.bytes();
Expand All @@ -3154,18 +3145,14 @@ export const Data_EthTransaction = {

fromJSON(object: any): Data_EthTransaction {
return {
data: isSet(object.data) ? bytesFromBase64(object.data) : new Uint8Array(),
transaction: isSet(object.transaction) ? bytesFromBase64(object.transaction) : undefined,
transaction: isObject(object.transaction) ? object.transaction : undefined,
transactionReceipt: isSet(object.transactionReceipt) ? bytesFromBase64(object.transactionReceipt) : undefined,
};
},

toJSON(message: Data_EthTransaction): unknown {
const obj: any = {};
message.data !== undefined &&
(obj.data = base64FromBytes(message.data !== undefined ? message.data : new Uint8Array()));
message.transaction !== undefined &&
(obj.transaction = message.transaction !== undefined ? base64FromBytes(message.transaction) : undefined);
message.transaction !== undefined && (obj.transaction = message.transaction);
message.transactionReceipt !== undefined && (obj.transactionReceipt = message.transactionReceipt !== undefined
? base64FromBytes(message.transactionReceipt)
: undefined);
Expand All @@ -3174,21 +3161,20 @@ export const Data_EthTransaction = {

fromPartial(object: DeepPartial<Data_EthTransaction>): Data_EthTransaction {
const message = createBaseData_EthTransaction();
message.data = object.data ?? new Uint8Array();
message.transaction = object.transaction ?? undefined;
message.transactionReceipt = object.transactionReceipt ?? undefined;
return message;
},
};

function createBaseData_EthTrace(): Data_EthTrace {
return { data: new Uint8Array(), transaction: undefined, transactionReceipt: undefined };
return { trace: undefined, transaction: undefined, transactionReceipt: undefined };
}

export const Data_EthTrace = {
encode(message: Data_EthTrace, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.data.length !== 0) {
writer.uint32(10).bytes(message.data);
if (message.trace !== undefined) {
Struct.encode(Struct.wrap(message.trace), writer.uint32(34).fork()).ldelim();
}
if (message.transaction !== undefined) {
writer.uint32(18).bytes(message.transaction);
Expand All @@ -3206,8 +3192,8 @@ export const Data_EthTrace = {
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.data = reader.bytes();
case 4:
message.trace = Struct.unwrap(Struct.decode(reader, reader.uint32()));
break;
case 2:
message.transaction = reader.bytes();
Expand All @@ -3225,16 +3211,15 @@ export const Data_EthTrace = {

fromJSON(object: any): Data_EthTrace {
return {
data: isSet(object.data) ? bytesFromBase64(object.data) : new Uint8Array(),
trace: isObject(object.trace) ? object.trace : undefined,
transaction: isSet(object.transaction) ? bytesFromBase64(object.transaction) : undefined,
transactionReceipt: isSet(object.transactionReceipt) ? bytesFromBase64(object.transactionReceipt) : undefined,
};
},

toJSON(message: Data_EthTrace): unknown {
const obj: any = {};
message.data !== undefined &&
(obj.data = base64FromBytes(message.data !== undefined ? message.data : new Uint8Array()));
message.trace !== undefined && (obj.trace = message.trace);
message.transaction !== undefined &&
(obj.transaction = message.transaction !== undefined ? base64FromBytes(message.transaction) : undefined);
message.transactionReceipt !== undefined && (obj.transactionReceipt = message.transactionReceipt !== undefined
Expand All @@ -3245,7 +3230,7 @@ export const Data_EthTrace = {

fromPartial(object: DeepPartial<Data_EthTrace>): Data_EthTrace {
const message = createBaseData_EthTrace();
message.data = object.data ?? new Uint8Array();
message.trace = object.trace ?? undefined;
message.transaction = object.transaction ?? undefined;
message.transactionReceipt = object.transactionReceipt ?? undefined;
return message;
Expand Down
36 changes: 26 additions & 10 deletions sdk/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,19 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
}

const promises: Promise<ProcessResult>[] = []
const jsonString = Utf8ArrayToStr(request.data.ethLog?.data || request.data.raw)
const log: Log = JSON.parse(jsonString)
let log: Log
if (request.data.ethLog) {
log = request.data.ethLog.log as Log
} else {
const jsonString = Utf8ArrayToStr(request.data.raw)
log = JSON.parse(jsonString)
}

for (const handlerId of request.handlerIds) {
const handler = this.eventHandlers[handlerId]
promises.push(
handler(log).catch((e) => {
throw new ServerError(Status.INTERNAL, 'error processing log: ' + jsonString + '\n' + errorString(e))
throw new ServerError(Status.INTERNAL, 'error processing log: ' + JSON.stringify(log) + '\n' + errorString(e))
})
)
}
Expand Down Expand Up @@ -574,10 +579,13 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
if (!binding.data) {
throw new ServerError(Status.INVALID_ARGUMENT, "Block can't be empty")
}

const jsonString = Utf8ArrayToStr(binding.data.ethBlock?.data || binding.data.raw)

const block: Block = JSON.parse(jsonString)
let block: Block
if (binding.data.ethBlock) {
block = binding.data.ethBlock.block as Block
} else {
const jsonString = Utf8ArrayToStr(binding.data.raw)
block = JSON.parse(jsonString)
}

const promises: Promise<ProcessResult>[] = []
for (const handlerId of binding.handlerIds) {
Expand All @@ -594,15 +602,23 @@ export class ProcessorServiceImpl implements ProcessorServiceImplementation {
if (!binding.data) {
throw new ServerError(Status.INVALID_ARGUMENT, "Trace can't be empty")
}
const jsonString = Utf8ArrayToStr(binding.data.ethTrace?.data || binding.data.raw)
const trace: Trace = JSON.parse(jsonString)
let trace: Trace
if (binding.data.ethTrace) {
trace = binding.data.ethTrace.trace as Trace
} else {
const jsonString = Utf8ArrayToStr(binding.data.raw)
trace = JSON.parse(jsonString)
}

const promises: Promise<ProcessResult>[] = []

for (const handlerId of binding.handlerIds) {
promises.push(
this.traceHandlers[handlerId](trace).catch((e) => {
throw new ServerError(Status.INTERNAL, 'error processing trace: ' + jsonString + '\n' + errorString(e))
throw new ServerError(
Status.INTERNAL,
'error processing trace: ' + JSON.stringify(trace) + '\n' + errorString(e)
)
})
)
}
Expand Down
10 changes: 4 additions & 6 deletions sdk/src/testing/test-processor-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class TestProcessorServer implements ProcessorServiceImplementation {
return {
data: {
raw: new Uint8Array(),
ethTrace: { data: toBytes(trace) },
ethTrace: { trace },
},
handlerIds: [config.handlerId],
handlerType: HandlerType.ETH_TRACE,
Expand Down Expand Up @@ -177,7 +177,7 @@ export class TestProcessorServer implements ProcessorServiceImplementation {
return {
data: {
raw: new Uint8Array(),
ethLog: { data: toBytes(log) },
ethLog: { log },
},
handlerIds: [config.handlerId],
handlerType: HandlerType.ETH_LOG,
Expand Down Expand Up @@ -239,7 +239,7 @@ export class TestProcessorServer implements ProcessorServiceImplementation {
return {
data: {
raw: new Uint8Array(),
ethLog: { data: toBytes(log) },
ethLog: { log },
},
handlerIds: [config.handlerId],
handlerType: HandlerType.ETH_LOG,
Expand Down Expand Up @@ -273,9 +273,7 @@ export class TestProcessorServer implements ProcessorServiceImplementation {
const binding: DataBinding = {
data: {
raw: new Uint8Array(),
ethBlock: {
data: toBytes(block),
},
ethBlock: { block },
},
handlerType: HandlerType.ETH_BLOCK,
handlerIds: [],
Expand Down

0 comments on commit 775c39d

Please sign in to comment.