Commit 64f5ff55 authored by Mark Tyneway's avatar Mark Tyneway Committed by GitHub

supervisor: add Identifier JSON marshaling (#10718)

* supervisor: add Identifier JSON marshaling

Adds json marshaling so that the type used within
is easier to deal with.

* op-supervisor: fix identifier struct

---------
Co-authored-by: default avatarprotolambda <proto@protolambda.com>
parent 7cb6cdde
package types package types
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/holiman/uint256"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
) )
type Identifier struct { type Identifier struct {
Origin common.Address
BlockNumber uint64
LogIndex uint64
Timestamp uint64
ChainID uint256.Int // flat, not a pointer, to make Identifier safe as map key
}
type identifierMarshaling struct {
Origin common.Address `json:"origin"` Origin common.Address `json:"origin"`
BlockNumber hexutil.Uint64 `json:"blockNumber"` BlockNumber hexutil.Uint64 `json:"blockNumber"`
LogIndex hexutil.Uint64 `json:"logIndex"` LogIndex hexutil.Uint64 `json:"logIndex"`
...@@ -16,6 +27,29 @@ type Identifier struct { ...@@ -16,6 +27,29 @@ type Identifier struct {
ChainID hexutil.U256 `json:"chainID"` ChainID hexutil.U256 `json:"chainID"`
} }
func (id Identifier) MarshalJSON() ([]byte, error) {
var enc identifierMarshaling
enc.Origin = id.Origin
enc.BlockNumber = hexutil.Uint64(id.BlockNumber)
enc.LogIndex = hexutil.Uint64(id.LogIndex)
enc.Timestamp = hexutil.Uint64(id.Timestamp)
enc.ChainID = (hexutil.U256)(id.ChainID)
return json.Marshal(&enc)
}
func (id *Identifier) UnmarshalJSON(input []byte) error {
var dec identifierMarshaling
if err := json.Unmarshal(input, &dec); err != nil {
return err
}
id.Origin = dec.Origin
id.BlockNumber = uint64(dec.BlockNumber)
id.LogIndex = uint64(dec.LogIndex)
id.Timestamp = uint64(dec.Timestamp)
id.ChainID = (uint256.Int)(dec.ChainID)
return nil
}
type SafetyLevel string type SafetyLevel string
func (lvl SafetyLevel) String() string { func (lvl SafetyLevel) String() string {
......
package types
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
"github.com/ethereum/go-ethereum/common"
"github.com/holiman/uint256"
)
func FuzzRoundtripIdentifierJSONMarshal(f *testing.F) {
f.Fuzz(func(t *testing.T, origin []byte, blockNumber uint64, logIndex uint64, timestamp uint64, chainID []byte) {
if len(chainID) > 32 {
chainID = chainID[:32]
}
id := Identifier{
Origin: common.BytesToAddress(origin),
BlockNumber: blockNumber,
LogIndex: logIndex,
Timestamp: timestamp,
ChainID: uint256.Int{},
}
id.ChainID.SetBytes(chainID)
raw, err := json.Marshal(&id)
require.NoError(t, err)
var dec Identifier
require.NoError(t, json.Unmarshal(raw, &dec))
require.Equal(t, id.Origin, dec.Origin)
require.Equal(t, id.BlockNumber, dec.BlockNumber)
require.Equal(t, id.LogIndex, dec.LogIndex)
require.Equal(t, id.Timestamp, dec.Timestamp)
require.Equal(t, id.ChainID, dec.ChainID)
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment