...
 
Commits (2)
......@@ -42,7 +42,7 @@ import (
"git.pirl.io/community/pirl/params"
"git.pirl.io/community/pirl/rlp"
"git.pirl.io/community/pirl/trie"
"github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru"
)
var (
......@@ -1123,9 +1123,8 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
}
abort, results := bc.engine.VerifyHeaders(bc, headers, seals)
defer close(abort)
// errChain := bc.checkChainForAttack(chain)
fmt.Println("This is the current fast block : ", bc.CurrentFastBlock().NumberU64())
fmt.Println("This is the current block : ", bc.CurrentBlock().NumberU64())
// Peek the error for the first block to decide the directing import logic
it := newInsertIterator(chain, results, bc.validator)
......@@ -1146,6 +1145,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
// Falls through to the block import
}
errChain := bc.checkChainForAttack(chain)
switch {
// First block is pruned, insert as sidechain and reorg only if TD grows enough
......@@ -1173,10 +1173,10 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
bc.reportBlock(block, nil, err)
return it.index, events, coalescedLogs, err
//Check if there is a penatly value in chain
// case errChain == ErrDelayTooHigh:
// stats.ignored += len(it.chain)
// bc.reportBlock(block, nil, errChain)
// return it.index, events, coalescedLogs, errChain
case errChain == ErrDelayTooHigh:
stats.ignored += len(it.chain)
bc.reportBlock(block, nil, errChain)
return it.index, events, coalescedLogs, errChain
}
// No validation errors for the first block (or chain prefix skipped)
for ; block != nil && err == nil; block, err = it.next() {
......
......@@ -34,5 +34,5 @@ var (
ErrNonceTooHigh = errors.New("nonce too high")
// ErrDelayTooHigh is returned when the delay between the blocks in the presented chain is too high
ErrDelayTooHigh = errors.New("Chain time values are not right!")
ErrDelayTooHigh = errors.New("chain time values are not right")
)
......@@ -2,13 +2,15 @@ package core
import (
"errors"
"sort"
"git.pirl.io/community/pirl/core/types"
"git.pirl.io/community/pirl/log"
"git.pirl.io/community/pirl/params"
"sort"
)
var syncStatus bool
func (bc *BlockChain) checkChainForAttack(blocks types.Blocks) error {
// Copyright 2014 The go-ethereum Authors
// Copyright 2018 Pirl Sprl
......@@ -33,10 +35,8 @@ func (bc *BlockChain) checkChainForAttack(blocks types.Blocks) error {
timeMap := make(map[uint64]int64)
tipOfTheMainChain := bc.CurrentBlock().NumberU64()
if !syncStatus {
if tipOfTheMainChain == blocks[0].NumberU64() - 1 {
if tipOfTheMainChain == blocks[0].NumberU64()-1 {
//fmt.Println("We are synced")
syncStatus = true
} else {
......@@ -45,7 +45,6 @@ func (bc *BlockChain) checkChainForAttack(blocks types.Blocks) error {
}
}
if len(blocks) > 0 && bc.CurrentBlock().NumberU64() > uint64(params.TimeCapsuleBlock) {
if syncStatus && len(blocks) > int(params.TimeCapsuleLength) {
for _, b := range blocks {
......@@ -56,7 +55,7 @@ func (bc *BlockChain) checkChainForAttack(blocks types.Blocks) error {
p := make(PairList, len(timeMap))
index := 0
for k, v := range timeMap {
p[index] = Pair {k, v}
p[index] = Pair{k, v}
index++
}
sort.Sort(p)
......@@ -73,16 +72,16 @@ func (bc *BlockChain) checkChainForAttack(blocks types.Blocks) error {
}
//fmt.Println("Penalty value for the chain :", penalty)
context := []interface{}{
"synced", syncStatus, "number", tipOfTheMainChain, "incoming_number", blocks[0].NumberU64() - 1, "penalty", penalty ,"implementation", "The Pirl Team",
"synced", syncStatus, "number", tipOfTheMainChain, "incoming_number", blocks[0].NumberU64() - 1, "penalty", penalty, "implementation", "The Pirl Team",
}
log.Info("checking legitimity of the chain", context... )
log.Info("checking legitimity of the chain", context...)
if penalty > 0 {
context := []interface{}{
"penalty", penalty,
}
log.Error("Chain is a malicious and we should reject it", context... )
log.Error("Chain is a malicious and we should reject it", context...)
err = ErrDelayTooHigh
}
......@@ -94,7 +93,7 @@ func (bc *BlockChain) checkChainForAttack(blocks types.Blocks) error {
return err
}
func calculatePenaltyTimeForBlock(tipOfTheMainChain , incomingBlock uint64) int64 {
func calculatePenaltyTimeForBlock(tipOfTheMainChain, incomingBlock uint64) int64 {
if incomingBlock < tipOfTheMainChain {
return int64(tipOfTheMainChain - incomingBlock)
}
......@@ -127,13 +126,13 @@ func calculateMulti(diff uint64) uint64 {
return 1
}
// A data structure to hold key/value pairs
// Pair data structure to hold key/value pairs
type Pair struct {
Key uint64
Value int64
}
// A slice of pairs that implements sort.Interface to sort by values
// PairList is a slice of pairs that implements sort.Interface to sort by values
type PairList []Pair
func (p PairList) Len() int { return len(p) }
......
......@@ -338,6 +338,7 @@ func (d *Downloader) Synchronise(id string, head common.Hash, td *big.Int, mode
log.Warn("Downloader wants to drop peer, but peerdrop-function is not set", "peer", id)
} else {
d.dropPeer(id)
d.peers.Unregister(id)
}
default:
log.Warn("Synchronisation failed, retrying", "err", err)
......