github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/extrinsic_status_test.go (about)

     1  // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
     2  //
     3  // Copyright 2020 Stafi Protocol
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package types_test
    18  
    19  import (
    20  	"encoding/json"
    21  	"testing"
    22  
    23  	. "github.com/stafiprotocol/go-substrate-rpc-client/types"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  var testExtrinsicStatus0 = ExtrinsicStatus{IsFuture: true}
    28  var testExtrinsicStatus1 = ExtrinsicStatus{IsReady: true}
    29  var testExtrinsicStatus2 = ExtrinsicStatus{IsBroadcast: true, AsBroadcast: []Text{"This", "is", "broadcast"}}
    30  var testExtrinsicStatus3 = ExtrinsicStatus{IsInBlock: true, AsInBlock: NewHash([]byte{0xaa})}
    31  var testExtrinsicStatus4 = ExtrinsicStatus{IsRetracted: true, AsRetracted: NewHash([]byte{0xbb})}
    32  var testExtrinsicStatus5 = ExtrinsicStatus{IsFinalityTimeout: true, AsFinalityTimeout: NewHash([]byte{0xcc})}
    33  var testExtrinsicStatus6 = ExtrinsicStatus{IsFinalized: true, AsFinalized: NewHash([]byte{0xdd})}
    34  var testExtrinsicStatus7 = ExtrinsicStatus{IsUsurped: true, AsUsurped: NewHash([]byte{0xee})}
    35  var testExtrinsicStatus8 = ExtrinsicStatus{IsDropped: true}
    36  var testExtrinsicStatus9 = ExtrinsicStatus{IsInvalid: true}
    37  
    38  func TestExtrinsicStatus_EncodeDecode(t *testing.T) {
    39  	assertRoundtrip(t, testExtrinsicStatus0)
    40  	assertRoundtrip(t, testExtrinsicStatus1)
    41  	assertRoundtrip(t, testExtrinsicStatus2)
    42  	assertRoundtrip(t, testExtrinsicStatus3)
    43  	assertRoundtrip(t, testExtrinsicStatus4)
    44  	assertRoundtrip(t, testExtrinsicStatus5)
    45  	assertRoundtrip(t, testExtrinsicStatus6)
    46  	assertRoundtrip(t, testExtrinsicStatus7)
    47  	assertRoundtrip(t, testExtrinsicStatus8)
    48  	assertRoundtrip(t, testExtrinsicStatus9)
    49  }
    50  
    51  func TestExtrinsicStatus_Encode(t *testing.T) {
    52  	assertEncode(t, []encodingAssert{
    53  		{testExtrinsicStatus0, []byte{0x00}},
    54  		{testExtrinsicStatus1, []byte{0x01}},
    55  		{testExtrinsicStatus2, MustHexDecodeString("0x020c10546869730869732462726f616463617374")},
    56  		{testExtrinsicStatus3, MustHexDecodeString("0x03aa00000000000000000000000000000000000000000000000000000000000000")}, //nolint:lll
    57  		{testExtrinsicStatus4, MustHexDecodeString("0x04bb00000000000000000000000000000000000000000000000000000000000000")}, //nolint:lll
    58  		{testExtrinsicStatus5, MustHexDecodeString("0x05cc00000000000000000000000000000000000000000000000000000000000000")}, //nolint:lll
    59  		{testExtrinsicStatus6, MustHexDecodeString("0x06dd00000000000000000000000000000000000000000000000000000000000000")}, //nolint:lll
    60  		{testExtrinsicStatus7, MustHexDecodeString("0x07ee00000000000000000000000000000000000000000000000000000000000000")}, //nolint:lll
    61  		{testExtrinsicStatus8, []byte{0x08}},
    62  		{testExtrinsicStatus9, []byte{0x09}},
    63  	})
    64  }
    65  
    66  func TestExtrinsicStatus_Decode(t *testing.T) {
    67  	assertDecode(t, []decodingAssert{
    68  		{[]byte{0x00}, testExtrinsicStatus0},
    69  		{[]byte{0x01}, testExtrinsicStatus1},
    70  		{MustHexDecodeString("0x020c10546869730869732462726f616463617374"), testExtrinsicStatus2},
    71  		{MustHexDecodeString("0x03aa00000000000000000000000000000000000000000000000000000000000000"), testExtrinsicStatus3}, //nolint:lll
    72  		{MustHexDecodeString("0x04bb00000000000000000000000000000000000000000000000000000000000000"), testExtrinsicStatus4}, //nolint:lll
    73  		{MustHexDecodeString("0x05cc00000000000000000000000000000000000000000000000000000000000000"), testExtrinsicStatus5}, //nolint:lll
    74  		{MustHexDecodeString("0x06dd00000000000000000000000000000000000000000000000000000000000000"), testExtrinsicStatus6}, //nolint:lll
    75  		{MustHexDecodeString("0x07ee00000000000000000000000000000000000000000000000000000000000000"), testExtrinsicStatus7}, //nolint:lll
    76  		{[]byte{0x08}, testExtrinsicStatus8},
    77  		{[]byte{0x09}, testExtrinsicStatus9},
    78  	})
    79  }
    80  
    81  var testExtrinsicStatusTestCases = []struct {
    82  	encoded []byte
    83  	decoded ExtrinsicStatus
    84  }{
    85  	{
    86  		[]byte("\"future\""),
    87  		ExtrinsicStatus{IsFuture: true},
    88  	}, {
    89  		[]byte("\"ready\""),
    90  		ExtrinsicStatus{IsReady: true},
    91  	}, {
    92  		[]byte("{\"broadcast\":[\"hello\",\"world\"]}"),
    93  		ExtrinsicStatus{IsBroadcast: true, AsBroadcast: []Text{"hello", "world"}},
    94  	}, {
    95  		[]byte("{\"inBlock\":\"0x95e3b7f86541d06306691a2fe8cbd935d0bdd28ea14fe515e2db0fa87847f8f8\"}"),
    96  		ExtrinsicStatus{IsInBlock: true, AsInBlock: NewHash(MustHexDecodeString(
    97  			"0x95e3b7f86541d06306691a2fe8cbd935d0bdd28ea14fe515e2db0fa87847f8f8"))},
    98  	}, {
    99  		[]byte("{\"retracted\":\"0x95e3b7f86541d06306691a2fe8cbd935d0bdd28ea14fe515e2db0fa87847f8f8\"}"),
   100  		ExtrinsicStatus{IsRetracted: true, AsRetracted: NewHash(MustHexDecodeString(
   101  			"0x95e3b7f86541d06306691a2fe8cbd935d0bdd28ea14fe515e2db0fa87847f8f8"))},
   102  	}, {
   103  		[]byte("{\"finalityTimeout\":\"0x95e3b7f86541d06306691a2fe8cbd935d0bdd28ea14fe515e2db0fa87847f8f8\"}"),
   104  		ExtrinsicStatus{IsFinalityTimeout: true, AsFinalityTimeout: NewHash(MustHexDecodeString(
   105  			"0x95e3b7f86541d06306691a2fe8cbd935d0bdd28ea14fe515e2db0fa87847f8f8"))},
   106  	}, {
   107  		[]byte("{\"finalized\":\"0x95e3b7f86541d06306691a2fe8cbd935d0bdd28ea14fe515e2db0fa87847f8f8\"}"),
   108  		ExtrinsicStatus{IsFinalized: true, AsFinalized: NewHash(MustHexDecodeString(
   109  			"0x95e3b7f86541d06306691a2fe8cbd935d0bdd28ea14fe515e2db0fa87847f8f8"))},
   110  	}, {
   111  		[]byte("{\"usurped\":\"0x95e3b7f86541d06306691a2fe8cbd935d0bdd28ea14fe515e2db0fa87847f8ab\"}"),
   112  		ExtrinsicStatus{IsUsurped: true, AsUsurped: NewHash(MustHexDecodeString(
   113  			"0x95e3b7f86541d06306691a2fe8cbd935d0bdd28ea14fe515e2db0fa87847f8ab"))},
   114  	}, {
   115  		[]byte("\"dropped\""),
   116  		ExtrinsicStatus{IsDropped: true},
   117  	}, {
   118  		[]byte("\"invalid\""),
   119  		ExtrinsicStatus{IsInvalid: true},
   120  	},
   121  }
   122  
   123  func TestExtrinsicStatus_UnmarshalJSON(t *testing.T) {
   124  	for _, test := range testExtrinsicStatusTestCases {
   125  		var actual ExtrinsicStatus
   126  		err := json.Unmarshal(test.encoded, &actual)
   127  		assert.NoError(t, err)
   128  		assert.Equal(t, test.decoded, actual)
   129  	}
   130  }
   131  
   132  func TestExtrinsicStatus_MarshalJSON(t *testing.T) {
   133  	for _, test := range testExtrinsicStatusTestCases {
   134  		actual, err := json.Marshal(test.decoded)
   135  		assert.NoError(t, err)
   136  		assert.Equal(t, test.encoded, actual)
   137  	}
   138  }