1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package host
import (
"context"
"errors"
"testing"
"time"
"github.com/ethereum-optimism/optimism/op-node/chaincfg"
preimage "github.com/ethereum-optimism/optimism/op-preimage"
"github.com/ethereum-optimism/optimism/op-program/chainconfig"
"github.com/ethereum-optimism/optimism/op-program/client"
"github.com/ethereum-optimism/optimism/op-program/client/l1"
"github.com/ethereum-optimism/optimism/op-program/host/config"
"github.com/ethereum-optimism/optimism/op-program/host/kvstore"
"github.com/ethereum-optimism/optimism/op-program/io"
"github.com/ethereum-optimism/optimism/op-service/testlog"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/stretchr/testify/require"
)
func TestServerMode(t *testing.T) {
dir := t.TempDir()
l1Head := common.Hash{0x11}
l2OutputRoot := common.Hash{0x33}
cfg := config.NewConfig(chaincfg.Goerli, chainconfig.OPGoerliChainConfig, l1Head, common.Hash{0x22}, l2OutputRoot, common.Hash{0x44}, 1000)
cfg.DataDir = dir
cfg.ServerMode = true
preimageServer, preimageClient, err := io.CreateBidirectionalChannel()
require.NoError(t, err)
defer preimageClient.Close()
hintServer, hintClient, err := io.CreateBidirectionalChannel()
require.NoError(t, err)
defer hintClient.Close()
logger := testlog.Logger(t, log.LevelTrace)
result := make(chan error)
go func() {
result <- PreimageServer(context.Background(), logger, cfg, preimageServer, hintServer)
}()
pClient := preimage.NewOracleClient(preimageClient)
hClient := preimage.NewHintWriter(hintClient)
l1PreimageOracle := l1.NewPreimageOracle(pClient, hClient)
require.Equal(t, l1Head.Bytes(), pClient.Get(client.L1HeadLocalIndex), "Should get l1 head preimages")
require.Equal(t, l2OutputRoot.Bytes(), pClient.Get(client.L2OutputRootLocalIndex), "Should get l2 output root preimages")
// Should exit when a preimage is unavailable
require.Panics(t, func() {
l1PreimageOracle.HeaderByBlockHash(common.HexToHash("0x1234"))
}, "Preimage should not be available")
require.ErrorIs(t, waitFor(result), kvstore.ErrNotFound)
}
func waitFor(ch chan error) error {
timeout := time.After(30 * time.Second)
select {
case err := <-ch:
return err
case <-timeout:
return errors.New("timed out")
}
}