forked from iotexproject/iotex-core
/
builder.go
84 lines (71 loc) · 2.55 KB
/
builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) 2018 IoTeX
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package block
import (
"bytes"
"github.com/pkg/errors"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/pkg/hash"
"github.com/iotexproject/iotex-core/pkg/keypair"
"github.com/iotexproject/iotex-core/pkg/version"
)
// Builder is used to construct Block.
type Builder struct{ blk Block }
// NewBuilder creates a Builder.
func NewBuilder(ra RunnableActions) *Builder {
return &Builder{
blk: Block{
Header: Header{
version: version.ProtocolVersion,
height: ra.blockHeight,
timestamp: ra.blockTimeStamp,
txRoot: ra.txHash,
pubkey: ra.blockProducerPubKey,
},
Body: Body{
Actions: ra.actions,
},
},
}
}
// SetVersion sets the protocol version for block which is building.
func (b *Builder) SetVersion(v uint32) *Builder {
b.blk.Header.version = v
return b
}
// SetPrevBlockHash sets the previous block hash for block which is building.
func (b *Builder) SetPrevBlockHash(h hash.Hash256) *Builder {
b.blk.Header.prevBlockHash = h
return b
}
// SetDeltaStateDigest sets the new delta state digest after running actions included in this building block
func (b *Builder) SetDeltaStateDigest(h hash.Hash256) *Builder {
b.blk.Header.deltaStateDigest = h
return b
}
// SetReceipts sets the receipts after running actions included in this building block.
func (b *Builder) SetReceipts(receipts []*action.Receipt) *Builder {
b.blk.Receipts = receipts // make a shallow copy
return b
}
// SetReceiptRoot sets the receipt root after running actions included in this building block.
func (b *Builder) SetReceiptRoot(h hash.Hash256) *Builder {
b.blk.Header.receiptRoot = h
return b
}
// SignAndBuild signs and then builds a block.
func (b *Builder) SignAndBuild(signerPrvKey keypair.PrivateKey) (Block, error) {
if !bytes.Equal(b.blk.Header.pubkey.Bytes(), signerPrvKey.PublicKey().Bytes()) {
return Block{}, errors.New("public key from the signer doesn't match that from runnable actions")
}
h := b.blk.Header.HashHeaderCore()
sig, err := signerPrvKey.Sign(h[:])
if err != nil {
return Block{}, errors.New("failed to sign block")
}
b.blk.Header.blockSig = sig
return b.blk, nil
}