Skip to content

Commit

Permalink
[jsonrpc] Fix newHeads subscription returns inconsistent names and ty…
Browse files Browse the repository at this point in the history
…pes (#267)

# Description

The `eth_subscibe` with `newHeads` return data inconsistent with other
endpoints.
The PR fixes it.

expected:
```json
{
    "parentHash": "0x97aaaa7beb1c1b214b5be2e7f7c10c0b6a4cbd03199c6b801261d52259b61861",
    "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
    "miner": "0x92bB5D1A856E54157232b43Fe1C9C7e89eE36ABE",
    "stateRoot": "0xfbc57a3dae227fe8fe9c6257da16aea23f66d40d814e07a6071a9c4d1a6871ff",
    "txRoot": "0xe2e4d8c039867942bbb51678a7b9eb1021eeb8a112d610941a440ef33698cc96",
    "receiptsRoot": "0xaab4695d4870a57327b11f93c3654fee07ce0b317cee231ae003781d5deb5cf5",
    "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    "difficulty": "0x4d3a89",
    "number": "0x4d3a89",
    "gasLimit": "0x1c9c380",
    "gasUsed": "0x10b2a",
    "timestamp": "0x638895ac",
    "extraData": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD5AWT4VJTWaLulB9BDj/Iu6M6zQTI3ZWaaJJRxdZlv+dy4+7g7aOK4jxoCkBRVW5SSu10ahW5UFXIytD/hycfonuNqvpRk8mhuOsdJLDs/Zi0y7Li2v6VI/rhBN1vHq66zRutiXAwD4IED/n2u3kDvXY4EjSe/hiFdOsNxtBgbDwNVrxB7eHTIFXDKu4SVr1Y/Lyr3Mi25m+YdWwH4ybhBktNBzNLycczqZpElmSBOWxhkFXZWoQyQOnGNwUtWV54CWTualYHIQ3IebuJkVGuceWb6nHikvgq67q5RrhI0IAC4QXLtLnCtU/IqFrYkhYVsezrGluc4hPikBhFbkHg3taFDCS52rxtF1sjOiJt+yvalDplds7f0j2CA/QT9mky1jEIBuEE/vPa7tBS4dLnuSJR23mjd8qZ7crVV6ECmfhAUew9/o3xGG5Cmh1/rvHNjrGehL0m3WmME6kbcHJ+lM6ptM3wqAQ==",
    "mixHash": "0x63746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365",
    "nonce": "0x0000000000000000",
    "hash": "0xc0b316518ba0b13fde70c1fba9984d3d55c0b0d2d3ba951581dca71dab814d81"
}
```

actual:
```json
{
    "ParentHash": "0x97aaaa7beb1c1b214b5be2e7f7c10c0b6a4cbd03199c6b801261d52259b61861",
    "Sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
    "Miner": "0x92bB5D1A856E54157232b43Fe1C9C7e89eE36ABE",
    "StateRoot": "0xfbc57a3dae227fe8fe9c6257da16aea23f66d40d814e07a6071a9c4d1a6871ff",
    "TxRoot": "0xe2e4d8c039867942bbb51678a7b9eb1021eeb8a112d610941a440ef33698cc96",
    "ReceiptsRoot": "0xaab4695d4870a57327b11f93c3654fee07ce0b317cee231ae003781d5deb5cf5",
    "LogsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    "Difficulty": 5061257,
    "Number": 5061257,
    "GasLimit": 30000000,
    "GasUsed": 68394,
    "Timestamp": 1669895596,
    "ExtraData": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD5AWT4VJTWaLulB9BDj/Iu6M6zQTI3ZWaaJJRxdZlv+dy4+7g7aOK4jxoCkBRVW5SSu10ahW5UFXIytD/hycfonuNqvpRk8mhuOsdJLDs/Zi0y7Li2v6VI/rhBN1vHq66zRutiXAwD4IED/n2u3kDvXY4EjSe/hiFdOsNxtBgbDwNVrxB7eHTIFXDKu4SVr1Y/Lyr3Mi25m+YdWwH4ybhBktNBzNLycczqZpElmSBOWxhkFXZWoQyQOnGNwUtWV54CWTualYHIQ3IebuJkVGuceWb6nHikvgq67q5RrhI0IAC4QXLtLnCtU/IqFrYkhYVsezrGluc4hPikBhFbkHg3taFDCS52rxtF1sjOiJt+yvalDplds7f0j2CA/QT9mky1jEIBuEE/vPa7tBS4dLnuSJR23mjd8qZ7crVV6ECmfhAUew9/o3xGG5Cmh1/rvHNjrGehL0m3WmME6kbcHJ+lM6ptM3wqAQ==",
    "MixHash": "0x63746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365",
    "Nonce": "0x0000000000000000",
    "Hash": "0xc0b316518ba0b13fde70c1fba9984d3d55c0b0d2d3ba951581dca71dab814d81"
}
```

# Changes include

- [x] Bugfix (non-breaking change that solves an issue)

## Testing

- [x] I have tested this code with the official test suite
  • Loading branch information
DarianShawn authored Dec 6, 2022
1 parent ea69d44 commit 71c31ce
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 37 deletions.
10 changes: 5 additions & 5 deletions jsonrpc/filter_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ type blockFilter struct {
}

// takeBlockUpdates advances blocks from head to latest and returns header array
func (f *blockFilter) takeBlockUpdates() []*types.Header {
func (f *blockFilter) takeBlockUpdates() []*jsonHeader {
updates, newHead := f.block.getUpdates()

f.Lock()
Expand Down Expand Up @@ -852,7 +852,7 @@ func (b *blockStream) push(header *types.Header) {
defer b.lock.Unlock()

newHead := &headElem{
header: header.Copy(),
header: toJSONHeader(header),
}

if b.head != nil {
Expand All @@ -863,12 +863,12 @@ func (b *blockStream) push(header *types.Header) {
}

type headElem struct {
header *types.Header
header *jsonHeader
next *headElem
}

func (h *headElem) getUpdates() ([]*types.Header, *headElem) {
res := make([]*types.Header, 0)
func (h *headElem) getUpdates() ([]*jsonHeader, *headElem) {
res := make([]*jsonHeader, 0)

cur := h

Expand Down
76 changes: 44 additions & 32 deletions jsonrpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,50 +81,62 @@ func toTransaction(
return res
}

type jsonHeader struct {
ParentHash types.Hash `json:"parentHash"`
Sha3Uncles types.Hash `json:"sha3Uncles"`
Miner types.Address `json:"miner"`
StateRoot types.Hash `json:"stateRoot"`
TxRoot types.Hash `json:"transactionsRoot"`
ReceiptsRoot types.Hash `json:"receiptsRoot"`
LogsBloom types.Bloom `json:"logsBloom"`
Difficulty argUint64 `json:"difficulty"`
Number argUint64 `json:"number"`
GasLimit argUint64 `json:"gasLimit"`
GasUsed argUint64 `json:"gasUsed"`
Timestamp argUint64 `json:"timestamp"`
ExtraData argBytes `json:"extraData"`
MixHash types.Hash `json:"mixHash"`
Nonce types.Nonce `json:"nonce"`
Hash types.Hash `json:"hash"`
}

func toJSONHeader(h *types.Header) *jsonHeader {
return &jsonHeader{
ParentHash: h.ParentHash,
Sha3Uncles: h.Sha3Uncles,
Miner: h.Miner,
StateRoot: h.StateRoot,
TxRoot: h.TxRoot,
ReceiptsRoot: h.ReceiptsRoot,
LogsBloom: h.LogsBloom,
Difficulty: argUint64(h.Difficulty),
Number: argUint64(h.Number),
GasLimit: argUint64(h.GasLimit),
GasUsed: argUint64(h.GasUsed),
Timestamp: argUint64(h.Timestamp),
ExtraData: argBytes(h.ExtraData),
MixHash: h.MixHash,
Nonce: h.Nonce,
Hash: h.Hash,
}
}

type block struct {
ParentHash types.Hash `json:"parentHash"`
Sha3Uncles types.Hash `json:"sha3Uncles"`
Miner types.Address `json:"miner"`
StateRoot types.Hash `json:"stateRoot"`
TxRoot types.Hash `json:"transactionsRoot"`
ReceiptsRoot types.Hash `json:"receiptsRoot"`
LogsBloom types.Bloom `json:"logsBloom"`
Difficulty argUint64 `json:"difficulty"`
jsonHeader
TotalDifficulty argUint64 `json:"totalDifficulty"`
Size argUint64 `json:"size"`
Number argUint64 `json:"number"`
GasLimit argUint64 `json:"gasLimit"`
GasUsed argUint64 `json:"gasUsed"`
Timestamp argUint64 `json:"timestamp"`
ExtraData argBytes `json:"extraData"`
MixHash types.Hash `json:"mixHash"`
Nonce types.Nonce `json:"nonce"`
Hash types.Hash `json:"hash"`
Transactions []transactionOrHash `json:"transactions"`
Uncles []types.Hash `json:"uncles"`
}

func toBlock(b *types.Block, fullTx bool) *block {
h := b.Header
jh := toJSONHeader(h)

res := &block{
ParentHash: h.ParentHash,
Sha3Uncles: h.Sha3Uncles,
Miner: h.Miner,
StateRoot: h.StateRoot,
TxRoot: h.TxRoot,
ReceiptsRoot: h.ReceiptsRoot,
LogsBloom: h.LogsBloom,
Difficulty: argUint64(h.Difficulty),
jsonHeader: *jh,
TotalDifficulty: argUint64(h.Difficulty), // not needed for POS
Size: argUint64(b.Size()),
Number: argUint64(h.Number),
GasLimit: argUint64(h.GasLimit),
GasUsed: argUint64(h.GasUsed),
Timestamp: argUint64(h.Timestamp),
ExtraData: argBytes(h.ExtraData),
MixHash: h.MixHash,
Nonce: h.Nonce,
Hash: h.Hash,
Transactions: []transactionOrHash{},
Uncles: []types.Hash{},
}
Expand Down
59 changes: 59 additions & 0 deletions jsonrpc/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,62 @@ func TestToTransaction_Returns_V_R_S_ValuesWithoutLeading0(t *testing.T) {
assert.Equal(t, hexWithoutLeading0, string(jsonR))
assert.Equal(t, hexWithoutLeading0, string(jsonS))
}

func Test_toJSONHeader(t *testing.T) {
rawHeaderStr := `{
"parentHash": "0x5b1840d0a559112b42208b074284c92d32ddc876ab36d3a40a6ef109c3230899",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"miner": "0x32fC6Db2B0500fF103A810B46Daa26d35CE5172f",
"stateRoot": "0x7d29e307e0a2e7c00ec8ab96b39d31496e4a27184818c9b586bf4f0a252c46e0",
"transactionsRoot": "0x0fef8f013ada9a1b491499a90441a109ffaf28e018c9f8540811468a39e8fdba",
"receiptsRoot": "0xd55415bfb3eb6f64e8501f4821c38b33284828692d98df442fba7946e7b5e205",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x75f",
"number": "0x75f",
"gasLimit": "0x500000",
"gasUsed": "0x5701",
"timestamp": "0x6389b20e",
"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000f89ed59432fc6db2b0500ff103a810b46daa26d35ce5172fb841d8a49a60cbeb222cf4239f8d14581422b938c0265fdeee8fba36e7506be4e8d859fec6b334391b2394fad8dd319851544ff8915dea92b2c6a2c889a3565a8b3801f843b84165f19bb45cae54537a950c0df109277767c65a8a6c738a46a5a89db3d57bc8f473ddcf0d276728fc935d8b1dc0e3db23521f38c935e924201411404f250e7aa801",
"mixHash": "0x63746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365",
"nonce": "0x0000000000000000",
"hash": "0x85492e41d07c4886706650c1d0754856dc7c92d1dd311fb18f6d425c2dd1f897"
}`

var bloom types.Bloom
err := bloom.UnmarshalText([]byte("0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"))

assert.NoError(t, err)

expectedHeader := &jsonHeader{
ParentHash: types.StringToHash("0x5b1840d0a559112b42208b074284c92d32ddc876ab36d3a40a6ef109c3230899"),
Sha3Uncles: types.StringToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"),
Miner: types.StringToAddress("0x32fC6Db2B0500fF103A810B46Daa26d35CE5172f"),
StateRoot: types.StringToHash("0x7d29e307e0a2e7c00ec8ab96b39d31496e4a27184818c9b586bf4f0a252c46e0"),
TxRoot: types.StringToHash("0x0fef8f013ada9a1b491499a90441a109ffaf28e018c9f8540811468a39e8fdba"),
ReceiptsRoot: types.StringToHash("0xd55415bfb3eb6f64e8501f4821c38b33284828692d98df442fba7946e7b5e205"),
LogsBloom: bloom,
Difficulty: 1887,
Number: 1887,
GasLimit: 5242880,
GasUsed: 22273,
Timestamp: 1669968398,
ExtraData: types.StringToBytes("0x0000000000000000000000000000000000000000000000000000000000000000f89ed59432fc6db2b0500ff103a810b46daa26d35ce5172fb841d8a49a60cbeb222cf4239f8d14581422b938c0265fdeee8fba36e7506be4e8d859fec6b334391b2394fad8dd319851544ff8915dea92b2c6a2c889a3565a8b3801f843b84165f19bb45cae54537a950c0df109277767c65a8a6c738a46a5a89db3d57bc8f473ddcf0d276728fc935d8b1dc0e3db23521f38c935e924201411404f250e7aa801"),
MixHash: types.StringToHash("0x63746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365"),
Nonce: types.Nonce{},
Hash: types.StringToHash("0x85492e41d07c4886706650c1d0754856dc7c92d1dd311fb18f6d425c2dd1f897"),
}

raw, err := json.Marshal(rawHeaderStr)
if !assert.NoError(t, err) {
t.FailNow()
}

jh := &jsonHeader{}

err = json.Unmarshal(raw, &jh)
if !assert.NoError(t, err) {
t.FailNow()
}

assert.Equal(t, expectedHeader, jh)
}

0 comments on commit 71c31ce

Please sign in to comment.