Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/adr-fraud-handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Faulty Tolly committed Sep 19, 2024
2 parents 4963924 + 66f9b35 commit 94923c0
Show file tree
Hide file tree
Showing 71 changed files with 6,949 additions and 2,817 deletions.
2 changes: 1 addition & 1 deletion .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ packages:
github.com/dymensionxyz/dymint/third_party/dymension/sequencer/types:
interfaces:
QueryClient:
github.com/dymensionxyz/dymint/third_party/dymension/rollapp/types:
github.com/dymensionxyz/dymint/types/pb/dymensionxyz/dymension/rollapp:
interfaces:
QueryClient:
github.com/tendermint/tendermint/abci/types:
Expand Down
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,16 @@ all: check build test install
include tests.mk

###############################################################################
### Build Dymint ###
### Mocks ###
###############################################################################

mock-gen:
go install github.com/vektra/mockery/[email protected]
mockery
.PHONY: mock-gen

###############################################################################
### Build Dymint ###
###############################################################################

build:
Expand Down
2 changes: 1 addition & 1 deletion block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (m *Manager) applyBlock(block *types.Block, commit *types.Commit, blockMeta

// Prune old heights, if requested by ABCI app.
if 0 < retainHeight {
err = m.PruneBlocks(uint64(retainHeight))
_, err := m.PruneBlocks(uint64(retainHeight))
if err != nil {
m.logger.Error("prune blocks", "retain_height", retainHeight, "err", err)
}
Expand Down
3 changes: 3 additions & 0 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ func (m *Manager) Start(ctx context.Context) error {
}

/* ----------------------------- sequencer mode ----------------------------- */
// Subscribe to batch events, to update last submitted height in case batch confirmation was lost. This could happen if the sequencer crash/restarted just after submitting a batch to the settlement and by the time we query the last batch, this batch wasn't accepted yet.
go uevent.MustSubscribe(ctx, m.Pubsub, "updateSubmittedHeightLoop", settlement.EventQueryNewSettlementBatchAccepted, m.UpdateLastSubmittedHeight, m.logger)

// Sequencer must wait till DA is synced to start submitting blobs
<-m.DAClient.Synced()
err = m.syncFromSettlement()
Expand Down
2 changes: 0 additions & 2 deletions block/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/rand"
"errors"
"sync/atomic"

"testing"
"time"

Expand Down Expand Up @@ -378,7 +377,6 @@ func TestApplyLocalBlock_WithFraudCheck(t *testing.T) {
}

func TestRetrieveDaBatchesFailed(t *testing.T) {

manager, err := testutil.GetManager(testutil.GetManagerConfig(), nil, 1, 1, 0, nil, nil)
require.NoError(t, err)
require.NotNil(t, manager)
Expand Down
23 changes: 11 additions & 12 deletions block/pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,34 @@ package block
import (
"context"
"fmt"

"github.com/dymensionxyz/gerr-cosmos/gerrc"
)

func (m *Manager) PruneBlocks(retainHeight uint64) error {
if m.IsProposer() && m.NextHeightToSubmit() < retainHeight { // do not delete anything that we might submit in future
return fmt.Errorf("cannot prune blocks before they have been submitted: retain height %d: next height to submit: %d: %w",
retainHeight,
m.NextHeightToSubmit(),
gerrc.ErrInvalidArgument)
// PruneBlocks prune all block related data from dymint store up to (but not including) retainHeight. It returns the number of blocks pruned, used for testing.
func (m *Manager) PruneBlocks(retainHeight uint64) (uint64, error) {
nextSubmissionHeight := m.NextHeightToSubmit()
if m.IsProposer() && nextSubmissionHeight < retainHeight { // do not delete anything that we might submit in future
m.logger.Debug("cannot prune blocks before they have been submitted. using height last submitted height for pruning", "retain_height", retainHeight, "height_to_submit", m.NextHeightToSubmit())
retainHeight = nextSubmissionHeight
}

err := m.P2PClient.RemoveBlocks(context.Background(), m.State.BaseHeight, retainHeight)
if err != nil {
m.logger.Error("pruning blocksync store", "retain_height", retainHeight, "err", err)
}
pruned, err := m.Store.PruneBlocks(m.State.BaseHeight, retainHeight)
pruned, err := m.Store.PruneStore(m.State.BaseHeight, retainHeight, m.logger)
if err != nil {
return fmt.Errorf("prune block store: %w", err)
return 0, fmt.Errorf("prune block store: %w", err)
}

// TODO: prune state/indexer and state/txindexer??

m.State.BaseHeight = retainHeight
_, err = m.Store.SaveState(m.State, nil)
if err != nil {
return fmt.Errorf("save state: %w", err)
return 0, fmt.Errorf("save state: %w", err)
}

m.logger.Info("pruned blocks", "pruned", pruned, "retain_height", retainHeight)
return nil

return pruned, nil
}
19 changes: 11 additions & 8 deletions block/pruning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/dymensionxyz/dymint/da"
"github.com/dymensionxyz/dymint/testutil"
"github.com/dymensionxyz/dymint/version"
"github.com/dymensionxyz/gerr-cosmos/gerrc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -64,13 +65,15 @@ func TestPruningRetainHeight(t *testing.T) {
_, _, err = manager.ProduceApplyGossipBlock(ctx, true)
require.NoError(err)
}

validRetainHeight := lastSubmitted + 1 // the max possible valid retain height
for i := validRetainHeight + 1; i < manager.State.Height(); i++ {
err = manager.PruneBlocks(i)
require.Error(err) // cannot prune blocks before they have been submitted
validRetainHeight := manager.NextHeightToSubmit() // the max possible valid retain height
for i := validRetainHeight; i < manager.State.Height(); i++ {
expectedPruned := validRetainHeight - manager.State.BaseHeight
pruned, err := manager.PruneBlocks(i)
if i <= validRetainHeight {
require.NoError(err)
assert.Equal(t, expectedPruned, pruned)
} else {
require.Error(gerrc.ErrInvalidArgument)
}
}

err = manager.PruneBlocks(validRetainHeight)
require.NoError(err)
}
23 changes: 23 additions & 0 deletions block/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"time"

"github.com/dymensionxyz/gerr-cosmos/gerrc"
"github.com/tendermint/tendermint/libs/pubsub"
"golang.org/x/sync/errgroup"

"github.com/dymensionxyz/dymint/da"
"github.com/dymensionxyz/dymint/settlement"
"github.com/dymensionxyz/dymint/types"
uatomic "github.com/dymensionxyz/dymint/utils/atomic"
uchannel "github.com/dymensionxyz/dymint/utils/channel"
Expand Down Expand Up @@ -115,6 +117,12 @@ func SubmitLoopInner(
logger.Error("Create and submit batch", "err", err, "pending", pending)
panic(err)
}
// this could happen if we timed-out waiting for acceptance in the previous iteration, but the batch was indeed submitted.
// we panic here cause restarting may reset the last batch submitted counter and the sequencer can potentially resume submitting batches.
if errors.Is(err, gerrc.ErrAlreadyExists) {
logger.Debug("Batch already accepted", "err", err, "pending", pending)
panic(err)
}
return err
}
timeLastSubmission = time.Now()
Expand Down Expand Up @@ -224,6 +232,7 @@ func (m *Manager) SubmitBatch(batch *types.Batch) error {
if err != nil {
return fmt.Errorf("sl client submit batch: start height: %d: end height: %d: %w", batch.StartHeight(), batch.EndHeight(), err)
}

m.logger.Info("Submitted batch to SL.", "start height", batch.StartHeight(), "end height", batch.EndHeight())

types.RollappHubHeightGauge.Set(float64(batch.EndHeight()))
Expand Down Expand Up @@ -263,3 +272,17 @@ func (m *Manager) GetUnsubmittedBytes() int {
func (m *Manager) GetUnsubmittedBlocks() uint64 {
return m.State.Height() - m.LastSubmittedHeight.Load()
}

// UpdateLastSubmittedHeight will update last height submitted height upon events.
// This may be necessary in case we crashed/restarted before getting response for our submission to the settlement layer.
func (m *Manager) UpdateLastSubmittedHeight(event pubsub.Message) {
eventData, ok := event.Data().(*settlement.EventDataNewBatchAccepted)
if !ok {
m.logger.Error("onReceivedBatch", "err", "wrong event data received")
return
}
h := eventData.EndHeight
if m.LastSubmittedHeight.Load() < h {
m.LastSubmittedHeight.Store(h)
}
}
4 changes: 3 additions & 1 deletion da/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/tendermint/tendermint/libs/pubsub"
)

const maxBlobSize = 2097152 // 2MB (equivalent to avail or celestia)

// DataAvailabilityLayerClient is a generic client that proxies all DA requests via gRPC.
type DataAvailabilityLayerClient struct {
config Config
Expand Down Expand Up @@ -121,7 +123,7 @@ func (d *DataAvailabilityLayerClient) CheckBatchAvailability(daMetaData *da.DASu

// GetMaxBlobSizeBytes returns the maximum allowed blob size in the DA, used to check the max batch size configured
func (d *DataAvailabilityLayerClient) GetMaxBlobSizeBytes() uint32 {
return 0
return maxBlobSize
}

// RetrieveBatches proxies RetrieveBlocks request to gRPC server.
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/cosmos/cosmos-sdk v0.46.16
github.com/dgraph-io/badger/v3 v3.2103.3
github.com/dymensionxyz/cosmosclient v0.4.2-beta.0.20240821081230-b4018b2bac13
github.com/dymensionxyz/dymension/v3 v3.1.0-rc03.0.20240411195658-f7cd96f53b56
github.com/dymensionxyz/gerr-cosmos v1.0.0
github.com/go-kit/kit v0.12.0
github.com/gofrs/uuid v4.3.0+incompatible
Expand Down Expand Up @@ -49,8 +48,10 @@ require (
github.com/celestiaorg/go-square/merkle v0.0.0-20240429192549-dea967e1533b // indirect
github.com/cskr/pubsub v1.0.2 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/hashicorp/go-getter v1.7.5 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/ipfs/go-block-format v0.2.0 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
google.golang.org/api v0.169.0 // indirect
)

Expand All @@ -75,7 +76,7 @@ require (
github.com/cometbft/cometbft v0.37.2
github.com/cometbft/cometbft-db v0.11.0 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.3
github.com/cosmos/gogoproto v1.5.0
github.com/cosmos/gogoproto v1.5.0 // indirect
github.com/creachadair/taskgroup v0.3.2 // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/decred/base58 v1.0.4 // indirect
Expand Down Expand Up @@ -116,7 +117,7 @@ require (
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.uber.org/dig v1.17.1 // indirect
go.uber.org/fx v1.20.1 // indirect
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a // indirect
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
Expand Down Expand Up @@ -287,7 +288,6 @@ require (
github.com/linxGnu/grocksdb v1.8.12 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/common v0.47.0 // indirect
github.com/regen-network/cosmos-proto v0.3.1 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f // indirect
go.uber.org/mock v0.4.0 // indirect
Expand Down
Loading

0 comments on commit 94923c0

Please sign in to comment.