github.com/lazyledger/lazyledger-core@v0.35.0-dev.0.20210613111200-4c651f053571/light/provider/http/http_test.go (about) 1 package http_test 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 12 "github.com/lazyledger/lazyledger-core/abci/example/kvstore" 13 "github.com/lazyledger/lazyledger-core/light/provider" 14 lighthttp "github.com/lazyledger/lazyledger-core/light/provider/http" 15 rpcclient "github.com/lazyledger/lazyledger-core/rpc/client" 16 rpchttp "github.com/lazyledger/lazyledger-core/rpc/client/http" 17 rpctest "github.com/lazyledger/lazyledger-core/rpc/test" 18 "github.com/lazyledger/lazyledger-core/types" 19 ) 20 21 func TestNewProvider(t *testing.T) { 22 c, err := lighthttp.New("chain-test", "192.168.0.1:26657") 23 require.NoError(t, err) 24 require.Equal(t, fmt.Sprintf("%s", c), "http{http://192.168.0.1:26657}") 25 26 c, err = lighthttp.New("chain-test", "http://153.200.0.1:26657") 27 require.NoError(t, err) 28 require.Equal(t, fmt.Sprintf("%s", c), "http{http://153.200.0.1:26657}") 29 30 c, err = lighthttp.New("chain-test", "153.200.0.1") 31 require.NoError(t, err) 32 require.Equal(t, fmt.Sprintf("%s", c), "http{http://153.200.0.1}") 33 } 34 35 func TestMain(m *testing.M) { 36 app := kvstore.NewApplication() 37 app.RetainBlocks = 9 38 node := rpctest.StartTendermint(app) 39 40 code := m.Run() 41 42 rpctest.StopTendermint(node) 43 os.Exit(code) 44 } 45 46 func TestProvider(t *testing.T) { 47 cfg := rpctest.GetConfig() 48 defer os.RemoveAll(cfg.RootDir) 49 rpcAddr := cfg.RPC.ListenAddress 50 genDoc, err := types.GenesisDocFromFile(cfg.GenesisFile()) 51 if err != nil { 52 panic(err) 53 } 54 chainID := genDoc.ChainID 55 t.Log("chainID:", chainID) 56 57 c, err := rpchttp.New(rpcAddr, "/websocket") 58 require.Nil(t, err) 59 60 p := lighthttp.NewWithClient(chainID, c) 61 require.NoError(t, err) 62 require.NotNil(t, p) 63 64 // let it produce some blocks 65 err = rpcclient.WaitForHeight(c, 10, nil) 66 require.NoError(t, err) 67 68 // let's get the highest block 69 sh, err := p.LightBlock(context.Background(), 0) 70 require.NoError(t, err) 71 assert.True(t, sh.Height < 1000) 72 73 // let's check this is valid somehow 74 assert.Nil(t, sh.ValidateBasic(chainID)) 75 76 // historical queries now work :) 77 lower := sh.Height - 3 78 sh, err = p.LightBlock(context.Background(), lower) 79 require.NoError(t, err) 80 assert.Equal(t, lower, sh.Height) 81 82 // fetching missing heights (both future and pruned) should return appropriate errors 83 _, err = p.LightBlock(context.Background(), 1000) 84 require.Error(t, err) 85 assert.Equal(t, provider.ErrLightBlockNotFound, err) 86 87 _, err = p.LightBlock(context.Background(), 1) 88 require.Error(t, err) 89 assert.Equal(t, provider.ErrLightBlockNotFound, err) 90 }