From fd3c5e18480ced24ba8e1c088065893885a8b03b Mon Sep 17 00:00:00 2001 From: coderink Date: Tue, 30 Jul 2019 10:33:44 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E7=BB=99header.Time=E8=B5=8B=E5=80=BC?= =?UTF-8?q?=E6=97=B6=E5=A6=82=E6=9E=9C=E5=9C=A8Finalize=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E4=B8=AD,=E5=9C=A8=E6=94=B6=E5=88=B0=E8=BF=9C=E7=AB=AF?= =?UTF-8?q?=E6=8C=96=E5=87=BA=E7=9A=84=E5=9D=97=E7=9A=84=E6=97=B6=E5=80=99?= =?UTF-8?q?,=E4=BC=9A=E5=9C=A8=E9=AA=8C=E8=AF=81state=E7=9A=84=E6=97=B6?= =?UTF-8?q?=E5=80=99=E4=B9=9F=E4=BC=9A=E5=86=8D=E6=AC=A1=E8=B0=83=E7=94=A8?= =?UTF-8?q?Finalize=E5=87=BD=E6=95=B0,=E5=B0=86=E5=86=8D=E6=AC=A1=E7=BB=99?= =?UTF-8?q?header.Time=E8=B5=8B=E5=80=BC,=E4=BD=BF=E5=BE=97state=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E4=B8=8D=E4=B8=80=E6=A0=B7,=E4=BB=8E=E8=80=8C?= =?UTF-8?q?=E4=BA=A7=E7=94=9F=E5=88=86=E5=8F=89.=E6=A8=A1=E6=8B=9F?= =?UTF-8?q?=E9=87=8D=E7=8E=B0,=E9=83=A8=E7=BD=B2=E6=99=BA=E8=83=BD?= =?UTF-8?q?=E5=90=88=E7=BA=A6,=E5=90=88=E7=BA=A6=E4=B8=AD=E8=B0=83?= =?UTF-8?q?=E7=94=A8now()=E5=87=BD=E6=95=B0=E7=BB=99=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E8=B5=8B=E5=80=BC,=E7=94=B1=E4=BA=8Enow()=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E5=8F=96=E5=80=BC=E4=B8=BAheader.Time=E5=80=BC,=E4=BD=BF?= =?UTF-8?q?=E5=BE=97=E6=89=93=E5=8C=85=E5=9D=97=E6=89=93=E5=8C=85=E7=9A=84?= =?UTF-8?q?header.Time=E5=80=BC=E4=B8=8E=E9=AA=8C=E8=AF=81=E6=97=B6header.?= =?UTF-8?q?Time=E5=80=BC=E4=B8=8D=E4=B8=80=E6=A0=B7,=E4=BB=8E=E4=BA=8B?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E6=97=B6stateRoot=E9=AA=8C=E8=AF=81=E4=B8=8D?= =?UTF-8?q?=E9=80=9A=E8=BF=87,=E4=BA=A7=E7=94=9Fbad=20block=E4=BB=8E?= =?UTF-8?q?=E8=80=8C=E5=88=86=E5=8F=89.=202.=20=E6=8C=87=E9=92=88=E8=B5=8B?= =?UTF-8?q?=E5=80=BC=E5=8F=91=E7=94=9F=E6=B5=85=E6=8B=B7=E8=B4=9D.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- consensus/alien/alien.go | 23 +++++++++++++---------- consensus/alien/snapshot.go | 2 +- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/consensus/alien/alien.go b/consensus/alien/alien.go index 4ee063f9..c002e2d4 100644 --- a/consensus/alien/alien.go +++ b/consensus/alien/alien.go @@ -594,6 +594,18 @@ func (a *Alien) Prepare(chain consensus.ChainReader, header *types.Header) error // Set the correct difficulty header.Difficulty = new(big.Int).Set(defaultDifficulty) + + number := header.Number.Uint64() + + parent := chain.GetHeader(header.ParentHash, number-1) + if parent == nil { + return consensus.ErrUnknownAncestor + } + header.Time = new(big.Int).Add(parent.Time, new(big.Int).SetUint64(a.config.Period)) + if header.Time.Int64() < time.Now().Unix() { + header.Time = big.NewInt(time.Now().Unix()) + } + // If now is later than genesis timestamp, skip prepare if a.config.GenesisTimestamp < uint64(time.Now().Unix()) { return nil @@ -743,20 +755,11 @@ func (a *Alien) mcConfirmBlock(chain consensus.ChainReader, header *types.Header // rewards given, and returns the final block. func (a *Alien) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) { - number := header.Number.Uint64() - // Mix digest is reserved for now, set to empty header.MixDigest = common.Hash{} // Ensure the timestamp has the correct delay - parent := chain.GetHeader(header.ParentHash, number-1) - if parent == nil { - return nil, consensus.ErrUnknownAncestor - } - header.Time = new(big.Int).Add(parent.Time, new(big.Int).SetUint64(a.config.Period)) - if header.Time.Int64() < time.Now().Unix() { - header.Time = big.NewInt(time.Now().Unix()) - } + // Ensure the extra data has all it's components if len(header.Extra) < extraVanity { diff --git a/consensus/alien/snapshot.go b/consensus/alien/snapshot.go index da0e7467..3607b317 100644 --- a/consensus/alien/snapshot.go +++ b/consensus/alien/snapshot.go @@ -1046,7 +1046,7 @@ func (s *Snapshot) updateSnapshotByVotes(votes []Vote, headerNumber *big.Int) { } s.Votes[vote.Voter] = &Vote{vote.Voter, vote.Candidate, new(big.Int).Set(vote.Stake)} - s.Voters[vote.Voter] = headerNumber + s.Voters[vote.Voter] = new(big.Int).Set(headerNumber) } }