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
b6b5ec8f
Commit
b6b5ec8f
authored
Sep 08, 2016
by
Péter Szilágyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core/types: add core type marshal methods too
parent
ca37730c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
131 additions
and
0 deletions
+131
-0
json.go
core/types/json.go
+21
-0
json_test.go
core/types/json_test.go
+77
-0
receipt.go
core/types/receipt.go
+15
-0
transaction.go
core/types/transaction.go
+18
-0
No files found.
core/types/json.go
View file @
b6b5ec8f
...
@@ -26,6 +26,13 @@ import (
...
@@ -26,6 +26,13 @@ import (
type
hexBytes
[]
byte
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
{
func
(
b
*
hexBytes
)
UnmarshalJSON
(
input
[]
byte
)
error
{
if
len
(
input
)
<
2
||
input
[
0
]
!=
'"'
||
input
[
len
(
input
)
-
1
]
!=
'"'
{
if
len
(
input
)
<
2
||
input
[
0
]
!=
'"'
||
input
[
len
(
input
)
-
1
]
!=
'"'
{
return
fmt
.
Errorf
(
"cannot unmarshal non-string into hexBytes"
)
return
fmt
.
Errorf
(
"cannot unmarshal non-string into hexBytes"
)
...
@@ -44,6 +51,13 @@ func (b *hexBytes) UnmarshalJSON(input []byte) error {
...
@@ -44,6 +51,13 @@ func (b *hexBytes) UnmarshalJSON(input []byte) error {
type
hexBig
big
.
Int
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
{
func
(
b
*
hexBig
)
UnmarshalJSON
(
input
[]
byte
)
error
{
raw
,
err
:=
checkHexNumber
(
input
)
raw
,
err
:=
checkHexNumber
(
input
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -59,6 +73,13 @@ func (b *hexBig) UnmarshalJSON(input []byte) error {
...
@@ -59,6 +73,13 @@ func (b *hexBig) UnmarshalJSON(input []byte) error {
type
hexUint64
uint64
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
{
func
(
b
*
hexUint64
)
UnmarshalJSON
(
input
[]
byte
)
error
{
raw
,
err
:=
checkHexNumber
(
input
)
raw
,
err
:=
checkHexNumber
(
input
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
core/types/json_test.go
View file @
b6b5ec8f
...
@@ -2,6 +2,7 @@ package types
...
@@ -2,6 +2,7 @@ package types
import
(
import
(
"encoding/json"
"encoding/json"
"reflect"
"testing"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common"
...
@@ -44,6 +45,31 @@ func TestUnmarshalHeader(t *testing.T) {
...
@@ -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
{
var
unmarshalTransactionTests
=
map
[
string
]
struct
{
input
string
input
string
wantHash
common
.
Hash
wantHash
common
.
Hash
...
@@ -94,6 +120,32 @@ func TestUnmarshalTransaction(t *testing.T) {
...
@@ -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
{
var
unmarshalReceiptTests
=
map
[
string
]
struct
{
input
string
input
string
wantError
error
wantError
error
...
@@ -119,6 +171,31 @@ func TestUnmarshalReceipt(t *testing.T) {
...
@@ -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
{
func
checkError
(
t
*
testing
.
T
,
testname
string
,
got
,
want
error
)
bool
{
if
got
==
nil
{
if
got
==
nil
{
if
want
!=
nil
{
if
want
!=
nil
{
...
...
core/types/receipt.go
View file @
b6b5ec8f
...
@@ -84,6 +84,21 @@ func (r *Receipt) DecodeRLP(s *rlp.Stream) error {
...
@@ -84,6 +84,21 @@ func (r *Receipt) DecodeRLP(s *rlp.Stream) error {
return
nil
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.
// UnmarshalJSON decodes the web3 RPC receipt format.
func
(
r
*
Receipt
)
UnmarshalJSON
(
input
[]
byte
)
error
{
func
(
r
*
Receipt
)
UnmarshalJSON
(
input
[]
byte
)
error
{
var
dec
jsonReceipt
var
dec
jsonReceipt
...
...
core/types/transaction.go
View file @
b6b5ec8f
...
@@ -128,6 +128,24 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
...
@@ -128,6 +128,24 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
return
err
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.
// UnmarshalJSON decodes the web3 RPC transaction format.
func
(
tx
*
Transaction
)
UnmarshalJSON
(
input
[]
byte
)
error
{
func
(
tx
*
Transaction
)
UnmarshalJSON
(
input
[]
byte
)
error
{
var
dec
jsonTransaction
var
dec
jsonTransaction
...
...
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