github.com/vipernet-xyz/tm@v0.34.24/light/example_test.go (about)

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