Skip to content

Commit daaeb0a

Browse files
authored
Merge pull request #155 from getamis/feature/consensus_failed
Fix consensus failed if the pause timestamp isn't 1
2 parents 3d49176 + 262ae3f commit daaeb0a

File tree

16 files changed

+40
-72
lines changed

16 files changed

+40
-72
lines changed

cmd/geth/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ var (
122122
configFileFlag,
123123
utils.IstanbulRequestTimeoutFlag,
124124
utils.IstanbulBlockPeriodFlag,
125-
utils.IstanbulBlockPauseTimeFlag,
126125
}
127126

128127
rpcFlags = []cli.Flag{

cmd/geth/usage.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ import (
2222
"io"
2323
"sort"
2424

25+
"strings"
26+
2527
"github.com/ethereum/go-ethereum/cmd/utils"
2628
"github.com/ethereum/go-ethereum/internal/debug"
2729
"gopkg.in/urfave/cli.v1"
28-
"strings"
2930
)
3031

3132
// AppHelpTemplate is the test template for the default, global app help topic.
@@ -226,7 +227,6 @@ var AppHelpFlagGroups = []flagGroup{
226227
Flags: []cli.Flag{
227228
utils.IstanbulRequestTimeoutFlag,
228229
utils.IstanbulBlockPeriodFlag,
229-
utils.IstanbulBlockPauseTimeFlag,
230230
},
231231
},
232232
}

cmd/utils/flags.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -534,11 +534,6 @@ var (
534534
Usage: "Default minimum difference between two consecutive block's timestamps in seconds",
535535
Value: eth.DefaultConfig.Istanbul.BlockPeriod,
536536
}
537-
IstanbulBlockPauseTimeFlag = cli.Uint64Flag{
538-
Name: "istanbul.blockpausetime",
539-
Usage: "Pause time when zero tx in previous block, values should be larger than istanbul.blockperiod",
540-
Value: eth.DefaultConfig.Istanbul.BlockPauseTime,
541-
}
542537
)
543538

544539
// MakeDataDir retrieves the currently requested data directory, terminating
@@ -964,9 +959,6 @@ func setIstanbul(ctx *cli.Context, cfg *eth.Config) {
964959
if ctx.GlobalIsSet(IstanbulBlockPeriodFlag.Name) {
965960
cfg.Istanbul.BlockPeriod = ctx.GlobalUint64(IstanbulBlockPeriodFlag.Name)
966961
}
967-
if ctx.GlobalIsSet(IstanbulBlockPauseTimeFlag.Name) {
968-
cfg.Istanbul.BlockPauseTime = ctx.GlobalUint64(IstanbulBlockPauseTimeFlag.Name)
969-
}
970962
}
971963

972964
// checkExclusive verifies that only a single isntance of the provided flags was

consensus/istanbul/backend/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func (sb *backend) Verify(proposal istanbul.Proposal) (time.Duration, error) {
239239

240240
// Sign implements istanbul.Backend.Sign
241241
func (sb *backend) Sign(data []byte) ([]byte, error) {
242-
hashData := crypto.Keccak256([]byte(data))
242+
hashData := crypto.Keccak256(data)
243243
return crypto.Sign(hashData, sb.privateKey)
244244
}
245245

consensus/istanbul/backend/backend_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,8 @@ func TestCommit(t *testing.T) {
148148
for _, test := range testCases {
149149
expBlock := test.expectedBlock()
150150
go func() {
151-
select {
152-
case result := <-backend.commitCh:
153-
commitCh <- result
154-
return
155-
}
151+
result := <-backend.commitCh
152+
commitCh <- result
156153
}()
157154

158155
backend.proposedBlockHash = expBlock.Hash()

consensus/istanbul/backend/engine.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ var (
8181
// errEmptyCommittedSeals is returned if the field of committed seals is zero.
8282
errEmptyCommittedSeals = errors.New("zero committed seals")
8383
// errMismatchTxhashes is returned if the TxHash in header is mismatch.
84-
errMismatchTxhashes = errors.New("mismatch transcations hashes")
84+
errMismatchTxhashes = errors.New("mismatch transactions hashes")
8585
)
8686
var (
8787
defaultDifficulty = big.NewInt(1)
@@ -366,6 +366,12 @@ func (sb *backend) Prepare(chain consensus.ChainReader, header *types.Header) er
366366
return err
367367
}
368368
header.Extra = extra
369+
370+
// set header's timestamp
371+
header.Time = new(big.Int).Add(parent.Time, new(big.Int).SetUint64(sb.config.BlockPeriod))
372+
if header.Time.Int64() < time.Now().Unix() {
373+
header.Time = big.NewInt(time.Now().Unix())
374+
}
369375
return nil
370376
}
371377

@@ -454,21 +460,7 @@ func (sb *backend) CalcDifficulty(chain consensus.ChainReader, time uint64, pare
454460

455461
// update timestamp and signature of the block based on its number of transactions
456462
func (sb *backend) updateBlock(parent *types.Header, block *types.Block) (*types.Block, error) {
457-
// set block period based the number of tx
458-
var period uint64
459-
if len(block.Transactions()) == 0 {
460-
period = sb.config.BlockPauseTime
461-
} else {
462-
period = sb.config.BlockPeriod
463-
}
464-
465-
// set header timestamp
466463
header := block.Header()
467-
header.Time = new(big.Int).Add(parent.Time, new(big.Int).SetUint64(period))
468-
time := now().Unix()
469-
if header.Time.Int64() < time {
470-
header.Time = big.NewInt(time)
471-
}
472464
// sign the hash
473465
seal, err := sb.Sign(sigHash(header).Bytes())
474466
if err != nil {

consensus/istanbul/backend/engine_test.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,12 @@ func TestSealStopChannel(t *testing.T) {
163163
stop := make(chan struct{}, 1)
164164
eventSub := engine.EventMux().Subscribe(istanbul.RequestEvent{})
165165
eventLoop := func() {
166-
select {
167-
case ev := <-eventSub.Chan():
168-
_, ok := ev.Data.(istanbul.RequestEvent)
169-
if !ok {
170-
t.Errorf("unexpected event comes: %v", reflect.TypeOf(ev.Data))
171-
}
172-
stop <- struct{}{}
166+
ev := <-eventSub.Chan()
167+
_, ok := ev.Data.(istanbul.RequestEvent)
168+
if !ok {
169+
t.Errorf("unexpected event comes: %v", reflect.TypeOf(ev.Data))
173170
}
171+
stop <- struct{}{}
174172
eventSub.Unsubscribe()
175173
}
176174
go eventLoop()
@@ -189,14 +187,12 @@ func TestSealCommittedOtherHash(t *testing.T) {
189187
otherBlock := makeBlockWithoutSeal(chain, engine, block)
190188
eventSub := engine.EventMux().Subscribe(istanbul.RequestEvent{})
191189
eventLoop := func() {
192-
select {
193-
case ev := <-eventSub.Chan():
194-
_, ok := ev.Data.(istanbul.RequestEvent)
195-
if !ok {
196-
t.Errorf("unexpected event comes: %v", reflect.TypeOf(ev.Data))
197-
}
198-
engine.Commit(otherBlock, [][]byte{})
190+
ev := <-eventSub.Chan()
191+
_, ok := ev.Data.(istanbul.RequestEvent)
192+
if !ok {
193+
t.Errorf("unexpected event comes: %v", reflect.TypeOf(ev.Data))
199194
}
195+
engine.Commit(otherBlock, [][]byte{})
200196
eventSub.Unsubscribe()
201197
}
202198
go eventLoop()
@@ -208,10 +204,8 @@ func TestSealCommittedOtherHash(t *testing.T) {
208204

209205
const timeoutDura = 2 * time.Second
210206
timeout := time.NewTimer(timeoutDura)
211-
select {
212-
case <-timeout.C:
213-
// wait 2 seconds to ensure we cannot get any blocks from Istanbul
214-
}
207+
<-timeout.C
208+
// wait 2 seconds to ensure we cannot get any blocks from Istanbul
215209
}
216210

217211
func TestSealCommitted(t *testing.T) {

consensus/istanbul/backend/snapshot.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,9 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) {
207207
// Tally up the new vote from the validator
208208
var authorize bool
209209
switch {
210-
case bytes.Compare(header.Nonce[:], nonceAuthVote) == 0:
210+
case bytes.Equal(header.Nonce[:], nonceAuthVote):
211211
authorize = true
212-
case bytes.Compare(header.Nonce[:], nonceDropVote) == 0:
212+
case bytes.Equal(header.Nonce[:], nonceDropVote):
213213
authorize = false
214214
default:
215215
return nil, errInvalidVote

consensus/istanbul/backend/snapshot_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func TestVoting(t *testing.T) {
354354
for j, vote := range tt.votes {
355355
headers[j] = &types.Header{
356356
Number: big.NewInt(int64(j) + 1),
357-
Time: big.NewInt(int64(j) * int64(config.BlockPauseTime)),
357+
Time: big.NewInt(int64(j) * int64(config.BlockPeriod)),
358358
Coinbase: accounts.address(vote.voted),
359359
Difficulty: defaultDifficulty,
360360
MixDigest: types.IstanbulDigest,
@@ -417,7 +417,7 @@ func TestSaveAndLoad(t *testing.T) {
417417
},
418418
},
419419
Tally: map[common.Address]Tally{
420-
common.StringToAddress("1234567893"): Tally{
420+
common.StringToAddress("1234567893"): {
421421
Authorize: false,
422422
Votes: 20,
423423
},

consensus/istanbul/config.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,15 @@ const (
2424
)
2525

2626
type Config struct {
27-
RequestTimeout uint64 `toml:",omitempty"` // The timeout for each Istanbul round in milliseconds. This timeout should be larger than BlockPauseTime
27+
RequestTimeout uint64 `toml:",omitempty"` // The timeout for each Istanbul round in milliseconds.
2828
BlockPeriod uint64 `toml:",omitempty"` // Default minimum difference between two consecutive block's timestamps in second
29-
BlockPauseTime uint64 `toml:",omitempty"` // Delay time if no tx in block, the value should be larger than BlockPeriod
3029
ProposerPolicy ProposerPolicy `toml:",omitempty"` // The policy for proposer selection
3130
Epoch uint64 `toml:",omitempty"` // The number of blocks after which to checkpoint and reset the pending votes
3231
}
3332

3433
var DefaultConfig = &Config{
3534
RequestTimeout: 10000,
3635
BlockPeriod: 1,
37-
BlockPauseTime: 2,
3836
ProposerPolicy: RoundRobin,
3937
Epoch: 30000,
4038
}

0 commit comments

Comments
 (0)