github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/consensus/bcrp/bcrp_test.go (about)

     1  package bcrp
     2  
     3  import (
     4  	"encoding/hex"
     5  	"testing"
     6  )
     7  
     8  func TestIsBCRPScript(t *testing.T) {
     9  	tests := []struct {
    10  		program  string
    11  		expected bool
    12  	}{
    13  		{
    14  			program:  "",
    15  			expected: false,
    16  		},
    17  		{
    18  			program:  "ae20ac20f5cdb9ada2ae9836bcfff32126d6b885aa3f73ee111a95d1bf37f3904aca5151ad",
    19  			expected: false,
    20  		},
    21  		{
    22  			// not OP_FAIL
    23  			program:  "69046263727001012820e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c0",
    24  			expected: false,
    25  		},
    26  		{
    27  			// not bcrp
    28  			program:  "6a044243525001012820e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c0",
    29  			expected: false,
    30  		},
    31  		{
    32  			// not version 1
    33  			program:  "6a046263727001022820e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c0",
    34  			expected: false,
    35  		},
    36  		{
    37  			// p2wpkh script
    38  			program:  "001437e1aec83a4e6587ca9609e4e5aa728db7007449",
    39  			expected: false,
    40  		},
    41  		{
    42  			// p2wsh script
    43  			program:  "00200824e931fb806bd77fdcd291aad3bd0a4493443a4120062bd659e64a3e0bac66",
    44  			expected: false,
    45  		},
    46  		{
    47  			// len(contract) == 0
    48  			program:  "6a046263727001016a",
    49  			expected: false,
    50  		},
    51  		{
    52  			// len(contract) == 1
    53  			program:  "6a04626372700101016a",
    54  			expected: true,
    55  		},
    56  		{
    57  			// 1 < len(contract) < 75
    58  			program:  "6a046263727001012820e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c0",
    59  			expected: true,
    60  		},
    61  		{
    62  			// len(contract) == 75
    63  			program:  "6a046263727001014b20e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403",
    64  			expected: true,
    65  		},
    66  		{
    67  			// 75 < len(contract) < 256
    68  			program:  "6a046263727001014c9620e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403",
    69  			expected: true,
    70  		},
    71  		{
    72  			// len(contract) == 256
    73  			program:  "6a046263727001014d000120e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e",
    74  			expected: true,
    75  		},
    76  		{
    77  			// len(contract) > 256
    78  			program:  "6a046263727001014d2c0120e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403",
    79  			expected: true,
    80  		},
    81  	}
    82  
    83  	for i, test := range tests {
    84  		program, err := hex.DecodeString(test.program)
    85  		if err != nil {
    86  			t.Fatal(err)
    87  		}
    88  
    89  		expected := IsBCRPScript(program)
    90  		if expected != test.expected {
    91  			t.Errorf("TestIsTemplateRegister #%d failed: got %v want %v", i, expected, test.expected)
    92  		}
    93  	}
    94  }
    95  
    96  func TestIsCallContractScript(t *testing.T) {
    97  	tests := []struct {
    98  		program  string
    99  		expected bool
   100  	}{
   101  		{
   102  			program:  "",
   103  			expected: false,
   104  		},
   105  		{
   106  			program:  "6a4c04626372704c01014c2820e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c0",
   107  			expected: false,
   108  		},
   109  		{
   110  			program:  "00204e4f02d43bf50171f7f25d046b7f016002da410fc00d2e8902e7b170c98cf946",
   111  			expected: false,
   112  		},
   113  		{
   114  			program:  "51204e4f02d43bf50171f7f25d046b7f016002da410fc00d2e8902e7b170c98cf946",
   115  			expected: false,
   116  		},
   117  		{
   118  			// p2wpkh script
   119  			program:  "001437e1aec83a4e6587ca9609e4e5aa728db7007449",
   120  			expected: false,
   121  		},
   122  		{
   123  			// p2wsh script
   124  			program:  "00200824e931fb806bd77fdcd291aad3bd0a4493443a4120062bd659e64a3e0bac66",
   125  			expected: false,
   126  		},
   127  		{
   128  			program:  "0462637270204e4f02d43bf50171f7f25d046b7f016002da410fc00d2e8902e7b170c98cf946",
   129  			expected: true,
   130  		},
   131  	}
   132  
   133  	for i, test := range tests {
   134  		program, err := hex.DecodeString(test.program)
   135  		if err != nil {
   136  			t.Fatal(err)
   137  		}
   138  
   139  		expected := IsCallContractScript(program)
   140  		if expected != test.expected {
   141  			t.Errorf("TestIsCallContractScript #%d failed: got %v want %v", i, expected, test.expected)
   142  		}
   143  	}
   144  }
   145  
   146  func TestParseContract(t *testing.T) {
   147  	tests := []struct {
   148  		program  string
   149  		expected string
   150  	}{
   151  		{
   152  			// BCRP script format: OP_FAIL + OP_DATA_4 + "bcrp" + OP_DATA_1 + "1" + {{dynamic_op}} + contract
   153  			program:  "6a04626372700101100164740a52797b937b788791698700c0",
   154  			expected: "0164740a52797b937b788791698700c0",
   155  		},
   156  		{
   157  			// BCRP script format: OP_FAIL + OP_DATA_4 + "bcrp" + OP_DATA_1 + "1" + {{dynamic_op}} + contract
   158  			program:  "6a046263727001014d2c0120e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403",
   159  			expected: "20e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e78740320e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c020e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403",
   160  		},
   161  	}
   162  
   163  	for i, test := range tests {
   164  		program, err := hex.DecodeString(test.program)
   165  		if err != nil {
   166  			t.Fatal(err)
   167  		}
   168  
   169  		contract, err := ParseContract(program)
   170  		if err != nil {
   171  			t.Fatal(err)
   172  		}
   173  
   174  		expected := hex.EncodeToString(contract[:])
   175  		if expected != test.expected {
   176  			t.Errorf("TestParseContract #%d failed: got %v want %v", i, expected, test.expected)
   177  		}
   178  	}
   179  }
   180  
   181  func TestParseContractHash(t *testing.T) {
   182  	tests := []struct {
   183  		program  string
   184  		expected string
   185  	}{
   186  		{
   187  			// call contract script format: OP_DATA_4 + "bcrp"+ OP_DATA_32 + SHA3-256(contract)
   188  			program:  "0462637270204e4f02d43bf50171f7f25d046b7f016002da410fc00d2e8902e7b170c98cf946",
   189  			expected: "4e4f02d43bf50171f7f25d046b7f016002da410fc00d2e8902e7b170c98cf946",
   190  		},
   191  	}
   192  
   193  	for i, test := range tests {
   194  		program, err := hex.DecodeString(test.program)
   195  		if err != nil {
   196  			t.Fatal(err)
   197  		}
   198  
   199  		hash, err := ParseContractHash(program)
   200  		if err != nil {
   201  			t.Fatal(err)
   202  		}
   203  
   204  		expected := hex.EncodeToString(hash[:])
   205  		if expected != test.expected {
   206  			t.Errorf("TestParseContractHash #%d failed: got %v want %v", i, expected, test.expected)
   207  		}
   208  	}
   209  }