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
1609df32
Commit
1609df32
authored
Nov 27, 2016
by
Felix Lange
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ethclient: use package hexutil for JSON handling
parent
24f28877
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
34 deletions
+35
-34
ethclient.go
ethclient/ethclient.go
+35
-34
No files found.
ethclient/ethclient.go
View file @
1609df32
...
...
@@ -24,6 +24,7 @@ import (
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/rlp"
...
...
@@ -156,9 +157,9 @@ func (ec *Client) TransactionByHash(ctx context.Context, hash common.Hash) (*typ
// TransactionCount returns the total number of transactions in the given block.
func
(
ec
*
Client
)
TransactionCount
(
ctx
context
.
Context
,
blockHash
common
.
Hash
)
(
uint
,
error
)
{
var
num
rpc
.
HexNumber
var
num
hexutil
.
Uint
err
:=
ec
.
c
.
CallContext
(
ctx
,
&
num
,
"eth_getBlockTransactionCountByHash"
,
blockHash
)
return
num
.
Uint
(
),
err
return
uint
(
num
),
err
}
// TransactionInBlock returns a single transaction at index in the given block.
...
...
@@ -196,11 +197,11 @@ func toBlockNumArg(number *big.Int) string {
}
type
rpcProgress
struct
{
StartingBlock
rpc
.
HexNumber
CurrentBlock
rpc
.
HexNumber
HighestBlock
rpc
.
HexNumber
PulledStates
rpc
.
HexNumber
KnownStates
rpc
.
HexNumber
StartingBlock
hexutil
.
Uint64
CurrentBlock
hexutil
.
Uint64
HighestBlock
hexutil
.
Uint64
PulledStates
hexutil
.
Uint64
KnownStates
hexutil
.
Uint64
}
// SyncProgress retrieves the current progress of the sync algorithm. If there's
...
...
@@ -220,11 +221,11 @@ func (ec *Client) SyncProgress(ctx context.Context) (*ethereum.SyncProgress, err
return
nil
,
err
}
return
&
ethereum
.
SyncProgress
{
StartingBlock
:
progress
.
StartingBlock
.
Uint64
(
),
CurrentBlock
:
progress
.
CurrentBlock
.
Uint64
(
),
HighestBlock
:
progress
.
HighestBlock
.
Uint64
(
),
PulledStates
:
progress
.
PulledStates
.
Uint64
(
),
KnownStates
:
progress
.
KnownStates
.
Uint64
(
),
StartingBlock
:
uint64
(
progress
.
StartingBlock
),
CurrentBlock
:
uint64
(
progress
.
CurrentBlock
),
HighestBlock
:
uint64
(
progress
.
HighestBlock
),
PulledStates
:
uint64
(
progress
.
PulledStates
),
KnownStates
:
uint64
(
progress
.
KnownStates
),
},
nil
}
...
...
@@ -239,7 +240,7 @@ func (ec *Client) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header)
// BalanceAt returns the wei balance of the given account.
// The block number can be nil, in which case the balance is taken from the latest known block.
func
(
ec
*
Client
)
BalanceAt
(
ctx
context
.
Context
,
account
common
.
Address
,
blockNumber
*
big
.
Int
)
(
*
big
.
Int
,
error
)
{
var
result
rpc
.
HexNumber
var
result
hexutil
.
Big
err
:=
ec
.
c
.
CallContext
(
ctx
,
&
result
,
"eth_getBalance"
,
account
,
toBlockNumArg
(
blockNumber
))
return
(
*
big
.
Int
)(
&
result
),
err
}
...
...
@@ -247,7 +248,7 @@ func (ec *Client) BalanceAt(ctx context.Context, account common.Address, blockNu
// StorageAt returns the value of key in the contract storage of the given account.
// The block number can be nil, in which case the value is taken from the latest known block.
func
(
ec
*
Client
)
StorageAt
(
ctx
context
.
Context
,
account
common
.
Address
,
key
common
.
Hash
,
blockNumber
*
big
.
Int
)
([]
byte
,
error
)
{
var
result
rpc
.
Hex
Bytes
var
result
hexutil
.
Bytes
err
:=
ec
.
c
.
CallContext
(
ctx
,
&
result
,
"eth_getStorageAt"
,
account
,
key
,
toBlockNumArg
(
blockNumber
))
return
result
,
err
}
...
...
@@ -255,7 +256,7 @@ func (ec *Client) StorageAt(ctx context.Context, account common.Address, key com
// CodeAt returns the contract code of the given account.
// The block number can be nil, in which case the code is taken from the latest known block.
func
(
ec
*
Client
)
CodeAt
(
ctx
context
.
Context
,
account
common
.
Address
,
blockNumber
*
big
.
Int
)
([]
byte
,
error
)
{
var
result
rpc
.
Hex
Bytes
var
result
hexutil
.
Bytes
err
:=
ec
.
c
.
CallContext
(
ctx
,
&
result
,
"eth_getCode"
,
account
,
toBlockNumArg
(
blockNumber
))
return
result
,
err
}
...
...
@@ -263,9 +264,9 @@ func (ec *Client) CodeAt(ctx context.Context, account common.Address, blockNumbe
// NonceAt returns the account nonce of the given account.
// The block number can be nil, in which case the nonce is taken from the latest known block.
func
(
ec
*
Client
)
NonceAt
(
ctx
context
.
Context
,
account
common
.
Address
,
blockNumber
*
big
.
Int
)
(
uint64
,
error
)
{
var
result
rpc
.
HexNumber
var
result
hexutil
.
Uint64
err
:=
ec
.
c
.
CallContext
(
ctx
,
&
result
,
"eth_getTransactionCount"
,
account
,
toBlockNumArg
(
blockNumber
))
return
result
.
Uint64
(
),
err
return
uint64
(
result
),
err
}
// Filters
...
...
@@ -299,21 +300,21 @@ func toFilterArg(q ethereum.FilterQuery) interface{} {
// PendingBalanceAt returns the wei balance of the given account in the pending state.
func
(
ec
*
Client
)
PendingBalanceAt
(
ctx
context
.
Context
,
account
common
.
Address
)
(
*
big
.
Int
,
error
)
{
var
result
rpc
.
HexNumber
var
result
hexutil
.
Big
err
:=
ec
.
c
.
CallContext
(
ctx
,
&
result
,
"eth_getBalance"
,
account
,
"pending"
)
return
(
*
big
.
Int
)(
&
result
),
err
}
// PendingStorageAt returns the value of key in the contract storage of the given account in the pending state.
func
(
ec
*
Client
)
PendingStorageAt
(
ctx
context
.
Context
,
account
common
.
Address
,
key
common
.
Hash
)
([]
byte
,
error
)
{
var
result
rpc
.
Hex
Bytes
var
result
hexutil
.
Bytes
err
:=
ec
.
c
.
CallContext
(
ctx
,
&
result
,
"eth_getStorageAt"
,
account
,
key
,
"pending"
)
return
result
,
err
}
// PendingCodeAt returns the contract code of the given account in the pending state.
func
(
ec
*
Client
)
PendingCodeAt
(
ctx
context
.
Context
,
account
common
.
Address
)
([]
byte
,
error
)
{
var
result
rpc
.
Hex
Bytes
var
result
hexutil
.
Bytes
err
:=
ec
.
c
.
CallContext
(
ctx
,
&
result
,
"eth_getCode"
,
account
,
"pending"
)
return
result
,
err
}
...
...
@@ -321,16 +322,16 @@ func (ec *Client) PendingCodeAt(ctx context.Context, account common.Address) ([]
// PendingNonceAt returns the account nonce of the given account in the pending state.
// This is the nonce that should be used for the next transaction.
func
(
ec
*
Client
)
PendingNonceAt
(
ctx
context
.
Context
,
account
common
.
Address
)
(
uint64
,
error
)
{
var
result
rpc
.
HexNumber
var
result
hexutil
.
Uint64
err
:=
ec
.
c
.
CallContext
(
ctx
,
&
result
,
"eth_getTransactionCount"
,
account
,
"pending"
)
return
result
.
Uint64
(
),
err
return
uint64
(
result
),
err
}
// PendingTransactionCount returns the total number of transactions in the pending state.
func
(
ec
*
Client
)
PendingTransactionCount
(
ctx
context
.
Context
)
(
uint
,
error
)
{
var
num
rpc
.
HexNumber
var
num
hexutil
.
Uint
err
:=
ec
.
c
.
CallContext
(
ctx
,
&
num
,
"eth_getBlockTransactionCountByNumber"
,
"pending"
)
return
num
.
Uint
(
),
err
return
uint
(
num
),
err
}
// TODO: SubscribePendingTransactions (needs server side)
...
...
@@ -344,29 +345,29 @@ func (ec *Client) PendingTransactionCount(ctx context.Context) (uint, error) {
// case the code is taken from the latest known block. Note that state from very old
// blocks might not be available.
func
(
ec
*
Client
)
CallContract
(
ctx
context
.
Context
,
msg
ethereum
.
CallMsg
,
blockNumber
*
big
.
Int
)
([]
byte
,
error
)
{
var
hex
string
var
hex
hexutil
.
Bytes
err
:=
ec
.
c
.
CallContext
(
ctx
,
&
hex
,
"eth_call"
,
toCallArg
(
msg
),
toBlockNumArg
(
blockNumber
))
if
err
!=
nil
{
return
nil
,
err
}
return
common
.
FromHex
(
hex
)
,
nil
return
hex
,
nil
}
// PendingCallContract executes a message call transaction using the EVM.
// The state seen by the contract call is the pending state.
func
(
ec
*
Client
)
PendingCallContract
(
ctx
context
.
Context
,
msg
ethereum
.
CallMsg
)
([]
byte
,
error
)
{
var
hex
string
var
hex
hexutil
.
Bytes
err
:=
ec
.
c
.
CallContext
(
ctx
,
&
hex
,
"eth_call"
,
toCallArg
(
msg
),
"pending"
)
if
err
!=
nil
{
return
nil
,
err
}
return
common
.
FromHex
(
hex
)
,
nil
return
hex
,
nil
}
// SuggestGasPrice retrieves the currently suggested gas price to allow a timely
// execution of a transaction.
func
(
ec
*
Client
)
SuggestGasPrice
(
ctx
context
.
Context
)
(
*
big
.
Int
,
error
)
{
var
hex
rpc
.
HexNumber
var
hex
hexutil
.
Big
if
err
:=
ec
.
c
.
CallContext
(
ctx
,
&
hex
,
"eth_gasPrice"
);
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -378,7 +379,7 @@ func (ec *Client) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
// the true gas limit requirement as other transactions may be added or removed by miners,
// but it should provide a basis for setting a reasonable default.
func
(
ec
*
Client
)
EstimateGas
(
ctx
context
.
Context
,
msg
ethereum
.
CallMsg
)
(
*
big
.
Int
,
error
)
{
var
hex
rpc
.
HexNumber
var
hex
hexutil
.
Big
err
:=
ec
.
c
.
CallContext
(
ctx
,
&
hex
,
"eth_estimateGas"
,
toCallArg
(
msg
))
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -404,16 +405,16 @@ func toCallArg(msg ethereum.CallMsg) interface{} {
"to"
:
msg
.
To
,
}
if
len
(
msg
.
Data
)
>
0
{
arg
[
"data"
]
=
fmt
.
Sprintf
(
"%#x"
,
msg
.
Data
)
arg
[
"data"
]
=
hexutil
.
Bytes
(
msg
.
Data
)
}
if
msg
.
Value
!=
nil
{
arg
[
"value"
]
=
fmt
.
Sprintf
(
"%#x"
,
msg
.
Value
)
arg
[
"value"
]
=
(
*
hexutil
.
Big
)(
msg
.
Value
)
}
if
msg
.
Gas
!=
nil
{
arg
[
"gas"
]
=
fmt
.
Sprintf
(
"%#x"
,
msg
.
Gas
)
arg
[
"gas"
]
=
(
*
hexutil
.
Big
)(
msg
.
Gas
)
}
if
msg
.
GasPrice
!=
nil
{
arg
[
"gasPrice"
]
=
fmt
.
Sprintf
(
"%#x"
,
msg
.
GasPrice
)
arg
[
"gasPrice"
]
=
(
*
hexutil
.
Big
)(
msg
.
GasPrice
)
}
return
arg
}
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