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

Fix various decoding errors with reference types #1677

Merged
merged 9 commits into from Jan 30, 2019
Merged

Conversation

haltman-at
Copy link
Contributor

@haltman-at haltman-at commented Jan 29, 2019

This PR fixes a number of decoding errors with reference types. Specifically:

  1. Storage arrays whose base type is longer than a word now work properly.
  2. Mappings whose value type is longer than a word now work properly (if they didn't already?).
  3. Mappings whose value type is a signed integer shorter than a full word now work properly.
  4. Statically-sized arrays in memory now work properly.
  5. Struct types with mappings in them now work properly in memory. Actually, now structs in memory work at all! Turns out there was a bug causing errors when you attempted to decode any struct in memory.
  6. Decoding of storage local variables is now supported.

Also, the file truffle-decode-utils/allocation.ts has now been deleted; its contents have been moved into truffle-decoder/allocate/storage.ts, except for normalizeSlot, which is gone. (Turns out it was already unused, and I could have deleted it in my last PR like I wanted to, but I didn't notice that then.)

Note that in order to add support for local variables, I ended up rewriting decode/index.ts to change how the dispatching works there, and added a new file decode/stack.ts, along with some new entry points to storage and memory decoding (decodeStorage, decodeMemory, decodeStorageByAddress).

I also added a test for these fixes; it doesn't fit in well with the other decoding tests, but I figured it was better than not including it. Also incidentally I turned down the timeout on the variable ID tests from 30 seconds (!) to just 4 seconds. Hopefully that's not too short and this PR passes Travis without me having to turn that back up!

…or mappings in memory structs, remove the now-unused baseDefinition().
…storage.ts, adjusting references appropriately. (Also, fix an incorrect invocation of storageSize in mapping decoding.)
…ndex.ts so that it dispatches purely based on the pointer type (adding a new stack.ts for the stack), and add extra dispatcher functions in memory.ts and storage.ts to make this work.
…own the timeout on the ID tests from 30 seconds (!) to 4.
Copy link
Contributor

@gnidan gnidan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's an initial round of comments while we're waiting on Travis. they're mostly devoid of usefulness.

};

export interface Slot {
key?: any; // TODO:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's this TODO?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's preexisting -- I assume @seesemichaelj put that there. I assume it's about the fact that key is of type any, which should be narrowed down to something more specific (I figure I'll get to that when I get to going over mapping keys more generally). You're seeing it anew due to it having moved to a different file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe I'll wait until the new decoder output format, actually. In any case the point is I'll handle it eventually.

import BN from "bn.js";
import Web3 from "web3";
import { EvmStruct, EvmMapping } from "../interface/contract-decoder";
import clonedeep from "lodash.clonedeep";

export default async function decodeStorageReference(definition: DecodeUtils.AstDefinition, pointer: StoragePointer, info: EvmInfo, web3?: Web3, contractAddress?: string): Promise<any> {
export default async function decodeStorage(definition: DecodeUtils.AstDefinition, pointer: StoragePointer, info: EvmInfo, web3?: Web3, contractAddress?: string): Promise <any> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gosh this line is long

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then turn on prettier :P

@@ -269,7 +351,7 @@ export default async function decodeStorageReference(definition: DecodeUtils.Ast
}

result.members[memberName] =
await decode(definition.typeName.valueType, valuePointer, info, web3, contractAddress);
await decode(valueDefinition, valuePointer, info, web3, contractAddress);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gosh, changes to this file are sure getting difficult to read about. it all seems reasonable though. we should think about getting this more organized so it doesn't require as large a brain-cache size to look at PRs.

@coveralls
Copy link

coveralls commented Jan 29, 2019

Coverage Status

Coverage decreased (-33.5%) to 36.325% when pulling 63ca4b5 on reference-types into 28815e6 on develop.

…. Also, fix an error in decodeStorageByAddress where it didn't pass enough arguments to read.
@haltman-at
Copy link
Contributor Author

Let me add a comment on this most recent commit, because it changes the structure somewhat. Literal pointers are now no longer passed to decodeValue, since they might not be values (the old code got this right, silly me), but rather to decodeLiteral. Since literal pointers come from the stack (unless they're string literals), decodeLiteral resides in stack.ts, with decodeStack now calling it. String literals will be dispatched by decodeLiteral to decodeValue, but this is kind of a hack, and I'm intending to change this in a later PR.

Copy link
Contributor

@gnidan gnidan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exciting effort! Looks good @haltman-at

@haltman-at haltman-at merged commit 67ac461 into develop Jan 30, 2019
@haltman-at haltman-at deleted the reference-types branch January 30, 2019 02:18
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants