Skip to content

Commit e7074ca

Browse files
authored
refactor(core)!: remove PrepareNamespace (#2683)
Remove `PrepareNamespace`. The caller must know what the namespace is: s it bytes? Or is it a string. If we are using `PrepareNamespace`, we are losing this information, so strings of 29 characters will trigger the wrong path and do not pass validation.
1 parent 8f996eb commit e7074ca

File tree

16 files changed

+174
-258
lines changed

16 files changed

+174
-258
lines changed

apps/evm/single/cmd/run.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"context"
5-
"encoding/hex"
65
"fmt"
76
"path/filepath"
87

@@ -30,27 +29,27 @@ var RunCmd = &cobra.Command{
3029
Aliases: []string{"node", "run"},
3130
Short: "Run the evolve node with EVM execution client",
3231
RunE: func(cmd *cobra.Command, args []string) error {
33-
executor, err := createExecutionClient(cmd)
34-
if err != nil {
35-
return err
36-
}
32+
executor, err := createExecutionClient(cmd)
33+
if err != nil {
34+
return err
35+
}
3736

38-
nodeConfig, err := rollcmd.ParseConfig(cmd)
39-
if err != nil {
40-
return err
41-
}
37+
nodeConfig, err := rollcmd.ParseConfig(cmd)
38+
if err != nil {
39+
return err
40+
}
4241

43-
logger := rollcmd.SetupLogger(nodeConfig.Log)
42+
logger := rollcmd.SetupLogger(nodeConfig.Log)
4443

45-
// Attach logger to the EVM engine client if available
46-
if ec, ok := executor.(*evm.EngineClient); ok {
47-
ec.SetLogger(logger.With().Str("module", "engine_client").Logger())
48-
}
44+
// Attach logger to the EVM engine client if available
45+
if ec, ok := executor.(*evm.EngineClient); ok {
46+
ec.SetLogger(logger.With().Str("module", "engine_client").Logger())
47+
}
4948

50-
headerNamespace := da.PrepareNamespace([]byte(nodeConfig.DA.GetNamespace()))
51-
dataNamespace := da.PrepareNamespace([]byte(nodeConfig.DA.GetDataNamespace()))
49+
headerNamespace := da.NamespaceFromString(nodeConfig.DA.GetNamespace())
50+
dataNamespace := da.NamespaceFromString(nodeConfig.DA.GetDataNamespace())
5251

53-
logger.Info().Str("headerNamespace", hex.EncodeToString(headerNamespace)).Str("dataNamespace", hex.EncodeToString(dataNamespace)).Msg("namespaces")
52+
logger.Info().Str("headerNamespace", headerNamespace.HexString()).Str("dataNamespace", dataNamespace.HexString()).Msg("namespaces")
5453

5554
daJrpc, err := jsonrpc.NewClient(context.Background(), logger, nodeConfig.DA.Address, nodeConfig.DA.AuthToken, nodeConfig.DA.GasPrice, nodeConfig.DA.GasMultiplier)
5655
if err != nil {

apps/grpc/single/cmd/run.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package cmd
22

33
import (
4-
"encoding/hex"
54
"fmt"
65
"path/filepath"
76

87
"github.com/spf13/cobra"
98

10-
coreda "github.com/evstack/ev-node/core/da"
9+
"github.com/evstack/ev-node/core/da"
1110
"github.com/evstack/ev-node/core/execution"
1211
"github.com/evstack/ev-node/da/jsonrpc"
1312
executiongrpc "github.com/evstack/ev-node/execution/grpc"
@@ -47,10 +46,10 @@ The execution client must implement the Evolve execution gRPC interface.`,
4746

4847
logger := rollcmd.SetupLogger(nodeConfig.Log)
4948

50-
headerNamespace := coreda.PrepareNamespace([]byte(nodeConfig.DA.GetNamespace()))
51-
dataNamespace := coreda.PrepareNamespace([]byte(nodeConfig.DA.GetDataNamespace()))
49+
headerNamespace := da.NamespaceFromString(nodeConfig.DA.GetNamespace())
50+
dataNamespace := da.NamespaceFromString(nodeConfig.DA.GetDataNamespace())
5251

53-
logger.Info().Str("headerNamespace", hex.EncodeToString(headerNamespace)).Str("dataNamespace", hex.EncodeToString(dataNamespace)).Msg("namespaces")
52+
logger.Info().Str("headerNamespace", headerNamespace.HexString()).Str("dataNamespace", dataNamespace.HexString()).Msg("namespaces")
5453

5554
// Create DA client
5655
daJrpc, err := jsonrpc.NewClient(cmd.Context(), logger, nodeConfig.DA.Address, nodeConfig.DA.AuthToken, nodeConfig.DA.GasPrice, nodeConfig.DA.GasMultiplier)

apps/testapp/cmd/run.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"context"
5-
"encoding/hex"
65
"fmt"
76
"path/filepath"
87

@@ -47,10 +46,10 @@ var RunCmd = &cobra.Command{
4746
ctx, cancel := context.WithCancel(context.Background())
4847
defer cancel()
4948

50-
headerNamespace := da.PrepareNamespace([]byte(nodeConfig.DA.GetNamespace()))
51-
dataNamespace := da.PrepareNamespace([]byte(nodeConfig.DA.GetDataNamespace()))
49+
headerNamespace := da.NamespaceFromString(nodeConfig.DA.GetNamespace())
50+
dataNamespace := da.NamespaceFromString(nodeConfig.DA.GetDataNamespace())
5251

53-
logger.Info().Str("headerNamespace", hex.EncodeToString(headerNamespace)).Str("dataNamespace", hex.EncodeToString(dataNamespace)).Msg("namespaces")
52+
logger.Info().Str("headerNamespace", headerNamespace.HexString()).Str("dataNamespace", dataNamespace.HexString()).Msg("namespaces")
5453

5554
daJrpc, err := jsonrpc.NewClient(ctx, logger, nodeConfig.DA.Address, nodeConfig.DA.AuthToken, nodeConfig.DA.GasPrice, nodeConfig.DA.GasMultiplier)
5655
if err != nil {

block/internal/submitting/da_submitter.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ type DASubmitter struct {
120120
genesis genesis.Genesis
121121
options common.BlockOptions
122122
logger zerolog.Logger
123+
124+
// calculate namespaces bytes once and reuse them
125+
namespaceBz []byte
126+
namespaceDataBz []byte
123127
}
124128

125129
// NewDASubmitter creates a new DA submitter
@@ -130,12 +134,15 @@ func NewDASubmitter(
130134
options common.BlockOptions,
131135
logger zerolog.Logger,
132136
) *DASubmitter {
137+
133138
return &DASubmitter{
134-
da: da,
135-
config: config,
136-
genesis: genesis,
137-
options: options,
138-
logger: logger.With().Str("component", "da_submitter").Logger(),
139+
da: da,
140+
config: config,
141+
genesis: genesis,
142+
options: options,
143+
logger: logger.With().Str("component", "da_submitter").Logger(),
144+
namespaceBz: coreda.NamespaceFromString(config.DA.GetNamespace()).Bytes(),
145+
namespaceDataBz: coreda.NamespaceFromString(config.DA.GetDataNamespace()).Bytes(),
139146
}
140147
}
141148

@@ -199,7 +206,7 @@ func (s *DASubmitter) SubmitHeaders(ctx context.Context, cache cache.Manager) er
199206
}
200207
},
201208
"header",
202-
[]byte(s.config.DA.GetNamespace()),
209+
s.namespaceBz,
203210
[]byte(s.config.DA.SubmitOptions),
204211
cache,
205212
)
@@ -242,7 +249,7 @@ func (s *DASubmitter) SubmitData(ctx context.Context, cache cache.Manager, signe
242249
}
243250
},
244251
"data",
245-
[]byte(s.config.DA.GetDataNamespace()),
252+
s.namespaceDataBz,
246253
[]byte(s.config.DA.SubmitOptions),
247254
cache,
248255
)

block/internal/submitting/da_submitter_mocks_test.go

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ func TestSubmitToDA_MempoolRetry_IncreasesGasAndSucceeds(t *testing.T) {
4545

4646
// First attempt returns a mempool-related error (mapped to StatusNotIncludedInBlock)
4747
// Expect gasPrice=1.0
48-
ns := []byte("ns")
48+
49+
nsBz := coreda.NamespaceFromString("ns").Bytes()
50+
4951
opts := []byte("opts")
5052
// capture gas prices used
5153
var usedGas []float64
5254
mockDA.
53-
On("SubmitWithOptions", mock.Anything, mock.Anything, mock.AnythingOfType("float64"), ns, opts).
55+
On("SubmitWithOptions", mock.Anything, mock.Anything, mock.AnythingOfType("float64"), nsBz, opts).
5456
Run(func(args mock.Arguments) {
5557
usedGas = append(usedGas, args.Get(2).(float64))
5658
}).
@@ -60,7 +62,7 @@ func TestSubmitToDA_MempoolRetry_IncreasesGasAndSucceeds(t *testing.T) {
6062
// Second attempt should use doubled gas price = 2.0 and succeed for all items
6163
ids := [][]byte{[]byte("id1"), []byte("id2"), []byte("id3")}
6264
mockDA.
63-
On("SubmitWithOptions", mock.Anything, mock.Anything, mock.AnythingOfType("float64"), ns, opts).
65+
On("SubmitWithOptions", mock.Anything, mock.Anything, mock.AnythingOfType("float64"), nsBz, opts).
6466
Run(func(args mock.Arguments) {
6567
usedGas = append(usedGas, args.Get(2).(float64))
6668
}).
@@ -81,7 +83,7 @@ func TestSubmitToDA_MempoolRetry_IncreasesGasAndSucceeds(t *testing.T) {
8183
marshalString,
8284
func(_ []string, _ *coreda.ResultSubmit, _ float64) {},
8385
"item",
84-
ns,
86+
nsBz,
8587
opts,
8688
nil,
8789
)
@@ -99,21 +101,22 @@ func TestSubmitToDA_UnknownError_RetriesSameGasThenSucceeds(t *testing.T) {
99101
// Initial gas price comes from config (set below), so DA.GasPrice is not called
100102
mockDA.On("GasMultiplier", mock.Anything).Return(3.0, nil).Once()
101103

102-
ns := []byte("ns")
104+
nsBz := coreda.NamespaceFromString("ns").Bytes()
105+
103106
opts := []byte("opts")
104107
var usedGas []float64
105108

106109
// First attempt: unknown failure -> reasonFailure, gas unchanged for next attempt
107110
mockDA.
108-
On("SubmitWithOptions", mock.Anything, mock.Anything, mock.AnythingOfType("float64"), ns, opts).
111+
On("SubmitWithOptions", mock.Anything, mock.Anything, mock.AnythingOfType("float64"), nsBz, opts).
109112
Run(func(args mock.Arguments) { usedGas = append(usedGas, args.Get(2).(float64)) }).
110113
Return(nil, errors.New("boom")).
111114
Once()
112115

113116
// Second attempt: same gas, success
114117
ids := [][]byte{[]byte("id1")}
115118
mockDA.
116-
On("SubmitWithOptions", mock.Anything, mock.Anything, mock.AnythingOfType("float64"), ns, opts).
119+
On("SubmitWithOptions", mock.Anything, mock.Anything, mock.AnythingOfType("float64"), nsBz, opts).
117120
Run(func(args mock.Arguments) { usedGas = append(usedGas, args.Get(2).(float64)) }).
118121
Return(ids, nil).
119122
Once()
@@ -131,7 +134,7 @@ func TestSubmitToDA_UnknownError_RetriesSameGasThenSucceeds(t *testing.T) {
131134
marshalString,
132135
func(_ []string, _ *coreda.ResultSubmit, _ float64) {},
133136
"item",
134-
ns,
137+
nsBz,
135138
opts,
136139
nil,
137140
)
@@ -147,14 +150,15 @@ func TestSubmitToDA_TooBig_HalvesBatch(t *testing.T) {
147150
// Use fixed gas from config to simplify
148151
mockDA.On("GasMultiplier", mock.Anything).Return(2.0, nil).Once()
149152

150-
ns := []byte("ns")
153+
nsBz := coreda.NamespaceFromString("ns").Bytes()
154+
151155
opts := []byte("opts")
152156
// record sizes of batches sent to DA
153157
var batchSizes []int
154158

155159
// First attempt: too big -> should halve and retry
156160
mockDA.
157-
On("SubmitWithOptions", mock.Anything, mock.Anything, mock.Anything, ns, opts).
161+
On("SubmitWithOptions", mock.Anything, mock.Anything, mock.Anything, nsBz, opts).
158162
Run(func(args mock.Arguments) {
159163
blobs := args.Get(1).([][]byte)
160164
batchSizes = append(batchSizes, len(blobs))
@@ -165,7 +169,7 @@ func TestSubmitToDA_TooBig_HalvesBatch(t *testing.T) {
165169
// Second attempt: expect half the size, succeed
166170
ids := [][]byte{[]byte("id1"), []byte("id2")}
167171
mockDA.
168-
On("SubmitWithOptions", mock.Anything, mock.Anything, mock.Anything, ns, opts).
172+
On("SubmitWithOptions", mock.Anything, mock.Anything, mock.Anything, nsBz, opts).
169173
Run(func(args mock.Arguments) {
170174
blobs := args.Get(1).([][]byte)
171175
batchSizes = append(batchSizes, len(blobs))
@@ -186,7 +190,7 @@ func TestSubmitToDA_TooBig_HalvesBatch(t *testing.T) {
186190
marshalString,
187191
func(_ []string, _ *coreda.ResultSubmit, _ float64) {},
188192
"item",
189-
ns,
193+
nsBz,
190194
opts,
191195
nil,
192196
)
@@ -202,21 +206,22 @@ func TestSubmitToDA_SentinelNoGas_PreservesGasAcrossRetries(t *testing.T) {
202206
// GasMultiplier is still called once, but should not affect gas when sentinel is used
203207
mockDA.On("GasMultiplier", mock.Anything).Return(10.0, nil).Once()
204208

205-
ns := []byte("ns")
209+
nsBz := coreda.NamespaceFromString("ns").Bytes()
210+
206211
opts := []byte("opts")
207212
var usedGas []float64
208213

209214
// First attempt: mempool-ish error
210215
mockDA.
211-
On("SubmitWithOptions", mock.Anything, mock.Anything, mock.AnythingOfType("float64"), ns, opts).
216+
On("SubmitWithOptions", mock.Anything, mock.Anything, mock.AnythingOfType("float64"), nsBz, opts).
212217
Run(func(args mock.Arguments) { usedGas = append(usedGas, args.Get(2).(float64)) }).
213218
Return(nil, coreda.ErrTxAlreadyInMempool).
214219
Once()
215220

216221
// Second attempt: should use same sentinel gas (-1), succeed
217222
ids := [][]byte{[]byte("id1")}
218223
mockDA.
219-
On("SubmitWithOptions", mock.Anything, mock.Anything, mock.AnythingOfType("float64"), ns, opts).
224+
On("SubmitWithOptions", mock.Anything, mock.Anything, mock.AnythingOfType("float64"), nsBz, opts).
220225
Run(func(args mock.Arguments) { usedGas = append(usedGas, args.Get(2).(float64)) }).
221226
Return(ids, nil).
222227
Once()
@@ -234,7 +239,7 @@ func TestSubmitToDA_SentinelNoGas_PreservesGasAcrossRetries(t *testing.T) {
234239
marshalString,
235240
func(_ []string, _ *coreda.ResultSubmit, _ float64) {},
236241
"item",
237-
ns,
242+
nsBz,
238243
opts,
239244
nil,
240245
)
@@ -249,18 +254,19 @@ func TestSubmitToDA_PartialSuccess_AdvancesWindow(t *testing.T) {
249254
mockDA := mocks.NewMockDA(t)
250255
mockDA.On("GasMultiplier", mock.Anything).Return(2.0, nil).Once()
251256

252-
ns := []byte("ns")
257+
nsBz := coreda.NamespaceFromString("ns").Bytes()
258+
253259
opts := []byte("opts")
254260
// track how many items postSubmit sees across attempts
255261
var totalSubmitted int
256262

257263
// First attempt: success for first 2 of 3
258264
firstIDs := [][]byte{[]byte("id1"), []byte("id2")}
259-
mockDA.On("SubmitWithOptions", mock.Anything, mock.Anything, mock.Anything, ns, opts).Return(firstIDs, nil).Once()
265+
mockDA.On("SubmitWithOptions", mock.Anything, mock.Anything, mock.Anything, nsBz, opts).Return(firstIDs, nil).Once()
260266

261267
// Second attempt: success for remaining 1
262268
secondIDs := [][]byte{[]byte("id3")}
263-
mockDA.On("SubmitWithOptions", mock.Anything, mock.Anything, mock.Anything, ns, opts).Return(secondIDs, nil).Once()
269+
mockDA.On("SubmitWithOptions", mock.Anything, mock.Anything, mock.Anything, nsBz, opts).Return(secondIDs, nil).Once()
264270

265271
s := newTestSubmitter(mockDA, func(c *config.Config) { c.DA.GasPrice = 1.0 })
266272

@@ -273,7 +279,7 @@ func TestSubmitToDA_PartialSuccess_AdvancesWindow(t *testing.T) {
273279
marshalString,
274280
func(submitted []string, _ *coreda.ResultSubmit, _ float64) { totalSubmitted += len(submitted) },
275281
"item",
276-
ns,
282+
nsBz,
277283
opts,
278284
nil,
279285
)

block/internal/syncing/da_retriever.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ const (
2929
type DARetriever struct {
3030
da coreda.DA
3131
cache cache.Manager
32-
config config.Config
3332
genesis genesis.Genesis
3433
options common.BlockOptions
3534
logger zerolog.Logger
35+
36+
// calculate namespaces bytes once and reuse them
37+
namespaceBz []byte
38+
namespaceDataBz []byte
3639
}
3740

3841
// NewDARetriever creates a new DA retriever
@@ -45,12 +48,13 @@ func NewDARetriever(
4548
logger zerolog.Logger,
4649
) *DARetriever {
4750
return &DARetriever{
48-
da: da,
49-
cache: cache,
50-
config: config,
51-
genesis: genesis,
52-
options: options,
53-
logger: logger.With().Str("component", "da_retriever").Logger(),
51+
da: da,
52+
cache: cache,
53+
genesis: genesis,
54+
options: options,
55+
logger: logger.With().Str("component", "da_retriever").Logger(),
56+
namespaceBz: coreda.NamespaceFromString(config.DA.GetNamespace()).Bytes(),
57+
namespaceDataBz: coreda.NamespaceFromString(config.DA.GetDataNamespace()).Bytes(),
5458
}
5559
}
5660

@@ -100,19 +104,15 @@ func (r *DARetriever) fetchBlobs(ctx context.Context, daHeight uint64) (coreda.R
100104
ctx, cancel := context.WithTimeout(ctx, dAFetcherTimeout)
101105
defer cancel()
102106

103-
// Get namespaces
104-
headerNamespace := []byte(r.config.DA.GetNamespace())
105-
dataNamespace := []byte(r.config.DA.GetDataNamespace())
106-
107107
// Retrieve from both namespaces
108-
headerRes := types.RetrieveWithHelpers(ctx, r.da, r.logger, daHeight, headerNamespace)
108+
headerRes := types.RetrieveWithHelpers(ctx, r.da, r.logger, daHeight, r.namespaceBz)
109109

110110
// If namespaces are the same, return header result
111-
if string(headerNamespace) == string(dataNamespace) {
111+
if bytes.Equal(r.namespaceBz, r.namespaceDataBz) {
112112
return headerRes, r.validateBlobResponse(headerRes, daHeight)
113113
}
114114

115-
dataRes := types.RetrieveWithHelpers(ctx, r.da, r.logger, daHeight, dataNamespace)
115+
dataRes := types.RetrieveWithHelpers(ctx, r.da, r.logger, daHeight, r.namespaceDataBz)
116116

117117
// Validate responses
118118
headerErr := r.validateBlobResponse(headerRes, daHeight)

0 commit comments

Comments
 (0)