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