Commit c2260ca0 authored by pcw109550's avatar pcw109550

op-node: Refactor to expose span batch derivation

parent 918459c4
...@@ -172,9 +172,7 @@ func TestBatchRoundTrip(t *testing.T) { ...@@ -172,9 +172,7 @@ func TestBatchRoundTrip(t *testing.T) {
err = dec.UnmarshalBinary(enc) err = dec.UnmarshalBinary(enc)
require.NoError(t, err) require.NoError(t, err)
if dec.GetBatchType() == SpanBatchType { if dec.GetBatchType() == SpanBatchType {
rawSpanBatch, ok := dec.inner.(*RawSpanBatch) _, err := DeriveSpanBatch(&dec, blockTime, genesisTimestamp, chainID)
require.True(t, ok)
_, err := rawSpanBatch.derive(blockTime, genesisTimestamp, chainID)
require.NoError(t, err) require.NoError(t, err)
} }
require.Equal(t, batch, &dec, "Batch not equal test case %v", i) require.Equal(t, batch, &dec, "Batch not equal test case %v", i)
...@@ -222,9 +220,7 @@ func TestBatchRoundTripRLP(t *testing.T) { ...@@ -222,9 +220,7 @@ func TestBatchRoundTripRLP(t *testing.T) {
err = dec.DecodeRLP(s) err = dec.DecodeRLP(s)
require.NoError(t, err) require.NoError(t, err)
if dec.GetBatchType() == SpanBatchType { if dec.GetBatchType() == SpanBatchType {
rawSpanBatch, ok := dec.inner.(*RawSpanBatch) _, err = DeriveSpanBatch(&dec, blockTime, genesisTimestamp, chainID)
require.True(t, ok)
_, err := rawSpanBatch.derive(blockTime, genesisTimestamp, chainID)
require.NoError(t, err) require.NoError(t, err)
} }
require.Equal(t, batch, &dec, "Batch not equal test case %v", i) require.Equal(t, batch, &dec, "Batch not equal test case %v", i)
......
...@@ -104,16 +104,7 @@ func (cr *ChannelInReader) NextBatch(ctx context.Context) (Batch, error) { ...@@ -104,16 +104,7 @@ func (cr *ChannelInReader) NextBatch(ctx context.Context) (Batch, error) {
// This is just for early dropping invalid batches as soon as possible. // This is just for early dropping invalid batches as soon as possible.
return nil, NewTemporaryError(fmt.Errorf("cannot accept span batch in L1 block %s at time %d", origin, origin.Time)) return nil, NewTemporaryError(fmt.Errorf("cannot accept span batch in L1 block %s at time %d", origin, origin.Time))
} }
rawSpanBatch, ok := batchData.inner.(*RawSpanBatch) return DeriveSpanBatch(batchData, cr.cfg.BlockTime, cr.cfg.Genesis.L2Time, cr.cfg.L2ChainID)
if !ok {
return nil, NewCriticalError(errors.New("failed type assertion to SpanBatch"))
}
// If the batch type is Span batch, derive block inputs from RawSpanBatch.
spanBatch, err := rawSpanBatch.derive(cr.cfg.BlockTime, cr.cfg.Genesis.L2Time, cr.cfg.L2ChainID)
if err != nil {
return nil, err
}
return spanBatch, nil
default: default:
// error is bubbled up to user, but pipeline can skip the batch and continue after. // error is bubbled up to user, but pipeline can skip the batch and continue after.
return nil, NewTemporaryError(fmt.Errorf("unrecognized batch type: %d", batchData.GetBatchType())) return nil, NewTemporaryError(fmt.Errorf("unrecognized batch type: %d", batchData.GetBatchType()))
......
...@@ -416,6 +416,16 @@ func (b *RawSpanBatch) derive(blockTime, genesisTimestamp uint64, chainID *big.I ...@@ -416,6 +416,16 @@ func (b *RawSpanBatch) derive(blockTime, genesisTimestamp uint64, chainID *big.I
return &spanBatch, nil return &spanBatch, nil
} }
// ToSpanBatch converts RawSpanBatch to SpanBatch,
// which implements a wrapper of derive method of RawSpanBatch
func (b *RawSpanBatch) ToSpanBatch(blockTime, genesisTimestamp uint64, chainID *big.Int) (*SpanBatch, error) {
spanBatch, err := b.derive(blockTime, genesisTimestamp, chainID)
if err != nil {
return nil, err
}
return spanBatch, nil
}
// spanBatchElement is a derived form of input to build a L2 block. // spanBatchElement is a derived form of input to build a L2 block.
// similar to SingularBatch, but does not have ParentHash and EpochHash // similar to SingularBatch, but does not have ParentHash and EpochHash
// because Span batch spec does not contain parent hash and epoch hash of every block in the span. // because Span batch spec does not contain parent hash and epoch hash of every block in the span.
...@@ -604,6 +614,16 @@ func NewSpanBatch(singularBatches []*SingularBatch) *SpanBatch { ...@@ -604,6 +614,16 @@ func NewSpanBatch(singularBatches []*SingularBatch) *SpanBatch {
return spanBatch return spanBatch
} }
// DeriveSpanBatch derives SpanBatch from BatchData.
func DeriveSpanBatch(batchData *BatchData, blockTime, genesisTimestamp uint64, chainID *big.Int) (*SpanBatch, error) {
rawSpanBatch, ok := batchData.inner.(*RawSpanBatch)
if !ok {
return nil, NewCriticalError(errors.New("failed type assertion to SpanBatch"))
}
// If the batch type is Span batch, derive block inputs from RawSpanBatch.
return rawSpanBatch.ToSpanBatch(blockTime, genesisTimestamp, chainID)
}
// SpanBatchBuilder is a utility type to build a SpanBatch by adding a SingularBatch one by one. // SpanBatchBuilder is a utility type to build a SpanBatch by adding a SingularBatch one by one.
// makes easier to stack SingularBatches and convert to RawSpanBatch for encoding. // makes easier to stack SingularBatches and convert to RawSpanBatch for encoding.
type SpanBatchBuilder struct { type SpanBatchBuilder struct {
......
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