Skip to content

Commit 65e6319

Browse files
committed
core/vm: use package hexutil for JSON handling
1 parent ec75953 commit 65e6319

2 files changed

Lines changed: 23 additions & 28 deletions

File tree

‎core/vm/log.go‎

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"io"
2424

2525
"github.com/ethereum/go-ethereum/common"
26+
"github.com/ethereum/go-ethereum/common/hexutil"
2627
"github.com/ethereum/go-ethereum/rlp"
2728
)
2829

@@ -47,12 +48,12 @@ type Log struct {
4748
type jsonLog struct {
4849
Address *common.Address `json:"address"`
4950
Topics *[]common.Hash `json:"topics"`
50-
Data string `json:"data"`
51-
BlockNumber string `json:"blockNumber"`
52-
TxIndex string `json:"transactionIndex"`
51+
Data *hexutil.Bytes `json:"data"`
52+
BlockNumber *hexutil.Uint64 `json:"blockNumber"`
53+
TxIndex *hexutil.Uint `json:"transactionIndex"`
5354
TxHash *common.Hash `json:"transactionHash"`
5455
BlockHash *common.Hash `json:"blockHash"`
55-
Index string `json:"logIndex"`
56+
Index *hexutil.Uint `json:"logIndex"`
5657
}
5758

5859
func NewLog(address common.Address, topics []common.Hash, data []byte, number uint64) *Log {
@@ -85,12 +86,12 @@ func (r *Log) MarshalJSON() ([]byte, error) {
8586
return json.Marshal(&jsonLog{
8687
Address: &r.Address,
8788
Topics: &r.Topics,
88-
Data: fmt.Sprintf("0x%x", r.Data),
89-
BlockNumber: fmt.Sprintf("0x%x", r.BlockNumber),
90-
TxIndex: fmt.Sprintf("0x%x", r.TxIndex),
89+
Data: (*hexutil.Bytes)(&r.Data),
90+
BlockNumber: (*hexutil.Uint64)(&r.BlockNumber),
91+
TxIndex: (*hexutil.Uint)(&r.TxIndex),
9192
TxHash: &r.TxHash,
9293
BlockHash: &r.BlockHash,
93-
Index: fmt.Sprintf("0x%x", r.Index),
94+
Index: (*hexutil.Uint)(&r.Index),
9495
})
9596
}
9697

@@ -100,29 +101,20 @@ func (r *Log) UnmarshalJSON(input []byte) error {
100101
if err := json.Unmarshal(input, &dec); err != nil {
101102
return err
102103
}
103-
if dec.Address == nil || dec.Topics == nil || dec.Data == "" || dec.BlockNumber == "" ||
104-
dec.TxIndex == "" || dec.TxHash == nil || dec.BlockHash == nil || dec.Index == "" {
104+
if dec.Address == nil || dec.Topics == nil || dec.Data == nil || dec.BlockNumber == nil ||
105+
dec.TxIndex == nil || dec.TxHash == nil || dec.BlockHash == nil || dec.Index == nil {
105106
return errMissingLogFields
106107
}
107-
declog := Log{
108-
Address: *dec.Address,
109-
Topics: *dec.Topics,
110-
TxHash: *dec.TxHash,
111-
BlockHash: *dec.BlockHash,
108+
*r = Log{
109+
Address: *dec.Address,
110+
Topics: *dec.Topics,
111+
Data: *dec.Data,
112+
BlockNumber: uint64(*dec.BlockNumber),
113+
TxHash: *dec.TxHash,
114+
TxIndex: uint(*dec.TxIndex),
115+
BlockHash: *dec.BlockHash,
116+
Index: uint(*dec.Index),
112117
}
113-
if _, err := fmt.Sscanf(dec.Data, "0x%x", &declog.Data); err != nil {
114-
return fmt.Errorf("invalid hex log data")
115-
}
116-
if _, err := fmt.Sscanf(dec.BlockNumber, "0x%x", &declog.BlockNumber); err != nil {
117-
return fmt.Errorf("invalid hex log block number")
118-
}
119-
if _, err := fmt.Sscanf(dec.TxIndex, "0x%x", &declog.TxIndex); err != nil {
120-
return fmt.Errorf("invalid hex log tx index")
121-
}
122-
if _, err := fmt.Sscanf(dec.Index, "0x%x", &declog.Index); err != nil {
123-
return fmt.Errorf("invalid hex log index")
124-
}
125-
*r = declog
126118
return nil
127119
}
128120

‎core/vm/log_test.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ var unmarshalLogTests = map[string]struct {
2828
"ok": {
2929
input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","blockHash":"0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056","blockNumber":"0x1ecfa4","data":"0x000000000000000000000000000000000000000000000001a055690d9db80000","logIndex":"0x2","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615","0x000000000000000000000000f9dff387dcb5cc4cca5b91adb07a95f54e9f1bb6"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3"}`,
3030
},
31+
"empty data": {
32+
input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","blockHash":"0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056","blockNumber":"0x1ecfa4","data":"0x","logIndex":"0x2","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615","0x000000000000000000000000f9dff387dcb5cc4cca5b91adb07a95f54e9f1bb6"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3"}`,
33+
},
3134
"missing data": {
3235
input: `{"address":"0xecf8f87f810ecf450940c9f60066b4a7a501d6a7","blockHash":"0x656c34545f90a730a19008c0e7a7cd4fb3895064b48d6d69761bd5abad681056","blockNumber":"0x1ecfa4","logIndex":"0x2","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x00000000000000000000000080b2c9d7cbbf30a1b0fc8983c647d754c6525615","0x000000000000000000000000f9dff387dcb5cc4cca5b91adb07a95f54e9f1bb6"],"transactionHash":"0x3b198bfd5d2907285af009e9ae84a0ecd63677110d89d7e030251acb87f6487e","transactionIndex":"0x3"}`,
3336
wantError: errMissingLogFields,

0 commit comments

Comments
 (0)