Skip to content

Commit

Permalink
Merge 805b96b into 5047789
Browse files Browse the repository at this point in the history
  • Loading branch information
Musiczombie committed Feb 25, 2019
2 parents 5047789 + 805b96b commit 1c1265d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 8 deletions.
21 changes: 13 additions & 8 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"github.com/fractalplatform/fractal/types"
"github.com/fractalplatform/fractal/utils/fdb"
"github.com/fractalplatform/fractal/utils/rlp"
lru "github.com/hashicorp/golang-lru"
"github.com/hashicorp/golang-lru"
)

const (
Expand Down Expand Up @@ -461,7 +461,7 @@ func (bc *BlockChain) procFutureBlocks() {
if len(blocks) > 0 {
types.BlockBy(types.Number).Sort(blocks)
for i := range blocks {
bc.InsertChain(blocks[i : i+1])
bc.InsertChain(blocks[i: i+1])
}
}
}
Expand All @@ -470,7 +470,7 @@ func (bc *BlockChain) procFutureBlocks() {
type WriteStatus byte

const (
NonStatTy WriteStatus = iota
NonStatTy WriteStatus = iota
CanonStatTy
SideStatTy
)
Expand Down Expand Up @@ -596,6 +596,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks) (int, []*event.Event, []*t
defer bc.chainmu.Unlock()

var (
stats = insertStats{startTime: time.Now()}
events = make([]*event.Event, 0, len(chain))
lastCanon *types.Block
coalescedLogs []*types.Log
Expand All @@ -611,15 +612,14 @@ func (bc *BlockChain) insertChain(chain types.Blocks) (int, []*event.Event, []*t
log.Debug("Premature abort during blocks processing")
break
}

bstart := time.Now()
err := bc.validator.ValidateHeader(block.Header(), true)
if err == nil {
err = bc.Validator().ValidateBody(block)
}
switch {
case err == processor.ErrKnownBlock:
if bc.CurrentBlock().NumberU64() >= block.NumberU64() {
stats.ignored += 1
continue
}
case err == processor.ErrFutureBlock:
Expand All @@ -628,9 +628,13 @@ func (bc *BlockChain) insertChain(chain types.Blocks) (int, []*event.Event, []*t
return i, events, coalescedLogs, fmt.Errorf("future block: %v > %v", block.Time(), max)
}
bc.futureBlocks.Add(block.Hash(), block)
stats.ignored += 1
stats.queued += 1
continue
case err == processor.ErrUnknownAncestor && bc.futureBlocks.Contains(block.ParentHash()):
bc.futureBlocks.Add(block.Hash(), block)
stats.ignored += 1
stats.queued += 1
continue
case err == processor.ErrPrunedAncestor:
// Block competing with the canonical chain, store in the db, but don't process
Expand Down Expand Up @@ -711,12 +715,13 @@ func (bc *BlockChain) insertChain(chain types.Blocks) (int, []*event.Event, []*t
if err := bc.WriteBlockWithState(block, receipts, state); err != nil {
return i, events, coalescedLogs, err
}

log.Info("Inserted new block", "number", block.Number(), "hash", block.Hash().String(), "time", block.Time().Int64(), "txs", len(block.Txs), "gas", block.GasUsed(), "diff", block.Difficulty(), "elapsed", common.PrettyDuration(time.Since(bstart)))
stats.processed += 1
stats.txsCnt += len(block.Txs)
stats.usedGas += usedGas
stats.report(chain, i)
coalescedLogs = append(coalescedLogs, logs...)
lastCanon = block
}

if lastCanon != nil && bc.CurrentBlock().Hash() == lastCanon.Hash() {
events = append(events, &event.Event{Typecode: event.ChainHeadEv, Data: lastCanon})
}
Expand Down
69 changes: 69 additions & 0 deletions blockchain/blockchain_insert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2018 The Fractal Team Authors
// This file is part of the fractal project.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package blockchain

import (
"time"

"github.com/fractalplatform/fractal/types"
"github.com/fractalplatform/fractal/common"

"github.com/ethereum/go-ethereum/log"
)

// insertStats tracks and reports on block insertion.
type insertStats struct {
queued, processed, ignored, txsCnt int
usedGas uint64
startTime time.Time
}

// statsReportLimit is the time limit during import and export after which we
// always print out progress. This avoids the user wondering what's going on.
const statsReportLimit = 8 * time.Second

// report prints statistics if some number of blocks have been processed
// or more than a few seconds have passed since the last message.
func (st *insertStats) report(chain []*types.Block, index int) {
// Fetch the timings for the batch
var (
now = time.Now()
elapsed = now.Sub(st.startTime)
)
// If we're at the last block of the batch or report period reached, log
if index == len(chain)-1 || elapsed >= statsReportLimit {
end := chain[index]

// Assemble the log context and send it to the logger
context := []interface{}{
"blocks", st.processed, "txs", st.txsCnt, "mgas", float64(st.usedGas) / 1000000,
"elapsed", common.PrettyDuration(elapsed), "mgasps", float64(st.usedGas) * 1000 / float64(elapsed),
"number", end.Number(), "hash", end.Hash(),
}

if st.queued > 0 {
context = append(context, []interface{}{"queued", st.queued}...)
}
if st.ignored > 0 {
context = append(context, []interface{}{"ignored", st.ignored}...)
}
log.Info("Imported new chain segment", context...)

// Bump the stats reported to the next section
*st = insertStats{startTime: now,}
}
}

0 comments on commit 1c1265d

Please sign in to comment.