Skip to content

Commit

Permalink
add cmd argument: minichainwork
Browse files Browse the repository at this point in the history
  • Loading branch information
whunmr committed Dec 3, 2018
1 parent 790424e commit 05a159c
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 13 deletions.
1 change: 1 addition & 0 deletions conf/opts.go
Expand Up @@ -33,6 +33,7 @@ type Opts struct {
MaxMempool int64 `long:"maxmempool" default:"300000000"`
SpendZeroConfChange uint8 `long:"spendzeroconfchange" default:"1"`
MaxTimeAdjustment uint64 `long:"maxtimeadjustment" default:"4200" description:"Maximum allowed median peer time offset adjustment. Local perspective of time may be influenced by peers forward or backward by this amount."`
MinimumChainWork string `long:"minimumchainwork"`
}

func InitArgs(args []string) (*Opts, error) {
Expand Down
2 changes: 2 additions & 0 deletions initmain.go
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/copernet/copernicus/model"
"github.com/copernet/copernicus/model/chain"
"github.com/copernet/copernicus/model/mempool"
"github.com/copernet/copernicus/model/pow"
"github.com/copernet/copernicus/model/utxo"
"github.com/copernet/copernicus/model/wallet"
"github.com/copernet/copernicus/persist"
Expand All @@ -35,6 +36,7 @@ func appInitMain(args []string) {
} else if conf.Cfg.P2PNet.RegTest {
model.SetRegTestParams()
}
pow.UpdateMinimumChainWork()

fmt.Println("Current data dir:\033[0;32m", conf.DataDir, "\033[0m")

Expand Down
3 changes: 2 additions & 1 deletion logic/lchain/lchain.go
Expand Up @@ -80,8 +80,9 @@ func ConnectBlock(pblock *block.Block, pindex *blockindex.BlockIndex, view *utxo
// selection of any particular chain but makes validating some faster by
// effectively caching the result of part of the verification.
if bi := gChain.FindBlockIndex(chain.HashAssumeValid); bi != nil {
minimumChainWork := pow.MiniChainWork()
if bi.GetAncestor(pindex.Height) == pindex && tip.GetAncestor(pindex.Height) == pindex &&
tip.ChainWork.Cmp(pow.HashToBig(&params.MinimumChainWork)) > 0 {
tip.ChainWork.Cmp(&minimumChainWork) > 0 {
// This block is a member of the assumed verified chain and an
// ancestor of the best header. The equivalent time check
// discourages hashpower from extorting the network via DOS
Expand Down
2 changes: 2 additions & 0 deletions model/bitcoinparams.go
Expand Up @@ -26,6 +26,8 @@ var (
// 2^255 -1
regressingPowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 255), bigOne)
testNetPowLimit = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 224), bigOne)

miniChainWork big.Int
)

type ChainTxData struct {
Expand Down
5 changes: 2 additions & 3 deletions model/chain/syncing_state.go
@@ -1,7 +1,6 @@
package chain

import (
"github.com/copernet/copernicus/model"
"github.com/copernet/copernicus/model/blockindex"
"github.com/copernet/copernicus/model/pow"
"github.com/copernet/copernicus/util"
Expand All @@ -27,8 +26,8 @@ func isRecentTip(tip *blockindex.BlockIndex) bool {
}

func hasEnoughWork(tip *blockindex.BlockIndex) bool {
minWorkSum := pow.HashToBig(&model.ActiveNetParams.MinimumChainWork)
return tip.ChainWork.Cmp(minWorkSum) > 0
minWorkSum := pow.MiniChainWork()
return tip.ChainWork.Cmp(&minWorkSum) > 0
}

func (ds *SyncingState) IsAlmostSynced() bool {
Expand Down
19 changes: 19 additions & 0 deletions model/pow/difficulty.go
Expand Up @@ -5,8 +5,10 @@
package pow

import (
"github.com/copernet/copernicus/conf"
"math"
"math/big"
"strings"

"github.com/copernet/copernicus/model"
"github.com/copernet/copernicus/model/blockindex"
Expand All @@ -21,6 +23,8 @@ var (
// oneLsh256 is 1 shifted left 256 bits. It is defined here to avoid
// the overhead of creating it multiple times.
oneLsh256 = new(big.Int).Lsh(bigOne, 256)

miniChainWork big.Int
)

// HashToBig converts a chainHash.Hash into a big.Int that can be used to
Expand Down Expand Up @@ -189,3 +193,18 @@ func bits(w []big.Word) uint {
}
return 0
}

func MiniChainWork() big.Int {
return miniChainWork
}

func UpdateMinimumChainWork() {
miniChainWork = *HashToBig(&model.ActiveNetParams.MinimumChainWork)

mcw := strings.TrimPrefix(conf.Args.MinimumChainWork, "0x")

hash, err := util.GetHashFromStr(mcw)
if err == nil && !hash.IsNull() {
miniChainWork = *HashToBig(hash)
}
}
52 changes: 52 additions & 0 deletions model/pow/difficulty_test.go
Expand Up @@ -5,8 +5,11 @@
package pow

import (
"github.com/copernet/copernicus/conf"
"github.com/copernet/copernicus/model"
"github.com/copernet/copernicus/model/blockindex"
"github.com/copernet/copernicus/util"
"github.com/stretchr/testify/assert"
"math/big"
"testing"
)
Expand Down Expand Up @@ -107,3 +110,52 @@ func TestGetBlockProofEquivalentTime(t *testing.T) {
t.Errorf("GetBlockProofEquivalentTime2 Error, exp = %d, actual = %d", exp, actual)
}
}

func Test_using_valid_miniChainWork_in_args(t *testing.T) {
conf.Cfg = conf.InitConfig([]string{"--minimumchainwork=65"}) //0x65

UpdateMinimumChainWork()

mcw := MiniChainWork()
assert.Equal(t, *HashToBig(util.HashFromString("65")), mcw)
assert.Equal(t, *big.NewInt(0x65), mcw)
}

func Test_using_valid_hex_format_miniChainWork_in_args(t *testing.T) {
conf.Cfg = conf.InitConfig([]string{"--minimumchainwork=0x65"}) //0x65

UpdateMinimumChainWork()

mcw := MiniChainWork()
assert.Equal(t, *HashToBig(util.HashFromString("65")), mcw)
assert.Equal(t, *big.NewInt(0x65), mcw)
}

func Test_using_default_minimum_chain_work_if_miniChainWork_in_args_is_empty(t *testing.T) {
conf.Cfg = conf.InitConfig([]string{"--minimumchainwork=\"\""})

UpdateMinimumChainWork()

assert.Equal(t, *HashToBig(&model.ActiveNetParams.MinimumChainWork), MiniChainWork())
}

func Test_using_default_minimum_chain_work_if_miniChainWork_in_args_is_malformed(t *testing.T) {
conf.Cfg = conf.InitConfig([]string{"--minimumchainwork=abc_invalid_minichainwork_value"}) //0x65

UpdateMinimumChainWork()

assert.Equal(t, *HashToBig(&model.ActiveNetParams.MinimumChainWork), MiniChainWork())
}

func Test_using_default_minimum_chain_work_in_regtest(t *testing.T) {
conf.Cfg = conf.InitConfig([]string{"--minimumchainwork=abc_invalid_minichainwork_value"}) //0x65
model.SetRegTestParams()
UpdateMinimumChainWork()

defaultMCW := *HashToBig(&model.ActiveNetParams.MinimumChainWork)
assert.True(t, defaultMCW.Cmp(big.NewInt(-1)) == 1)
assert.True(t, defaultMCW.Cmp(big.NewInt(0)) == 0)
assert.True(t, defaultMCW.Cmp(big.NewInt(1)) == -1)

assert.Equal(t, defaultMCW, MiniChainWork())
}
18 changes: 9 additions & 9 deletions net/syncmanager/syncmanager.go
Expand Up @@ -6,6 +6,7 @@ package syncmanager

import (
"container/list"
"github.com/copernet/copernicus/model/pow"
"net"
"sync"
"sync/atomic"
Expand All @@ -21,7 +22,6 @@ import (
"github.com/copernet/copernicus/model/chain"
"github.com/copernet/copernicus/model/mempool"
"github.com/copernet/copernicus/model/outpoint"
"github.com/copernet/copernicus/model/pow"
"github.com/copernet/copernicus/model/tx"
"github.com/copernet/copernicus/model/utxo"
"github.com/copernet/copernicus/net/wire"
Expand Down Expand Up @@ -698,17 +698,17 @@ func (sm *SyncManager) fetchHeaderBlocks(peer *peer.Peer) {
return
}

minWorkSum := pow.HashToBig(&model.ActiveNetParams.MinimumChainWork)
if pindexBestKnownBlock.ChainWork.Cmp(&(gChain.Tip().ChainWork)) == -1 ||
pindexBestKnownBlock.ChainWork.Cmp(minWorkSum) == -1 {
log.Info("peer(%d) ChainWork less than us, hash nothing interesting",
peer.ID())
return
}

pindexWalk = gChain.FindFork(pindexBestKnownBlock)
}

minWorkSum := pow.MiniChainWork()
if pindexBestKnownBlock.ChainWork.Cmp(&(gChain.Tip().ChainWork)) == -1 ||
pindexBestKnownBlock.ChainWork.Cmp(&minWorkSum) == -1 {
log.Info("peer(%d) ChainWork less than us, hash nothing interesting",
peer.ID())
return
}

vToFetch := list.New()
// Never fetch further than the best block we know the peer has, or more
// than BLOCK_DOWNLOAD_WINDOW + 1 beyond the last linked block we have in
Expand Down

0 comments on commit 05a159c

Please sign in to comment.