- add comment

- small changes
pull/7/head
yann300 8 years ago
parent e3934b915e
commit 34b6c6c347
  1. 60
      src/solidity/decodeInfo.js
  2. 16
      test/decodeInfo.js

@ -3,9 +3,10 @@
* Uint decode the given @arg type * Uint decode the given @arg type
* *
* @param {String} type - type given by the AST (e.g uint256, uint32) * @param {String} type - type given by the AST (e.g uint256, uint32)
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder} * @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName}
*/ */
function Uint (type) { function Uint (type) {
type === 'uint' ? 'uint256' : type
return { return {
needsFreeStorageSlot: false, needsFreeStorageSlot: false,
storageBytes: parseInt(type.replace('uint', '')) / 8, storageBytes: parseInt(type.replace('uint', '')) / 8,
@ -17,9 +18,10 @@ function Uint (type) {
* Int decode the given @arg type * Int decode the given @arg type
* *
* @param {String} type - type given by the AST (e.g int256, int32) * @param {String} type - type given by the AST (e.g int256, int32)
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder} * @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName}
*/ */
function Int (type) { function Int (type) {
type === 'int' ? 'int256' : type
return { return {
needsFreeStorageSlot: false, needsFreeStorageSlot: false,
storageBytes: parseInt(type.replace('int', '')) / 8, storageBytes: parseInt(type.replace('int', '')) / 8,
@ -31,7 +33,7 @@ function Int (type) {
* Address decode the given @arg type * Address decode the given @arg type
* *
* @param {String} type - type given by the AST (e.g address) * @param {String} type - type given by the AST (e.g address)
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder} * @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName}
*/ */
function Address (type) { function Address (type) {
return { return {
@ -45,7 +47,7 @@ function Address (type) {
* Bool decode the given @arg type * Bool decode the given @arg type
* *
* @param {String} type - type given by the AST (e.g bool) * @param {String} type - type given by the AST (e.g bool)
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder} * @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName}
*/ */
function Bool (type) { function Bool (type) {
return { return {
@ -59,7 +61,7 @@ function Bool (type) {
* DynamicByteArray decode the given @arg type * DynamicByteArray decode the given @arg type
* *
* @param {String} type - type given by the AST (e.g bytes storage ref) * @param {String} type - type given by the AST (e.g bytes storage ref)
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder} * @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName}
*/ */
function DynamicByteArray (type) { function DynamicByteArray (type) {
return { return {
@ -73,16 +75,13 @@ function DynamicByteArray (type) {
* FixedByteArray decode the given @arg type * FixedByteArray decode the given @arg type
* *
* @param {String} type - type given by the AST (e.g bytes16) * @param {String} type - type given by the AST (e.g bytes16)
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder} * @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName}
*/ */
function FixedByteArray (type) { function FixedByteArray (type) {
if (type.split(' ')) {
type = type.split(' ')[0]
}
return { return {
needsFreeStorageSlot: false, needsFreeStorageSlot: false,
storageBytes: parseInt(type.replace('bytes', '')), storageBytes: parseInt(type.replace('bytes', '')),
typeName: type.split(' ')[0] typeName: type
} }
} }
@ -90,7 +89,7 @@ function FixedByteArray (type) {
* StringType decode the given @arg type * StringType decode the given @arg type
* *
* @param {String} type - type given by the AST (e.g string storage ref) * @param {String} type - type given by the AST (e.g string storage ref)
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder} * @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName}
*/ */
function StringType (type) { function StringType (type) {
return { return {
@ -104,7 +103,7 @@ function StringType (type) {
* ArrayType decode the given @arg type * ArrayType decode the given @arg type
* *
* @param {String} type - type given by the AST (e.g int256[] storage ref, int256[] storage ref[] storage ref) * @param {String} type - type given by the AST (e.g int256[] storage ref, int256[] storage ref[] storage ref)
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder, arraySize, subArray} * @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, arraySize, subArray}
*/ */
function ArrayType (type, stateDefinitions) { function ArrayType (type, stateDefinitions) {
var arraySize var arraySize
@ -113,7 +112,7 @@ function ArrayType (type, stateDefinitions) {
var underlyingType = extractUnderlyingType(type) var underlyingType = extractUnderlyingType(type)
arraySize = extractArraySize(type) arraySize = extractArraySize(type)
var dimensions = extractDimensions(type) var dimensions = extractArrayInfo(type)
type = underlyingType + dimensions.join('') type = underlyingType + dimensions.join('')
var subArrayType = type.substring(0, type.lastIndexOf('[')) var subArrayType = type.substring(0, type.lastIndexOf('['))
@ -147,14 +146,13 @@ function ArrayType (type, stateDefinitions) {
* Enum decode the given @arg type * Enum decode the given @arg type
* *
* @param {String} type - type given by the AST (e.g enum enumDef) * @param {String} type - type given by the AST (e.g enum enumDef)
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder} * @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, enum}
*/ */
function Enum (type, stateDefinitions) { function Enum (type, stateDefinitions) {
var extracted = type.split(' ')
return { return {
needsFreeStorageSlot: false, needsFreeStorageSlot: false,
storageBytes: 1, storageBytes: 1,
typeName: extracted[0] + ' ' + extracted[1], typeName: type,
enum: getEnum(type, stateDefinitions) enum: getEnum(type, stateDefinitions)
} }
} }
@ -163,24 +161,24 @@ function Enum (type, stateDefinitions) {
* Struct decode the given @arg type * Struct decode the given @arg type
* *
* @param {String} type - type given by the AST (e.g struct structDef storage ref) * @param {String} type - type given by the AST (e.g struct structDef storage ref)
* @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, decoder, members} * @return {Object} returns decoded info about the current type: { needsFreeStorageSlot, storageBytes, typeName, members}
*/ */
function Struct (type, stateDefinitions) { function Struct (type, stateDefinitions) {
var extracted = type.split(' ') var extracted = type.split(' ')
type = extracted[0] + ' ' + extracted[1] type = 'struct ' + extracted[1] // the second item is the name of the struct definition (newly declared type)
var membersDetails = getStructMembers(type, stateDefinitions) var memberDetails = getStructMembers(type, stateDefinitions) // type is used to extract the ast struct definition
return { return {
needsFreeStorageSlot: true, needsFreeStorageSlot: true,
storageBytes: membersDetails.storageBytes, storageBytes: memberDetails.storageBytes,
typeName: type, typeName: type,
members: membersDetails.members members: memberDetails.members
} }
} }
/** /**
* retrieve enum declaration of the given @arg type * retrieve enum declaration of the given @arg type
* *
* @param {String} type - type given by the AST * @param {String} type - type given by the AST (e.g enum enumDef)
* @param {Object} stateDefinitions - all state declarations given by the AST (including struct and enum type declaration) * @param {Object} stateDefinitions - all state declarations given by the AST (including struct and enum type declaration)
* @return {Array} - containing all value declaration of the current enum type * @return {Array} - containing all value declaration of the current enum type
*/ */
@ -233,7 +231,7 @@ function getStructMembers (typeName, stateDefinitions) {
*/ */
function extractArraySize (typeName) { function extractArraySize (typeName) {
if (typeName.indexOf('[') !== -1) { if (typeName.indexOf('[') !== -1) {
var squareBracket = /\[([0-9]+|\s*)\]/g var squareBracket = /\[[^\]]*\]/g // /\[([0-9]+|\s*)\]/g
var dim = typeName.match(squareBracket) var dim = typeName.match(squareBracket)
var size = dim[dim.length - 1] var size = dim[dim.length - 1]
if (size === '[]') { if (size === '[]') {
@ -248,15 +246,15 @@ function extractArraySize (typeName) {
* extract the underlying type * extract the underlying type
* *
* @param {String} fullType - type given by the AST (ex: uint[2] storage ref[2]) * @param {String} fullType - type given by the AST (ex: uint[2] storage ref[2])
* @return {String} return the first part of the full type. don not keep the array declaration ( uint[2] storage ref[2] will return uint) * @return {String} return the first part of the full type. do not keep the array declaration ( uint[2] storage ref[2] will return uint)
*/ */
function extractUnderlyingType (fullType) { function extractUnderlyingType (fullType) {
var splitted = fullType.split(' ') var split = fullType.split(' ')
if (fullType.indexOf('enum') === 0 || fullType.indexOf('struct') === 0) { if (fullType.indexOf('enum') === 0 || fullType.indexOf('struct') === 0) {
return splitted[0] + ' ' + splitted[1] return split[0] + ' ' + split[1]
} }
if (splitted.length > 0) { if (split.length > 0) {
fullType = splitted[0] fullType = split[0]
} }
if (fullType[fullType.length - 1] === ']') { if (fullType[fullType.length - 1] === ']') {
return fullType.substring(0, fullType.indexOf('[')) return fullType.substring(0, fullType.indexOf('['))
@ -270,7 +268,7 @@ function extractUnderlyingType (fullType) {
* @param {String} fullType - type given by the AST * @param {String} fullType - type given by the AST
* @return {Array} containing all the dimensions and size of the array (e.g ['[3]', '[]'] ) * @return {Array} containing all the dimensions and size of the array (e.g ['[3]', '[]'] )
*/ */
function extractDimensions (fullType) { function extractArrayInfo (fullType) {
var ret = [] var ret = []
if (fullType.indexOf('[') !== -1) { if (fullType.indexOf('[') !== -1) {
var squareBracket = /\[([0-9]+|\s*)\]/g var squareBracket = /\[([0-9]+|\s*)\]/g
@ -286,7 +284,7 @@ function extractDimensions (fullType) {
* @param {String} fullType - type given by the AST (ex: uint[2] storage ref[2]) * @param {String} fullType - type given by the AST (ex: uint[2] storage ref[2])
* @return {String} returns the token type (used to instanciate the right decoder) (uint[2] storage ref[2] will return 'array', uint256 will return uintX) * @return {String} returns the token type (used to instanciate the right decoder) (uint[2] storage ref[2] will return 'array', uint256 will return uintX)
*/ */
function extractTokenType (fullType) { function typeClass (fullType) {
if (fullType.indexOf('[') !== -1) { if (fullType.indexOf('[') !== -1) {
return 'array' return 'array'
} }
@ -317,7 +315,7 @@ function decode (type, stateDefinitions) {
'int': Int, 'int': Int,
'uint': Uint 'uint': Uint
} }
var currentType = extractTokenType(type) var currentType = typeClass(type)
return decodeInfos[currentType](type, stateDefinitions) return decodeInfos[currentType](type, stateDefinitions)
} }

@ -14,6 +14,8 @@ tape('solidity', function (t) {
checkDecodeInfo(st, decodeInfo, false, 32, 'uint256') checkDecodeInfo(st, decodeInfo, false, 32, 'uint256')
decodeInfo = index.solidity.decodeInfo.decode(stateDec[3].attributes.type, stateDec) decodeInfo = index.solidity.decodeInfo.decode(stateDec[3].attributes.type, stateDec)
checkDecodeInfo(st, decodeInfo, false, 32, 'uint256') checkDecodeInfo(st, decodeInfo, false, 32, 'uint256')
decodeInfo = index.solidity.decodeInfo.decode(stateDec[4].attributes.type, stateDec)
checkDecodeInfo(st, decodeInfo, false, 16, 'bytes16')
stateDec = index.solidity.astHelper.extractStateVariables('contractStructAndArray', output.sources) stateDec = index.solidity.astHelper.extractStateVariables('contractStructAndArray', output.sources)
decodeInfo = index.solidity.decodeInfo.decode(stateDec[1].attributes.type, stateDec) decodeInfo = index.solidity.decodeInfo.decode(stateDec[1].attributes.type, stateDec)
@ -30,6 +32,11 @@ tape('solidity', function (t) {
checkDecodeInfo(st, decodeInfo, true, 32, 'int8[]') checkDecodeInfo(st, decodeInfo, true, 32, 'int8[]')
decodeInfo = index.solidity.decodeInfo.decode(stateDec[2].attributes.type, stateDec) decodeInfo = index.solidity.decodeInfo.decode(stateDec[2].attributes.type, stateDec)
checkDecodeInfo(st, decodeInfo, true, 4 * 32, 'int16[][3][][4]') checkDecodeInfo(st, decodeInfo, true, 4 * 32, 'int16[][3][][4]')
stateDec = index.solidity.astHelper.extractStateVariables('contractEnum', output.sources)
decodeInfo = index.solidity.decodeInfo.decode(stateDec[1].attributes.type, stateDec)
checkDecodeInfo(st, decodeInfo, false, 1, 'enum enumDef')
st.end() st.end()
}) })
}) })
@ -48,6 +55,7 @@ var contracts = `
contract contractUint is baseContract { contract contractUint is baseContract {
uint256 ui; uint256 ui;
uint ui1; uint ui1;
bytes16 b;
} }
contract contractStructAndArray { contract contractStructAndArray {
@ -65,4 +73,12 @@ var contracts = `
int8[] i8dyn; int8[] i8dyn;
int16[][3][][4] i16dyn; int16[][3][][4] i16dyn;
} }
contract contractEnum {
enum enumDef {
first,
second
}
enumDef enum1;
}
` `

Loading…
Cancel
Save