Skip to content

Commit

Permalink
add test for deleteFinalizedMsgsFromRedis
Browse files Browse the repository at this point in the history
  • Loading branch information
ganeshvanahalli committed Jul 16, 2024
1 parent ab8e6a8 commit 4cff8b3
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
8 changes: 4 additions & 4 deletions arbnode/seq_coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ func (c *SeqCoordinator) deleteFinalizedMsgsFromRedis(ctx context.Context, final
prevFinalized, err := c.getRemoteFinalizedMsgCount(ctx)
if errors.Is(err, redis.Nil) {
var keys []string
for msg := finalized; ; msg-- {
for msg := finalized; msg > 0; msg-- {
exists, err := c.Client.Exists(ctx, redisutil.MessageKeyFor(msg), redisutil.MessageSigKeyFor(msg)).Result()
if exists == 0 || err != nil {
break
Expand All @@ -543,11 +543,11 @@ func (c *SeqCoordinator) deleteFinalizedMsgsFromRedis(ctx context.Context, final
} else if err != nil {
return fmt.Errorf("error getting finalizedMsgCount value from redis: %w", err)
}
remoteMsgCount, err := c.GetRemoteMsgCount()
remoteMsgCount, err := c.getRemoteMsgCountImpl(ctx, c.Client)
if err != nil {
return fmt.Errorf("cannot get remote message count: %w", err)
}
msgToDelete := min(finalized, remoteMsgCount)
msgToDelete := min(finalized, remoteMsgCount-1)
if prevFinalized < msgToDelete {
var keys []string
for msg := prevFinalized + 1; msg <= msgToDelete; msg++ {
Expand Down Expand Up @@ -604,7 +604,7 @@ func (c *SeqCoordinator) update(ctx context.Context) time.Duration {
var messages []arbostypes.MessageWithMetadata
msgToRead := localMsgCount
var msgReadErr error
for msgToRead < readUntil && localMsgCount >= remoteFinalizedMsgCount {
for msgToRead < readUntil && localMsgCount > remoteFinalizedMsgCount {
var resString string
resString, msgReadErr = c.Client.Get(ctx, redisutil.MessageKeyFor(msgToRead)).Result()
if msgReadErr != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,87 @@ func TestRedisSeqCoordinatorAtomic(t *testing.T) {
}

}

func TestSeqCoordinatorDeletesFinalizedMessages(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

coordConfig := TestSeqCoordinatorConfig
coordConfig.LockoutDuration = time.Millisecond * 100
coordConfig.LockoutSpare = time.Millisecond * 10
coordConfig.Signer.ECDSA.AcceptSequencer = false
coordConfig.Signer.SymmetricFallback = true
coordConfig.Signer.SymmetricSign = true
coordConfig.Signer.Symmetric.Dangerous.DisableSignatureVerification = true
coordConfig.Signer.Symmetric.SigningKey = ""

nullSigner, err := signature.NewSignVerify(&coordConfig.Signer, nil, nil)
Require(t, err)

redisUrl := redisutil.CreateTestRedis(ctx, t)
coordConfig.RedisUrl = redisUrl

config := coordConfig
config.MyUrl = "test"
redisCoordinator, err := redisutil.NewRedisCoordinator(config.RedisUrl)
Require(t, err)
coordinator := &SeqCoordinator{
RedisCoordinator: *redisCoordinator,
config: config,
signer: nullSigner,
}

// Add messages to redis
var keys []string
msgBytes, err := coordinator.msgCountToSignedBytes(0)
Require(t, err)
for i := arbutil.MessageIndex(1); i <= 10; i++ {
err = coordinator.Client.Set(ctx, redisutil.MessageKeyFor(i), msgBytes, time.Hour).Err()
Require(t, err)
err = coordinator.Client.Set(ctx, redisutil.MessageSigKeyFor(i), msgBytes, time.Hour).Err()
Require(t, err)
keys = append(keys, redisutil.MessageKeyFor(i), redisutil.MessageSigKeyFor(i))
}
// Set msgCount key
msgCountBytes, err := coordinator.msgCountToSignedBytes(11)
Require(t, err)
err = coordinator.Client.Set(ctx, redisutil.MSG_COUNT_KEY, msgCountBytes, time.Hour).Err()
Require(t, err)
exists, err := coordinator.Client.Exists(ctx, keys...).Result()
Require(t, err)
if exists != 20 {
t.Fatal("couldn't find all messages and signatures in redis")
}

// Set finalizedMsgCount and delete finalized messages
err = coordinator.deleteFinalizedMsgsFromRedis(ctx, 5)
Require(t, err)

// Check if messages and signatures were deleted successfully
exists, err = coordinator.Client.Exists(ctx, keys[:10]...).Result()
Require(t, err)
if exists != 0 {
t.Fatal("finalized messages and signatures in range 1 to 5 were not deleted")
}

// Check if finalizedMsgCount was set to correct value
finalized, err := coordinator.getRemoteFinalizedMsgCount(ctx)
Require(t, err)
if finalized != 5 {
t.Fatalf("incorrect finalizedMsgCount, want: 5, have: %d", finalized)
}

// Try deleting finalized messages when theres already a finalizedMsgCount
err = coordinator.deleteFinalizedMsgsFromRedis(ctx, 7)
Require(t, err)
exists, err = coordinator.Client.Exists(ctx, keys[10:14]...).Result()
Require(t, err)
if exists != 0 {
t.Fatal("finalized messages and signatures in range 6 to 7 were not deleted")
}
finalized, err = coordinator.getRemoteFinalizedMsgCount(ctx)
Require(t, err)
if finalized != 7 {
t.Fatalf("incorrect finalizedMsgCount, want: 7, have: %d", finalized)
}
}

0 comments on commit 4cff8b3

Please sign in to comment.