github.com/lazyledger/lazyledger-core@v0.35.0-dev.0.20210613111200-4c651f053571/light/mbt/driver_test.go (about)

     1  // nolint:unused
     2  package mbt
     3  
     4  import (
     5  	"io/ioutil"
     6  	"path/filepath"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	tmjson "github.com/lazyledger/lazyledger-core/libs/json"
    13  	"github.com/lazyledger/lazyledger-core/light"
    14  	"github.com/lazyledger/lazyledger-core/types"
    15  )
    16  
    17  const jsonDir = "./json"
    18  
    19  func TestVerify(t *testing.T) {
    20  	t.Skip("Tendermint's Model based tests for the light client need to be reworked to match LazyLedger types")
    21  
    22  	filenames := jsonFilenames(t)
    23  
    24  	for _, filename := range filenames {
    25  		filename := filename
    26  		t.Run(filename, func(t *testing.T) {
    27  
    28  			jsonBlob, err := ioutil.ReadFile(filename)
    29  			if err != nil {
    30  				t.Fatal(err)
    31  			}
    32  
    33  			var tc testCase
    34  			err = tmjson.Unmarshal(jsonBlob, &tc)
    35  			if err != nil {
    36  				t.Fatal(err)
    37  			}
    38  
    39  			t.Log(tc.Description)
    40  
    41  			var (
    42  				trustedSignedHeader = tc.Initial.SignedHeader
    43  				trustedNextVals     = tc.Initial.NextValidatorSet
    44  				trustingPeriod      = time.Duration(tc.Initial.TrustingPeriod) * time.Nanosecond
    45  			)
    46  
    47  			for _, input := range tc.Input {
    48  				var (
    49  					newSignedHeader = input.LightBlock.SignedHeader
    50  					newVals         = input.LightBlock.ValidatorSet
    51  				)
    52  
    53  				err = light.Verify(
    54  					&trustedSignedHeader,
    55  					&trustedNextVals,
    56  					newSignedHeader,
    57  					newVals,
    58  					trustingPeriod,
    59  					input.Now,
    60  					1*time.Second,
    61  					light.DefaultTrustLevel,
    62  				)
    63  
    64  				t.Logf("%d -> %d", trustedSignedHeader.Height, newSignedHeader.Height)
    65  
    66  				switch input.Verdict {
    67  				case "SUCCESS":
    68  					require.NoError(t, err)
    69  				case "NOT_ENOUGH_TRUST":
    70  					require.IsType(t, light.ErrNewValSetCantBeTrusted{}, err)
    71  				case "INVALID":
    72  					switch err.(type) {
    73  					case light.ErrOldHeaderExpired:
    74  					case light.ErrInvalidHeader:
    75  					default:
    76  						t.Fatalf("expected either ErrInvalidHeader or ErrOldHeaderExpired, but got %v", err)
    77  					}
    78  				default:
    79  					t.Fatalf("unexpected verdict: %q", input.Verdict)
    80  				}
    81  
    82  				if err == nil { // advance
    83  					trustedSignedHeader = *newSignedHeader
    84  					trustedNextVals = *input.LightBlock.NextValidatorSet
    85  				}
    86  			}
    87  		})
    88  	}
    89  }
    90  
    91  // jsonFilenames returns a list of files in jsonDir directory
    92  func jsonFilenames(t *testing.T) []string {
    93  	matches, err := filepath.Glob(filepath.Join(jsonDir, "*.json"))
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  	return matches
    98  }
    99  
   100  type testCase struct {
   101  	Description string      `json:"description"`
   102  	Initial     initialData `json:"initial"`
   103  	Input       []inputData `json:"input"`
   104  }
   105  
   106  type initialData struct {
   107  	SignedHeader     types.SignedHeader `json:"signed_header"`
   108  	NextValidatorSet types.ValidatorSet `json:"next_validator_set"`
   109  	TrustingPeriod   uint64             `json:"trusting_period"`
   110  	Now              time.Time          `json:"now"`
   111  }
   112  
   113  type inputData struct {
   114  	LightBlock lightBlockWithNextValidatorSet `json:"block"`
   115  	Now        time.Time                      `json:"now"`
   116  	Verdict    string                         `json:"verdict"`
   117  }
   118  
   119  // In tendermint-rs, NextValidatorSet is used to verify new blocks (opposite to
   120  // Go tendermint).
   121  type lightBlockWithNextValidatorSet struct {
   122  	*types.SignedHeader `json:"signed_header"`
   123  	ValidatorSet        *types.ValidatorSet `json:"validator_set"`
   124  	NextValidatorSet    *types.ValidatorSet `json:"next_validator_set"`
   125  }