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

Commit

Permalink
Merge pull request #5895 from trufflesuite/sne
Browse files Browse the repository at this point in the history
Reverse ENS resolution support in debugger and decoder
  • Loading branch information
haltman-at authored May 8, 2023
2 parents 226cb4d + fd97757 commit dfe233e
Show file tree
Hide file tree
Showing 53 changed files with 1,205 additions and 218 deletions.
12 changes: 8 additions & 4 deletions packages/codec/lib/abi-data/decode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ export function* decodeAbiReferenceByAddress(
return {
type: dataType,
kind: "value" as const,
value: decodedChildren
value: decodedChildren,
interpretations: {}
};

case "struct":
Expand Down Expand Up @@ -369,7 +370,8 @@ export function* decodeAbiReferenceStatic(
return {
type: dataType,
kind: "value" as const,
value: decodedChildren
value: decodedChildren,
interpretations: {}
};

case "struct":
Expand Down Expand Up @@ -451,7 +453,8 @@ function* decodeAbiStructByPosition(
return {
type: dataType,
kind: "value" as const,
value: decodedMembers
value: decodedMembers,
interpretations: {}
};
}

Expand Down Expand Up @@ -490,6 +493,7 @@ function* decodeAbiTupleByPosition(
return {
type: dataType,
kind: "value" as const,
value: decodedMembers
value: decodedMembers,
interpretations: {}
};
}
21 changes: 14 additions & 7 deletions packages/codec/lib/abify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ export function abifyResult(
value: {
asAddress: coercedResult.value.address,
rawAsHex: coercedResult.value.rawAddress
}
},
interpretations: coercedResult.interpretations
};
case "error":
switch (coercedResult.error.kind) {
Expand Down Expand Up @@ -213,7 +214,8 @@ export function abifyResult(
type: <Format.Types.TupleType>(
abifyType(result.type, userDefinedTypes)
), //note: may throw exception
value: abifiedMembers
value: abifiedMembers,
interpretations: coercedResult.interpretations
};
case "error":
return {
Expand All @@ -230,7 +232,8 @@ export function abifyResult(
case "value":
return abifyResult(coercedResult.value, userDefinedTypes);
case "error":
return <Format.Errors.BuiltInValueErrorResult>{ //I have no idea what TS is thinking here
return <Format.Errors.BuiltInValueErrorResult>{
//I have no idea what TS is thinking here
...coercedResult,
type: abifyType(result.type, userDefinedTypes)
};
Expand All @@ -252,7 +255,8 @@ export function abifyResult(
kind: "value",
value: {
asBN: coercedResult.value.numericAsBN.clone()
}
},
interpretations: coercedResult.interpretations
};
case "error":
switch (coercedResult.error.kind) {
Expand All @@ -262,7 +266,8 @@ export function abifyResult(
kind: "value",
value: {
asBN: coercedResult.error.rawAsBN.clone()
}
},
interpretations: {}
};
case "EnumPaddingError":
return {
Expand All @@ -282,7 +287,8 @@ export function abifyResult(
kind: "value",
value: {
asBN: numericValue
}
},
interpretations: {}
};
} else {
return {
Expand Down Expand Up @@ -319,7 +325,8 @@ export function abifyResult(
type: <Format.Types.ArrayType>(
abifyType(result.type, userDefinedTypes)
),
value: abifiedMembers
value: abifiedMembers,
interpretations: coercedResult.interpretations
};
case "error":
return {
Expand Down
3 changes: 2 additions & 1 deletion packages/codec/lib/ast-constant/decode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export function* decodeConstant(
kind: "value" as const,
value: {
asHex: Conversion.toHexString(bytes, size, true) //padding in case of short string literal
}
},
interpretations: {}
}; //we'll skip including a raw value, as that would be meaningless
}

Expand Down
106 changes: 83 additions & 23 deletions packages/codec/lib/basic/decode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import * as Conversion from "@truffle/codec/conversion";
import * as Format from "@truffle/codec/format";
import * as Contexts from "@truffle/codec/contexts";
import type * as Pointer from "@truffle/codec/pointer";
import type { DecoderRequest, DecoderOptions } from "@truffle/codec/types";
import type {
DecoderRequest,
DecoderOptions,
EnsPrimaryNameRequest
} from "@truffle/codec/types";
import type { PaddingMode, PaddingType } from "@truffle/codec/common";
import * as Evm from "@truffle/codec/evm";
import { handleDecodingError, StopDecodingError } from "@truffle/codec/errors";
import { byteLength } from "@truffle/codec/basic/allocate";
import { decodeString } from "@truffle/codec/bytes/decode";

export function* decodeBasic(
dataType: Format.Types.Type,
Expand Down Expand Up @@ -70,7 +75,8 @@ export function* decodeBasic(
//no idea why need coercion here
type: fullType,
kind: "value" as const,
value: underlyingResult
value: underlyingResult,
interpretations: {}
};
case "error":
//wrap the error and return an error result!
Expand Down Expand Up @@ -115,13 +121,15 @@ export function* decodeBasic(
return {
type: dataType,
kind: "value" as const,
value: { asBoolean: false }
value: { asBoolean: false },
interpretations: {}
};
} else if (numeric.eqn(1)) {
return {
type: dataType,
kind: "value" as const,
value: { asBoolean: true }
value: { asBoolean: true },
interpretations: {}
};
} else {
let error = {
Expand Down Expand Up @@ -164,7 +172,8 @@ export function* decodeBasic(
value: {
asBN: Conversion.toBN(bytes),
rawAsBN: Conversion.toBN(rawBytes)
}
},
interpretations: {}
};
case "int":
//first, check padding (if needed)
Expand All @@ -191,10 +200,11 @@ export function* decodeBasic(
value: {
asBN: Conversion.toSignedBN(bytes),
rawAsBN: Conversion.toSignedBN(rawBytes)
}
},
interpretations: {}
};

case "address":
case "address": {
if (!checkPadding(bytes, dataType, paddingMode)) {
let error = {
kind: "AddressPaddingError" as const,
Expand All @@ -211,16 +221,25 @@ export function* decodeBasic(
};
}
bytes = removePadding(bytes, dataType, paddingMode);
return {
const address = Evm.Utils.toAddress(bytes);
let decoded = {
type: dataType,
kind: "value" as const,
value: {
asAddress: Evm.Utils.toAddress(bytes),
asAddress: address,
rawAsHex: Conversion.toHexString(rawBytes)
}
},
interpretations: {}
};
//now: attach interpretations
const ensName = yield* reverseEnsResolve(address);
if (ensName !== null) {
decoded.interpretations = { ensName };
}
return decoded;
}

case "contract":
case "contract": {
if (!checkPadding(bytes, dataType, paddingMode)) {
let error = {
kind: "ContractPaddingError" as const,
Expand All @@ -241,11 +260,19 @@ export function* decodeBasic(
Format.Types.fullType(dataType, info.userDefinedTypes)
);
const contractValueInfo = yield* decodeContract(bytes, info);
return {
let decoded = {
type: fullType,
kind: "value" as const,
value: contractValueInfo
value: contractValueInfo,
interpretations: {}
};
//now: attach interpretations
const ensName = yield* reverseEnsResolve(contractValueInfo.address);
if (ensName !== null) {
decoded.interpretations = { ensName };
}
return decoded;
}

case "bytes":
//NOTE: we assume this is a *static* bytestring,
Expand Down Expand Up @@ -277,7 +304,8 @@ export function* decodeBasic(
value: {
asHex: Conversion.toHexString(bytes),
rawAsHex: Conversion.toHexString(rawBytes)
}
},
interpretations: {}
};

case "function":
Expand All @@ -304,11 +332,25 @@ export function* decodeBasic(
Evm.Utils.ADDRESS_SIZE,
Evm.Utils.ADDRESS_SIZE + Evm.Utils.SELECTOR_SIZE
);
return {
const valueInfo = yield* decodeExternalFunction(
address,
selector,
info
);
let decoded = {
type: dataType,
kind: "value" as const,
value: yield* decodeExternalFunction(address, selector, info)
value: valueInfo,
interpretations: {}
};
//now: attach interpretations
const contractEnsName = yield* reverseEnsResolve(
valueInfo.contract.address
);
if (contractEnsName !== null) {
decoded.interpretations = { contractEnsName };
}
return decoded;
case "internal":
//note: we used to error if we hit this point with strict === true,
//since internal function pointers don't go in the ABI, and strict
Expand Down Expand Up @@ -403,7 +445,8 @@ export function* decodeBasic(
value: {
name,
numericAsBN: numeric
}
},
interpretations: {}
};
} else {
let error = {
Expand Down Expand Up @@ -467,7 +510,8 @@ export function* decodeBasic(
value: {
asBig,
rawAsBig
}
},
interpretations: {}
};
}
case "ufixed": {
Expand Down Expand Up @@ -505,7 +549,8 @@ export function* decodeBasic(
value: {
asBig,
rawAsBig
}
},
interpretations: {}
};
}
}
Expand Down Expand Up @@ -620,7 +665,8 @@ function decodeInternalFunction(
context,
deployedProgramCounter: deployedPc,
constructorProgramCounter: constructorPc
}
},
interpretations: {}
};
}
//also before we continue: is the PC zero? if so let's just return that
Expand All @@ -633,7 +679,8 @@ function decodeInternalFunction(
context,
deployedProgramCounter: deployedPc,
constructorProgramCounter: constructorPc
}
},
interpretations: {}
};
}
//another check: is only the deployed PC zero?
Expand Down Expand Up @@ -699,7 +746,8 @@ function decodeInternalFunction(
context,
deployedProgramCounter: deployedPc,
constructorProgramCounter: constructorPc
}
},
interpretations: {}
};
}
const name = functionEntry.name;
Expand All @@ -718,7 +766,8 @@ function decodeInternalFunction(
id,
definedIn,
mutability
}
},
interpretations: {}
};
}

Expand Down Expand Up @@ -844,6 +893,17 @@ function checkPaddingSigned(bytes: Uint8Array, length: number): boolean {
return padding.every(paddingByte => paddingByte === signByte);
}

function* reverseEnsResolve(
address: string
): Generator<
EnsPrimaryNameRequest,
Format.Values.StringValueInfo | null,
Uint8Array | null
> {
const nameAsBytes = yield { type: "ens-primary-name" as const, address };
return nameAsBytes !== null ? decodeString(nameAsBytes) : null;
}

//the following types are intended for internal use only
/**
* @hidden
Expand Down
6 changes: 4 additions & 2 deletions packages/codec/lib/bytes/decode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ export function* decodeBytes(
kind: "value" as const,
value: {
asHex: Conversion.toHexString(bytes)
}
},
interpretations: {}
};

case "string":
return {
type: dataType,
kind: "value" as const,
value: decodeString(bytes)
value: decodeString(bytes),
interpretations: {}
};
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/codec/lib/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ export function cleanBool(
kind: "value",
value: {
asBoolean: true
}
},
interpretations: {}
};
default:
return result;
Expand Down
Loading

0 comments on commit dfe233e

Please sign in to comment.