Skip to content

Commit 5649ded

Browse files
Ganesha UpadhyayaGanesha Upadhyaya
authored andcommitted
remove hash dependency, separate validate & apply, populate ISRs before signing
1 parent 61f1a9e commit 5649ded

5 files changed

Lines changed: 77 additions & 92 deletions

File tree

block/manager.go

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"sync/atomic"
1010
"time"
1111

12-
"github.com/celestiaorg/go-header"
1312
"github.com/libp2p/go-libp2p/core/crypto"
1413
abci "github.com/tendermint/tendermint/abci/types"
1514
tmcrypto "github.com/tendermint/tendermint/crypto"
@@ -327,6 +326,10 @@ func (m *Manager) trySyncNextBlock(ctx context.Context, daHeight uint64) error {
327326

328327
if b != nil && commit != nil {
329328
m.logger.Info("Syncing block", "height", b.SignedHeader.Header.Height())
329+
// Validate the received block before applying
330+
if err := m.executor.Validate(m.lastState, b); err != nil {
331+
return fmt.Errorf("failed to validate block: %w", err)
332+
}
330333
newState, responses, err := m.executor.ApplyBlock(ctx, m.lastState, b)
331334
if err != nil {
332335
return fmt.Errorf("failed to ApplyBlock: %w", err)
@@ -530,29 +533,6 @@ func (m *Manager) publishBlock(ctx context.Context) error {
530533
m.logger.Info("Creating and publishing block", "height", newHeight)
531534
block = m.executor.CreateBlock(newHeight, lastCommit, lastHeaderHash, m.lastState)
532535
m.logger.Debug("block info", "num_tx", len(block.Data.Txs))
533-
534-
dataHash, err := m.submitBlockToDA(ctx, block, false)
535-
if err != nil {
536-
m.logger.Error("Failed to submit block to DA Layer")
537-
return err
538-
}
539-
block.SignedHeader.Header.DataHash = dataHash
540-
541-
commit, err = m.getCommit(block.SignedHeader.Header)
542-
if err != nil {
543-
return err
544-
}
545-
546-
// set the commit to current block's signed header
547-
block.SignedHeader.Commit = *commit
548-
549-
block.SignedHeader.Validators = m.lastState.Validators
550-
551-
// SaveBlock commits the DB tx
552-
err = m.store.SaveBlock(block, commit)
553-
if err != nil {
554-
return err
555-
}
556536
}
557537

558538
// Apply the block but DONT commit
@@ -561,11 +541,24 @@ func (m *Manager) publishBlock(ctx context.Context) error {
561541
return err
562542
}
563543

564-
if commit == nil {
565-
commit, err = m.getCommit(block.SignedHeader.Header)
566-
if err != nil {
567-
return err
568-
}
544+
// Before taking the hash, we need updated ISRs, hence after ApplyBlock
545+
block.SignedHeader.Header.DataHash, err = block.Data.Hash()
546+
if err != nil {
547+
return err
548+
}
549+
550+
// Sign the block and set the commit to current block's signed header along with signers
551+
commit, err = m.getCommit(block.SignedHeader.Header)
552+
if err != nil {
553+
return err
554+
}
555+
556+
block.SignedHeader.Commit = *commit
557+
block.SignedHeader.Validators = m.lastState.Validators
558+
559+
// Validate the created block before storing
560+
if err := m.executor.Validate(m.lastState, block); err != nil {
561+
return fmt.Errorf("failed to validate block: %w", err)
569562
}
570563

571564
// SaveBlock commits the DB tx
@@ -574,8 +567,7 @@ func (m *Manager) publishBlock(ctx context.Context) error {
574567
return err
575568
}
576569

577-
_, err = m.submitBlockToDA(ctx, block, true)
578-
if err != nil {
570+
if err := m.submitBlockToDA(ctx, block); err != nil {
579571
m.logger.Error("Failed to submit block to DA Layer")
580572
return err
581573
}
@@ -621,33 +613,44 @@ func (m *Manager) publishBlock(ctx context.Context) error {
621613
return nil
622614
}
623615

624-
func (m *Manager) submitBlockToDA(ctx context.Context, block *types.Block, onlyHeader bool) (header.Hash, error) {
616+
func (m *Manager) submitBlockToDA(ctx context.Context, block *types.Block) error {
625617
m.logger.Info("submitting block to DA layer", "height", block.SignedHeader.Header.Height())
626618

627619
submitted := false
628620
backoff := initialBackoff
629-
var res da.ResultSubmitBlock
630621
for attempt := 1; ctx.Err() == nil && !submitted && attempt <= maxSubmitAttempts; attempt++ {
631-
if onlyHeader {
632-
res = m.dalc.SubmitBlockHeader(ctx, &block.SignedHeader)
633-
} else {
634-
res = m.dalc.SubmitBlockData(ctx, &block.Data)
635-
}
636-
if res.Code == da.StatusSuccess {
637-
m.logger.Info("successfully submitted Rollkit block to DA layer", "rollkitHeight", block.SignedHeader.Header.Height(), "daHeight", res.DAHeight)
622+
headerRes := m.dalc.SubmitBlockHeader(ctx, &block.SignedHeader)
623+
dataRes := m.dalc.SubmitBlockData(ctx, &block.Data)
624+
if headerRes.Code == da.StatusSuccess && dataRes.Code == da.StatusSuccess {
625+
m.logger.Info(
626+
"successfully submitted Rollkit block to DA layer",
627+
"rollkitHeight",
628+
block.SignedHeader.Header.Height(),
629+
"daHeight of the block header",
630+
headerRes.DAHeight,
631+
"daHeight of the block data",
632+
dataRes.DAHeight,
633+
)
638634
submitted = true
639635
} else {
640-
m.logger.Error("DA layer submission failed", "error", res.Message, "attempt", attempt)
636+
var errMsg string
637+
if headerRes.Code == da.StatusError {
638+
errMsg = headerRes.Message
639+
}
640+
if dataRes.Code == da.StatusError {
641+
errMsg += "," + dataRes.Message
642+
}
643+
m.logger.Error("DA layer submission failed", "error", errMsg, "attempt", attempt)
641644
time.Sleep(backoff)
642645
backoff = m.exponentialBackoff(backoff)
643646
}
644647
}
645648

646649
if !submitted {
647-
return nil, fmt.Errorf("failed to submit block to DA layer after %d attempts", maxSubmitAttempts)
650+
return fmt.Errorf("failed to submit block to DA layer after %d attempts", maxSubmitAttempts)
648651
}
649652

650-
return res.Hash, nil
653+
return nil
651654
}
652655

653656
func (m *Manager) exponentialBackoff(backoff time.Duration) time.Duration {

da/celestia/celestia.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,23 +134,12 @@ func (c *DataAvailabilityLayerClient) SubmitBlockData(ctx context.Context, data
134134
}
135135
}
136136

137-
dataHash, err := data.Hash()
138-
if err != nil {
139-
return da.ResultSubmitBlock{
140-
BaseResult: da.BaseResult{
141-
Code: da.StatusError,
142-
Message: err.Error(),
143-
},
144-
}
145-
}
146-
147137
return da.ResultSubmitBlock{
148138
BaseResult: da.BaseResult{
149139
Code: da.StatusSuccess,
150140
Message: "tx hash: " + txResponse.TxHash,
151141
DAHeight: uint64(txResponse.Height),
152142
},
153-
Hash: dataHash,
154143
}
155144
}
156145

da/da.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package da
33
import (
44
"context"
55

6-
"github.com/celestiaorg/go-header"
76
ds "github.com/ipfs/go-datastore"
87

98
"github.com/rollkit/rollkit/log"
@@ -39,7 +38,7 @@ type ResultSubmitBlock struct {
3938
BaseResult
4039
// Not sure if this needs to be bubbled up to other
4140
// parts of Rollkit.
42-
Hash header.Hash
41+
// Hash header.Hash
4342
}
4443

4544
// ResultCheckBlock contains information about block availability, returned from DA layer client.

da/mock/mock.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ func (m *DataAvailabilityLayerClient) SubmitBlockData(ctx context.Context, data
142142
Message: "OK",
143143
DAHeight: daHeight,
144144
},
145-
Hash: hash,
146145
}
147146
}
148147

state/executor.go

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,6 @@ func (e *BlockExecutor) CreateBlock(height uint64, lastCommit *types.Commit, las
135135

136136
// ApplyBlock validates and executes the block.
137137
func (e *BlockExecutor) ApplyBlock(ctx context.Context, state types.State, block *types.Block) (types.State, *tmstate.ABCIResponses, error) {
138-
err := e.validate(state, block)
139-
if err != nil {
140-
return types.State{}, nil, err
141-
}
142-
143138
// This makes calls to the AppClient
144139
resp, err := e.execute(ctx, state, block)
145140
if err != nil {
@@ -202,6 +197,36 @@ func (e *BlockExecutor) VerifyFraudProof(fraudProof *abci.FraudProof, expectedVa
202197

203198
}
204199

200+
func (e *BlockExecutor) Validate(state types.State, block *types.Block) error {
201+
err := block.ValidateBasic()
202+
if err != nil {
203+
return err
204+
}
205+
if block.SignedHeader.Header.Version.App != state.Version.Consensus.App ||
206+
block.SignedHeader.Header.Version.Block != state.Version.Consensus.Block {
207+
return errors.New("block version mismatch")
208+
}
209+
if state.LastBlockHeight <= 0 && block.SignedHeader.Header.Height() != state.InitialHeight {
210+
return errors.New("initial block height mismatch")
211+
}
212+
if state.LastBlockHeight > 0 && block.SignedHeader.Header.Height() != state.LastBlockHeight+1 {
213+
return errors.New("block height mismatch")
214+
}
215+
if !bytes.Equal(block.SignedHeader.Header.AppHash[:], state.AppHash[:]) {
216+
return errors.New("AppHash mismatch")
217+
}
218+
219+
if !bytes.Equal(block.SignedHeader.Header.LastResultsHash[:], state.LastResultsHash[:]) {
220+
return errors.New("LastResultsHash mismatch")
221+
}
222+
223+
if !bytes.Equal(block.SignedHeader.Header.AggregatorsHash[:], state.Validators.Hash()) {
224+
return errors.New("AggregatorsHash mismatch")
225+
}
226+
227+
return nil
228+
}
229+
205230
func (e *BlockExecutor) updateState(state types.State, block *types.Block, abciResponses *tmstate.ABCIResponses, validatorUpdates []*tmtypes.Validator) (types.State, error) {
206231
nValSet := state.NextValidators.Copy()
207232
lastHeightValSetChanged := state.LastHeightValidatorsChanged
@@ -277,36 +302,6 @@ func (e *BlockExecutor) commit(ctx context.Context, state types.State, block *ty
277302
return resp.Data, uint64(resp.RetainHeight), err
278303
}
279304

280-
func (e *BlockExecutor) validate(state types.State, block *types.Block) error {
281-
err := block.ValidateBasic()
282-
if err != nil {
283-
return err
284-
}
285-
if block.SignedHeader.Header.Version.App != state.Version.Consensus.App ||
286-
block.SignedHeader.Header.Version.Block != state.Version.Consensus.Block {
287-
return errors.New("block version mismatch")
288-
}
289-
if state.LastBlockHeight <= 0 && block.SignedHeader.Header.Height() != state.InitialHeight {
290-
return errors.New("initial block height mismatch")
291-
}
292-
if state.LastBlockHeight > 0 && block.SignedHeader.Header.Height() != state.LastBlockHeight+1 {
293-
return errors.New("block height mismatch")
294-
}
295-
if !bytes.Equal(block.SignedHeader.Header.AppHash[:], state.AppHash[:]) {
296-
return errors.New("AppHash mismatch")
297-
}
298-
299-
if !bytes.Equal(block.SignedHeader.Header.LastResultsHash[:], state.LastResultsHash[:]) {
300-
return errors.New("LastResultsHash mismatch")
301-
}
302-
303-
if !bytes.Equal(block.SignedHeader.Header.AggregatorsHash[:], state.Validators.Hash()) {
304-
return errors.New("AggregatorsHash mismatch")
305-
}
306-
307-
return nil
308-
}
309-
310305
func (e *BlockExecutor) execute(ctx context.Context, state types.State, block *types.Block) (*tmstate.ABCIResponses, error) {
311306
abciResponses := new(tmstate.ABCIResponses)
312307
abciResponses.DeliverTxs = make([]*abci.ResponseDeliverTx, len(block.Data.Txs))

0 commit comments

Comments
 (0)