Skip to content

Commit 906f304

Browse files
authored
Set height, timestamp, and public key in block builder (iotexproject#1675)
1 parent edbcde1 commit 906f304

File tree

10 files changed

+52
-84
lines changed

10 files changed

+52
-84
lines changed

action/protocol/poll/protocol.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,14 @@ func MustGetProtocol(registry *protocol.Registry) Protocol {
8181
}
8282
p, ok := registry.Find(ProtocolID)
8383
if !ok {
84-
log.S().Panic("rolldpos protocol is not registered")
84+
log.S().Panic("poll protocol is not registered")
8585
}
86+
8687
pp, ok := p.(Protocol)
8788
if !ok {
88-
log.S().Panic("fail to cast to poll protocol")
89+
log.S().Panic("fail to cast poll protocol")
8990
}
91+
9092
return pp
9193
}
9294

api/api_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -1485,8 +1485,11 @@ func TestServer_GetEpochMeta(t *testing.T) {
14851485
if height > 0 && height <= 4 {
14861486
pk := identityset.PrivateKey(int(height))
14871487
blk, err := block.NewBuilder(
1488-
block.NewRunnableActionsBuilder().SetHeight(height).Build(pk.PublicKey()),
1489-
).SignAndBuild(pk)
1488+
block.NewRunnableActionsBuilder().Build(),
1489+
).
1490+
SetHeight(height).
1491+
SetTimestamp(time.Time{}).
1492+
SignAndBuild(pk)
14901493
if err != nil {
14911494
return &block.Header{}, err
14921495
}

blockchain/block/block.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,7 @@ func (b *Block) VerifyReceiptRoot(root hash.Hash256) error {
111111

112112
// RunnableActions abstructs RunnableActions from a Block.
113113
func (b *Block) RunnableActions() RunnableActions {
114-
return RunnableActions{
115-
blockHeight: b.Header.height,
116-
blockTimeStamp: b.Header.timestamp,
117-
blockProducerPubKey: b.Header.pubkey,
118-
actions: b.Actions,
119-
txHash: b.txRoot,
120-
}
114+
return RunnableActions{actions: b.Actions, txHash: b.txRoot}
121115
}
122116

123117
// Finalize creates a footer for the block

blockchain/block/block_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,11 @@ func makeBlock(tb testing.TB, n int) *Block {
199199
sevlps = append(sevlps, sevlp)
200200
}
201201
rap := RunnableActionsBuilder{}
202-
ra := rap.
203-
SetHeight(1).
204-
SetTimeStamp(time.Now()).
205-
AddActions(sevlps...).
206-
Build(identityset.PrivateKey(0).PublicKey())
202+
ra := rap.AddActions(sevlps...).
203+
Build()
207204
blk, err := NewBuilder(ra).
205+
SetHeight(1).
206+
SetTimestamp(time.Now()).
208207
SetVersion(1).
209208
SetReceiptRoot(hash.Hash256b([]byte("hello, world!"))).
210209
SetDeltaStateDigest(hash.Hash256b([]byte("world, hello!"))).

blockchain/block/builder.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
package block
88

99
import (
10-
"bytes"
10+
"time"
1111

1212
"github.com/iotexproject/go-pkgs/bloom"
1313
"github.com/iotexproject/go-pkgs/crypto"
@@ -26,11 +26,8 @@ func NewBuilder(ra RunnableActions) *Builder {
2626
return &Builder{
2727
blk: Block{
2828
Header: Header{
29-
version: version.ProtocolVersion,
30-
height: ra.blockHeight,
31-
timestamp: ra.blockTimeStamp,
32-
txRoot: ra.txHash,
33-
pubkey: ra.blockProducerPubKey,
29+
version: version.ProtocolVersion,
30+
txRoot: ra.txHash,
3431
},
3532
Body: Body{
3633
Actions: ra.actions,
@@ -39,6 +36,18 @@ func NewBuilder(ra RunnableActions) *Builder {
3936
}
4037
}
4138

39+
// SetTimestamp sets the block timestamp
40+
func (b *Builder) SetTimestamp(ts time.Time) *Builder {
41+
b.blk.Header.timestamp = ts
42+
return b
43+
}
44+
45+
// SetHeight sets the block height
46+
func (b *Builder) SetHeight(h uint64) *Builder {
47+
b.blk.Header.height = h
48+
return b
49+
}
50+
4251
// SetVersion sets the protocol version for block which is building.
4352
func (b *Builder) SetVersion(v uint32) *Builder {
4453
b.blk.Header.version = v
@@ -77,10 +86,7 @@ func (b *Builder) SetLogsBloom(f bloom.BloomFilter) *Builder {
7786

7887
// SignAndBuild signs and then builds a block.
7988
func (b *Builder) SignAndBuild(signerPrvKey crypto.PrivateKey) (Block, error) {
80-
if !bytes.Equal(b.blk.Header.pubkey.Bytes(), signerPrvKey.PublicKey().Bytes()) {
81-
return Block{}, errors.New("public key from the signer doesn't match that from runnable actions")
82-
}
83-
89+
b.blk.Header.pubkey = signerPrvKey.PublicKey()
8490
h := b.blk.Header.HashHeaderCore()
8591
sig, err := signerPrvKey.Sign(h[:])
8692
if err != nil {

blockchain/block/builder_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ import (
1717
)
1818

1919
func TestBuilder(t *testing.T) {
20-
ra := NewRunnableActionsBuilder().
21-
SetHeight(1).
22-
SetTimeStamp(testutil.TimestampNow()).
23-
Build(identityset.PrivateKey(29).PublicKey())
20+
ra := NewRunnableActionsBuilder().Build()
2421

2522
nblk, err := NewBuilder(ra).
23+
SetHeight(1).
24+
SetTimestamp(testutil.TimestampNow()).
2625
SetPrevBlockHash(hash.ZeroHash256).
2726
SignAndBuild(identityset.PrivateKey(29))
2827
require.NoError(t, err)

blockchain/block/runnable.go

+3-37
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,15 @@
77
package block
88

99
import (
10-
"time"
11-
12-
"github.com/iotexproject/go-pkgs/crypto"
1310
"github.com/iotexproject/go-pkgs/hash"
1411

1512
"github.com/iotexproject/iotex-core/action"
1613
)
1714

1815
// RunnableActions is abstructed from block which contains information to execute all actions in a block.
1916
type RunnableActions struct {
20-
blockHeight uint64
21-
blockTimeStamp time.Time
22-
blockProducerPubKey crypto.PublicKey
23-
txHash hash.Hash256
24-
actions []action.SealedEnvelope
25-
}
26-
27-
// BlockHeight returns block height.
28-
func (ra RunnableActions) BlockHeight() uint64 {
29-
return ra.blockHeight
30-
}
31-
32-
// BlockTimeStamp returns blockTimeStamp.
33-
func (ra RunnableActions) BlockTimeStamp() time.Time {
34-
return ra.blockTimeStamp
35-
}
36-
37-
// BlockProducerPubKey return BlockProducerPubKey.
38-
func (ra RunnableActions) BlockProducerPubKey() crypto.PublicKey {
39-
return ra.blockProducerPubKey
17+
txHash hash.Hash256
18+
actions []action.SealedEnvelope
4019
}
4120

4221
// TxHash returns TxHash.
@@ -53,18 +32,6 @@ type RunnableActionsBuilder struct{ ra RunnableActions }
5332
// NewRunnableActionsBuilder creates a RunnableActionsBuilder.
5433
func NewRunnableActionsBuilder() *RunnableActionsBuilder { return &RunnableActionsBuilder{} }
5534

56-
// SetHeight sets the block height for block which is building.
57-
func (b *RunnableActionsBuilder) SetHeight(h uint64) *RunnableActionsBuilder {
58-
b.ra.blockHeight = h
59-
return b
60-
}
61-
62-
// SetTimeStamp sets the time stamp for block which is building.
63-
func (b *RunnableActionsBuilder) SetTimeStamp(ts time.Time) *RunnableActionsBuilder {
64-
b.ra.blockTimeStamp = ts
65-
return b
66-
}
67-
6835
// AddActions adds actions for block which is building.
6936
func (b *RunnableActionsBuilder) AddActions(acts ...action.SealedEnvelope) *RunnableActionsBuilder {
7037
if b.ra.actions == nil {
@@ -75,8 +42,7 @@ func (b *RunnableActionsBuilder) AddActions(acts ...action.SealedEnvelope) *Runn
7542
}
7643

7744
// Build signs and then builds a block.
78-
func (b *RunnableActionsBuilder) Build(producerPubKey crypto.PublicKey) RunnableActions {
79-
b.ra.blockProducerPubKey = producerPubKey
45+
func (b *RunnableActionsBuilder) Build() RunnableActions {
8046
b.ra.txHash = calculateTxRoot(b.ra.actions)
8147
return b.ra
8248
}

blockchain/blockchain.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -508,10 +508,8 @@ func (bc *blockchain) MintNewBlock(
508508

509509
sk := bc.config.ProducerPrivateKey()
510510
ra := block.NewRunnableActionsBuilder().
511-
SetHeight(newblockHeight).
512-
SetTimeStamp(timestamp).
513511
AddActions(actions...).
514-
Build(sk.PublicKey())
512+
Build()
515513

516514
prevBlkHash := bc.tipHash
517515
// The first block's previous block hash is pointing to the digest of genesis config. This is to guarantee all nodes
@@ -520,6 +518,8 @@ func (bc *blockchain) MintNewBlock(
520518
prevBlkHash = bc.config.Genesis.Hash()
521519
}
522520
blk, err := block.NewBuilder(ra).
521+
SetHeight(newblockHeight).
522+
SetTimestamp(timestamp).
523523
SetPrevBlockHash(prevBlkHash).
524524
SetDeltaStateDigest(ws.Digest()).
525525
SetReceipts(rc).
@@ -652,7 +652,7 @@ func (bc *blockchain) startExistingBlockchain() error {
652652
if err != nil {
653653
return errors.Wrap(err, "failed to obtain working set from state factory")
654654
}
655-
if _, err := bc.runActions(blk.RunnableActions(), ws); err != nil {
655+
if _, err := bc.runActions(blk, ws); err != nil {
656656
return err
657657
}
658658

@@ -718,7 +718,7 @@ func (bc *blockchain) validateBlock(blk *block.Block) error {
718718
return errors.Wrap(err, "Failed to obtain working set from state factory")
719719
}
720720
runTimer := bc.timerFactory.NewTimer("runActions")
721-
receipts, err := bc.runActions(blk.RunnableActions(), ws)
721+
receipts, err := bc.runActions(blk, ws)
722722
runTimer.End()
723723
if err != nil {
724724
log.L().Panic("Failed to update state.", zap.Uint64("tipHeight", bc.tipHeight), zap.Error(err))
@@ -782,27 +782,27 @@ func (bc *blockchain) commitBlock(blk *block.Block) error {
782782
}
783783

784784
func (bc *blockchain) runActions(
785-
acts block.RunnableActions,
785+
blk *block.Block,
786786
ws factory.WorkingSet,
787787
) ([]*action.Receipt, error) {
788788
if bc.sf == nil {
789789
return nil, errors.New("statefactory cannot be nil")
790790
}
791791

792-
producer, err := address.FromBytes(acts.BlockProducerPubKey().Hash())
792+
producer, err := address.FromBytes(blk.PublicKey().Hash())
793793
if err != nil {
794794
return nil, err
795795
}
796796

797-
ctx, err := bc.context(context.Background(), producer, acts.BlockHeight(), acts.BlockTimeStamp())
797+
ctx, err := bc.context(context.Background(), producer, blk.Height(), blk.Timestamp())
798798
if err != nil {
799799
return nil, err
800800
}
801801

802802
raCtx := protocol.MustGetRunActionsCtx(ctx)
803803
raCtx.History = ws.History()
804+
registry := raCtx.Registry
804805
ctx = protocol.WithRunActionsCtx(ctx, raCtx)
805-
registry := protocol.MustGetRunActionsCtx(ctx).Registry
806806
for _, p := range registry.All() {
807807
if pp, ok := p.(protocol.PreStatesCreator); ok {
808808
if err := pp.CreatePreStates(ctx, ws); err != nil {
@@ -812,7 +812,7 @@ func (bc *blockchain) runActions(
812812
}
813813
// TODO: verify whether the post system actions are appended tail
814814

815-
return ws.RunActions(ctx, acts.BlockHeight(), acts.Actions())
815+
return ws.RunActions(ctx, blk.Height(), blk.RunnableActions().Actions())
816816
}
817817

818818
func (bc *blockchain) pickAndRunActions(ctx context.Context, actionMap map[string][]action.SealedEnvelope,

blocksync/blocksync_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func TestBlockSyncerProcessBlockOutOfOrder(t *testing.T) {
257257
cs1.EXPECT().Calibrate(gomock.Any()).Times(3)
258258

259259
bs1, err := NewBlockSyncer(cfg, chain1, ap1, cs1, opts...)
260-
require.Nil(err)
260+
require.NoError(err)
261261
registry2 := protocol.NewRegistry()
262262
require.NoError(registry2.Register(account.ProtocolID, acc))
263263
require.NoError(registry2.Register(rolldpos.ProtocolID, rp))
@@ -325,7 +325,7 @@ func TestBlockSyncerProcessBlockSync(t *testing.T) {
325325

326326
ctx := context.Background()
327327
cfg, err := newTestConfig()
328-
require.Nil(err)
328+
require.NoError(err)
329329
registry := protocol.NewRegistry()
330330
acc := account.NewProtocol()
331331
require.NoError(registry.Register(account.ProtocolID, acc))
@@ -352,7 +352,7 @@ func TestBlockSyncerProcessBlockSync(t *testing.T) {
352352
cs1.EXPECT().ValidateBlockFooter(gomock.Any()).Return(nil).Times(3)
353353
cs1.EXPECT().Calibrate(gomock.Any()).Times(3)
354354
bs1, err := NewBlockSyncer(cfg, chain1, ap1, cs1, opts...)
355-
require.Nil(err)
355+
require.NoError(err)
356356
registry2 := protocol.NewRegistry()
357357
require.NoError(registry2.Register(account.ProtocolID, acc))
358358
require.NoError(registry2.Register(rolldpos.ProtocolID, rolldposProtocol))

consensus/scheme/rolldpos/rolldpos_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,10 @@ func makeBlock(t *testing.T, accountIndex, numOfEndosements int, makeInvalidEndo
147147
}
148148
timeT := time.Unix(blkTime, 0)
149149
rap := block.RunnableActionsBuilder{}
150-
ra := rap.
151-
SetHeight(uint64(height)).
152-
SetTimeStamp(timeT).
153-
Build(identityset.PrivateKey(accountIndex).PublicKey())
150+
ra := rap.Build()
154151
blk, err := block.NewBuilder(ra).
152+
SetHeight(uint64(height)).
153+
SetTimestamp(timeT).
155154
SetVersion(1).
156155
SetReceiptRoot(hash.Hash256b([]byte("hello, world!"))).
157156
SetDeltaStateDigest(hash.Hash256b([]byte("world, hello!"))).

0 commit comments

Comments
 (0)