github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/test/integration/standard_transaction_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/bytom/bytom/account"
    10  	"github.com/bytom/bytom/blockchain/pseudohsm"
    11  	"github.com/bytom/bytom/blockchain/signers"
    12  	"github.com/bytom/bytom/crypto/ed25519/chainkd"
    13  	"github.com/bytom/bytom/protocol/bc/types"
    14  	"github.com/bytom/bytom/protocol/validation"
    15  	"github.com/bytom/bytom/test"
    16  	dbm "github.com/bytom/bytom/database/leveldb"
    17  )
    18  
    19  func TestP2PKH(t *testing.T) {
    20  	dirPath, err := ioutil.TempDir(".", "TestP2PKH")
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	defer os.RemoveAll(dirPath)
    25  
    26  	testDB := dbm.NewDB("testdb", "leveldb", "temp")
    27  	defer os.RemoveAll("temp")
    28  
    29  	chain, _, _, err := test.MockChain(testDB)
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  
    34  	accountManager := account.NewManager(testDB, chain)
    35  	hsm, err := pseudohsm.New(dirPath)
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  
    40  	xpub, _, err := hsm.XCreate("TestP2PKH", "password", "en")
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	testAccount, err := accountManager.Create([]chainkd.XPub{xpub.XPub}, 1, "testAccount", signers.BIP0044)
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	utxo := test.MockUTXO(controlProg)
    55  	tpl, tx, err := test.MockTx(utxo, testAccount)
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	if _, err := test.MockSign(tpl, hsm, "password"); err != nil {
    61  		t.Fatal(err)
    62  	}
    63  
    64  	tx.SerializedSize = 1
    65  	if _, err = validation.ValidateTx(types.MapTx(tx), test.MockBlock()); err != nil {
    66  		t.Fatal(err)
    67  	}
    68  }
    69  
    70  func TestBip0032P2PKH(t *testing.T) {
    71  	dirPath, err := ioutil.TempDir(".", "TestP2PKH")
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  	defer os.RemoveAll(dirPath)
    76  
    77  	testDB := dbm.NewDB("testdb", "leveldb", "temp")
    78  	defer os.RemoveAll("temp")
    79  
    80  	chain, _, _, err := test.MockChain(testDB)
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  
    85  	accountManager := account.NewManager(testDB, chain)
    86  	hsm, err := pseudohsm.New(dirPath)
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  
    91  	xpub, _, err := hsm.XCreate("TestP2PKH", "password", "en")
    92  	if err != nil {
    93  		t.Fatal(err)
    94  	}
    95  
    96  	testAccount, err := accountManager.Create([]chainkd.XPub{xpub.XPub}, 1, "testAccount1", signers.BIP0044)
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  
   101  	signer, err := signers.Create("account", []chainkd.XPub{xpub.XPub}, 1, 2, signers.BIP0032)
   102  	if err != nil {
   103  		t.Fatal(err)
   104  	}
   105  
   106  	testAccount.Signer = signer
   107  	rawAccount, err := json.Marshal(testAccount)
   108  	if err != nil {
   109  		t.Fatal(err)
   110  	}
   111  
   112  	testDB.Set(account.Key(testAccount.ID), rawAccount)
   113  	controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  
   118  	utxo := test.MockUTXO(controlProg)
   119  	tpl, tx, err := test.MockTx(utxo, testAccount)
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  
   124  	if _, err := test.MockSign(tpl, hsm, "password"); err != nil {
   125  		t.Fatal(err)
   126  	}
   127  
   128  	tx.SerializedSize = 1
   129  	if _, err = validation.ValidateTx(types.MapTx(tx), test.MockBlock()); err != nil {
   130  		t.Fatal(err)
   131  	}
   132  }
   133  
   134  func TestP2SH(t *testing.T) {
   135  	dirPath, err := ioutil.TempDir(".", "TestP2SH")
   136  	if err != nil {
   137  		t.Fatal(err)
   138  	}
   139  	defer os.RemoveAll(dirPath)
   140  
   141  	testDB := dbm.NewDB("testdb", "leveldb", "temp")
   142  	defer os.RemoveAll("temp")
   143  
   144  	chain, _, _, err := test.MockChain(testDB)
   145  	if err != nil {
   146  		t.Fatal(err)
   147  	}
   148  
   149  	accountManager := account.NewManager(testDB, chain)
   150  	hsm, err := pseudohsm.New(dirPath)
   151  	if err != nil {
   152  		t.Fatal(err)
   153  	}
   154  
   155  	xpub1, _, err := hsm.XCreate("TestP2SH1", "password", "en")
   156  	if err != nil {
   157  		t.Fatal(err)
   158  	}
   159  
   160  	xpub2, _, err := hsm.XCreate("TestP2SH2", "password", "en")
   161  	if err != nil {
   162  		t.Fatal(err)
   163  	}
   164  
   165  	testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, "testAccount", signers.BIP0044)
   166  	if err != nil {
   167  		t.Fatal(err)
   168  	}
   169  
   170  	controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
   171  	if err != nil {
   172  		t.Fatal(err)
   173  	}
   174  
   175  	utxo := test.MockUTXO(controlProg)
   176  	tpl, tx, err := test.MockTx(utxo, testAccount)
   177  	if err != nil {
   178  		t.Fatal(err)
   179  	}
   180  
   181  	// the number of sign transaction is equal to the count of xpub for account
   182  	if _, err := test.MockSign(tpl, hsm, "password"); err != nil {
   183  		t.Fatal(err)
   184  	}
   185  	if _, err := test.MockSign(tpl, hsm, "password"); err != nil {
   186  		t.Fatal(err)
   187  	}
   188  
   189  	tx.SerializedSize = 1
   190  	if _, err = validation.ValidateTx(types.MapTx(tx), test.MockBlock()); err != nil {
   191  		t.Fatal(err)
   192  	}
   193  }
   194  
   195  func TestBip0032P2SH(t *testing.T) {
   196  	dirPath, err := ioutil.TempDir(".", "TestP2SH")
   197  	if err != nil {
   198  		t.Fatal(err)
   199  	}
   200  	defer os.RemoveAll(dirPath)
   201  
   202  	testDB := dbm.NewDB("testdb", "leveldb", "temp")
   203  	defer os.RemoveAll("temp")
   204  
   205  	chain, _, _, err := test.MockChain(testDB)
   206  	if err != nil {
   207  		t.Fatal(err)
   208  	}
   209  
   210  	accountManager := account.NewManager(testDB, chain)
   211  	hsm, err := pseudohsm.New(dirPath)
   212  	if err != nil {
   213  		t.Fatal(err)
   214  	}
   215  
   216  	xpub1, _, err := hsm.XCreate("TestP2SH1", "password", "en")
   217  	if err != nil {
   218  		t.Fatal(err)
   219  	}
   220  
   221  	xpub2, _, err := hsm.XCreate("TestP2SH2", "password", "en")
   222  	if err != nil {
   223  		t.Fatal(err)
   224  	}
   225  
   226  	testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, "testAccount", signers.BIP0044)
   227  	if err != nil {
   228  		t.Fatal(err)
   229  	}
   230  	signer, err := signers.Create("account", []chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, 2, signers.BIP0032)
   231  	if err != nil {
   232  		t.Fatal(err)
   233  	}
   234  
   235  	testAccount.Signer = signer
   236  	rawAccount, err := json.Marshal(testAccount)
   237  	if err != nil {
   238  		t.Fatal(err)
   239  	}
   240  
   241  	testDB.Set(account.Key(testAccount.ID), rawAccount)
   242  
   243  	controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
   244  	if err != nil {
   245  		t.Fatal(err)
   246  	}
   247  
   248  	utxo := test.MockUTXO(controlProg)
   249  	tpl, tx, err := test.MockTx(utxo, testAccount)
   250  	if err != nil {
   251  		t.Fatal(err)
   252  	}
   253  
   254  	// the number of sign transaction is equal to the count of xpub for account
   255  	if _, err := test.MockSign(tpl, hsm, "password"); err != nil {
   256  		t.Fatal(err)
   257  	}
   258  	if _, err := test.MockSign(tpl, hsm, "password"); err != nil {
   259  		t.Fatal(err)
   260  	}
   261  
   262  	tx.SerializedSize = 1
   263  	if _, err = validation.ValidateTx(types.MapTx(tx), test.MockBlock()); err != nil {
   264  		t.Fatal(err)
   265  	}
   266  }
   267  
   268  func TestMutilNodeSign(t *testing.T) {
   269  	dirPath, err := ioutil.TempDir(".", "TestMutilNodeSign")
   270  	if err != nil {
   271  		t.Fatal(err)
   272  	}
   273  	defer os.RemoveAll(dirPath)
   274  
   275  	testDB := dbm.NewDB("testdb", "leveldb", "temp")
   276  	defer os.RemoveAll("temp")
   277  
   278  	chain, _, _, err := test.MockChain(testDB)
   279  	if err != nil {
   280  		t.Fatal(err)
   281  	}
   282  
   283  	accountManager := account.NewManager(testDB, chain)
   284  	hsm, err := pseudohsm.New(dirPath)
   285  	if err != nil {
   286  		t.Fatal(err)
   287  	}
   288  
   289  	xpub1, _, err := hsm.XCreate("TestMutilNodeSign1", "password1", "en")
   290  	if err != nil {
   291  		t.Fatal(err)
   292  	}
   293  
   294  	xpub2, _, err := hsm.XCreate("TestMutilNodeSign2", "password2", "en")
   295  	if err != nil {
   296  		t.Fatal(err)
   297  	}
   298  
   299  	testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, "testAccount", signers.BIP0044)
   300  	if err != nil {
   301  		t.Fatal(err)
   302  	}
   303  
   304  	controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
   305  	if err != nil {
   306  		t.Fatal(err)
   307  	}
   308  
   309  	utxo := test.MockUTXO(controlProg)
   310  	tpl, tx, err := test.MockTx(utxo, testAccount)
   311  	if err != nil {
   312  		t.Fatal(err)
   313  	}
   314  
   315  	if finishSign, err := test.MockSign(tpl, hsm, "password"); err != nil {
   316  		t.Fatal(err)
   317  	} else if finishSign == true {
   318  		t.Fatal("sign progress is finish, but either xpub1 nor xpub2 is signed")
   319  	}
   320  
   321  	if finishSign, err := test.MockSign(tpl, hsm, "password1"); err != nil {
   322  		t.Fatal(err)
   323  	} else if finishSign == true {
   324  		t.Fatal("sign progress is finish, but xpub2 is not signed")
   325  	}
   326  
   327  	if finishSign, err := test.MockSign(tpl, hsm, "password2"); err != nil {
   328  		t.Fatal(err)
   329  	} else if finishSign == false {
   330  		t.Fatal("sign progress is not finish,  but both xpub1 and xpub2 is signed")
   331  	}
   332  
   333  	tx.SerializedSize = 1
   334  	if _, err = validation.ValidateTx(types.MapTx(tx), test.MockBlock()); err != nil {
   335  		t.Fatal(err)
   336  	}
   337  }
   338  
   339  func TestBip0032MutilNodeSign(t *testing.T) {
   340  	dirPath, err := ioutil.TempDir(".", "TestMutilNodeSign")
   341  	if err != nil {
   342  		t.Fatal(err)
   343  	}
   344  	defer os.RemoveAll(dirPath)
   345  
   346  	testDB := dbm.NewDB("testdb", "leveldb", "temp")
   347  	defer os.RemoveAll("temp")
   348  
   349  	chain, _, _, err := test.MockChain(testDB)
   350  	if err != nil {
   351  		t.Fatal(err)
   352  	}
   353  
   354  	accountManager := account.NewManager(testDB, chain)
   355  	hsm, err := pseudohsm.New(dirPath)
   356  	if err != nil {
   357  		t.Fatal(err)
   358  	}
   359  
   360  	xpub1, _, err := hsm.XCreate("TestMutilNodeSign1", "password1", "en")
   361  	if err != nil {
   362  		t.Fatal(err)
   363  	}
   364  
   365  	xpub2, _, err := hsm.XCreate("TestMutilNodeSign2", "password2", "en")
   366  	if err != nil {
   367  		t.Fatal(err)
   368  	}
   369  
   370  	testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, "testAccount", signers.BIP0044)
   371  	if err != nil {
   372  		t.Fatal(err)
   373  	}
   374  
   375  	signer, err := signers.Create("account", []chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, 2, signers.BIP0032)
   376  	if err != nil {
   377  		t.Fatal(err)
   378  	}
   379  
   380  	testAccount.Signer = signer
   381  	rawAccount, err := json.Marshal(testAccount)
   382  	if err != nil {
   383  		t.Fatal(err)
   384  	}
   385  
   386  	testDB.Set(account.Key(testAccount.ID), rawAccount)
   387  
   388  	controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
   389  	if err != nil {
   390  		t.Fatal(err)
   391  	}
   392  
   393  	utxo := test.MockUTXO(controlProg)
   394  	tpl, tx, err := test.MockTx(utxo, testAccount)
   395  	if err != nil {
   396  		t.Fatal(err)
   397  	}
   398  
   399  	if finishSign, err := test.MockSign(tpl, hsm, "password"); err != nil {
   400  		t.Fatal(err)
   401  	} else if finishSign == true {
   402  		t.Fatal("sign progress is finish, but either xpub1 nor xpub2 is signed")
   403  	}
   404  
   405  	if finishSign, err := test.MockSign(tpl, hsm, "password1"); err != nil {
   406  		t.Fatal(err)
   407  	} else if finishSign == true {
   408  		t.Fatal("sign progress is finish, but xpub2 is not signed")
   409  	}
   410  
   411  	if finishSign, err := test.MockSign(tpl, hsm, "password2"); err != nil {
   412  		t.Fatal(err)
   413  	} else if finishSign == false {
   414  		t.Fatal("sign progress is not finish,  but both xpub1 and xpub2 is signed")
   415  	}
   416  
   417  	tx.SerializedSize = 1
   418  	if _, err = validation.ValidateTx(types.MapTx(tx), test.MockBlock()); err != nil {
   419  		t.Fatal(err)
   420  	}
   421  }