|
|
@@ -19,6 +19,7 @@ package types
|
|
|
|
|
|
import (
|
|
|
"encoding/binary"
|
|
|
+ "encoding/json"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"io"
|
|
|
@@ -88,6 +89,35 @@ type Header struct {
|
|
|
Nonce BlockNonce `json:"nonce"`
|
|
|
}
|
|
|
|
|
|
+func (h *Header) GetHeaderJSON() (string, error) {
|
|
|
+ // 创建一个包含字段名和字段值的映射
|
|
|
+ headerMap := map[string]interface{}{
|
|
|
+ "ParentHash": h.ParentHash,
|
|
|
+ "UncleHash": h.UncleHash,
|
|
|
+ "Coinbase": h.Coinbase,
|
|
|
+ "Root": h.Root,
|
|
|
+ "TxHash": h.TxHash,
|
|
|
+ "ReceiptHash": h.ReceiptHash,
|
|
|
+ "Bloom": h.Bloom,
|
|
|
+ "Difficulty": h.Difficulty,
|
|
|
+ "Number": h.Number,
|
|
|
+ "GasLimit": h.GasLimit,
|
|
|
+ "GasUsed": h.GasUsed,
|
|
|
+ "Time": h.Time,
|
|
|
+ "Extra": h.Extra,
|
|
|
+ "MixDigest": h.MixDigest,
|
|
|
+ "Nonce": h.Nonce,
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将映射转换为 JSON 字符串
|
|
|
+ jsonData, err := json.Marshal(headerMap)
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+
|
|
|
+ return string(jsonData), nil
|
|
|
+}
|
|
|
+
|
|
|
// field type overrides for gencodec
|
|
|
type headerMarshaling struct {
|
|
|
Difficulty *hexutil.Big
|