github.com/iotexproject/iotex-core@v1.14.1-rc1/blockchain/block/testing.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package block
     7  
     8  import (
     9  	"time"
    10  
    11  	"github.com/iotexproject/go-pkgs/crypto"
    12  	"github.com/iotexproject/go-pkgs/hash"
    13  	"github.com/pkg/errors"
    14  	"go.uber.org/zap"
    15  
    16  	"github.com/iotexproject/iotex-core/action"
    17  	"github.com/iotexproject/iotex-core/pkg/log"
    18  	"github.com/iotexproject/iotex-core/pkg/version"
    19  )
    20  
    21  // TestingBuilder is used to construct Block.
    22  type TestingBuilder struct{ blk Block }
    23  
    24  // NewTestingBuilder creates a Builder.
    25  func NewTestingBuilder() *TestingBuilder {
    26  	return &TestingBuilder{
    27  		blk: Block{
    28  			Header: Header{
    29  				version: version.ProtocolVersion,
    30  			},
    31  		},
    32  	}
    33  }
    34  
    35  // SetVersion sets the protocol version for block which is building.
    36  func (b *TestingBuilder) SetVersion(v uint32) *TestingBuilder {
    37  	b.blk.Header.version = v
    38  	return b
    39  }
    40  
    41  // SetHeight sets the block height for block which is building.
    42  func (b *TestingBuilder) SetHeight(h uint64) *TestingBuilder {
    43  	b.blk.Header.height = h
    44  	return b
    45  }
    46  
    47  // SetTimeStamp sets the time stamp for block which is building.
    48  func (b *TestingBuilder) SetTimeStamp(ts time.Time) *TestingBuilder {
    49  	b.blk.Header.timestamp = ts
    50  	return b
    51  }
    52  
    53  // SetPrevBlockHash sets the previous block hash for block which is building.
    54  func (b *TestingBuilder) SetPrevBlockHash(h hash.Hash256) *TestingBuilder {
    55  	b.blk.Header.prevBlockHash = h
    56  	return b
    57  }
    58  
    59  // AddActions adds actions for block which is building.
    60  func (b *TestingBuilder) AddActions(acts ...*action.SealedEnvelope) *TestingBuilder {
    61  	if b.blk.Actions == nil {
    62  		b.blk.Actions = make([]*action.SealedEnvelope, 0)
    63  	}
    64  	b.blk.Actions = append(b.blk.Actions, acts...)
    65  	return b
    66  }
    67  
    68  // SetReceipts sets the receipts after running actions included in this building block.
    69  func (b *TestingBuilder) SetReceipts(receipts []*action.Receipt) *TestingBuilder {
    70  	b.blk.Receipts = receipts // make a shallow copy
    71  	return b
    72  }
    73  
    74  // SignAndBuild signs and then builds a block.
    75  func (b *TestingBuilder) SignAndBuild(signerPrvKey crypto.PrivateKey) (Block, error) {
    76  	var err error
    77  	b.blk.Header.txRoot, err = b.blk.CalculateTxRoot()
    78  	if err != nil {
    79  		log.L().Debug("error in getting hash", zap.Error(err))
    80  		return Block{}, errors.New("failed to get hash")
    81  	}
    82  	b.blk.Header.pubkey = signerPrvKey.PublicKey()
    83  	h := b.blk.Header.HashHeaderCore()
    84  	sig, err := signerPrvKey.Sign(h[:])
    85  	if err != nil {
    86  		log.L().Debug("error in getting hash", zap.Error(err))
    87  		return Block{}, errors.New("failed to sign block")
    88  	}
    89  	b.blk.Header.blockSig = sig
    90  	return b.blk, nil
    91  }
    92  
    93  // NewBlockDeprecated returns a new block
    94  // This method is deprecated. Only used in old tests.
    95  func NewBlockDeprecated(
    96  	chainID uint32,
    97  	height uint64,
    98  	prevBlockHash hash.Hash256,
    99  	timestamp time.Time,
   100  	producer crypto.PublicKey,
   101  	actions []*action.SealedEnvelope,
   102  ) *Block {
   103  	block := &Block{
   104  		Header: Header{
   105  			version:       version.ProtocolVersion,
   106  			height:        height,
   107  			timestamp:     timestamp,
   108  			prevBlockHash: prevBlockHash,
   109  			pubkey:        producer,
   110  			txRoot:        hash.ZeroHash256,
   111  			receiptRoot:   hash.ZeroHash256,
   112  		},
   113  		Body: Body{
   114  			Actions: actions,
   115  		},
   116  	}
   117  
   118  	var err error
   119  	block.Header.txRoot, err = block.CalculateTxRoot()
   120  	if err != nil {
   121  		return &Block{}
   122  	}
   123  	return block
   124  }