Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(settlement): use block descriptors as source of truth for blocks to apply #1172

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions block/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/dymensionxyz/dymint/settlement"
"github.com/dymensionxyz/dymint/testutil"
"github.com/dymensionxyz/dymint/types"
"github.com/dymensionxyz/dymint/types/pb/dymensionxyz/dymension/rollapp"
"github.com/dymensionxyz/dymint/version"

abci "github.com/tendermint/tendermint/abci/types"
Expand Down Expand Up @@ -394,12 +395,16 @@ func TestRetrieveDaBatchesFailed(t *testing.T) {
manager.DAClient = testutil.GetMockDALC(log.TestingLogger())
manager.Retriever = manager.DAClient.(da.BatchRetriever)

daMetaData := &da.DASubmitMetaData{
Client: da.Mock,
Height: 1,
batch := &settlement.Batch{
MetaData: &settlement.BatchMetaData{
DA: &da.DASubmitMetaData{
Client: da.Mock,
Height: 1,
},
},
}

err = manager.ApplyBatchFromSL(daMetaData)
err = manager.ApplyBatchFromSL(batch)
t.Log(err)
assert.ErrorIs(t, err, da.ErrBlobNotFound)
}
Expand Down Expand Up @@ -742,7 +747,19 @@ func TestDAFetch(t *testing.T) {
LastBlockAppHash: commitHash[:],
})

err := manager.ApplyBatchFromSL(c.daMetaData)
var bds []rollapp.BlockDescriptor
for _, block := range batch.Blocks {
bds = append(bds, rollapp.BlockDescriptor{
Height: block.Header.Height,
})
}
slBatch := &settlement.Batch{
MetaData: &settlement.BatchMetaData{
DA: c.daMetaData,
},
BlockDescriptors: bds,
}
err := manager.ApplyBatchFromSL(slBatch)
require.Equal(c.err, err)
})
}
Expand Down Expand Up @@ -814,8 +831,21 @@ func TestManager_ApplyBatchFromSL_FraudHandling(t *testing.T) {
LastBlockAppHash: commitHash[:],
})

var bds []rollapp.BlockDescriptor
for _, block := range batch.Blocks {
bds = append(bds, rollapp.BlockDescriptor{
Height: block.Header.Height,
})
}
slBatch := &settlement.Batch{
MetaData: &settlement.BatchMetaData{
DA: daResultSubmitBatch.SubmitMetaData,
},
BlockDescriptors: bds,
}

// Call ApplyBatchFromSL
err = manager.ApplyBatchFromSL(daResultSubmitBatch.SubmitMetaData)
err = manager.ApplyBatchFromSL(slBatch)

// Verify
require.True(errors.Is(err, gerrc.ErrFault))
Expand Down
19 changes: 13 additions & 6 deletions block/retriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,45 @@ import (
"github.com/dymensionxyz/gerr-cosmos/gerrc"

"github.com/dymensionxyz/dymint/da"
"github.com/dymensionxyz/dymint/settlement"
"github.com/dymensionxyz/dymint/types"
)

func (m *Manager) ApplyBatchFromSL(daMetaData *da.DASubmitMetaData) error {
m.logger.Debug("trying to retrieve batch from DA", "daHeight", daMetaData.Height)
batchResp := m.fetchBatch(daMetaData)
func (m *Manager) ApplyBatchFromSL(slBatch *settlement.Batch) error {
m.logger.Debug("trying to retrieve batch from DA", "daHeight", slBatch.MetaData.DA.Height)
batchResp := m.fetchBatch(slBatch.MetaData.DA)
if batchResp.Code != da.StatusSuccess {
return batchResp.Error
}

m.logger.Debug("retrieved batches", "n", len(batchResp.Batches), "daHeight", daMetaData.Height)
m.logger.Debug("retrieved batches", "n", len(batchResp.Batches), "daHeight", slBatch.MetaData.DA.Height)

m.retrieverMu.Lock()
defer m.retrieverMu.Unlock()

var lastAppliedHeight float64
blockIndex := 0
for _, batch := range batchResp.Batches {
for i, block := range batch.Blocks {
// We dont apply a block if not included in the block descriptor (adds support for rollback)
if blockIndex >= len(slBatch.BlockDescriptors) {
break
}
blockIndex++

if block.Header.Height != m.State.NextHeight() {
continue
}

// We dont validate because validateBlockBeforeApply already checks if the block is already applied, and we don't need to fail there.
err := m.applyBlockWithFraudHandling(block, batch.Commits[i], types.BlockMetaData{Source: types.DA, DAHeight: daMetaData.Height})
err := m.applyBlockWithFraudHandling(block, batch.Commits[i], types.BlockMetaData{Source: types.DA, DAHeight: slBatch.MetaData.DA.Height})
if err != nil {
return fmt.Errorf("apply block: height: %d: %w", block.Header.Height, err)
}

lastAppliedHeight = float64(block.Header.Height)

m.blockCache.Delete(block.Header.Height)

}
}
types.LastReceivedDAHeightGauge.Set(lastAppliedHeight)
Expand Down
2 changes: 1 addition & 1 deletion block/slvalidator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func TestStateUpdateValidator_ValidateStateUpdate(t *testing.T) {
}
// otherwise load them from DA
} else {
manager.ApplyBatchFromSL(slBatch.MetaData.DA)
manager.ApplyBatchFromSL(slBatch.Batch)
}

for _, bd := range bds {
Expand Down
2 changes: 1 addition & 1 deletion block/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (m *Manager) SettlementSyncLoop(ctx context.Context) error {
}
m.logger.Info("Retrieved state update from SL.", "state_index", settlementBatch.StateIndex)

err = m.ApplyBatchFromSL(settlementBatch.MetaData.DA)
err = m.ApplyBatchFromSL(settlementBatch.Batch)
if err != nil {
m.logger.Error("process next DA batch", "err", err)
}
Expand Down
Loading