gitlab.com/gpdionisio/tendermint@v0.34.19-dev2/light/example_test.go (about)

     1  package light_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io/ioutil"
     7  	stdlog "log"
     8  	"os"
     9  	"testing"
    10  	"time"
    11  
    12  	dbm "github.com/tendermint/tm-db"
    13  
    14  	"github.com/tendermint/tendermint/abci/example/kvstore"
    15  	"github.com/tendermint/tendermint/libs/log"
    16  	"github.com/tendermint/tendermint/light"
    17  	"github.com/tendermint/tendermint/light/provider"
    18  	httpp "github.com/tendermint/tendermint/light/provider/http"
    19  	dbs "github.com/tendermint/tendermint/light/store/db"
    20  	rpctest "github.com/tendermint/tendermint/rpc/test"
    21  )
    22  
    23  // Automatically getting new headers and verifying them.
    24  func ExampleClient_Update() {
    25  	// give Tendermint time to generate some blocks
    26  	time.Sleep(5 * time.Second)
    27  
    28  	dbDir, err := ioutil.TempDir("", "light-client-example")
    29  	if err != nil {
    30  		stdlog.Fatal(err)
    31  	}
    32  	defer os.RemoveAll(dbDir)
    33  
    34  	var (
    35  		config  = rpctest.GetConfig()
    36  		chainID = config.ChainID()
    37  	)
    38  
    39  	primary, err := httpp.New(chainID, config.RPC.ListenAddress)
    40  	if err != nil {
    41  		stdlog.Fatal(err)
    42  	}
    43  
    44  	block, err := primary.LightBlock(context.Background(), 2)
    45  	if err != nil {
    46  		stdlog.Fatal(err)
    47  	}
    48  
    49  	db, err := dbm.NewGoLevelDB("light-client-db", dbDir)
    50  	if err != nil {
    51  		stdlog.Fatal(err)
    52  	}
    53  
    54  	c, err := light.NewClient(
    55  		context.Background(),
    56  		chainID,
    57  		light.TrustOptions{
    58  			Period: 504 * time.Hour, // 21 days
    59  			Height: 2,
    60  			Hash:   block.Hash(),
    61  		},
    62  		primary,
    63  		[]provider.Provider{primary}, // NOTE: primary should not be used here
    64  		dbs.New(db, chainID),
    65  		light.Logger(log.TestingLogger()),
    66  	)
    67  	if err != nil {
    68  		stdlog.Fatal(err)
    69  	}
    70  	defer func() {
    71  		if err := c.Cleanup(); err != nil {
    72  			stdlog.Fatal(err)
    73  		}
    74  	}()
    75  
    76  	time.Sleep(2 * time.Second)
    77  
    78  	h, err := c.Update(context.Background(), time.Now())
    79  	if err != nil {
    80  		stdlog.Fatal(err)
    81  	}
    82  
    83  	if h != nil && h.Height > 2 {
    84  		fmt.Println("successful update")
    85  	} else {
    86  		fmt.Println("update failed")
    87  	}
    88  	// Output: successful update
    89  }
    90  
    91  // Manually getting light blocks and verifying them.
    92  func ExampleClient_VerifyLightBlockAtHeight() {
    93  	// give Tendermint time to generate some blocks
    94  	time.Sleep(5 * time.Second)
    95  
    96  	dbDir, err := ioutil.TempDir("", "light-client-example")
    97  	if err != nil {
    98  		stdlog.Fatal(err)
    99  	}
   100  	defer os.RemoveAll(dbDir)
   101  
   102  	var (
   103  		config  = rpctest.GetConfig()
   104  		chainID = config.ChainID()
   105  	)
   106  
   107  	primary, err := httpp.New(chainID, config.RPC.ListenAddress)
   108  	if err != nil {
   109  		stdlog.Fatal(err)
   110  	}
   111  
   112  	block, err := primary.LightBlock(context.Background(), 2)
   113  	if err != nil {
   114  		stdlog.Fatal(err)
   115  	}
   116  
   117  	db, err := dbm.NewGoLevelDB("light-client-db", dbDir)
   118  	if err != nil {
   119  		stdlog.Fatal(err)
   120  	}
   121  
   122  	c, err := light.NewClient(
   123  		context.Background(),
   124  		chainID,
   125  		light.TrustOptions{
   126  			Period: 504 * time.Hour, // 21 days
   127  			Height: 2,
   128  			Hash:   block.Hash(),
   129  		},
   130  		primary,
   131  		[]provider.Provider{primary}, // NOTE: primary should not be used here
   132  		dbs.New(db, chainID),
   133  		light.Logger(log.TestingLogger()),
   134  	)
   135  	if err != nil {
   136  		stdlog.Fatal(err)
   137  	}
   138  	defer func() {
   139  		if err := c.Cleanup(); err != nil {
   140  			stdlog.Fatal(err)
   141  		}
   142  	}()
   143  
   144  	_, err = c.VerifyLightBlockAtHeight(context.Background(), 3, time.Now())
   145  	if err != nil {
   146  		stdlog.Fatal(err)
   147  	}
   148  
   149  	h, err := c.TrustedLightBlock(3)
   150  	if err != nil {
   151  		stdlog.Fatal(err)
   152  	}
   153  
   154  	fmt.Println("got header", h.Height)
   155  	// Output: got header 3
   156  }
   157  
   158  func TestMain(m *testing.M) {
   159  	// start a tendermint node (and kvstore) in the background to test against
   160  	app := kvstore.NewApplication()
   161  	node := rpctest.StartTendermint(app, rpctest.SuppressStdout)
   162  
   163  	code := m.Run()
   164  
   165  	// and shut down proper at the end
   166  	rpctest.StopTendermint(node)
   167  	os.Exit(code)
   168  }