github.com/turingchain2020/turingchain@v1.1.21/types/block_test.go (about) 1 package types 2 3 import ( 4 "encoding/hex" 5 "strings" 6 "testing" 7 8 "github.com/turingchain2020/turingchain/common/address" 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestBlock(t *testing.T) { 14 cfg := NewTuringchainConfig(GetDefaultCfgstring()) 15 b := &Block{} 16 assert.Equal(t, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", hex.EncodeToString(b.Hash(cfg))) 17 assert.Equal(t, b.HashOld(), b.HashNew()) 18 assert.Equal(t, b.HashOld(), b.Hash(cfg)) 19 b.Height = 10 20 b.Difficulty = 1 21 assert.NotEqual(t, b.HashOld(), b.HashNew()) 22 assert.NotEqual(t, b.HashOld(), b.HashNew()) 23 assert.Equal(t, b.HashNew(), b.HashByForkHeight(10)) 24 assert.Equal(t, b.HashOld(), b.HashByForkHeight(11)) 25 assert.Equal(t, true, b.CheckSign(cfg)) 26 27 b.Txs = append(b.Txs, &Transaction{}) 28 assert.Equal(t, false, b.CheckSign(cfg)) 29 b.Txs = append(b.Txs, &Transaction{}) 30 b.Txs = append(b.Txs, &Transaction{}) 31 b.Txs = append(b.Txs, &Transaction{}) 32 b.Txs = append(b.Txs, &Transaction{}) 33 assert.Equal(t, false, b.CheckSign(cfg)) 34 assert.False(t, VerifySignature(cfg, b, b.Txs[:2])) 35 assert.True(t, VerifySignature(cfg, b, b.Txs[:0])) 36 } 37 38 func TestFilterParaTxsByTitle(t *testing.T) { 39 cfg := NewTuringchainConfig(GetDefaultCfgstring()) 40 to := "14KEKbYtKKQm4wMthSK9J4La4nAiidGozt" 41 42 //构造一个主链交易 43 maintx := &Transaction{Execer: []byte("coins"), Payload: []byte("none")} 44 maintx.To = to 45 maintx, err := FormatTx(cfg, "coins", maintx) 46 require.NoError(t, err) 47 48 //构造一个平行链交易 49 execer := "user.p.hyb.none" 50 paratx := &Transaction{Execer: []byte(execer), Payload: []byte("none")} 51 paratx.To = address.ExecAddress(execer) 52 paratx, err = FormatTx(cfg, execer, paratx) 53 require.NoError(t, err) 54 55 //构造一个平行链交易组 56 execer1 := "user.p.hyb.coins" 57 tx1 := &Transaction{Execer: []byte(execer1), Payload: []byte("none")} 58 tx1.To = address.ExecAddress(execer1) 59 tx1, err = FormatTx(cfg, execer1, tx1) 60 require.NoError(t, err) 61 62 execer2 := "user.p.hyb.token" 63 tx2 := &Transaction{Execer: []byte(execer2), Payload: []byte("none")} 64 tx2.To = address.ExecAddress(execer2) 65 tx2, err = FormatTx(cfg, execer2, tx2) 66 require.NoError(t, err) 67 68 execer3 := "user.p.hyb.trade" 69 tx3 := &Transaction{Execer: []byte(execer3), Payload: []byte("none")} 70 tx3.To = address.ExecAddress(execer3) 71 tx3, err = FormatTx(cfg, execer3, tx3) 72 require.NoError(t, err) 73 74 var txs Transactions 75 txs.Txs = append(txs.Txs, tx1) 76 txs.Txs = append(txs.Txs, tx2) 77 txs.Txs = append(txs.Txs, tx3) 78 feeRate := cfg.GetMinTxFeeRate() 79 group, err := CreateTxGroup(txs.Txs, feeRate) 80 require.NoError(t, err) 81 82 //构造一个有平行链交易的区块 83 block := &Block{} 84 block.Version = 0 85 block.Height = 0 86 block.BlockTime = 1 87 block.Difficulty = 1 88 block.Txs = append(block.Txs, maintx) 89 block.Txs = append(block.Txs, paratx) 90 block.Txs = append(block.Txs, group.Txs...) 91 92 blockdetal := &BlockDetail{} 93 blockdetal.Block = block 94 95 maintxreceipt := &ReceiptData{Ty: ExecOk} 96 paratxreceipt := &ReceiptData{Ty: ExecPack} 97 grouppara1receipt := &ReceiptData{Ty: ExecPack} 98 grouppara2receipt := &ReceiptData{Ty: ExecPack} 99 grouppara3receipt := &ReceiptData{Ty: ExecPack} 100 101 blockdetal.Receipts = append(blockdetal.Receipts, maintxreceipt) 102 blockdetal.Receipts = append(blockdetal.Receipts, paratxreceipt) 103 blockdetal.Receipts = append(blockdetal.Receipts, grouppara1receipt) 104 blockdetal.Receipts = append(blockdetal.Receipts, grouppara2receipt) 105 blockdetal.Receipts = append(blockdetal.Receipts, grouppara3receipt) 106 107 txDetail := blockdetal.FilterParaTxsByTitle(cfg, "user.p.hyb.") 108 for _, tx := range txDetail.TxDetails { 109 if tx != nil { 110 execer := string(tx.Tx.Execer) 111 if !strings.HasPrefix(execer, "user.p.hyb.") && tx.Tx.GetGroupCount() != 0 { 112 assert.Equal(t, tx.Receipt.Ty, int32(ExecOk)) 113 } else { 114 assert.Equal(t, tx.Receipt.Ty, int32(ExecPack)) 115 } 116 } 117 } 118 }