Skip to content

Commit bcf5657

Browse files
committed
[release/1.3.4] core: Added new TD strategy which mitigate the risk for selfish mining
Assuming the following scenario where a miner has 15% of all hashing power and the ability to exert a moderate control over the network to the point where if the attacker sees a message A, it can't stop A from propagating, but what it **can** do is send a message B and ensure that most nodes see B before A. The attacker can then selfish mine and augment selfish mining strategy by giving his own blocks an advantage. This change makes the time at which a block is received less relevant and so the level of control an attacker has over the network no longer makes a difference. This change changes the current td algorithm `B_td > C_td` to the new algorithm `B_td > C_td || B_td == C_td && rnd < 0.5`.
1 parent 587bafa commit bcf5657

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

core/blockchain.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -729,14 +729,18 @@ func (self *BlockChain) writeHeader(header *types.Header) error {
729729
if ptd == nil {
730730
return ParentError(header.ParentHash)
731731
}
732-
td := new(big.Int).Add(header.Difficulty, ptd)
732+
733+
localTd := self.GetTd(self.currentHeader.Hash())
734+
externTd := new(big.Int).Add(header.Difficulty, ptd)
733735

734736
// Make sure no inconsistent state is leaked during insertion
735737
self.mu.Lock()
736738
defer self.mu.Unlock()
737739

738740
// If the total difficulty is higher than our known, add it to the canonical chain
739-
if td.Cmp(self.GetTd(self.currentHeader.Hash())) > 0 {
741+
// Second clause in the if statement reduces the vulnerability to selfish mining.
742+
// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
743+
if externTd.Cmp(localTd) > 0 || (externTd.Cmp(localTd) == 0 && mrand.Float64() < 0.5) {
740744
// Delete any canonical number assignments above the new head
741745
for i := header.Number.Uint64() + 1; GetCanonicalHash(self.chainDb, i) != (common.Hash{}); i++ {
742746
DeleteCanonicalHash(self.chainDb, i)
@@ -757,7 +761,7 @@ func (self *BlockChain) writeHeader(header *types.Header) error {
757761
self.currentHeader = types.CopyHeader(header)
758762
}
759763
// Irrelevant of the canonical status, write the header itself to the database
760-
if err := WriteTd(self.chainDb, header.Hash(), td); err != nil {
764+
if err := WriteTd(self.chainDb, header.Hash(), externTd); err != nil {
761765
glog.Fatalf("failed to write header total difficulty: %v", err)
762766
}
763767
if err := WriteHeader(self.chainDb, header); err != nil {
@@ -1052,14 +1056,18 @@ func (self *BlockChain) WriteBlock(block *types.Block) (status writeStatus, err
10521056
if ptd == nil {
10531057
return NonStatTy, ParentError(block.ParentHash())
10541058
}
1055-
td := new(big.Int).Add(block.Difficulty(), ptd)
1059+
1060+
localTd := self.GetTd(self.currentBlock.Hash())
1061+
externTd := new(big.Int).Add(block.Difficulty(), ptd)
10561062

10571063
// Make sure no inconsistent state is leaked during insertion
10581064
self.mu.Lock()
10591065
defer self.mu.Unlock()
10601066

10611067
// If the total difficulty is higher than our known, add it to the canonical chain
1062-
if td.Cmp(self.GetTd(self.currentBlock.Hash())) > 0 {
1068+
// Second clause in the if statement reduces the vulnerability to selfish mining.
1069+
// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
1070+
if externTd.Cmp(localTd) > 0 || (externTd.Cmp(localTd) == 0 && mrand.Float64() < 0.5) {
10631071
// Reorganize the chain if the parent is not the head block
10641072
if block.ParentHash() != self.currentBlock.Hash() {
10651073
if err := self.reorg(self.currentBlock, block); err != nil {
@@ -1073,7 +1081,7 @@ func (self *BlockChain) WriteBlock(block *types.Block) (status writeStatus, err
10731081
status = SideStatTy
10741082
}
10751083
// Irrelevant of the canonical status, write the block itself to the database
1076-
if err := WriteTd(self.chainDb, block.Hash(), td); err != nil {
1084+
if err := WriteTd(self.chainDb, block.Hash(), externTd); err != nil {
10771085
glog.Fatalf("failed to write block total difficulty: %v", err)
10781086
}
10791087
if err := WriteBlock(self.chainDb, block); err != nil {

tests/init.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ var (
4444
"TRANSCT__RandomByteAtTheEnd",
4545
"BLOCK__ZeroByteAtTheEnd",
4646
"TRANSCT__ZeroByteAtTheEnd",
47+
48+
"ChainAtoChainB_blockorder2",
49+
"ChainAtoChainB_blockorder1",
4750
}
4851

4952
/* Go client does not support transaction (account) nonces above 2^64. This

0 commit comments

Comments
 (0)