Commit e4a48f8c authored by Andreas Bigger's avatar Andreas Bigger

pending channel timeout tests :test_tube:

parent 7bbe25dd
...@@ -18,6 +18,92 @@ import ( ...@@ -18,6 +18,92 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// TestChannelManagerAddL2Block tests adding an L2 block to the channel manager.
func TestChannelManagerAddL2Block(t *testing.T) {
log := testlog.Logger(t, log.LvlCrit)
m := NewChannelManager(log, ChannelConfig{})
// Add one block and assert state changes
a := types.NewBlock(&types.Header{
Number: big.NewInt(0),
}, nil, nil, nil, nil)
err := m.AddL2Block(a)
require.NoError(t, err)
require.Equal(t, []*types.Block{a}, m.blocks)
require.Equal(t, a.Hash(), m.tip)
// Add another block and assert state changes
b := types.NewBlock(&types.Header{
Number: big.NewInt(1),
ParentHash: a.Hash(),
}, nil, nil, nil, nil)
err = m.AddL2Block(b)
require.NoError(t, err)
require.Equal(t, []*types.Block{a, b}, m.blocks)
require.Equal(t, b.Hash(), m.tip)
// Adding a block with an invalid parent hash should fail
c := types.NewBlock(&types.Header{
Number: big.NewInt(2),
ParentHash: common.Hash{},
}, nil, nil, nil, nil)
err = m.AddL2Block(c)
require.ErrorIs(t, ErrReorg, err)
}
// TestPendingChannelTimeout tests that the channel manager
// correctly identifies when a pending channel is timed out.
func TestPendingChannelTimeout(t *testing.T) {
// Create a new channel manager with a ChannelTimeout
log := testlog.Logger(t, log.LvlCrit)
m := NewChannelManager(log, ChannelConfig{
ChannelTimeout: 100,
})
// Pending channel is nil so is cannot be timed out
timeout := m.pendingChannelIsTimedOut()
require.False(t, timeout)
// Set the pending channel
err := m.ensurePendingChannel(eth.BlockID{})
require.NoError(t, err)
// There are no confirmed transactions so
// the pending channel cannot be timed out
timeout = m.pendingChannelIsTimedOut()
require.False(t, timeout)
// Manually set a confirmed transactions
// To avoid other methods clearing state
m.confirmedTransactions[frameID{
frameNumber: 0,
}] = eth.BlockID{
Number: 0,
}
m.confirmedTransactions[frameID{
frameNumber: 1,
}] = eth.BlockID{
Number: 99,
}
// Since the ChannelTimeout is 100, the
// pending channel should not be timed out
timeout = m.pendingChannelIsTimedOut()
require.False(t, timeout)
// Add a confirmed transaction with a higher number
// than the ChannelTimeout
m.confirmedTransactions[frameID{
frameNumber: 2,
}] = eth.BlockID{
Number: 101,
}
// Now the pending channel should be timed out
timeout = m.pendingChannelIsTimedOut()
require.True(t, timeout)
}
// TestChannelManagerReturnsErrReorg ensures that the channel manager // TestChannelManagerReturnsErrReorg ensures that the channel manager
// detects a reorg when it has cached L1 blocks. // detects a reorg when it has cached L1 blocks.
func TestChannelManagerReturnsErrReorg(t *testing.T) { func TestChannelManagerReturnsErrReorg(t *testing.T) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment