core/types: add Codeword to block header and gen_header

This commit is contained in:
Your Name 2024-04-23 13:33:26 +09:00
parent d92b1b34ad
commit 54c066bbd7
3 changed files with 57 additions and 3 deletions

View file

@ -86,6 +86,10 @@ type Header struct {
// BaseFee was added by EIP-1559 and is ignored in legacy headers.
BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"`
// Codeword was added by Worldlandhardfork and is ignored in legacy headers.
Codeword []byte `json:"codeword" rlp:"optional"`
// CodeLength was added by Worldlandhardfork and is ignored in legacy headers.
CodeLength uint64 `json:"codelength" rlp:"optional"`
/*
TODO (MariusVanDerWijden) Add this field once needed
@ -103,6 +107,8 @@ type headerMarshaling struct {
Time hexutil.Uint64
Extra hexutil.Bytes
BaseFee *hexutil.Big
//Codeword hexutil.Bytes
//CodeLength hexutil.Uint64
Hash common.Hash `json:"hash"` // adds call to Hash() in MarshalJSON
}
@ -117,6 +123,7 @@ var headerSize = common.StorageSize(reflect.TypeOf(Header{}).Size())
// Size returns the approximate memory used by all internal contents. It is used
// to approximate and limit the memory consumption of various caches.
func (h *Header) Size() common.StorageSize {
//return headerSize + common.StorageSize(len(h.Codeword)+len(h.Extra)+(h.Difficulty.BitLen()+h.Number.BitLen())/8)
return headerSize + common.StorageSize(len(h.Extra)+(h.Difficulty.BitLen()+h.Number.BitLen())/8)
}
@ -141,6 +148,9 @@ func (h *Header) SanityCheck() error {
return fmt.Errorf("too large base fee: bitlen %d", bfLen)
}
}
//maybe add codeword?
return nil
}
@ -248,6 +258,13 @@ func CopyHeader(h *Header) *Header {
cpy.Extra = make([]byte, len(h.Extra))
copy(cpy.Extra, h.Extra)
}
/*if len(h.Codeword) > 0 {
cpy.Codeword = make([]byte, len(h.Codeword))
copy(cpy.Codeword, h.Codeword)
}
if h.CodeLength != nil {
cpy.BaseFee = new(big.Int).Set(h.CodeLength)
}*/
return &cpy
}
@ -304,6 +321,17 @@ func (b *Block) ReceiptHash() common.Hash { return b.header.ReceiptHash }
func (b *Block) UncleHash() common.Hash { return b.header.UncleHash }
func (b *Block) Extra() []byte { return common.CopyBytes(b.header.Extra) }
func (b *Block) Codeword() []byte {
if b.header.Codeword == nil {
return nil
}
return common.CopyBytes(b.header.Codeword)
}
func (b *Block) CodeLength() uint64 {
return b.header.CodeLength
}
func (b *Block) BaseFee() *big.Int {
if b.header.BaseFee == nil {
return nil

View file

@ -33,6 +33,9 @@ func (h Header) MarshalJSON() ([]byte, error) {
Nonce BlockNonce `json:"nonce"`
BaseFee *hexutil.Big `json:"baseFeePerGas" rlp:"optional"`
Hash common.Hash `json:"hash"`
Codeword hexutil.Bytes `json:"codeword" rlp:"optional"`
CodeLength hexutil.Uint64 `json:"codelength" rlp:"optional"`
}
var enc Header
enc.ParentHash = h.ParentHash
@ -52,6 +55,8 @@ func (h Header) MarshalJSON() ([]byte, error) {
enc.Nonce = h.Nonce
enc.BaseFee = (*hexutil.Big)(h.BaseFee)
enc.Hash = h.Hash()
enc.Codeword = h.Codeword
enc.CodeLength = hexutil.Uint64(h.CodeLength)
return json.Marshal(&enc)
}
@ -74,6 +79,8 @@ func (h *Header) UnmarshalJSON(input []byte) error {
MixDigest *common.Hash `json:"mixHash"`
Nonce *BlockNonce `json:"nonce"`
BaseFee *hexutil.Big `json:"baseFeePerGas" rlp:"optional"`
Codeword *hexutil.Bytes `json:"codeword" rlp:"optional"`
CodeLength *hexutil.Uint64 `json:"codelength" rlp:"optional"`
}
var dec Header
if err := json.Unmarshal(input, &dec); err != nil {
@ -139,5 +146,12 @@ func (h *Header) UnmarshalJSON(input []byte) error {
if dec.BaseFee != nil {
h.BaseFee = (*big.Int)(dec.BaseFee)
}
if dec.Codeword == nil {
h.Codeword = *dec.Codeword
}
if dec.CodeLength == nil {
h.CodeLength = uint64(*dec.CodeLength)
}
return nil
}

View file

@ -34,6 +34,7 @@ func (obj *Header) EncodeRLP(_w io.Writer) error {
}
w.WriteBigInt(obj.Number)
}
w.WriteUint64(obj.GasLimit)
w.WriteUint64(obj.GasUsed)
w.WriteUint64(obj.Time)
@ -51,6 +52,17 @@ func (obj *Header) EncodeRLP(_w io.Writer) error {
w.WriteBigInt(obj.BaseFee)
}
}
_tmp2 := obj.Codeword != nil
if _tmp2 {
if obj.Codeword == nil {
w.Write(rlp.EmptyString)
} else {
w.WriteBytes(obj.Codeword)
}
}
if obj.CodeLength != 0 {
w.WriteUint64(obj.CodeLength)
}
w.ListEnd(_tmp0)
return w.Flush()
}