simplify some code

pull/5370/head
Iuri Matias 4 years ago
parent 5885b495fe
commit 6b21d57acb
  1. 3
      libs/remix-debug/src/solidity-decoder/types/Address.js
  2. 12
      libs/remix-debug/src/solidity-decoder/types/ArrayType.js
  3. 33
      libs/remix-debug/src/solidity-decoder/types/DynamicByteArray.js
  4. 11
      libs/remix-debug/src/solidity-decoder/types/Mapping.js
  5. 15
      libs/remix-debug/src/solidity-decoder/types/RefType.js
  6. 6
      libs/remix-debug/src/solidity-decoder/types/StringType.js
  7. 10
      libs/remix-debug/src/solidity-decoder/types/Struct.js
  8. 20
      libs/remix-debug/src/solidity-decoder/types/ValueType.js
  9. 30
      libs/remix-debug/src/solidity-decoder/types/util.js

@ -10,9 +10,8 @@ class Address extends ValueType {
decodeValue (value) { decodeValue (value) {
if (!value) { if (!value) {
return '0x0000000000000000000000000000000000000000' return '0x0000000000000000000000000000000000000000'
} else {
return '0x' + util.extractHexByteSlice(value, this.storageBytes, 0).toUpperCase()
} }
return '0x' + util.extractHexByteSlice(value, this.storageBytes, 0).toUpperCase()
} }
} }

@ -69,11 +69,7 @@ class ArrayType extends RefType {
currentLocation.offset = 0 currentLocation.offset = 0
} }
} }
return { return {value: ret, length: '0x' + size.toString(16), type: this.typeName}
value: ret,
length: '0x' + size.toString(16),
type: this.typeName
}
} }
decodeFromMemoryInternal (offset, memory) { decodeFromMemoryInternal (offset, memory) {
@ -89,11 +85,7 @@ class ArrayType extends RefType {
ret.push(this.underlyingType.decodeFromMemory(contentOffset, memory)) ret.push(this.underlyingType.decodeFromMemory(contentOffset, memory))
offset += 32 offset += 32
} }
return { return {value: ret, length: '0x' + length.toString(16), type: this.typeName}
value: ret,
length: '0x' + length.toString(16),
type: this.typeName
}
} }
} }

@ -16,10 +16,7 @@ class DynamicByteArray extends RefType {
value = await util.extractHexValue(location, storageResolver, this.storageBytes) value = await util.extractHexValue(location, storageResolver, this.storageBytes)
} catch (e) { } catch (e) {
console.log(e) console.log(e)
return { return {value: '<decoding failed - ' + e.message + '>', type: this.typeName}
value: '<decoding failed - ' + e.message + '>',
type: this.typeName
}
} }
const bn = new BN(value, 16) const bn = new BN(value, 16)
if (bn.testn(0)) { if (bn.testn(0)) {
@ -31,10 +28,7 @@ class DynamicByteArray extends RefType {
currentSlot = await util.readFromStorage(dataPos, storageResolver) currentSlot = await util.readFromStorage(dataPos, storageResolver)
} catch (e) { } catch (e) {
console.log(e) console.log(e)
return { return {value: '<decoding failed - ' + e.message + '>', type: this.typeName}
value: '<decoding failed - ' + e.message + '>',
type: this.typeName
}
} }
while (length.gt(ret.length) && ret.length < 32000) { while (length.gt(ret.length) && ret.length < 32000) {
currentSlot = currentSlot.replace('0x', '') currentSlot = currentSlot.replace('0x', '')
@ -44,24 +38,13 @@ class DynamicByteArray extends RefType {
currentSlot = await util.readFromStorage(dataPos, storageResolver) currentSlot = await util.readFromStorage(dataPos, storageResolver)
} catch (e) { } catch (e) {
console.log(e) console.log(e)
return { return {value: '<decoding failed - ' + e.message + '>', type: this.typeName}
value: '<decoding failed - ' + e.message + '>',
type: this.typeName
}
} }
} }
return { return {value: '0x' + ret.replace(/(00)+$/, ''), length: '0x' + length.toString(16), type: this.typeName}
value: '0x' + ret.replace(/(00)+$/, ''),
length: '0x' + length.toString(16),
type: this.typeName
}
} else { } else {
var size = parseInt(value.substr(value.length - 2, 2), 16) / 2 var size = parseInt(value.substr(value.length - 2, 2), 16) / 2
return { return {value: '0x' + value.substr(0, size * 2), length: '0x' + size.toString(16), type: this.typeName}
value: '0x' + value.substr(0, size * 2),
length: '0x' + size.toString(16),
type: this.typeName
}
} }
} }
@ -69,11 +52,7 @@ class DynamicByteArray extends RefType {
offset = 2 * offset offset = 2 * offset
let length = memory.substr(offset, 64) let length = memory.substr(offset, 64)
length = 2 * parseInt(length, 16) length = 2 * parseInt(length, 16)
return { return {length: '0x' + length.toString(16), value: '0x' + memory.substr(offset + 64, length), type: this.typeName}
length: '0x' + length.toString(16),
value: '0x' + memory.substr(offset + 64, length),
type: this.typeName
}
} }
} }

@ -28,20 +28,13 @@ class Mapping extends RefType {
const mappingPreimages = await storageResolver.mappingsLocation(corrections) const mappingPreimages = await storageResolver.mappingsLocation(corrections)
let ret = await this.decodeMappingsLocation(mappingPreimages, location, storageResolver) // fetch mapping storage changes let ret = await this.decodeMappingsLocation(mappingPreimages, location, storageResolver) // fetch mapping storage changes
ret = Object.assign({}, this.initialDecodedState, ret) // merge changes ret = Object.assign({}, this.initialDecodedState, ret) // merge changes
return { return {value: ret, type: this.typeName}
value: ret,
type: this.typeName
}
} }
decodeFromMemoryInternal (offset, memory) { decodeFromMemoryInternal (offset, memory) {
// mappings can only exist in storage and not in memory // mappings can only exist in storage and not in memory
// so this should never be called // so this should never be called
return { return {value: '<not implemented>', length: '0x', type: this.typeName}
value: '<not implemented>',
length: '0x',
type: this.typeName
}
} }
async decodeMappingsLocation (preimages, location, storageResolver) { async decodeMappingsLocation (preimages, location, storageResolver) {

@ -21,10 +21,7 @@ class RefType {
*/ */
async decodeFromStack (stackDepth, stack, memory, storageResolver) { async decodeFromStack (stackDepth, stack, memory, storageResolver) {
if (stack.length - 1 < stackDepth) { if (stack.length - 1 < stackDepth) {
return { return {error: '<decoding failed - stack underflow ' + stackDepth + '>', type: this.typeName}
error: '<decoding failed - stack underflow ' + stackDepth + '>',
type: this.typeName
}
} }
let offset = stack[stack.length - 1 - stackDepth] let offset = stack[stack.length - 1 - stackDepth]
if (this.isInStorage()) { if (this.isInStorage()) {
@ -33,19 +30,13 @@ class RefType {
return await this.decodeFromStorage({ offset: 0, slot: offset }, storageResolver) return await this.decodeFromStorage({ offset: 0, slot: offset }, storageResolver)
} catch (e) { } catch (e) {
console.log(e) console.log(e)
return { return {error: '<decoding failed - ' + e.message + '>', type: this.typeName}
error: '<decoding failed - ' + e.message + '>',
type: this.typeName
}
} }
} else if (this.isInMemory()) { } else if (this.isInMemory()) {
offset = parseInt(offset, 16) offset = parseInt(offset, 16)
return this.decodeFromMemoryInternal(offset, memory) return this.decodeFromMemoryInternal(offset, memory)
} else { } else {
return { return {error: '<decoding failed - no decoder for ' + this.location + '>', type: this.typeName}
error: '<decoding failed - no decoder for ' + this.location + '>',
type: this.typeName
}
} }
} }

@ -39,11 +39,7 @@ function format (decoded) {
} }
let value = decoded.value let value = decoded.value
value = value.replace('0x', '').replace(/(..)/g, '%$1') value = value.replace('0x', '').replace(/(..)/g, '%$1')
const ret = { const ret = {length: decoded.length, raw: decoded.value, type: 'string'}
length: decoded.length,
raw: decoded.value,
type: 'string'
}
try { try {
ret.value = decodeURIComponent(value) ret.value = decodeURIComponent(value)
} catch (e) { } catch (e) {

@ -22,10 +22,7 @@ class Struct extends RefType {
ret[item.name] = '<decoding failed - ' + e.message + '>' ret[item.name] = '<decoding failed - ' + e.message + '>'
} }
} }
return { return {value: ret, type: this.typeName}
value: ret,
type: this.typeName
}
} }
decodeFromMemoryInternal (offset, memory) { decodeFromMemoryInternal (offset, memory) {
@ -36,10 +33,7 @@ class Struct extends RefType {
ret[item.name] = member ret[item.name] = member
offset += 32 offset += 32
}) })
return { return {value: ret, type: this.typeName}
value: ret,
type: this.typeName
}
} }
} }

@ -19,16 +19,10 @@ class ValueType {
async decodeFromStorage (location, storageResolver) { async decodeFromStorage (location, storageResolver) {
try { try {
var value = await util.extractHexValue(location, storageResolver, this.storageBytes) var value = await util.extractHexValue(location, storageResolver, this.storageBytes)
return { return {value: this.decodeValue(value), type: this.typeName}
value: this.decodeValue(value),
type: this.typeName
}
} catch (e) { } catch (e) {
console.log(e) console.log(e)
return { return {value: '<decoding failed - ' + e.message + '>', type: this.typeName}
value: '<decoding failed - ' + e.message + '>',
type: this.typeName
}
} }
} }
@ -47,10 +41,7 @@ class ValueType {
} else { } else {
value = this.decodeValue(stack[stack.length - 1 - stackDepth].replace('0x', '')) value = this.decodeValue(stack[stack.length - 1 - stackDepth].replace('0x', ''))
} }
return { return {value, type: this.typeName}
value: value,
type: this.typeName
}
} }
/** /**
@ -62,10 +53,7 @@ class ValueType {
*/ */
decodeFromMemory (offset, memory) { decodeFromMemory (offset, memory) {
let value = memory.substr(2 * offset, 64) let value = memory.substr(2 * offset, 64)
return { return {value: this.decodeValue(value), type: this.typeName}
value: this.decodeValue(value),
type: this.typeName
}
} }
} }

@ -2,20 +2,6 @@
const ethutil = require('ethereumjs-util') const ethutil = require('ethereumjs-util')
const BN = require('ethereumjs-util').BN const BN = require('ethereumjs-util').BN
module.exports = {
readFromStorage: readFromStorage,
decodeIntFromHex: decodeIntFromHex,
extractHexValue: extractHexValue,
extractHexByteSlice: extractHexByteSlice,
toBN: toBN,
add: add,
sub: sub,
extractLocation: extractLocation,
removeLocation: removeLocation,
normalizeHex: normalizeHex,
extractLocationFromAstVariable: extractLocationFromAstVariable
}
function decodeIntFromHex (value, byteLength, signed) { function decodeIntFromHex (value, byteLength, signed) {
let bigNumber = new BN(value, 16) let bigNumber = new BN(value, 16)
if (signed) { if (signed) {
@ -24,21 +10,17 @@ function decodeIntFromHex (value, byteLength, signed) {
return bigNumber.toString(10) return bigNumber.toString(10)
} }
function readFromStorage (slot, storageResolver) { function readFromStorage(slot, storageResolver) {
const hexSlot = '0x' + normalizeHex(ethutil.bufferToHex(slot)) const hexSlot = '0x' + normalizeHex(ethutil.bufferToHex(slot))
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
storageResolver.storageSlot(hexSlot, (error, slot) => { storageResolver.storageSlot(hexSlot, (error, slot) => {
if (error) { if (error) {
return reject(error) return reject(error)
} else {
if (!slot) {
slot = {
key: slot,
value: ''
}
}
return resolve(normalizeHex(slot.value))
} }
if (!slot) {
slot = { key: slot, value: '' }
}
return resolve(normalizeHex(slot.value))
}) })
}) })
} }
@ -120,3 +102,5 @@ function normalizeHex (hex) {
} }
return hex return hex
} }
module.exports = {readFromStorage, decodeIntFromHex, extractHexValue, extractHexByteSlice, toBN, add, sub, extractLocation, removeLocation, normalizeHex, extractLocationFromAstVariable}
Loading…
Cancel
Save