Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Merge e4e5822 into 017c42f
Browse files Browse the repository at this point in the history
  • Loading branch information
eggplantzzz committed Oct 21, 2019
2 parents 017c42f + e4e5822 commit 98e62c0
Show file tree
Hide file tree
Showing 52 changed files with 3,026 additions and 1,414 deletions.
449 changes: 284 additions & 165 deletions packages/codec/lib/allocate/abi.ts

Large diffs are not rendered by default.

380 changes: 234 additions & 146 deletions packages/codec/lib/allocate/storage.ts

Large diffs are not rendered by default.

228 changes: 137 additions & 91 deletions packages/codec/lib/core/decoding.ts

Large diffs are not rendered by default.

292 changes: 181 additions & 111 deletions packages/codec/lib/decode/abi.ts

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions packages/codec/lib/decode/constant.ts
Expand Up @@ -5,14 +5,17 @@ import * as CodecUtils from "../utils";
import { Types, Values } from "../format";
import read from "../read";
import decodeValue from "./value";
import { ConstantDefinitionPointer} from "../types/pointer";
import { ConstantDefinitionPointer } from "../types/pointer";
import { EvmInfo } from "../types/evm";
import { DecoderRequest } from "../types/request";
import { DecodingError } from "../decode/errors";
import BN from "bn.js";

export default function* decodeConstant(dataType: Types.Type, pointer: ConstantDefinitionPointer, info: EvmInfo): Generator<DecoderRequest, Values.Result, Uint8Array> {

export default function* decodeConstant(
dataType: Types.Type,
pointer: ConstantDefinitionPointer,
info: EvmInfo
): Generator<DecoderRequest, Values.Result, Uint8Array> {
debug("pointer %o", pointer);

//normally, we just dispatch to decodeValue.
Expand All @@ -21,13 +24,12 @@ export default function* decodeConstant(dataType: Types.Type, pointer: ConstantD
//of the word, but readDefinition will put them at the *end* of the
//word. So we'll have to adjust things ourselves.

if(dataType.typeClass === "bytes" && dataType.kind === "static") {
if (dataType.typeClass === "bytes" && dataType.kind === "static") {
let size = dataType.length;
let word: Uint8Array;
try {
word = yield* read(pointer, info.state);
}
catch(error) {
} catch (error) {
return {
type: dataType,
kind: "error" as const,
Expand Down
14 changes: 10 additions & 4 deletions packages/codec/lib/decode/event.ts
Expand Up @@ -11,17 +11,23 @@ import { DecoderOptions } from "../types/options";
import { DecoderRequest } from "../types/request";
import { StopDecodingError } from "../decode/errors";

export default function* decodeTopic(dataType: Types.Type, pointer: EventTopicPointer, info: EvmInfo, options: DecoderOptions = {}): Generator<DecoderRequest, Values.Result, Uint8Array> {
if(TypeUtils.isReferenceType(dataType)) {
export default function* decodeTopic(
dataType: Types.Type,
pointer: EventTopicPointer,
info: EvmInfo,
options: DecoderOptions = {}
): Generator<DecoderRequest, Values.Result, Uint8Array> {
if (TypeUtils.isReferenceType(dataType)) {
//we cannot decode reference types "stored" in topics; we have to just return an error
let bytes: Uint8Array = yield* read(pointer, info.state);
let raw: string = ConversionUtils.toHexString(bytes);
//NOTE: even in strict mode we want to just return this, not throw an error here
return <Errors.ErrorResult> { //dunno why TS is failing here
return <Errors.ErrorResult>{
//dunno why TS is failing here
type: dataType,
kind: "error" as const,
error: {
kind: "IndexedReferenceTypeError" as const,
kind: "IndexedReferenceTypeError" as const,
type: dataType,
raw
}
Expand Down
94 changes: 53 additions & 41 deletions packages/codec/lib/decode/memory.ts
Expand Up @@ -13,24 +13,31 @@ import { EvmInfo } from "../types/evm";
import { DecoderRequest } from "../types/request";
import { DecodingError } from "../decode/errors";

export default function* decodeMemory(dataType: Types.Type, pointer: MemoryPointer, info: EvmInfo): Generator<DecoderRequest, Values.Result, Uint8Array> {
if(TypeUtils.isReferenceType(dataType)) {
export default function* decodeMemory(
dataType: Types.Type,
pointer: MemoryPointer,
info: EvmInfo
): Generator<DecoderRequest, Values.Result, Uint8Array> {
if (TypeUtils.isReferenceType(dataType)) {
return yield* decodeMemoryReferenceByAddress(dataType, pointer, info);
}
else {
} else {
return yield* decodeValue(dataType, pointer, info);
}
}

export function* decodeMemoryReferenceByAddress(dataType: Types.ReferenceType, pointer: DataPointer, info: EvmInfo): Generator<DecoderRequest, Values.Result, Uint8Array> {
export function* decodeMemoryReferenceByAddress(
dataType: Types.ReferenceType,
pointer: DataPointer,
info: EvmInfo
): Generator<DecoderRequest, Values.Result, Uint8Array> {
const { state } = info;
// debug("pointer %o", pointer);
let rawValue: Uint8Array;
try {
rawValue = yield* read(pointer, state);
}
catch(error) {
return <Errors.ErrorResult> { //dunno why TS is failing here
} catch (error) {
return <Errors.ErrorResult>{
//dunno why TS is failing here
type: dataType,
kind: "error" as const,
error: (<DecodingError>error).error
Expand All @@ -41,9 +48,9 @@ export function* decodeMemoryReferenceByAddress(dataType: Types.ReferenceType, p
let startPosition: number;
try {
startPosition = startPositionAsBN.toNumber();
}
catch(_) {
return <Errors.ErrorResult> { //again with the TS failures...
} catch (_) {
return <Errors.ErrorResult>{
//again with the TS failures...
type: dataType,
kind: "error" as const,
error: {
Expand All @@ -57,19 +64,21 @@ export function* decodeMemoryReferenceByAddress(dataType: Types.ReferenceType, p
let length: number;

switch (dataType.typeClass) {

case "bytes":
case "string":
//initial word contains length
try {
rawLength = yield* read({
location: "memory" as const,
start: startPosition,
length: CodecUtils.EVM.WORD_SIZE
}, state);
}
catch(error) {
return <Errors.ErrorResult> { //dunno why TS is failing here
rawLength = yield* read(
{
location: "memory" as const,
start: startPosition,
length: CodecUtils.EVM.WORD_SIZE
},
state
);
} catch (error) {
return <Errors.ErrorResult>{
//dunno why TS is failing here
type: dataType,
kind: "error" as const,
error: (<DecodingError>error).error
Expand All @@ -78,9 +87,9 @@ export function* decodeMemoryReferenceByAddress(dataType: Types.ReferenceType, p
lengthAsBN = CodecUtils.Conversion.toBN(rawLength);
try {
length = lengthAsBN.toNumber();
}
catch(_) {
return <Errors.BytesDynamicErrorResult|Errors.StringErrorResult> { //again with the TS failures...
} catch (_) {
return <Errors.BytesDynamicErrorResult | Errors.StringErrorResult>{
//again with the TS failures...
type: dataType,
kind: "error" as const,
error: {
Expand All @@ -99,17 +108,18 @@ export function* decodeMemoryReferenceByAddress(dataType: Types.ReferenceType, p
return yield* decodeValue(dataType, childPointer, info);

case "array":

if (dataType.kind === "dynamic") {
//initial word contains array length
try {
rawLength = yield* read({
location: "memory" as const,
start: startPosition,
length: CodecUtils.EVM.WORD_SIZE
}, state);
}
catch(error) {
rawLength = yield* read(
{
location: "memory" as const,
start: startPosition,
length: CodecUtils.EVM.WORD_SIZE
},
state
);
} catch (error) {
return {
type: dataType,
kind: "error" as const,
Expand All @@ -119,14 +129,12 @@ export function* decodeMemoryReferenceByAddress(dataType: Types.ReferenceType, p
lengthAsBN = CodecUtils.Conversion.toBN(rawLength);
startPosition += CodecUtils.EVM.WORD_SIZE; //increment startPosition
//to next word, as first word was used for length
}
else {
} else {
lengthAsBN = dataType.length;
}
try {
length = lengthAsBN.toNumber();
}
catch(_) {
} catch (_) {
return {
type: dataType,
kind: "error" as const,
Expand All @@ -140,7 +148,7 @@ export function* decodeMemoryReferenceByAddress(dataType: Types.ReferenceType, p
let baseType = dataType.baseType;

let decodedChildren: Values.Result[] = [];
for(let index = 0; index < length; index++) {
for (let index = 0; index < length; index++) {
decodedChildren.push(
yield* decodeMemory(
baseType,
Expand All @@ -161,11 +169,14 @@ export function* decodeMemoryReferenceByAddress(dataType: Types.ReferenceType, p
};

case "struct":
const { allocations: { memory: allocations }, userDefinedTypes } = info;
const {
allocations: { memory: allocations },
userDefinedTypes
} = info;

const typeId = dataType.id;
const structAllocation = allocations[parseInt(typeId)];
if(!structAllocation) {
if (!structAllocation) {
return {
type: dataType,
kind: "error" as const,
Expand All @@ -179,7 +190,7 @@ export function* decodeMemoryReferenceByAddress(dataType: Types.ReferenceType, p
debug("structAllocation %O", structAllocation);

let decodedMembers: Values.NameValuePair[] = [];
for(let index = 0; index < structAllocation.members.length; index++) {
for (let index = 0; index < structAllocation.members.length; index++) {
const memberAllocation = structAllocation.members[index];
const memberPointer = memberAllocation.pointer;
const childPointer: MemoryPointer = {
Expand All @@ -190,8 +201,9 @@ export function* decodeMemoryReferenceByAddress(dataType: Types.ReferenceType, p

let memberName = memberAllocation.definition.name;
let storedType = <Types.StructType>userDefinedTypes[typeId];
if(!storedType) {
return <Errors.ErrorResult> { //dunno why TS is failing here
if (!storedType) {
return <Errors.ErrorResult>{
//dunno why TS is failing here
type: dataType,
kind: "error" as const,
error: {
Expand Down

0 comments on commit 98e62c0

Please sign in to comment.