diff --git a/jsonrpc/filter_manager.go b/jsonrpc/filter_manager.go index 88d353790f..f9b18969d9 100644 --- a/jsonrpc/filter_manager.go +++ b/jsonrpc/filter_manager.go @@ -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() @@ -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 { @@ -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 diff --git a/jsonrpc/types.go b/jsonrpc/types.go index a719a11739..e2f068a75a 100644 --- a/jsonrpc/types.go +++ b/jsonrpc/types.go @@ -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{}, } diff --git a/jsonrpc/types_test.go b/jsonrpc/types_test.go index c374e917b3..a81d31153c 100644 --- a/jsonrpc/types_test.go +++ b/jsonrpc/types_test.go @@ -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) +}