Skip to content

Commit e0b9064

Browse files
committed
superchain: add GetDepset() function
1 parent 6807599 commit e0b9064

File tree

3 files changed

+169
-1
lines changed

3 files changed

+169
-1
lines changed

superchain/chain.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,36 @@ func GetChain(chainID uint64) (*Chain, error) {
100100
return BuiltInConfigs.GetChain(chainID)
101101
}
102102

103+
func GetDepset(chainID uint64) (map[string]Dependency, error) {
104+
chain, err := BuiltInConfigs.GetChain(chainID)
105+
if err != nil {
106+
return nil, err
107+
}
108+
cfg, err := chain.Config()
109+
if err != nil {
110+
return nil, err
111+
}
112+
113+
if cfg.Hardforks.InteropTime == nil {
114+
return nil, nil
115+
}
116+
117+
// depset of 1 (self) is the default when no dependencies are specified but interop_time is set
118+
if cfg.Interop == nil {
119+
cfg.Interop = &Interop{
120+
Dependencies: make(map[string]Dependency),
121+
}
122+
self := Dependency{
123+
ChainIndex: 1,
124+
ActivationTime: *cfg.Hardforks.InteropTime,
125+
}
126+
127+
cfg.Interop.Dependencies[fmt.Sprintf("%d", cfg.ChainID)] = self
128+
}
129+
130+
return cfg.Interop.Dependencies, nil
131+
}
132+
103133
func (c *ChainConfigLoader) GetChain(chainID uint64) (*Chain, error) {
104134
chain, ok := c.Chains[chainID]
105135
if !ok {

superchain/chain_test.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package superchain
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestGetDepset(t *testing.T) {
10+
// Save BuiltInConfigs to restore later
11+
originalConfigs := BuiltInConfigs
12+
t.Cleanup(func() {
13+
BuiltInConfigs = originalConfigs
14+
})
15+
16+
t.Run("unknown chainID", func(t *testing.T) {
17+
BuiltInConfigs = &ChainConfigLoader{
18+
Chains: map[uint64]*Chain{},
19+
}
20+
21+
depset, err := GetDepset(999999)
22+
require.Nil(t, depset)
23+
require.Error(t, err)
24+
require.Contains(t, err.Error(), "unknown chain ID")
25+
})
26+
27+
t.Run("nil InteropTime", func(t *testing.T) {
28+
mockChain := &Chain{
29+
Name: "test",
30+
Network: "test",
31+
config: &ChainConfig{
32+
ChainID: 42,
33+
Hardforks: HardforkConfig{
34+
InteropTime: nil,
35+
},
36+
},
37+
}
38+
39+
// Set configOnce as already done
40+
mockChain.configOnce.Do(func() {})
41+
42+
// Replace chains map with our test chain
43+
BuiltInConfigs = &ChainConfigLoader{
44+
Chains: map[uint64]*Chain{42: mockChain},
45+
}
46+
47+
depset, err := GetDepset(42)
48+
require.Nil(t, depset)
49+
require.NoError(t, err)
50+
})
51+
52+
t.Run("nil Interop creates default depset", func(t *testing.T) {
53+
// Create mock chain with InteropTime but nil Interop
54+
activationTime := uint64(1234567890)
55+
mockChain := &Chain{
56+
Name: "test",
57+
Network: "test",
58+
config: &ChainConfig{
59+
ChainID: 42,
60+
Hardforks: HardforkConfig{
61+
InteropTime: &activationTime,
62+
},
63+
Interop: nil,
64+
},
65+
}
66+
67+
// Set configOnce as already done
68+
mockChain.configOnce.Do(func() {})
69+
70+
// Replace chains map with our test chain
71+
BuiltInConfigs = &ChainConfigLoader{
72+
Chains: map[uint64]*Chain{42: mockChain},
73+
}
74+
75+
depset, err := GetDepset(42)
76+
require.NoError(t, err)
77+
require.NotNil(t, depset)
78+
79+
// Verify the default dependency was created
80+
selfDep, exists := depset["42"]
81+
require.True(t, exists)
82+
require.Equal(t, uint32(1), selfDep.ChainIndex)
83+
require.Equal(t, activationTime, selfDep.ActivationTime)
84+
})
85+
86+
t.Run("existing Interop depset returned", func(t *testing.T) {
87+
// Create mock chain with existing Interop dependencies
88+
activationTime := uint64(1234567890)
89+
mockChain := &Chain{
90+
Name: "test",
91+
Network: "test",
92+
config: &ChainConfig{
93+
ChainID: 42,
94+
Hardforks: HardforkConfig{
95+
InteropTime: &activationTime,
96+
},
97+
Interop: &Interop{
98+
Dependencies: map[string]Dependency{
99+
"42": {ChainIndex: 1, ActivationTime: activationTime},
100+
"43": {ChainIndex: 2, ActivationTime: activationTime + 100},
101+
},
102+
},
103+
},
104+
}
105+
106+
// Set configOnce as already done
107+
mockChain.configOnce.Do(func() {})
108+
109+
// Replace chains map with our test chain
110+
BuiltInConfigs = &ChainConfigLoader{
111+
Chains: map[uint64]*Chain{42: mockChain},
112+
}
113+
114+
depset, err := GetDepset(42)
115+
require.NoError(t, err)
116+
require.NotNil(t, depset)
117+
require.Equal(t, 2, len(depset))
118+
119+
selfDep, exists := depset["42"]
120+
require.True(t, exists)
121+
require.Equal(t, uint32(1), selfDep.ChainIndex)
122+
123+
otherDep, exists := depset["43"]
124+
require.True(t, exists)
125+
require.Equal(t, uint32(2), otherDep.ChainIndex)
126+
require.Equal(t, activationTime+100, otherDep.ActivationTime)
127+
})
128+
}

superchain/types.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type ChainConfig struct {
2222
MaxSequencerDrift uint64 `toml:"max_sequencer_drift"`
2323
GasPayingToken *common.Address `toml:"gas_paying_token"`
2424
Hardforks HardforkConfig `toml:"hardforks"`
25+
Interop *Interop `toml:"interop,omitempty"`
2526
Optimism *OptimismConfig `toml:"optimism,omitempty"`
2627

2728
AltDA *AltDAConfig `toml:"alt_da,omitempty"`
@@ -33,6 +34,15 @@ type ChainConfig struct {
3334
Addresses AddressesConfig `toml:"addresses"`
3435
}
3536

37+
type Dependency struct {
38+
ChainIndex uint32 `json:"chainIndex" toml:"chain_index"`
39+
ActivationTime uint64 `json:"activationTime" toml:"activation_time"`
40+
}
41+
42+
type Interop struct {
43+
Dependencies map[string]Dependency `json:"dependencies" toml:"dependencies"`
44+
}
45+
3646
type HardforkConfig struct {
3747
CanyonTime *uint64 `toml:"canyon_time"`
3848
DeltaTime *uint64 `toml:"delta_time"`
@@ -42,7 +52,7 @@ type HardforkConfig struct {
4252
HoloceneTime *uint64 `toml:"holocene_time"`
4353
IsthmusTime *uint64 `toml:"isthmus_time"`
4454
JovianTime *uint64 `toml:"jovian_time"`
45-
55+
InteropTime *uint64 `toml:"interop_time"`
4656
// Optional Forks
4757
PectraBlobScheduleTime *uint64 `toml:"pectra_blob_schedule_time,omitempty"`
4858
}

0 commit comments

Comments
 (0)