Skip to content

Commit db77e0b

Browse files
authored
op-supervisor: Move ChainID and SuperRootResponse to op-service eth package (#13786)
1 parent ca4b1f6 commit db77e0b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+664
-639
lines changed

op-e2e/actions/interop/interop.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"time"
77

8+
"github.com/ethereum-optimism/optimism/op-service/eth"
89
"github.com/ethereum/go-ethereum/core"
910
"github.com/ethereum/go-ethereum/log"
1011
"github.com/stretchr/testify/require"
@@ -38,7 +39,7 @@ const (
3839

3940
// Chain holds the most common per-chain action-test data and actors
4041
type Chain struct {
41-
ChainID types.ChainID
42+
ChainID eth.ChainID
4243

4344
RollupCfg *rollup.Config
4445
L2Genesis *core.Genesis
@@ -148,9 +149,9 @@ func (sa *SupervisorActor) SignalFinalizedL1(t helpers.Testing) {
148149

149150
// worldToDepSet converts a set of chain configs into a dependency-set for the supervisor.
150151
func worldToDepSet(t helpers.Testing, worldOutput *interopgen.WorldOutput) *depset.StaticConfigDependencySet {
151-
depSetCfg := make(map[types.ChainID]*depset.StaticConfigDependency)
152+
depSetCfg := make(map[eth.ChainID]*depset.StaticConfigDependency)
152153
for _, out := range worldOutput.L2s {
153-
depSetCfg[types.ChainIDFromBig(out.Genesis.Config.ChainID)] = &depset.StaticConfigDependency{
154+
depSetCfg[eth.ChainIDFromBig(out.Genesis.Config.ChainID)] = &depset.StaticConfigDependency{
154155
ChainIndex: types.ChainIndex(out.Genesis.Config.ChainID.Uint64()),
155156
ActivationTime: 0,
156157
HistoryMinTime: 0,
@@ -231,7 +232,7 @@ func createL2Services(
231232
eng.EthClient(), eng.EngineClient(t, output.RollupCfg))
232233

233234
return &Chain{
234-
ChainID: types.ChainIDFromBig(output.Genesis.Config.ChainID),
235+
ChainID: eth.ChainIDFromBig(output.Genesis.Config.ChainID),
235236
RollupCfg: output.RollupCfg,
236237
L2Genesis: output.Genesis,
237238
BatcherAddr: crypto.PubkeyToAddress(batcherKey.PublicKey),

op-e2e/interop/interop_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/ethereum-optimism/optimism/op-service/dial"
11+
"github.com/ethereum-optimism/optimism/op-service/eth"
1112
oplog "github.com/ethereum-optimism/optimism/op-service/log"
1213
"github.com/ethereum-optimism/optimism/op-service/testlog"
1314
"github.com/ethereum/go-ethereum/log"
@@ -198,7 +199,7 @@ func TestInterop_EmitLogs(t *testing.T) {
198199
BlockNumber: log.BlockNumber,
199200
LogIndex: uint32(log.Index),
200201
Timestamp: block.Time(),
201-
ChainID: types.ChainIDFromBig(s2.ChainID(chainID)),
202+
ChainID: eth.ChainIDFromBig(s2.ChainID(chainID)),
202203
}
203204
return identifier, expectedHash
204205
}
@@ -290,7 +291,7 @@ func TestInteropBlockBuilding(t *testing.T) {
290291
BlockNumber: ev.BlockNumber,
291292
LogIndex: uint32(ev.Index),
292293
Timestamp: header.Time,
293-
ChainID: types.ChainIDFromBig(s2.ChainID(chainA)),
294+
ChainID: eth.ChainIDFromBig(s2.ChainID(chainA)),
294295
}
295296

296297
msgPayload := types.LogToMessagePayload(ev)

op-e2e/interop/supersystem.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212
"time"
1313

14+
"github.com/ethereum-optimism/optimism/op-service/eth"
1415
"github.com/stretchr/testify/require"
1516

1617
"github.com/ethereum/go-ethereum/accounts/abi/bind"
@@ -485,11 +486,11 @@ func (s *interopE2ESystem) prepareSupervisor() *supervisor.SupervisorService {
485486
L1RPC: s.l1.UserRPC().RPC(),
486487
Datadir: path.Join(s.t.TempDir(), "supervisor"),
487488
}
488-
depSet := make(map[supervisortypes.ChainID]*depset.StaticConfigDependency)
489+
depSet := make(map[eth.ChainID]*depset.StaticConfigDependency)
489490

490491
// Iterate over the L2 chain configs. The L2 nodes don't exist yet.
491492
for _, l2Out := range s.worldOutput.L2s {
492-
chainID := supervisortypes.ChainIDFromBig(l2Out.Genesis.Config.ChainID)
493+
chainID := eth.ChainIDFromBig(l2Out.Genesis.Config.ChainID)
493494
index, err := chainID.ToUInt32()
494495
require.NoError(s.t, err)
495496
depSet[chainID] = &depset.StaticConfigDependency{

op-node/rollup/interop/managed/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (ib *InteropAPI) BlockRefByNumber(ctx context.Context, num uint64) (eth.Blo
5151
return ib.backend.BlockRefByNumber(ctx, num)
5252
}
5353

54-
func (ib *InteropAPI) ChainID(ctx context.Context) (supervisortypes.ChainID, error) {
54+
func (ib *InteropAPI) ChainID(ctx context.Context) (eth.ChainID, error) {
5555
return ib.backend.ChainID(ctx)
5656
}
5757

op-node/rollup/interop/managed/system.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ func (m *ManagedMode) BlockRefByNumber(ctx context.Context, num uint64) (eth.Blo
276276
return m.l2.BlockRefByNumber(ctx, num)
277277
}
278278

279-
func (m *ManagedMode) ChainID(ctx context.Context) (supervisortypes.ChainID, error) {
280-
return supervisortypes.ChainIDFromBig(m.cfg.L2ChainID), nil
279+
func (m *ManagedMode) ChainID(ctx context.Context) (eth.ChainID, error) {
280+
return eth.ChainIDFromBig(m.cfg.L2ChainID), nil
281281
}
282282

283283
func (m *ManagedMode) OutputV0AtTimestamp(ctx context.Context, timestamp uint64) (*eth.OutputV0, error) {

op-service/eth/chain_id.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package eth
2+
3+
import (
4+
"fmt"
5+
"math"
6+
"math/big"
7+
8+
"github.com/holiman/uint256"
9+
)
10+
11+
type ChainID uint256.Int
12+
13+
func ChainIDFromBig(chainID *big.Int) ChainID {
14+
return ChainID(*uint256.MustFromBig(chainID))
15+
}
16+
17+
func ChainIDFromUInt64(i uint64) ChainID {
18+
return ChainID(*uint256.NewInt(i))
19+
}
20+
21+
func (id ChainID) String() string {
22+
return ((*uint256.Int)(&id)).Dec()
23+
}
24+
25+
func (id ChainID) ToUInt32() (uint32, error) {
26+
v := (*uint256.Int)(&id)
27+
if !v.IsUint64() {
28+
return 0, fmt.Errorf("ChainID too large for uint32: %v", id)
29+
}
30+
v64 := v.Uint64()
31+
if v64 > math.MaxUint32 {
32+
return 0, fmt.Errorf("ChainID too large for uint32: %v", id)
33+
}
34+
return uint32(v64), nil
35+
}
36+
37+
func (id *ChainID) ToBig() *big.Int {
38+
return (*uint256.Int)(id).ToBig()
39+
}
40+
41+
func (id ChainID) MarshalText() ([]byte, error) {
42+
return []byte(id.String()), nil
43+
}
44+
45+
func (id *ChainID) UnmarshalText(data []byte) error {
46+
var x uint256.Int
47+
err := x.UnmarshalText(data)
48+
if err != nil {
49+
return err
50+
}
51+
*id = ChainID(x)
52+
return nil
53+
}
54+
55+
func (id ChainID) Cmp(other ChainID) int {
56+
return (*uint256.Int)(&id).Cmp((*uint256.Int)(&other))
57+
}

op-service/eth/chain_id_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package eth
2+
3+
import (
4+
"math"
5+
"testing"
6+
7+
"github.com/holiman/uint256"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestChainID_String(t *testing.T) {
12+
tests := []struct {
13+
input ChainID
14+
expected string
15+
}{
16+
{ChainIDFromUInt64(0), "0"},
17+
{ChainIDFromUInt64(1), "1"},
18+
{ChainIDFromUInt64(871975192374), "871975192374"},
19+
{ChainIDFromUInt64(math.MaxInt64), "9223372036854775807"},
20+
{ChainID(*uint256.NewInt(math.MaxUint64)), "18446744073709551615"},
21+
{ChainID(*uint256.MustFromDecimal("1844674407370955161618446744073709551616")), "1844674407370955161618446744073709551616"},
22+
}
23+
for _, test := range tests {
24+
test := test
25+
t.Run(test.expected, func(t *testing.T) {
26+
t.Run("String", func(t *testing.T) {
27+
require.Equal(t, test.expected, test.input.String())
28+
})
29+
t.Run("MarshalText", func(t *testing.T) {
30+
data, err := test.input.MarshalText()
31+
require.NoError(t, err)
32+
require.Equal(t, test.expected, string(data))
33+
})
34+
t.Run("UnmarshalText", func(t *testing.T) {
35+
var id ChainID
36+
require.NoError(t, id.UnmarshalText([]byte(test.expected)))
37+
require.Equal(t, test.input, id)
38+
})
39+
})
40+
}
41+
}

op-service/eth/super_root.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package eth
22

33
import (
44
"encoding/binary"
5+
"encoding/json"
56
"errors"
67

8+
"github.com/ethereum/go-ethereum/common"
9+
"github.com/ethereum/go-ethereum/common/hexutil"
710
"github.com/ethereum/go-ethereum/crypto"
811
)
912

@@ -95,3 +98,72 @@ func unmarshalSuperRootV1(data []byte) (*SuperV1, error) {
9598
}
9699
return &output, nil
97100
}
101+
102+
type ChainRootInfo struct {
103+
ChainID ChainID `json:"chainID"`
104+
// Canonical is the output root of the latest canonical block at a particular Timestamp.
105+
Canonical Bytes32 `json:"canonical"`
106+
// Pending is the output root preimage for the latest block at a particular Timestamp prior to validation of
107+
// executing messages. If the original block was valid, this will be the preimage of the
108+
// output root from the Canonical array. If it was invalid, it will be the output root preimage from the
109+
// Optimistic Block Deposited Transaction added to the deposit-only block.
110+
Pending []byte `json:"pending"`
111+
}
112+
113+
type chainRootInfoMarshalling struct {
114+
ChainID ChainID `json:"chainID"`
115+
Canonical common.Hash `json:"canonical"`
116+
Pending hexutil.Bytes `json:"pending"`
117+
}
118+
119+
func (i ChainRootInfo) MarshalJSON() ([]byte, error) {
120+
return json.Marshal(&chainRootInfoMarshalling{
121+
ChainID: i.ChainID,
122+
Canonical: common.Hash(i.Canonical),
123+
Pending: i.Pending,
124+
})
125+
}
126+
127+
func (i *ChainRootInfo) UnmarshalJSON(input []byte) error {
128+
var dec chainRootInfoMarshalling
129+
if err := json.Unmarshal(input, &dec); err != nil {
130+
return err
131+
}
132+
i.ChainID = dec.ChainID
133+
i.Canonical = Bytes32(dec.Canonical)
134+
i.Pending = dec.Pending
135+
return nil
136+
}
137+
138+
type SuperRootResponse struct {
139+
Timestamp uint64 `json:"timestamp"`
140+
SuperRoot Bytes32 `json:"superRoot"`
141+
// Chains is the list of ChainRootInfo for each chain in the dependency set.
142+
// It represents the state of the chain at or before the Timestamp.
143+
Chains []ChainRootInfo `json:"chains"`
144+
}
145+
146+
type superRootResponseMarshalling struct {
147+
Timestamp hexutil.Uint64 `json:"timestamp"`
148+
SuperRoot common.Hash `json:"superRoot"`
149+
Chains []ChainRootInfo `json:"chains"`
150+
}
151+
152+
func (r SuperRootResponse) MarshalJSON() ([]byte, error) {
153+
return json.Marshal(&superRootResponseMarshalling{
154+
Timestamp: hexutil.Uint64(r.Timestamp),
155+
SuperRoot: common.Hash(r.SuperRoot),
156+
Chains: r.Chains,
157+
})
158+
}
159+
160+
func (r *SuperRootResponse) UnmarshalJSON(input []byte) error {
161+
var dec superRootResponseMarshalling
162+
if err := json.Unmarshal(input, &dec); err != nil {
163+
return err
164+
}
165+
r.Timestamp = uint64(dec.Timestamp)
166+
r.SuperRoot = Bytes32(dec.SuperRoot)
167+
r.Chains = dec.Chains
168+
return nil
169+
}

op-service/sources/supervisor_client.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (cl *SupervisorClient) CheckMessage(ctx context.Context, identifier types.I
7777
return result, nil
7878
}
7979

80-
func (cl *SupervisorClient) UnsafeView(ctx context.Context, chainID types.ChainID, unsafe types.ReferenceView) (types.ReferenceView, error) {
80+
func (cl *SupervisorClient) UnsafeView(ctx context.Context, chainID eth.ChainID, unsafe types.ReferenceView) (types.ReferenceView, error) {
8181
var result types.ReferenceView
8282
err := cl.client.CallContext(
8383
ctx,
@@ -91,7 +91,7 @@ func (cl *SupervisorClient) UnsafeView(ctx context.Context, chainID types.ChainI
9191
return result, nil
9292
}
9393

94-
func (cl *SupervisorClient) SafeView(ctx context.Context, chainID types.ChainID, safe types.ReferenceView) (types.ReferenceView, error) {
94+
func (cl *SupervisorClient) SafeView(ctx context.Context, chainID eth.ChainID, safe types.ReferenceView) (types.ReferenceView, error) {
9595
var result types.ReferenceView
9696
err := cl.client.CallContext(
9797
ctx,
@@ -105,7 +105,7 @@ func (cl *SupervisorClient) SafeView(ctx context.Context, chainID types.ChainID,
105105
return result, nil
106106
}
107107

108-
func (cl *SupervisorClient) Finalized(ctx context.Context, chainID types.ChainID) (eth.BlockID, error) {
108+
func (cl *SupervisorClient) Finalized(ctx context.Context, chainID eth.ChainID) (eth.BlockID, error) {
109109
var result eth.BlockID
110110
err := cl.client.CallContext(
111111
ctx,
@@ -124,7 +124,7 @@ func (cl *SupervisorClient) FinalizedL1(ctx context.Context) (eth.BlockRef, erro
124124
return result, err
125125
}
126126

127-
func (cl *SupervisorClient) CrossDerivedFrom(ctx context.Context, chainID types.ChainID, derived eth.BlockID) (eth.BlockRef, error) {
127+
func (cl *SupervisorClient) CrossDerivedFrom(ctx context.Context, chainID eth.ChainID, derived eth.BlockID) (eth.BlockRef, error) {
128128
var result eth.BlockRef
129129
err := cl.client.CallContext(
130130
ctx,
@@ -135,7 +135,7 @@ func (cl *SupervisorClient) CrossDerivedFrom(ctx context.Context, chainID types.
135135
return result, err
136136
}
137137

138-
func (cl *SupervisorClient) UpdateLocalUnsafe(ctx context.Context, chainID types.ChainID, head eth.BlockRef) error {
138+
func (cl *SupervisorClient) UpdateLocalUnsafe(ctx context.Context, chainID eth.ChainID, head eth.BlockRef) error {
139139
return cl.client.CallContext(
140140
ctx,
141141
nil,
@@ -144,7 +144,7 @@ func (cl *SupervisorClient) UpdateLocalUnsafe(ctx context.Context, chainID types
144144
head)
145145
}
146146

147-
func (cl *SupervisorClient) UpdateLocalSafe(ctx context.Context, chainID types.ChainID, derivedFrom eth.L1BlockRef, lastDerived eth.BlockRef) error {
147+
func (cl *SupervisorClient) UpdateLocalSafe(ctx context.Context, chainID eth.ChainID, derivedFrom eth.L1BlockRef, lastDerived eth.BlockRef) error {
148148
return cl.client.CallContext(
149149
ctx,
150150
nil,
@@ -154,8 +154,8 @@ func (cl *SupervisorClient) UpdateLocalSafe(ctx context.Context, chainID types.C
154154
lastDerived)
155155
}
156156

157-
func (cl *SupervisorClient) SuperRootAtTimestamp(ctx context.Context, timestamp hexutil.Uint64) (types.SuperRootResponse, error) {
158-
var result types.SuperRootResponse
157+
func (cl *SupervisorClient) SuperRootAtTimestamp(ctx context.Context, timestamp hexutil.Uint64) (eth.SuperRootResponse, error) {
158+
var result eth.SuperRootResponse
159159
err := cl.client.CallContext(
160160
ctx,
161161
&result,

0 commit comments

Comments
 (0)