Skip to content

Commit

Permalink
feat: add wait time before producing blocks (cosmos#278)
Browse files Browse the repository at this point in the history
Resolves cosmos#195
Resolves cosmos#196

Co-authored-by: Tomasz Zdybał <[email protected]>
  • Loading branch information
toanngosy and tzdybal authored Jan 23, 2023
1 parent 567b9f5 commit a972fac
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
17 changes: 17 additions & 0 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,24 @@ func (m *Manager) SetDALC(dalc da.DataAvailabilityLayerClient) {

// AggregationLoop is responsible for aggregating transactions into rollup-blocks.
func (m *Manager) AggregationLoop(ctx context.Context) {
initialHeight := uint64(m.genesis.InitialHeight)
height := m.store.Height()
var delay time.Duration

// TODO(tzdybal): double-check when https://github.com/celestiaorg/rollmint/issues/699 is resolved
if height < initialHeight {
delay = time.Until(m.genesis.GenesisTime)
} else {
delay = time.Until(m.lastState.LastBlockTime.Add(m.conf.BlockTime))
}

if delay > 0 {
m.logger.Info("Waiting to produce block", "delay", delay)
time.Sleep(delay)
}

timer := time.NewTimer(0)

for {
select {
case <-ctx.Done():
Expand Down
47 changes: 47 additions & 0 deletions node/full_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
crand "crypto/rand"
"fmt"
"math/rand"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -1022,3 +1023,49 @@ func TestStatus(t *testing.T) {
assert.Equal(tc.expected, res, tc)
}
}

func TestFutureGenesisTime(t *testing.T) {
t.Parallel()
assert := assert.New(t)
require := require.New(t)

var beginBlockTime time.Time
wg := sync.WaitGroup{}
wg.Add(1)
mockApp := &mocks.Application{}
mockApp.On("InitChain", mock.Anything).Return(abci.ResponseInitChain{})
mockApp.On("BeginBlock", mock.Anything).Return(abci.ResponseBeginBlock{}).Run(func(_ mock.Arguments) {
beginBlockTime = time.Now()
wg.Done()
})
mockApp.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{})
mockApp.On("Commit", mock.Anything).Return(abci.ResponseCommit{})
mockApp.On("DeliverTx", mock.Anything).Return(abci.ResponseDeliverTx{})
mockApp.On("CheckTx", mock.Anything).Return(abci.ResponseCheckTx{})
key, _, _ := crypto.GenerateEd25519Key(crand.Reader)
signingKey, _, _ := crypto.GenerateEd25519Key(crand.Reader)
genesisTime := time.Now().Local().Add(time.Second * time.Duration(1))
node, err := newFullNode(context.Background(), config.NodeConfig{
DALayer: "mock",
Aggregator: true,
BlockManagerConfig: config.BlockManagerConfig{
BlockTime: 200 * time.Millisecond,
}},
key, signingKey,
abcicli.NewLocalClient(nil, mockApp),
&tmtypes.GenesisDoc{
ChainID: "test",
InitialHeight: 1,
GenesisTime: genesisTime,
},
log.TestingLogger())
require.NoError(err)
require.NotNil(node)

err = node.Start()
require.NoError(err)

wg.Wait()

assert.True(beginBlockTime.After(genesisTime))
}

0 comments on commit a972fac

Please sign in to comment.