Skip to content

Commit

Permalink
fix(rpc): Fix status CatchingUp field updating (#971)
Browse files Browse the repository at this point in the history
(cherry picked from commit 402dd00)
  • Loading branch information
zale144 authored and omritoptix committed Aug 13, 2024
1 parent d57d5b7 commit 04002b0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 10 deletions.
1 change: 1 addition & 0 deletions block/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (m *Manager) onNewGossipedBlock(event pubsub.Message) {
return
}

m.UpdateTargetHeight(height)
types.LastReceivedP2PHeightGauge.Set(float64(height))

m.logger.Debug("Received new block via gossip.", "block height", height, "store height", m.State.Height(), "n cachedBlocks", m.blockCache.Size())
Expand Down
14 changes: 13 additions & 1 deletion block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type Manager struct {
*/
// The last height which was submitted to both sublayers, that we know of. When we produce new batches, we will
// start at this height + 1.
// It is ALSO used by the producer, because the producer needs to check if it can prune blocks and it wont'
// It is ALSO used by the producer, because the producer needs to check if it can prune blocks and it won't
// prune anything that might be submitted in the future. Therefore, it must be atomic.
LastSubmittedHeight atomic.Uint64

Expand All @@ -68,6 +68,9 @@ type Manager struct {
Retriever da.BatchRetriever
// get the next target height to sync local state to
targetSyncHeight diodes.Diode
// TargetHeight holds the value of the current highest block seen from either p2p (probably higher) or the DA
TargetHeight atomic.Uint64

// Cached blocks and commits for applying at future heights. The blocks may not be valid, because
// we can only do full validation in sequential order.
blockCache *Cache
Expand Down Expand Up @@ -227,3 +230,12 @@ func (m *Manager) syncBlockManager() error {
m.logger.Info("Synced.", "current height", m.State.Height(), "last submitted height", m.LastSubmittedHeight.Load())
return nil
}

func (m *Manager) UpdateTargetHeight(h uint64) {
for {
currentHeight := m.TargetHeight.Load()
if m.TargetHeight.CompareAndSwap(currentHeight, max(currentHeight, h)) {
break
}
}
}
35 changes: 34 additions & 1 deletion block/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ package block_test
import (
"context"
"crypto/rand"
"sync/atomic"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/libp2p/go-libp2p/core/crypto"

"github.com/dymensionxyz/dymint/block"
"github.com/dymensionxyz/dymint/p2p"
"github.com/dymensionxyz/dymint/settlement"
"github.com/dymensionxyz/dymint/testutil"
"github.com/dymensionxyz/dymint/types"
"github.com/libp2p/go-libp2p/core/crypto"

abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
Expand Down Expand Up @@ -439,3 +441,34 @@ func TestDAFetch(t *testing.T) {
})
}
}

func TestManager_updateTargetHeight(t *testing.T) {
tests := []struct {
name string
TargetHeight uint64
h uint64
expTargetHeight uint64
}{
{
name: "no update target height",
TargetHeight: 100,
h: 99,
expTargetHeight: 100,
}, {
name: "update target height",
TargetHeight: 100,
h: 101,
expTargetHeight: 101,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &block.Manager{
TargetHeight: atomic.Uint64{},
}
m.TargetHeight.Store(tt.TargetHeight)
m.UpdateTargetHeight(tt.h)
assert.Equal(t, tt.expTargetHeight, m.TargetHeight.Load())
})
}
}
5 changes: 3 additions & 2 deletions block/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"sync/atomic"
"time"

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

"github.com/dymensionxyz/dymint/da"
"github.com/dymensionxyz/dymint/types"
uchannel "github.com/dymensionxyz/dymint/utils/channel"
"github.com/dymensionxyz/gerr-cosmos/gerrc"
"golang.org/x/sync/errgroup"
)

// SubmitLoop is the main loop for submitting blocks to the DA and SL layers.
Expand Down
1 change: 1 addition & 0 deletions block/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (m *Manager) SyncToTargetHeightLoop(ctx context.Context) (err error) {
continue
}
types.RollappHubHeightGauge.Set(float64(h))
m.UpdateTargetHeight(h)
m.targetSyncHeight.Set(diodes.GenericDataType(&h))
m.logger.Info("Set new target sync height", "height", h)
case <-subscription.Cancelled():
Expand Down
10 changes: 4 additions & 6 deletions rpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ import (
"sort"
"time"

"github.com/dymensionxyz/dymint/types"

"github.com/dymensionxyz/dymint/version"

sdkerrors "cosmossdk.io/errors"

abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/config"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
Expand All @@ -29,6 +24,8 @@ import (

"github.com/dymensionxyz/dymint/mempool"
"github.com/dymensionxyz/dymint/node"
"github.com/dymensionxyz/dymint/types"
"github.com/dymensionxyz/dymint/version"
)

const (
Expand Down Expand Up @@ -750,13 +747,14 @@ func (c *Client) Status(ctx context.Context) (*ctypes.ResultStatus, error) {
LatestAppHash: latestAppHash[:],
LatestBlockHeight: int64(latestHeight),
LatestBlockTime: time.Unix(0, int64(latestBlockTimeNano)),
// CatchingUp is true if the node is not at the latest height received from p2p or da.
CatchingUp: c.node.BlockManager.TargetHeight.Load() > latestHeight,
// TODO(tzdybal): add missing fields
// EarliestBlockHash: earliestBlockHash,
// EarliestAppHash: earliestAppHash,
// EarliestBlockHeight: earliestBloc
// kHeight,
// EarliestBlockTime: time.Unix(0, earliestBlockTimeNano),
// CatchingUp: env.ConsensusReactor.WaitSync(),
},
// TODO(ItzhakBokris): update ValidatorInfo fields
ValidatorInfo: ctypes.ValidatorInfo{
Expand Down

0 comments on commit 04002b0

Please sign in to comment.