From d4f03387d6d7b366e5a151705f197716356b946e Mon Sep 17 00:00:00 2001 From: Tudor Malene Date: Wed, 22 Nov 2023 16:10:15 +0000 Subject: [PATCH] fix json serialisation for batch --- go/common/headers.go | 92 +++++++++++++++++++------ go/host/rpc/clientapi/client_api_eth.go | 10 +-- 2 files changed, 76 insertions(+), 26 deletions(-) diff --git a/go/common/headers.go b/go/common/headers.go index 66732a7db6..8052324bb7 100644 --- a/go/common/headers.go +++ b/go/common/headers.go @@ -12,7 +12,6 @@ import ( "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" "github.com/ten-protocol/go-ten/contracts/generated/MessageBus" "golang.org/x/crypto/sha3" ) @@ -47,32 +46,83 @@ type BatchHeader struct { TransfersTree common.Hash `json:"transfersTree"` // This is a merkle tree of all of the outbound value transfers for the MainNet } +type batchHeaderEncoding struct { + ParentHash L2BatchHash `json:"parentHash"` + Root common.Hash `json:"stateRoot"` + TxHash common.Hash `json:"transactionsRoot"` + ReceiptHash common.Hash `json:"receiptsRoot"` + Number *hexutil.Big `json:"number"` + SequencerOrderNo *hexutil.Big `json:"sequencerOrderNo"` + GasLimit hexutil.Uint64 `json:"gasLimit"` + GasUsed hexutil.Uint64 `json:"gasUsed"` + Time hexutil.Uint64 `json:"timestamp"` + Extra []byte `json:"extraData"` + BaseFee *hexutil.Big `json:"baseFeePerGas"` + Coinbase *common.Address `json:"miner"` + + // The custom Obscuro fields. + L1Proof L1BlockHash `json:"l1Proof"` // the L1 block used by the enclave to generate the current batch + R, S *hexutil.Big // signature values + CrossChainMessages []MessageBus.StructsCrossChainMessage `json:"crossChainMessages"` + LatestInboundCrossChainHash common.Hash `json:"inboundCrossChainHash"` // The block hash of the latest block that has been scanned for cross chain messages. + LatestInboundCrossChainHeight *hexutil.Big `json:"inboundCrossChainHeight"` // The block height of the latest block that has been scanned for cross chain messages. + TransfersTree common.Hash +} + // MarshalJSON custom marshals the BatchHeader into a json func (b *BatchHeader) MarshalJSON() ([]byte, error) { - type Alias BatchHeader - return json.Marshal(struct { - *Alias - Hash common.Hash `json:"hash"` - UncleHash *common.Hash `json:"sha3Uncles"` - Coinbase *common.Address `json:"miner"` - Bloom *types.Bloom `json:"logsBloom"` - Difficulty *big.Int `json:"difficulty"` - Nonce *types.BlockNonce `json:"nonce"` - - // BaseFee was added by EIP-1559 and is ignored in legacy headers. - BaseFee *hexutil.Big `json:"baseFeePerGas"` - }{ - (*Alias)(b), - b.Hash(), - nil, - &b.Coinbase, - nil, - nil, - nil, + return json.Marshal(batchHeaderEncoding{ + b.ParentHash, + b.Root, + b.TxHash, + b.ReceiptHash, + (*hexutil.Big)(b.Number), + (*hexutil.Big)(b.SequencerOrderNo), + hexutil.Uint64(b.GasLimit), + hexutil.Uint64(b.GasUsed), + hexutil.Uint64(b.Time), + b.Extra, (*hexutil.Big)(b.BaseFee), + &b.Coinbase, + b.L1Proof, + (*hexutil.Big)(b.R), + (*hexutil.Big)(b.S), + b.CrossChainMessages, + b.LatestInboundCrossChainHash, + (*hexutil.Big)(b.LatestInboundCrossChainHeight), + b.TransfersTree, }) } +func (b *BatchHeader) UnmarshalJSON(data []byte) error { + dec := new(batchHeaderEncoding) + err := json.Unmarshal(data, dec) + if err != nil { + return err + } + + b.ParentHash = dec.ParentHash + b.Root = dec.Root + b.TxHash = dec.TxHash + b.ReceiptHash = dec.ReceiptHash + b.Number = (*big.Int)(dec.Number) + b.SequencerOrderNo = (*big.Int)(dec.SequencerOrderNo) + b.GasLimit = uint64(dec.GasLimit) + b.GasUsed = uint64(dec.GasUsed) + b.Time = uint64(dec.Time) + b.Extra = dec.Extra + b.BaseFee = (*big.Int)(dec.BaseFee) + b.Coinbase = *dec.Coinbase + b.L1Proof = dec.L1Proof + b.R = (*big.Int)(dec.R) + b.S = (*big.Int)(dec.S) + b.CrossChainMessages = dec.CrossChainMessages + b.LatestInboundCrossChainHash = dec.LatestInboundCrossChainHash + b.LatestInboundCrossChainHeight = (*big.Int)(dec.LatestInboundCrossChainHeight) + b.TransfersTree = dec.TransfersTree + return nil +} + // RollupHeader is a public / plaintext struct that holds common properties of rollups. // All these fields are processed by the Management contract type RollupHeader struct { diff --git a/go/host/rpc/clientapi/client_api_eth.go b/go/host/rpc/clientapi/client_api_eth.go index 2e23b412bd..49bdfe03bf 100644 --- a/go/host/rpc/clientapi/client_api_eth.go +++ b/go/host/rpc/clientapi/client_api_eth.go @@ -48,21 +48,21 @@ func (api *EthereumAPI) BlockNumber() hexutil.Uint64 { } // GetBlockByNumber returns the header of the batch with the given height. -func (api *EthereumAPI) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, _ bool) (common.BatchHeader, error) { +func (api *EthereumAPI) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, _ bool) (*common.BatchHeader, error) { batchHash, err := api.batchNumberToBatchHash(number) if err != nil { - return common.BatchHeader{}, fmt.Errorf("could not find batch with height %d. Cause: %w", number, err) + return nil, fmt.Errorf("could not find batch with height %d. Cause: %w", number, err) } return api.GetBlockByHash(ctx, *batchHash, true) } // GetBlockByHash returns the header of the batch with the given hash. -func (api *EthereumAPI) GetBlockByHash(_ context.Context, hash gethcommon.Hash, _ bool) (common.BatchHeader, error) { +func (api *EthereumAPI) GetBlockByHash(_ context.Context, hash gethcommon.Hash, _ bool) (*common.BatchHeader, error) { batchHeader, err := api.host.DB().GetBatchHeader(hash) if err != nil { - return common.BatchHeader{}, err + return nil, err } - return *batchHeader, nil + return batchHeader, nil } // GasPrice is a placeholder for an RPC method required by MetaMask/Remix.