github.com/iotexproject/iotex-core@v1.14.1-rc1/blockchain/block/builder.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/bloom" 12 "github.com/iotexproject/go-pkgs/crypto" 13 "github.com/iotexproject/go-pkgs/hash" 14 "github.com/pkg/errors" 15 16 "github.com/iotexproject/iotex-core/action" 17 "github.com/iotexproject/iotex-core/pkg/version" 18 ) 19 20 // Builder is used to construct Block. 21 type Builder struct{ blk Block } 22 23 // NewBuilder creates a Builder. 24 func NewBuilder(ra RunnableActions) *Builder { 25 return &Builder{ 26 blk: Block{ 27 Header: Header{ 28 version: version.ProtocolVersion, 29 txRoot: ra.txHash, 30 }, 31 Body: Body{ 32 Actions: ra.actions, 33 }, 34 }, 35 } 36 } 37 38 // SetTimestamp sets the block timestamp 39 func (b *Builder) SetTimestamp(ts time.Time) *Builder { 40 b.blk.Header.timestamp = ts 41 return b 42 } 43 44 // SetHeight sets the block height 45 func (b *Builder) SetHeight(h uint64) *Builder { 46 b.blk.Header.height = h 47 return b 48 } 49 50 // SetVersion sets the protocol version for block which is building. 51 func (b *Builder) SetVersion(v uint32) *Builder { 52 b.blk.Header.version = v 53 return b 54 } 55 56 // SetPrevBlockHash sets the previous block hash for block which is building. 57 func (b *Builder) SetPrevBlockHash(h hash.Hash256) *Builder { 58 b.blk.Header.prevBlockHash = h 59 return b 60 } 61 62 // SetDeltaStateDigest sets the new delta state digest after running actions included in this building block 63 func (b *Builder) SetDeltaStateDigest(h hash.Hash256) *Builder { 64 b.blk.Header.deltaStateDigest = h 65 return b 66 } 67 68 // SetReceipts sets the receipts after running actions included in this building block. 69 func (b *Builder) SetReceipts(receipts []*action.Receipt) *Builder { 70 b.blk.Receipts = receipts // make a shallow copy 71 return b 72 } 73 74 // SetReceiptRoot sets the receipt root after running actions included in this building block. 75 func (b *Builder) SetReceiptRoot(h hash.Hash256) *Builder { 76 b.blk.Header.receiptRoot = h 77 return b 78 } 79 80 // SetLogsBloom sets the logs bloom filter value after running actions included in this building block. 81 func (b *Builder) SetLogsBloom(f bloom.BloomFilter) *Builder { 82 b.blk.Header.logsBloom = f 83 return b 84 } 85 86 // SignAndBuild signs and then builds a block. 87 func (b *Builder) SignAndBuild(signerPrvKey crypto.PrivateKey) (Block, error) { 88 b.blk.Header.pubkey = signerPrvKey.PublicKey() 89 h := b.blk.Header.HashHeaderCore() 90 sig, err := signerPrvKey.Sign(h[:]) 91 if err != nil { 92 return Block{}, errors.New("failed to sign block") 93 } 94 b.blk.Header.blockSig = sig 95 return b.blk, nil 96 } 97 98 // GetCurrentBlockHeader returns the current hash of Block Header Core 99 func (b *Builder) GetCurrentBlockHeader() Header { 100 return b.blk.Header 101 }