Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
e2d333d2
Commit
e2d333d2
authored
Apr 03, 2015
by
zsfelfoldi
Committed by
zelig
Apr 19, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
NatSpec contracts in genesis block, end to end test (unfinished)
parent
ac0e5e8b
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
217 additions
and
60 deletions
+217
-60
bytes.go
common/bytes.go
+17
-0
natspec.go
common/natspec/natspec.go
+2
-3
natspec_e2e_test.go
common/natspec/natspec_e2e_test.go
+143
-0
statereg.go
common/natspec/statereg.go
+0
-44
resolver.go
common/resolver/resolver.go
+6
-5
resolver_test.go
common/resolver/resolver_test.go
+6
-6
contracts.go
core/contracts.go
+33
-0
genesis.go
core/genesis.go
+8
-0
xeth.go
xeth/xeth.go
+2
-2
No files found.
common/bytes.go
View file @
e2d333d2
...
...
@@ -147,6 +147,23 @@ func Hex2Bytes(str string) []byte {
return
h
}
func
Hex2BytesFixed
(
str
string
,
flen
int
)
[]
byte
{
h
,
_
:=
hex
.
DecodeString
(
str
)
if
len
(
h
)
==
flen
{
return
h
}
else
{
if
len
(
h
)
>
flen
{
return
h
[
len
(
h
)
-
flen
:
len
(
h
)]
}
else
{
hh
:=
make
([]
byte
,
flen
)
copy
(
hh
[
flen
-
len
(
h
)
:
flen
],
h
[
:
])
return
hh
}
}
}
func
StringToByteFunc
(
str
string
,
cb
func
(
str
string
)
[]
byte
)
(
ret
[]
byte
)
{
if
len
(
str
)
>
1
&&
str
[
0
:
2
]
==
"0x"
&&
!
strings
.
Contains
(
str
,
"
\n
"
)
{
ret
=
Hex2Bytes
(
str
[
2
:
])
...
...
common/natspec/natspec.go
View file @
e2d333d2
...
...
@@ -49,11 +49,10 @@ func New(xeth *xeth.XEth, tx string, http *docserver.DocServer) (self *NatSpec,
// parse out host/domain
// set up nameresolver with natspecreg + urlhint contract addresses
stateReg
:=
NewStateReg
(
xeth
)
res
:=
resolver
.
New
(
xeth
,
stateReg
.
caNatSpec
,
stateReg
.
caURL
,
resolver
.
NameRegContractAddress
,
resolver
.
URLHintContractAddress
,
)
// resolve host via nameReg/UrlHint Resolver
...
...
common/natspec/natspec_e2e_test.go
0 → 100644
View file @
e2d333d2
package
natspec
import
(
"fmt"
"io/ioutil"
"math/big"
"os"
"testing"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/resolver"
"github.com/ethereum/go-ethereum/core"
//"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth"
xe
"github.com/ethereum/go-ethereum/xeth"
)
type
testFrontend
struct
{
t
*
testing
.
T
ethereum
*
eth
.
Ethereum
xeth
*
xe
.
XEth
}
func
(
f
*
testFrontend
)
UnlockAccount
(
acc
[]
byte
)
bool
{
f
.
t
.
Logf
(
"Unlocking account %v
\n
"
,
common
.
Bytes2Hex
(
acc
))
f
.
ethereum
.
AccountManager
()
.
Unlock
(
acc
,
"password"
)
return
true
}
func
(
testFrontend
)
ConfirmTransaction
(
message
string
)
bool
{
return
true
}
var
port
=
30300
func
testJEthRE
(
t
*
testing
.
T
)
(
ethereum
*
eth
.
Ethereum
,
err
error
)
{
os
.
RemoveAll
(
"/tmp/eth/"
)
err
=
os
.
MkdirAll
(
"/tmp/eth/keys/e273f01c99144c438695e10f24926dc1f9fbf62d/"
,
os
.
ModePerm
)
if
err
!=
nil
{
t
.
Errorf
(
"%v"
,
err
)
return
}
err
=
os
.
MkdirAll
(
"/tmp/eth/data"
,
os
.
ModePerm
)
if
err
!=
nil
{
t
.
Errorf
(
"%v"
,
err
)
return
}
ks
:=
crypto
.
NewKeyStorePlain
(
"/tmp/eth/keys"
)
ioutil
.
WriteFile
(
"/tmp/eth/keys/e273f01c99144c438695e10f24926dc1f9fbf62d/e273f01c99144c438695e10f24926dc1f9fbf62d"
,
[]
byte
(
`{"Id":"RhRXD+fNRKS4jx+7ZfEsNA==","Address":"4nPwHJkUTEOGleEPJJJtwfn79i0=","PrivateKey":"h4ACVpe74uIvi5Cg/2tX/Yrm2xdr3J7QoMbMtNX2CNc="}`
),
os
.
ModePerm
)
port
++
ethereum
,
err
=
eth
.
New
(
&
eth
.
Config
{
DataDir
:
"/tmp/eth"
,
AccountManager
:
accounts
.
NewManager
(
ks
),
Port
:
fmt
.
Sprintf
(
"%d"
,
port
),
MaxPeers
:
10
,
Name
:
"test"
,
})
if
err
!=
nil
{
t
.
Errorf
(
"%v"
,
err
)
return
}
return
}
func
(
self
*
testFrontend
)
insertTx
(
addr
,
contract
,
fnsig
string
,
args
[]
string
)
{
hash
:=
common
.
Bytes2Hex
(
crypto
.
Sha3
([]
byte
(
fnsig
)))
data
:=
"0x"
+
hash
[
0
:
8
]
for
_
,
arg
:=
range
args
{
data
=
data
+
common
.
Bytes2Hex
(
common
.
Hex2BytesFixed
(
arg
,
32
))
}
self
.
t
.
Logf
(
"Tx data: %v"
,
data
)
self
.
xeth
.
Transact
(
addr
,
contract
,
"100000000000"
,
"100000"
,
"100000"
,
data
)
cb
:=
common
.
HexToAddress
(
addr
)
stateDb
:=
self
.
ethereum
.
ChainManager
()
.
State
()
.
Copy
()
coinbase
:=
stateDb
.
GetStateObject
(
cb
)
coinbase
.
SetGasPool
(
big
.
NewInt
(
100000
))
block
:=
self
.
ethereum
.
ChainManager
()
.
NewBlock
(
cb
)
txs
:=
self
.
ethereum
.
TxPool
()
.
GetTransactions
()
tx
:=
txs
[
0
]
_
,
gas
,
err
:=
core
.
ApplyMessage
(
core
.
NewEnv
(
stateDb
,
self
.
ethereum
.
ChainManager
(),
tx
,
block
),
tx
,
coinbase
)
self
.
t
.
Logf
(
"ApplyMessage: gas %v err %v"
,
gas
,
err
)
self
.
ethereum
.
TxPool
()
.
RemoveSet
(
txs
)
self
.
xeth
=
self
.
xeth
.
WithState
(
stateDb
)
}
func
TestNatspecE2E
(
t
*
testing
.
T
)
{
ethereum
,
err
:=
testJEthRE
(
t
)
if
err
!=
nil
{
t
.
Errorf
(
"error creating jsre, got %v"
,
err
)
return
}
err
=
ethereum
.
Start
()
if
err
!=
nil
{
t
.
Errorf
(
"error starting ethereum: %v"
,
err
)
return
}
defer
ethereum
.
Stop
()
frontend
:=
&
testFrontend
{
t
:
t
,
ethereum
:
ethereum
}
frontend
.
xeth
=
xe
.
New
(
ethereum
,
frontend
)
addr
:=
frontend
.
xeth
.
Coinbase
()
if
addr
!=
"0x"
+
core
.
TestAccount
{
t
.
Errorf
(
"CoinBase %v does not match TestAccount 0x%v"
,
addr
,
core
.
TestAccount
)
}
t
.
Logf
(
"CoinBase is %v"
,
addr
)
balance
:=
frontend
.
xeth
.
BalanceAt
(
core
.
TestAccount
)
if
balance
!=
core
.
TestBalance
{
t
.
Errorf
(
"Balance %v does not match TestBalance %v"
,
balance
,
core
.
TestBalance
)
}
t
.
Logf
(
"Balance is %v"
,
balance
)
frontend
.
insertTx
(
addr
,
core
.
ContractAddrURLhint
,
"register(bytes32,bytes32)"
,
[]
string
{
"1234"
,
"5678"
})
t
.
Logf
(
"testcnt: %v"
,
frontend
.
xeth
.
StorageAt
(
core
.
ContractAddrURLhint
,
"00"
))
for
i
:=
0
;
i
<
10
;
i
++
{
t
.
Logf
(
"storage[%v] = %v"
,
i
,
frontend
.
xeth
.
StorageAt
(
"0x"
+
core
.
ContractAddrURLhint
,
fmt
.
Sprintf
(
"%v"
,
i
)))
}
rsv
:=
resolver
.
New
(
frontend
.
xeth
,
resolver
.
URLHintContractAddress
,
resolver
.
NameRegContractAddress
)
url
,
err2
:=
rsv
.
ContentHashToUrl
(
common
.
BytesToHash
(
common
.
Hex2BytesFixed
(
"1234"
,
32
)))
t
.
Logf
(
"URL: %v err: %v"
,
url
,
err2
)
/*
This test is unfinished; first we need to see the result of a
transaction in the contract storage (testcnt should be 1).
*/
}
common/natspec/statereg.go
deleted
100644 → 0
View file @
ac0e5e8b
package
natspec
import
(
"github.com/ethereum/go-ethereum/xeth"
)
type
StateReg
struct
{
xeth
*
xeth
.
XEth
caURL
,
caNatSpec
string
//contract addresses
}
func
NewStateReg
(
xeth
*
xeth
.
XEth
)
(
self
*
StateReg
)
{
self
=
&
StateReg
{}
self
.
xeth
=
xeth
self
.
testCreateContracts
()
return
}
const
codeURLhint
=
"0x33600081905550609c8060136000396000f30060003560e060020a900480632f926732"
+
"14601f578063f39ec1f714603157005b602b6004356024356044565b60006000f35b603a"
+
"600435607f565b8060005260206000f35b600054600160a060020a031633600160a06002"
+
"0a031614606257607b565b8060016000848152602001908152602001600020819055505b"
+
"5050565b60006001600083815260200190815260200160002054905091905056"
const
codeNatSpec
=
"0x33600081905550609c8060136000396000f30060003560e060020a900480632f926732"
+
"14601f578063f39ec1f714603157005b602b6004356024356044565b60006000f35b603a"
+
"600435607f565b8060005260206000f35b600054600160a060020a031633600160a06002"
+
"0a031614606257607b565b8060016000848152602001908152602001600020819055505b"
+
"5050565b60006001600083815260200190815260200160002054905091905056"
func
(
self
*
StateReg
)
testCreateContracts
()
{
var
err
error
self
.
caURL
,
err
=
self
.
xeth
.
Transact
(
self
.
xeth
.
Coinbase
(),
""
,
"100000"
,
""
,
self
.
xeth
.
DefaultGas
()
.
String
(),
codeURLhint
)
if
err
!=
nil
{
panic
(
err
)
}
self
.
caNatSpec
,
err
=
self
.
xeth
.
Transact
(
self
.
xeth
.
Coinbase
(),
""
,
"100000"
,
""
,
self
.
xeth
.
DefaultGas
()
.
String
(),
codeNatSpec
)
if
err
!=
nil
{
panic
(
err
)
}
}
common/resolver/resolver.go
View file @
e2d333d2
...
...
@@ -5,6 +5,7 @@ import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto"
)
...
...
@@ -17,8 +18,8 @@ The resolver is meant to be called by the roundtripper transport implementation
of a url scheme
*/
const
(
urlHintContractAddress
=
"urlhint"
nameRegContractAddress
=
"nameReg"
URLHintContractAddress
=
core
.
ContractAddrURLhint
NameRegContractAddress
=
core
.
ContractAddrHashReg
)
type
Resolver
struct
{
...
...
@@ -37,7 +38,7 @@ func New(eth Backend, uhca, nrca string) *Resolver {
func
(
self
*
Resolver
)
NameToContentHash
(
name
string
)
(
chash
common
.
Hash
,
err
error
)
{
// look up in nameReg
key
:=
storageAddress
(
0
,
common
.
Hex2Bytes
(
name
))
key
:=
storageAddress
(
1
,
common
.
Hex2BytesFixed
(
name
,
32
))
hash
:=
self
.
backend
.
StorageAt
(
self
.
nameRegContractAddress
,
key
)
copy
(
chash
[
:
],
common
.
Hex2Bytes
(
hash
))
return
...
...
@@ -45,8 +46,8 @@ func (self *Resolver) NameToContentHash(name string) (chash common.Hash, err err
func
(
self
*
Resolver
)
ContentHashToUrl
(
chash
common
.
Hash
)
(
uri
string
,
err
error
)
{
// look up in nameReg
key
:=
storageAddress
(
0
,
chash
[
:
])
uri
=
s
elf
.
backend
.
StorageAt
(
self
.
urlHintContractAddress
,
key
)
key
:=
storageAddress
(
2
,
chash
[
:
])
uri
=
s
tring
(
common
.
Hex2Bytes
(
self
.
backend
.
StorageAt
(
self
.
urlHintContractAddress
,
key
))
)
l
:=
len
(
uri
)
for
(
l
>
0
)
&&
(
uri
[
l
-
1
]
==
0
)
{
l
--
...
...
common/resolver/resolver_test.go
View file @
e2d333d2
...
...
@@ -23,13 +23,13 @@ func NewTestBackend() *testBackend {
self
:=
&
testBackend
{}
self
.
contracts
=
make
(
map
[
string
](
map
[
string
]
string
))
self
.
contracts
[
n
ameRegContractAddress
]
=
make
(
map
[
string
]
string
)
self
.
contracts
[
N
ameRegContractAddress
]
=
make
(
map
[
string
]
string
)
key
:=
storageAddress
(
0
,
common
.
Hex2Bytes
(
codehash
))
self
.
contracts
[
n
ameRegContractAddress
][
key
]
=
hash
self
.
contracts
[
N
ameRegContractAddress
][
key
]
=
hash
self
.
contracts
[
url
HintContractAddress
]
=
make
(
map
[
string
]
string
)
self
.
contracts
[
URL
HintContractAddress
]
=
make
(
map
[
string
]
string
)
key
=
storageAddress
(
0
,
common
.
Hex2Bytes
(
hash
))
self
.
contracts
[
url
HintContractAddress
][
key
]
=
url
self
.
contracts
[
URL
HintContractAddress
][
key
]
=
url
return
self
}
...
...
@@ -45,7 +45,7 @@ func (self *testBackend) StorageAt(ca, sa string) (res string) {
func
TestNameToContentHash
(
t
*
testing
.
T
)
{
b
:=
NewTestBackend
()
res
:=
New
(
b
,
urlHintContractAddress
,
n
ameRegContractAddress
)
res
:=
New
(
b
,
URLHintContractAddress
,
N
ameRegContractAddress
)
got
,
err
:=
res
.
NameToContentHash
(
codehash
)
if
err
!=
nil
{
t
.
Errorf
(
"expected no error, got %v"
,
err
)
...
...
@@ -58,7 +58,7 @@ func TestNameToContentHash(t *testing.T) {
func
TestContentHashToUrl
(
t
*
testing
.
T
)
{
b
:=
NewTestBackend
()
res
:=
New
(
b
,
urlHintContractAddress
,
n
ameRegContractAddress
)
res
:=
New
(
b
,
URLHintContractAddress
,
N
ameRegContractAddress
)
chash
:=
common
.
Hash
{}
copy
(
chash
[
:
],
common
.
Hex2Bytes
(
hash
))
got
,
err
:=
res
.
ContentHashToUrl
(
chash
)
...
...
core/contracts.go
0 → 100644
View file @
e2d333d2
package
core
const
(
// built-in contracts address and code
ContractAddrURLhint
=
"0000000000000000000000000000000000000008"
ContractCodeURLhint
=
"0x60bd80600c6000396000f30060003560e060020a900480632f92673214601557005b60216004356024356027565b60006000f35b6000805490816001019055506001600083815260200190815260200160002054600160a060020a0316600014806081575033600160a060020a03166001600084815260200190815260200160002054600160a060020a0316145b60885760b9565b3360016000848152602001908152602001600020819055508060026000848152602001908152602001600020819055505b505056"
//"0x60b180600c6000396000f30060003560e060020a900480632f92673214601557005b60216004356024356027565b60006000f35b6000600083815260200190815260200160002054600160a060020a0316600014806075575033600160a060020a03166000600084815260200190815260200160002054600160a060020a0316145b607c5760ad565b3360006000848152602001908152602001600020819055508060016000848152602001908152602001600020819055505b505056"
/*
contract URLhint {
function register(bytes32 _hash, bytes32 _url) {
testcnt++;
if (owner[_hash] == 0 || owner[_hash] == msg.sender) {
owner[_hash] = msg.sender;
url[_hash] = _url;
}
}
uint32 testcnt;
mapping (bytes32 => address) owner;
mapping (bytes32 => bytes32) url;
}
*/
ContractAddrHashReg
=
"0000000000000000000000000000000000000009"
ContractCodeHashReg
=
"0x3360008190555060628060136000396000f30060003560e060020a900480632f92673214601557005b60216004356024356027565b60006000f35b600054600160a060020a031633600160a060020a031614604557605e565b8060016000848152602001908152602001600020819055505b505056"
/*
import "owned";
contract HashReg is owned {
function register(bytes32 _code, bytes32 _abi) onlyowner {
abis[_code] = _abi;
}
mapping (bytes32 => bytes32) abis;
}
*/
)
core/genesis.go
View file @
e2d333d2
...
...
@@ -56,7 +56,15 @@ func GenesisBlock(db common.Database) *types.Block {
return
genesis
}
const
(
TestAccount
=
"e273f01c99144c438695e10f24926dc1f9fbf62d"
TestBalance
=
"1000000000000000000"
)
var
genesisData
=
[]
byte
(
`{
"`
+
TestAccount
+
`": {"balance": "`
+
TestBalance
+
`"},
"`
+
ContractAddrURLhint
+
`": {"balance": "`
+
TestBalance
+
`", "code": "`
+
ContractCodeURLhint
+
`" },
"`
+
ContractAddrHashReg
+
`": {"balance": "`
+
TestBalance
+
`", "code": "`
+
ContractCodeHashReg
+
`" },
"0000000000000000000000000000000000000001": {"balance": "1"},
"0000000000000000000000000000000000000002": {"balance": "1"},
"0000000000000000000000000000000000000003": {"balance": "1"},
...
...
xeth/xeth.go
View file @
e2d333d2
...
...
@@ -148,10 +148,10 @@ func (self *XEth) AtStateNum(num int64) *XEth {
}
}
return
self
.
w
ithState
(
st
)
return
self
.
W
ithState
(
st
)
}
func
(
self
*
XEth
)
w
ithState
(
statedb
*
state
.
StateDB
)
*
XEth
{
func
(
self
*
XEth
)
W
ithState
(
statedb
*
state
.
StateDB
)
*
XEth
{
xeth
:=
&
XEth
{
backend
:
self
.
backend
,
}
...
...
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