github.com/palcoin-project/palcd@v1.0.0/rpcclient/chain_test.go (about) 1 package rpcclient 2 3 import "testing" 4 5 // TestUnmarshalGetBlockChainInfoResult ensures that the SoftForks and 6 // UnifiedSoftForks fields of GetBlockChainInfoResult are properly unmarshaled 7 // when using the expected backend version. 8 func TestUnmarshalGetBlockChainInfoResultSoftForks(t *testing.T) { 9 t.Parallel() 10 11 tests := []struct { 12 name string 13 version BackendVersion 14 res []byte 15 compatible bool 16 }{ 17 { 18 name: "bitcoind < 0.19.0 with separate softforks", 19 version: BitcoindPre19, 20 res: []byte(`{"softforks": [{"version": 2}]}`), 21 compatible: true, 22 }, 23 { 24 name: "bitcoind >= 0.19.0 with separate softforks", 25 version: BitcoindPost19, 26 res: []byte(`{"softforks": [{"version": 2}]}`), 27 compatible: false, 28 }, 29 { 30 name: "bitcoind < 0.19.0 with unified softforks", 31 version: BitcoindPre19, 32 res: []byte(`{"softforks": {"segwit": {"type": "bip9"}}}`), 33 compatible: false, 34 }, 35 { 36 name: "bitcoind >= 0.19.0 with unified softforks", 37 version: BitcoindPost19, 38 res: []byte(`{"softforks": {"segwit": {"type": "bip9"}}}`), 39 compatible: true, 40 }, 41 } 42 43 for _, test := range tests { 44 success := t.Run(test.name, func(t *testing.T) { 45 // We'll start by unmarshaling the JSON into a struct. 46 // The SoftForks and UnifiedSoftForks field should not 47 // be set yet, as they are unmarshaled within a 48 // different function. 49 info, err := unmarshalPartialGetBlockChainInfoResult(test.res) 50 if err != nil { 51 t.Fatal(err) 52 } 53 if info.SoftForks != nil { 54 t.Fatal("expected SoftForks to be empty") 55 } 56 if info.UnifiedSoftForks != nil { 57 t.Fatal("expected UnifiedSoftForks to be empty") 58 } 59 60 // Proceed to unmarshal the softforks of the response 61 // with the expected version. If the version is 62 // incompatible with the response, then this should 63 // fail. 64 err = unmarshalGetBlockChainInfoResultSoftForks( 65 info, test.version, test.res, 66 ) 67 if test.compatible && err != nil { 68 t.Fatalf("unable to unmarshal softforks: %v", err) 69 } 70 if !test.compatible && err == nil { 71 t.Fatal("expected to not unmarshal softforks") 72 } 73 if !test.compatible { 74 return 75 } 76 77 // If the version is compatible with the response, we 78 // should expect to see the proper softforks field set. 79 if test.version == BitcoindPost19 && 80 info.SoftForks != nil { 81 t.Fatal("expected SoftForks to be empty") 82 } 83 if test.version == BitcoindPre19 && 84 info.UnifiedSoftForks != nil { 85 t.Fatal("expected UnifiedSoftForks to be empty") 86 } 87 }) 88 if !success { 89 return 90 } 91 } 92 }