Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
baas-ide
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
guxukai
baas-ide
Commits
3ea4abeb
Commit
3ea4abeb
authored
Nov 16, 2016
by
yann300
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor computeoffsets
parent
54060ecd
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
77 additions
and
73 deletions
+77
-73
astHelper.js
src/solidity/astHelper.js
+14
-5
decodeInfo.js
src/solidity/decodeInfo.js
+54
-31
stateDecoder.js
src/solidity/stateDecoder.js
+4
-32
decodeInfo.js
test/solidity/decodeInfo.js
+5
-5
No files found.
src/solidity/astHelper.js
View file @
3ea4abeb
...
...
@@ -37,28 +37,37 @@ function getLinearizedBaseContracts (id, contractsById) {
*
* @param {String} contractName - contract for which state var should be resolved
* @param {Object} sourcesList - sources list (containing root AST node)
* @return {Array} - return an array of AST node of all state variables (including inherited) (this will include all enum/struct declarations)
* @return {Object} - return an object containing: stateItems - list of all the children node of the @arg contractName
* stateVariables - list of all the variable declaration of the @arg contractName
*/
function
extractState
Variables
(
contractName
,
sourcesList
)
{
function
extractState
(
contractName
,
sourcesList
)
{
var
contracts
=
extractContractDefinitions
(
sourcesList
)
var
node
=
contracts
.
contractsByName
[
contractName
]
if
(
node
)
{
var
stateItems
=
[]
var
stateVar
=
[]
var
baseContracts
=
getLinearizedBaseContracts
(
node
.
id
,
contracts
.
contractsById
)
baseContracts
.
reverse
()
for
(
var
k
in
baseContracts
)
{
var
ctr
=
baseContracts
[
k
]
for
(
var
i
in
ctr
.
children
)
{
stateVar
.
push
(
ctr
.
children
[
i
])
var
item
=
ctr
.
children
[
i
]
stateItems
.
push
(
item
)
if
(
item
.
name
===
'VariableDeclaration'
)
{
stateVar
.
push
(
item
)
}
}
}
return
stateVar
return
{
stateItems
:
stateItems
,
stateVariables
:
stateVar
}
}
return
null
}
module
.
exports
=
{
extractState
Variables
:
extractStateVariables
,
extractState
:
extractState
,
extractContractDefinitions
:
extractContractDefinitions
,
getLinearizedBaseContracts
:
getLinearizedBaseContracts
}
src/solidity/decodeInfo.js
View file @
3ea4abeb
...
...
@@ -164,43 +164,17 @@ function getEnum (type, stateDefinitions) {
* @return {Array} containing all members of the current struct type
*/
function
getStructMembers
(
typeName
,
stateDefinitions
)
{
var
members
=
[]
for
(
var
k
in
stateDefinitions
)
{
var
dec
=
stateDefinitions
[
k
]
if
(
dec
.
name
===
'StructDefinition'
&&
typeName
===
dec
.
attributes
.
name
)
{
var
location
=
{
offset
:
0
,
slot
:
0
var
offsets
=
computeOffsets
(
dec
.
children
,
stateDefinitions
)
return
{
members
:
offsets
.
typesOffsets
,
storageBytes
:
offsets
.
endLocation
.
slot
}
for
(
var
i
in
dec
.
children
)
{
var
member
=
dec
.
children
[
i
]
var
type
=
parseType
(
member
.
attributes
.
type
,
stateDefinitions
)
if
(
location
.
offset
+
type
.
storageBytes
>
32
)
{
location
.
slot
++
location
.
offset
=
0
}
if
(
!
type
)
{
console
.
log
(
'unable to retrieve decode info of '
+
member
.
attributes
.
type
)
return
null
}
members
.
push
(
type
)
if
(
type
.
storageSlots
===
1
&&
location
.
offset
+
type
.
storageBytes
<=
32
)
{
location
.
offset
+=
type
.
storageBytes
}
else
{
location
.
slot
+=
type
.
storageSlots
location
.
offset
=
0
}
}
if
(
location
.
offset
>
0
)
{
location
.
slot
++
}
break
}
}
return
{
members
:
members
,
storageBytes
:
location
.
slot
}
return
null
}
/**
...
...
@@ -248,8 +222,57 @@ function parseType (type, stateDefinitions) {
return
decodeInfos
[
currentType
](
type
,
stateDefinitions
)
}
/**
* compute offset (slot offset and byte offset of the @arg list of types)
*
* @param {Array} types - list of types
* @param {Object} stateItems - all state definitions given by the AST (including struct and enum type declaration)
* @return {Array} - return an array of types item: {name, type, location}. location defines the byte offset and slot offset
*/
function
computeOffsets
(
types
,
stateItems
,
cb
)
{
var
ret
=
[]
var
location
=
{
offset
:
0
,
slot
:
0
}
for
(
var
i
in
types
)
{
var
variable
=
types
[
i
]
var
type
=
parseType
(
variable
.
attributes
.
type
,
stateItems
)
if
(
location
.
offset
+
type
.
storageBytes
>
32
)
{
location
.
slot
++
location
.
offset
=
0
}
if
(
!
type
)
{
console
.
log
(
'unable to retrieve decode info of '
+
variable
.
attributes
.
type
)
return
null
}
ret
.
push
({
name
:
variable
.
attributes
.
name
,
type
:
type
,
location
:
{
offset
:
location
.
offset
,
slot
:
location
.
slot
}
})
if
(
type
.
storageSlots
===
1
&&
location
.
offset
+
type
.
storageBytes
<=
32
)
{
location
.
offset
+=
type
.
storageBytes
}
else
{
location
.
slot
+=
type
.
storageSlots
location
.
offset
=
0
}
}
if
(
location
.
offset
>
0
)
{
location
.
slot
++
}
return
{
typesOffsets
:
ret
,
endLocation
:
location
}
}
module
.
exports
=
{
parseType
:
parseType
,
computeOffsets
:
computeOffsets
,
Uint
:
Uint
,
Address
:
Address
,
Bool
:
Bool
,
...
...
src/solidity/stateDecoder.js
View file @
3ea4abeb
...
...
@@ -25,40 +25,13 @@ function decodeState (stateVars, storageContent) {
* @return {Object} - return the location of all contract variables in the storage
*/
function
extractStateVariables
(
contractName
,
sourcesList
)
{
var
state
Definitions
=
astHelper
.
extractStateVariables
(
contractName
,
sourcesList
)
var
state
=
astHelper
.
extractState
(
contractName
,
sourcesList
)
var
ret
=
[]
if
(
!
state
Definitions
)
{
if
(
!
state
)
{
return
ret
}
var
location
=
{
offset
:
0
,
slot
:
0
}
for
(
var
k
in
stateDefinitions
)
{
var
variable
=
stateDefinitions
[
k
]
if
(
variable
.
name
===
'VariableDeclaration'
)
{
var
type
=
decodeInfo
.
parseType
(
variable
.
attributes
.
type
,
stateDefinitions
)
if
(
location
.
offset
+
type
.
storageBytes
>
32
)
{
location
.
slot
++
location
.
offset
=
0
}
ret
.
push
({
name
:
variable
.
attributes
.
name
,
type
:
type
,
location
:
{
offset
:
location
.
offset
,
slot
:
location
.
slot
}
})
if
(
type
.
storageSlots
===
1
&&
location
.
offset
+
type
.
storageBytes
<=
32
)
{
location
.
offset
+=
type
.
storageBytes
}
else
{
location
.
slot
+=
type
.
storageSlots
location
.
offset
=
0
}
}
}
return
ret
var
offsets
=
decodeInfo
.
computeOffsets
(
state
.
stateVariables
,
state
.
stateItems
)
return
offsets
.
typesOffsets
}
/**
...
...
@@ -79,4 +52,3 @@ module.exports = {
extractStateVariables
:
extractStateVariables
,
decodeState
:
decodeState
}
test/solidity/decodeInfo.js
View file @
3ea4abeb
...
...
@@ -8,7 +8,7 @@ tape('solidity', function (t) {
t
.
test
(
'astHelper, decodeInfo'
,
function
(
st
)
{
var
output
=
compiler
.
compile
(
contracts
,
0
)
var
stateDec
=
index
.
solidity
.
astHelper
.
extractState
Variables
(
'contractUint'
,
output
.
sources
)
var
stateDec
=
index
.
solidity
.
astHelper
.
extractState
(
'contractUint'
,
output
.
sources
).
stateItems
var
decodeInfo
=
index
.
solidity
.
decodeInfo
.
parseType
(
stateDec
[
0
].
attributes
.
type
,
stateDec
)
checkDecodeInfo
(
st
,
decodeInfo
,
1
,
1
,
'uint'
)
decodeInfo
=
index
.
solidity
.
decodeInfo
.
parseType
(
stateDec
[
2
].
attributes
.
type
,
stateDec
)
...
...
@@ -18,7 +18,7 @@ tape('solidity', function (t) {
decodeInfo
=
index
.
solidity
.
decodeInfo
.
parseType
(
stateDec
[
4
].
attributes
.
type
,
stateDec
)
checkDecodeInfo
(
st
,
decodeInfo
,
1
,
16
,
'bytesX'
)
stateDec
=
index
.
solidity
.
astHelper
.
extractState
Variables
(
'contractStructAndArray'
,
output
.
sources
)
stateDec
=
index
.
solidity
.
astHelper
.
extractState
(
'contractStructAndArray'
,
output
.
sources
).
stateItems
decodeInfo
=
index
.
solidity
.
decodeInfo
.
parseType
(
stateDec
[
1
].
attributes
.
type
,
stateDec
)
checkDecodeInfo
(
st
,
decodeInfo
,
2
,
32
,
'struct'
)
decodeInfo
=
index
.
solidity
.
decodeInfo
.
parseType
(
stateDec
[
2
].
attributes
.
type
,
stateDec
)
...
...
@@ -26,7 +26,7 @@ tape('solidity', function (t) {
decodeInfo
=
index
.
solidity
.
decodeInfo
.
parseType
(
stateDec
[
3
].
attributes
.
type
,
stateDec
)
checkDecodeInfo
(
st
,
decodeInfo
,
2
,
32
,
'array'
)
stateDec
=
index
.
solidity
.
astHelper
.
extractState
Variables
(
'contractArray'
,
output
.
sources
)
stateDec
=
index
.
solidity
.
astHelper
.
extractState
(
'contractArray'
,
output
.
sources
).
stateItems
decodeInfo
=
index
.
solidity
.
decodeInfo
.
parseType
(
stateDec
[
0
].
attributes
.
type
,
stateDec
)
checkDecodeInfo
(
st
,
decodeInfo
,
1
,
32
,
'array'
)
decodeInfo
=
index
.
solidity
.
decodeInfo
.
parseType
(
stateDec
[
1
].
attributes
.
type
,
stateDec
)
...
...
@@ -34,11 +34,11 @@ tape('solidity', function (t) {
decodeInfo
=
index
.
solidity
.
decodeInfo
.
parseType
(
stateDec
[
2
].
attributes
.
type
,
stateDec
)
checkDecodeInfo
(
st
,
decodeInfo
,
4
,
32
,
'array'
)
stateDec
=
index
.
solidity
.
astHelper
.
extractState
Variables
(
'contractEnum'
,
output
.
sources
)
stateDec
=
index
.
solidity
.
astHelper
.
extractState
(
'contractEnum'
,
output
.
sources
).
stateItems
decodeInfo
=
index
.
solidity
.
decodeInfo
.
parseType
(
stateDec
[
1
].
attributes
.
type
,
stateDec
)
checkDecodeInfo
(
st
,
decodeInfo
,
1
,
2
,
'enum'
)
stateDec
=
index
.
solidity
.
astHelper
.
extractState
Variables
(
'contractSmallVariable'
,
output
.
sources
)
stateDec
=
index
.
solidity
.
astHelper
.
extractState
(
'contractSmallVariable'
,
output
.
sources
).
stateItems
decodeInfo
=
index
.
solidity
.
decodeInfo
.
parseType
(
stateDec
[
0
].
attributes
.
type
,
stateDec
)
checkDecodeInfo
(
st
,
decodeInfo
,
1
,
1
,
'int'
)
decodeInfo
=
index
.
solidity
.
decodeInfo
.
parseType
(
stateDec
[
1
].
attributes
.
type
,
stateDec
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment