Skip to content

Conversation

@ohko4711
Copy link

  1. use json-iterator 100% compatibility with standard lib
  2. To avoid conflicts with json in the existing code, jsonIter is used instead.

tested with erigon's Genesis struct:

./gencodec -dir ./erigon-lib/types -type Genesis  -out gen_genesis-new.go

// Code generated by github.com/fjl/gencodec. DO NOT EDIT.

package types

import (
	"errors"
	"math/big"

	"github.com/erigontech/erigon-lib/chain"
	"github.com/erigontech/erigon-lib/common"
	jsoniter "github.com/json-iterator/go"
)

var jsonIter = jsoniter.ConfigCompatibleWithStandardLibrary

// MarshalJSON marshals as JSON.
func (g Genesis) MarshalJSON() ([]byte, error) {
	type Genesis struct {
		Config                *chain.Config  `json:"config"`
		Nonce                 uint64         `json:"nonce"`
		Timestamp             uint64         `json:"timestamp"`
		ExtraData             []byte         `json:"extraData"`
		GasLimit              uint64         `json:"gasLimit"   gencodec:"required"`
		Difficulty            *big.Int       `json:"difficulty" gencodec:"required"`
		Mixhash               common.Hash    `json:"mixHash"`
		Coinbase              common.Address `json:"coinbase"`
		Alloc                 GenesisAlloc   `json:"alloc"      gencodec:"required"`
		AuRaSeal              *AuRaSeal      `json:"seal"`
		Number                uint64         `json:"number"`
		GasUsed               uint64         `json:"gasUsed"`
		ParentHash            common.Hash    `json:"parentHash"`
		BaseFee               *big.Int       `json:"baseFeePerGas"`
		BlobGasUsed           *uint64        `json:"blobGasUsed"`
		ExcessBlobGas         *uint64        `json:"excessBlobGas"`
		ParentBeaconBlockRoot *common.Hash   `json:"parentBeaconBlockRoot"`
		RequestsHash          *common.Hash   `json:"requestsHash"`
	}
	var enc Genesis
	enc.Config = g.Config
	enc.Nonce = g.Nonce
	enc.Timestamp = g.Timestamp
	enc.ExtraData = g.ExtraData
	enc.GasLimit = g.GasLimit
	enc.Difficulty = g.Difficulty
	enc.Mixhash = g.Mixhash
	enc.Coinbase = g.Coinbase
	enc.Alloc = g.Alloc
	enc.AuRaSeal = g.AuRaSeal
	enc.Number = g.Number
	enc.GasUsed = g.GasUsed
	enc.ParentHash = g.ParentHash
	enc.BaseFee = g.BaseFee
	enc.BlobGasUsed = g.BlobGasUsed
	enc.ExcessBlobGas = g.ExcessBlobGas
	enc.ParentBeaconBlockRoot = g.ParentBeaconBlockRoot
	enc.RequestsHash = g.RequestsHash
	return jsonIter.Marshal(&enc)
}

// UnmarshalJSON unmarshals from JSON.
func (g *Genesis) UnmarshalJSON(input []byte) error {
	type Genesis struct {
		Config                *chain.Config   `json:"config"`
		Nonce                 *uint64         `json:"nonce"`
		Timestamp             *uint64         `json:"timestamp"`
		ExtraData             []byte          `json:"extraData"`
		GasLimit              *uint64         `json:"gasLimit"   gencodec:"required"`
		Difficulty            *big.Int        `json:"difficulty" gencodec:"required"`
		Mixhash               *common.Hash    `json:"mixHash"`
		Coinbase              *common.Address `json:"coinbase"`
		Alloc                 *GenesisAlloc   `json:"alloc"      gencodec:"required"`
		AuRaSeal              *AuRaSeal       `json:"seal"`
		Number                *uint64         `json:"number"`
		GasUsed               *uint64         `json:"gasUsed"`
		ParentHash            *common.Hash    `json:"parentHash"`
		BaseFee               *big.Int        `json:"baseFeePerGas"`
		BlobGasUsed           *uint64         `json:"blobGasUsed"`
		ExcessBlobGas         *uint64         `json:"excessBlobGas"`
		ParentBeaconBlockRoot *common.Hash    `json:"parentBeaconBlockRoot"`
		RequestsHash          *common.Hash    `json:"requestsHash"`
	}
	var dec Genesis
	if err := jsonIter.Unmarshal(input, &dec); err != nil {
		return err
	}
	if dec.Config != nil {
		g.Config = dec.Config
	}
	if dec.Nonce != nil {
		g.Nonce = *dec.Nonce
	}
	if dec.Timestamp != nil {
		g.Timestamp = *dec.Timestamp
	}
	if dec.ExtraData != nil {
		g.ExtraData = dec.ExtraData
	}
	if dec.GasLimit == nil {
		return errors.New("missing required field 'gasLimit' for Genesis")
	}
	g.GasLimit = *dec.GasLimit
	if dec.Difficulty == nil {
		return errors.New("missing required field 'difficulty' for Genesis")
	}
	g.Difficulty = dec.Difficulty
	if dec.Mixhash != nil {
		g.Mixhash = *dec.Mixhash
	}
	if dec.Coinbase != nil {
		g.Coinbase = *dec.Coinbase
	}
	if dec.Alloc == nil {
		return errors.New("missing required field 'alloc' for Genesis")
	}
	g.Alloc = *dec.Alloc
	if dec.AuRaSeal != nil {
		g.AuRaSeal = dec.AuRaSeal
	}
	if dec.Number != nil {
		g.Number = *dec.Number
	}
	if dec.GasUsed != nil {
		g.GasUsed = *dec.GasUsed
	}
	if dec.ParentHash != nil {
		g.ParentHash = *dec.ParentHash
	}
	if dec.BaseFee != nil {
		g.BaseFee = dec.BaseFee
	}
	if dec.BlobGasUsed != nil {
		g.BlobGasUsed = dec.BlobGasUsed
	}
	if dec.ExcessBlobGas != nil {
		g.ExcessBlobGas = dec.ExcessBlobGas
	}
	if dec.ParentBeaconBlockRoot != nil {
		g.ParentBeaconBlockRoot = dec.ParentBeaconBlockRoot
	}
	if dec.RequestsHash != nil {
		g.RequestsHash = dec.RequestsHash
	}
	return nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant