github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/test/block_test.go (about)

     1  // +build functional
     2  
     3  package test
     4  
     5  import (
     6  	"os"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/bytom/bytom/consensus"
    11  	dbm "github.com/bytom/bytom/database/leveldb"
    12  	"github.com/bytom/bytom/protocol/bc"
    13  	"github.com/bytom/bytom/protocol/bc/types"
    14  	"github.com/bytom/bytom/protocol/vm"
    15  )
    16  
    17  func TestBlockHeader(t *testing.T) {
    18  	db := dbm.NewDB("block_test_db", "leveldb", "block_test_db")
    19  	defer os.RemoveAll("block_test_db")
    20  	chain, _, _, _ := MockChain(db)
    21  	genesisHeader := chain.BestBlockHeader()
    22  	if err := AppendBlocks(chain, 1); err != nil {
    23  		t.Fatal(err)
    24  	}
    25  
    26  	cases := []struct {
    27  		desc       string
    28  		version    func() uint64
    29  		prevHeight func() uint64
    30  		timestamp  func() uint64
    31  		prevHash   func() *bc.Hash
    32  		valid      bool
    33  	}{
    34  		{
    35  			desc:       "block version is 1",
    36  			version:    func() uint64 { return 1 },
    37  			prevHeight: chain.BestBlockHeight,
    38  			timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
    39  			prevHash:   chain.BestBlockHash,
    40  			valid:      true,
    41  		},
    42  		{
    43  			desc:       "invalid block, misorder block height",
    44  			version:    func() uint64 { return chain.BestBlockHeader().Version },
    45  			prevHeight: func() uint64 { return chain.BestBlockHeight() + 1 },
    46  			timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
    47  			prevHash:   chain.BestBlockHash,
    48  			valid:      false,
    49  		},
    50  		{
    51  			desc:       "invalid prev hash, prev hash dismatch",
    52  			version:    func() uint64 { return chain.BestBlockHeader().Version },
    53  			prevHeight: chain.BestBlockHeight,
    54  			timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp + 1 },
    55  			prevHash:   func() *bc.Hash { hash := genesisHeader.Hash(); return &hash },
    56  			valid:      false,
    57  		},
    58  		{
    59  			desc:       "valid timestamp, greater than last block",
    60  			version:    func() uint64 { return chain.BestBlockHeader().Version },
    61  			prevHeight: chain.BestBlockHeight,
    62  			timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp + 3 },
    63  			prevHash:   chain.BestBlockHash,
    64  			valid:      true,
    65  		},
    66  		{
    67  			desc:       "valid timestamp, less than last block, but greater than median",
    68  			version:    func() uint64 { return chain.BestBlockHeader().Version },
    69  			prevHeight: chain.BestBlockHeight,
    70  			timestamp:  func() uint64 { return chain.BestBlockHeader().Timestamp - 1 },
    71  			prevHash:   chain.BestBlockHash,
    72  			valid:      true,
    73  		},
    74  		{
    75  			desc:       "invalid timestamp, less than median",
    76  			version:    func() uint64 { return chain.BestBlockHeader().Version },
    77  			prevHeight: chain.BestBlockHeight,
    78  			timestamp:  func() uint64 { return genesisHeader.Timestamp },
    79  			prevHash:   chain.BestBlockHash,
    80  			valid:      false,
    81  		},
    82  	}
    83  
    84  	for _, c := range cases {
    85  		block, err := NewBlock(chain, nil, []byte{byte(vm.OP_TRUE)})
    86  		if err != nil {
    87  			t.Fatal(err)
    88  		}
    89  
    90  		block.Version = c.version()
    91  		block.Height = c.prevHeight() + 1
    92  		block.Timestamp = c.timestamp()
    93  		block.PreviousBlockHash = *c.prevHash()
    94  		if err != nil && c.valid {
    95  			t.Fatal(err)
    96  		}
    97  
    98  		_, err = chain.ProcessBlock(block)
    99  		result := err == nil
   100  		if result != c.valid {
   101  			t.Fatalf("%s test failed, expected: %t, have: %t, err: %s", c.desc, c.valid, result, err)
   102  		}
   103  	}
   104  }
   105  
   106  func TestMaxBlockGas(t *testing.T) {
   107  	chainDB := dbm.NewDB("test_block_db", "leveldb", "test_block_db")
   108  	defer os.RemoveAll("test_block_db")
   109  	chain, _, _, err := MockChain(chainDB)
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  
   114  	if err := AppendBlocks(chain, 7); err != nil {
   115  		t.Fatal(err)
   116  	}
   117  
   118  	block, err := chain.GetBlockByHeight(1)
   119  	if err != nil {
   120  		t.Fatal(err)
   121  	}
   122  
   123  	tx, err := CreateTxFromTx(block.Transactions[0], 0, 600000000000, []byte{byte(vm.OP_TRUE)})
   124  	if err != nil {
   125  		t.Fatal(err)
   126  	}
   127  
   128  	outputAmount := uint64(600000000000)
   129  	txs := []*types.Tx{tx}
   130  	for i := 1; i < 50000; i++ {
   131  		outputAmount -= 10000000
   132  		tx, err := CreateTxFromTx(txs[i-1], 0, outputAmount, []byte{byte(vm.OP_TRUE)})
   133  		if err != nil {
   134  			t.Fatal(err)
   135  		}
   136  		txs = append(txs, tx)
   137  	}
   138  
   139  	block, err = NewBlock(chain, txs, []byte{byte(vm.OP_TRUE)})
   140  	if err != nil {
   141  		t.Fatal(err)
   142  	}
   143  
   144  	if _, err := chain.ProcessBlock(block); err == nil {
   145  		t.Fatalf("test max block gas failed")
   146  	}
   147  }