Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
whqwjb
go-ethereum
提交
b7f64041
G
go-ethereum
项目概览
whqwjb
/
go-ethereum
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
go-ethereum
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b7f64041
编写于
9月 09, 2016
作者:
B
bas-vk
提交者:
GitHub
9月 09, 2016
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2984 from karalabe/fix-head-sub
core/types, ethclient: fix broken subscriptions
上级
6fb8ae2b
b6b5ec8f
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
132 addition
and
1 deletion
+132
-1
core/types/json.go
core/types/json.go
+21
-0
core/types/json_test.go
core/types/json_test.go
+77
-0
core/types/receipt.go
core/types/receipt.go
+15
-0
core/types/transaction.go
core/types/transaction.go
+18
-0
ethclient/ethclient.go
ethclient/ethclient.go
+1
-1
未找到文件。
core/types/json.go
浏览文件 @
b7f64041
...
...
@@ -26,6 +26,13 @@ import (
type
hexBytes
[]
byte
func
(
b
*
hexBytes
)
MarshalJSON
()
([]
byte
,
error
)
{
if
b
!=
nil
{
return
[]
byte
(
fmt
.
Sprintf
(
`"0x%x"`
,
[]
byte
(
*
b
))),
nil
}
return
nil
,
nil
}
func
(
b
*
hexBytes
)
UnmarshalJSON
(
input
[]
byte
)
error
{
if
len
(
input
)
<
2
||
input
[
0
]
!=
'"'
||
input
[
len
(
input
)
-
1
]
!=
'"'
{
return
fmt
.
Errorf
(
"cannot unmarshal non-string into hexBytes"
)
...
...
@@ -44,6 +51,13 @@ func (b *hexBytes) UnmarshalJSON(input []byte) error {
type
hexBig
big
.
Int
func
(
b
*
hexBig
)
MarshalJSON
()
([]
byte
,
error
)
{
if
b
!=
nil
{
return
[]
byte
(
fmt
.
Sprintf
(
`"0x%x"`
,
(
*
big
.
Int
)(
b
))),
nil
}
return
nil
,
nil
}
func
(
b
*
hexBig
)
UnmarshalJSON
(
input
[]
byte
)
error
{
raw
,
err
:=
checkHexNumber
(
input
)
if
err
!=
nil
{
...
...
@@ -59,6 +73,13 @@ func (b *hexBig) UnmarshalJSON(input []byte) error {
type
hexUint64
uint64
func
(
b
*
hexUint64
)
MarshalJSON
()
([]
byte
,
error
)
{
if
b
!=
nil
{
return
[]
byte
(
fmt
.
Sprintf
(
`"0x%x"`
,
*
(
*
uint64
)(
b
))),
nil
}
return
nil
,
nil
}
func
(
b
*
hexUint64
)
UnmarshalJSON
(
input
[]
byte
)
error
{
raw
,
err
:=
checkHexNumber
(
input
)
if
err
!=
nil
{
...
...
core/types/json_test.go
浏览文件 @
b7f64041
...
...
@@ -2,6 +2,7 @@ package types
import
(
"encoding/json"
"reflect"
"testing"
"github.com/ethereum/go-ethereum/common"
...
...
@@ -44,6 +45,31 @@ func TestUnmarshalHeader(t *testing.T) {
}
}
func
TestMarshalHeader
(
t
*
testing
.
T
)
{
for
name
,
test
:=
range
unmarshalHeaderTests
{
if
test
.
wantError
!=
nil
{
continue
}
var
original
*
Header
json
.
Unmarshal
([]
byte
(
test
.
input
),
&
original
)
blob
,
err
:=
json
.
Marshal
(
original
)
if
err
!=
nil
{
t
.
Errorf
(
"test %q: failed to marshal header: %v"
,
name
,
err
)
continue
}
var
proced
*
Header
if
err
:=
json
.
Unmarshal
(
blob
,
&
proced
);
err
!=
nil
{
t
.
Errorf
(
"Test %q: failed to unmarshal marhsalled header: %v"
,
name
,
err
)
continue
}
if
!
reflect
.
DeepEqual
(
original
,
proced
)
{
t
.
Errorf
(
"test %q: header mismatch: have %+v, want %+v"
,
name
,
proced
,
original
)
continue
}
}
}
var
unmarshalTransactionTests
=
map
[
string
]
struct
{
input
string
wantHash
common
.
Hash
...
...
@@ -94,6 +120,32 @@ func TestUnmarshalTransaction(t *testing.T) {
}
}
func
TestMarshalTransaction
(
t
*
testing
.
T
)
{
for
name
,
test
:=
range
unmarshalTransactionTests
{
if
test
.
wantError
!=
nil
{
continue
}
var
original
*
Transaction
json
.
Unmarshal
([]
byte
(
test
.
input
),
&
original
)
blob
,
err
:=
json
.
Marshal
(
original
)
if
err
!=
nil
{
t
.
Errorf
(
"test %q: failed to marshal transaction: %v"
,
name
,
err
)
continue
}
var
proced
*
Transaction
if
err
:=
json
.
Unmarshal
(
blob
,
&
proced
);
err
!=
nil
{
t
.
Errorf
(
"Test %q: failed to unmarshal marhsalled transaction: %v"
,
name
,
err
)
continue
}
proced
.
Hash
()
// hack private fields to pass deep equal
if
!
reflect
.
DeepEqual
(
original
,
proced
)
{
t
.
Errorf
(
"test %q: transaction mismatch: have %+v, want %+v"
,
name
,
proced
,
original
)
continue
}
}
}
var
unmarshalReceiptTests
=
map
[
string
]
struct
{
input
string
wantError
error
...
...
@@ -119,6 +171,31 @@ func TestUnmarshalReceipt(t *testing.T) {
}
}
func
TestMarshalReceipt
(
t
*
testing
.
T
)
{
for
name
,
test
:=
range
unmarshalReceiptTests
{
if
test
.
wantError
!=
nil
{
continue
}
var
original
*
Receipt
json
.
Unmarshal
([]
byte
(
test
.
input
),
&
original
)
blob
,
err
:=
json
.
Marshal
(
original
)
if
err
!=
nil
{
t
.
Errorf
(
"test %q: failed to marshal receipt: %v"
,
name
,
err
)
continue
}
var
proced
*
Receipt
if
err
:=
json
.
Unmarshal
(
blob
,
&
proced
);
err
!=
nil
{
t
.
Errorf
(
"Test %q: failed to unmarshal marhsalled receipt: %v"
,
name
,
err
)
continue
}
if
!
reflect
.
DeepEqual
(
original
,
proced
)
{
t
.
Errorf
(
"test %q: receipt mismatch: have %+v, want %+v"
,
name
,
proced
,
original
)
continue
}
}
}
func
checkError
(
t
*
testing
.
T
,
testname
string
,
got
,
want
error
)
bool
{
if
got
==
nil
{
if
want
!=
nil
{
...
...
core/types/receipt.go
浏览文件 @
b7f64041
...
...
@@ -84,6 +84,21 @@ func (r *Receipt) DecodeRLP(s *rlp.Stream) error {
return
nil
}
// MarshalJSON encodes receipts into the web3 RPC response block format.
func
(
r
*
Receipt
)
MarshalJSON
()
([]
byte
,
error
)
{
root
:=
common
.
BytesToHash
(
r
.
PostState
)
return
json
.
Marshal
(
&
jsonReceipt
{
PostState
:
&
root
,
CumulativeGasUsed
:
(
*
hexBig
)(
r
.
CumulativeGasUsed
),
Bloom
:
&
r
.
Bloom
,
Logs
:
&
r
.
Logs
,
TxHash
:
&
r
.
TxHash
,
ContractAddress
:
&
r
.
ContractAddress
,
GasUsed
:
(
*
hexBig
)(
r
.
GasUsed
),
})
}
// UnmarshalJSON decodes the web3 RPC receipt format.
func
(
r
*
Receipt
)
UnmarshalJSON
(
input
[]
byte
)
error
{
var
dec
jsonReceipt
...
...
core/types/transaction.go
浏览文件 @
b7f64041
...
...
@@ -128,6 +128,24 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
return
err
}
// MarshalJSON encodes transactions into the web3 RPC response block format.
func
(
tx
*
Transaction
)
MarshalJSON
()
([]
byte
,
error
)
{
hash
,
v
:=
tx
.
Hash
(),
uint64
(
tx
.
data
.
V
)
return
json
.
Marshal
(
&
jsonTransaction
{
Hash
:
&
hash
,
AccountNonce
:
(
*
hexUint64
)(
&
tx
.
data
.
AccountNonce
),
Price
:
(
*
hexBig
)(
tx
.
data
.
Price
),
GasLimit
:
(
*
hexBig
)(
tx
.
data
.
GasLimit
),
Recipient
:
tx
.
data
.
Recipient
,
Amount
:
(
*
hexBig
)(
tx
.
data
.
Amount
),
Payload
:
(
*
hexBytes
)(
&
tx
.
data
.
Payload
),
V
:
(
*
hexUint64
)(
&
v
),
R
:
(
*
hexBig
)(
tx
.
data
.
R
),
S
:
(
*
hexBig
)(
tx
.
data
.
S
),
})
}
// UnmarshalJSON decodes the web3 RPC transaction format.
func
(
tx
*
Transaction
)
UnmarshalJSON
(
input
[]
byte
)
error
{
var
dec
jsonTransaction
...
...
ethclient/ethclient.go
浏览文件 @
b7f64041
...
...
@@ -227,7 +227,7 @@ func (ec *Client) SyncProgress(ctx context.Context) (*ethereum.SyncProgress, err
// SubscribeNewHead subscribes to notifications about the current blockchain head
// on the given channel.
func
(
ec
*
Client
)
SubscribeNewHead
(
ctx
context
.
Context
,
ch
chan
<-
*
types
.
Header
)
(
ethereum
.
Subscription
,
error
)
{
return
ec
.
c
.
EthSubscribe
(
ctx
,
ch
,
"new
Block
s"
,
map
[
string
]
struct
{}{})
return
ec
.
c
.
EthSubscribe
(
ctx
,
ch
,
"new
Head
s"
,
map
[
string
]
struct
{}{})
}
// State Access
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录