Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix: changed latestHeight to be updated on batch acceptance #257

Merged
merged 1 commit into from
Feb 19, 2023
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
23 changes: 21 additions & 2 deletions settlement/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (b *BaseLayerClient) Start() error {
endHeight = latestBatch.EndHeight
}
b.latestHeight = endHeight
b.logger.Info("Updated latest height from settlement layer", "latestHeight", b.latestHeight)
b.logger.Info("Updated latest height from settlement layer", "latestHeight", endHeight)
b.sequencersList, err = b.fetchSequencersList()
if err != nil {
if err == ErrNoSequencerForRollapp {
Expand All @@ -85,6 +85,7 @@ func (b *BaseLayerClient) Start() error {
return err
}
b.logger.Info("Updated sequencers list from settlement layer", "sequencersList", b.sequencersList)
go b.stateUpdatesHandler()

err = b.client.Start()
if err != nil {
Expand Down Expand Up @@ -123,7 +124,6 @@ func (b *BaseLayerClient) SubmitBatch(batch *types.Batch, daClient da.Client, da
}
}
b.logger.Info("Successfully submitted batch to settlement layer", "tx hash", txResp.GetTxHash())
atomic.StoreUint64(&b.latestHeight, batch.EndHeight)
return &ResultSubmitBatch{
BaseResult: BaseResult{Code: StatusSuccess},
}
Expand Down Expand Up @@ -206,3 +206,22 @@ func (b *BaseLayerClient) validateBatch(batch *types.Batch) error {
}
return nil
}

func (b *BaseLayerClient) stateUpdatesHandler() {
b.logger.Info("Started state updates handler loop")
subscription, err := b.pubsub.Subscribe(context.Background(), "stateUpdatesHandler", EventQueryNewSettlementBatchAccepted)
if err != nil {
b.logger.Error("failed to subscribe to state update events")
panic(err)
}
for {
select {
case event := <-subscription.Out():
b.logger.Debug("Received state update event", "eventData", event.Data())
eventData := event.Data().(*EventDataNewSettlementBatchAccepted)
atomic.StoreUint64(&b.latestHeight, eventData.EndHeight)
case <-subscription.Cancelled():
b.logger.Info("Subscription canceled")
}
}
}
13 changes: 9 additions & 4 deletions settlement/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"strings"
"sync/atomic"
"time"

tmp2p "github.com/tendermint/tendermint/p2p"

Expand Down Expand Up @@ -186,10 +187,14 @@ func (c *HubClient) PostBatch(batch *types.Batch, daClient da.Client, daResult *
if err != nil {
return PostBatchResp{err}, err
}
err = c.pubsub.PublishWithEvents(context.Background(), &settlement.EventDataNewSettlementBatchAccepted{EndHeight: settlementBatch.EndHeight}, map[string][]string{settlement.EventTypeKey: {settlement.EventNewSettlementBatchAccepted}})
if err != nil {
c.logger.Error("error publishing event", "error", err)
}
go func() {
// sleep for 100 miliseconds to mimic a delay in batch acceptance
time.Sleep(100 * time.Millisecond)
err = c.pubsub.PublishWithEvents(context.Background(), &settlement.EventDataNewSettlementBatchAccepted{EndHeight: settlementBatch.EndHeight}, map[string][]string{settlement.EventTypeKey: {settlement.EventNewSettlementBatchAccepted}})
if err != nil {
c.logger.Error("error publishing event", "error", err)
}
}()
return PostBatchResp{nil}, nil
}

Expand Down
3 changes: 3 additions & 0 deletions settlement/settlement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package settlement_test

import (
"testing"
"time"

"github.com/libp2p/go-libp2p-core/crypto"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -110,6 +111,8 @@ func TestSubmitAndRetrieve(t *testing.T) {
}
resultSubmitBatch := settlementClient.SubmitBatch(batch, da.Mock, daResult)
assert.Equal(resultSubmitBatch.Code, settlement.StatusSuccess)
// sleep for 500 ms to make sure batch got accepted by the settlement layer
time.Sleep(500 * time.Millisecond)
}

// Retrieve the latest batch and make sure it matches latest batch submitted
Expand Down