Skip to content

Commit

Permalink
Support --testnet and --regtest. By default, mainnet is enabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
qiwei9743 committed Sep 27, 2018
1 parent dc98b07 commit 9785875
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
25 changes: 21 additions & 4 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"reflect"
"time"

"errors"
"github.com/spf13/viper"
"gopkg.in/go-playground/validator.v8"
"path"
)

const (
Expand Down Expand Up @@ -69,11 +71,19 @@ func InitConfig(args []string) *Configuration {
if err != nil {
panic(err)
}
if opts.RegTest && opts.TestNet {
panic("Both testnet and regtest are true")
}

if len(opts.DataDir) > 0 {
DataDir = opts.DataDir
}

discover := opts.Discover
if opts.TestNet {
DataDir = path.Join(DataDir, "testnet")
} else if opts.RegTest {
DataDir = path.Join(DataDir, "regtest")
}

if !ExistDataDir(DataDir) {
err := os.MkdirAll(DataDir, os.ModePerm)
Expand Down Expand Up @@ -146,7 +156,13 @@ func InitConfig(args []string) *Configuration {

config.RPC.RPCKey = filepath.Join(defaultDataDir, "rpc.key")
config.RPC.RPCCert = filepath.Join(defaultDataDir, "rpc.cert")
config.P2PNet.Discover = discover == 1

if opts.RegTest {
config.P2PNet.RegTest = true
}
if opts.TestNet {
config.P2PNet.TestNet = true
}
return config
}

Expand Down Expand Up @@ -194,7 +210,9 @@ type Configuration struct {
ConnectPeersOnStart []string
DisableBanning bool `default:"true"`
BanThreshold uint32
SimNet bool `default:"false"`
TestNet bool
RegTest bool `default:"false"`
SimNet bool
DisableListen bool `default:"true"`
BlocksOnly bool `default:"true"` //Do not accept transactions from remote peers.
BanDuration time.Duration // How long to ban misbehaving peers
Expand All @@ -208,7 +226,6 @@ type Configuration struct {
Upnp bool `default:"false"` // Use UPnP to map our listening port outside of NAT
ExternalIPs []string // Add an ip to the list of local addresses we claim to listen on to peers
//AddCheckpoints []model.Checkpoint
Discover bool // Is our peer's addrLocal potentially useful as an external IP source
}
AddrMgr struct {
SimNet bool
Expand Down
8 changes: 5 additions & 3 deletions conf/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
type Opts struct {
DataDir string `long:"datadir" description:"specified program data dir"`

//Set -discover=0 in regtest framework
Discover int `long:"discover" default:"1" description:"Discover own IP addresses (default: 1 when listening and no -externalip or -proxy) "`
// //Set -discover=0 in regtest framework
// Discover int `long:"discover" default:"1" description:"Discover own IP addresses (default: 1 when listening and no -externalip or -proxy) "`
RegTest bool `long:"regtest" description:"initiate regtest"`
TestNet bool `long:"testnet" description:"initiate testnet"`
}

func InitArgs(args []string) (*Opts, error) {
Expand All @@ -22,5 +24,5 @@ func InitArgs(args []string) (*Opts, error) {
}

func (opts *Opts) String() string {
return fmt.Sprintf("datadir:%s ,Discover:%d", opts.DataDir, opts.Discover)
return fmt.Sprintf("datadir:%s regtest:%v testnet:%v", opts.DataDir, opts.RegTest, opts.TestNet)
}
10 changes: 9 additions & 1 deletion initmain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package main

import (
"fmt"

"github.com/copernet/copernicus/conf"
"github.com/copernet/copernicus/crypto"
"github.com/copernet/copernicus/log"
"github.com/copernet/copernicus/logic/lblockindex"
"github.com/copernet/copernicus/logic/lchain"
"github.com/copernet/copernicus/model"
"github.com/copernet/copernicus/model/chain"
"github.com/copernet/copernicus/model/mempool"
"github.com/copernet/copernicus/model/utxo"
Expand All @@ -16,8 +18,14 @@ import (
)

func appInitMain(args []string) {
//init config
conf.Cfg = conf.InitConfig(args)

if conf.Cfg.P2PNet.TestNet {
model.SetTestNetParams()
} else if conf.Cfg.P2PNet.RegTest {
model.SetRegTestParams()
}

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

//init log
Expand Down
10 changes: 9 additions & 1 deletion model/bitcoinparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

const AntiReplayCommitment = "Bitcoin: A Peer-to-Peer Electronic Cash System"

var ActiveNetParams = &TestNetParams
var ActiveNetParams = &MainNetParams

var (
bigOne = big.NewInt(1)
Expand Down Expand Up @@ -443,3 +443,11 @@ func IsDAAEnabled(height int32) bool {
func IsReplayProtectionEnabled(medianTimePast int64) bool {
return medianTimePast >= ActiveNetParams.MagneticAnomalyActivationTime
}

func SetTestNetParams() {
ActiveNetParams = &TestNetParams
}

func SetRegTestParams() {
ActiveNetParams = &RegressionNetParams
}
4 changes: 0 additions & 4 deletions net/addrmgr/addrmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,6 @@ func (a *AddrManager) updateAddress(netAddr, srcAddr *wire.NetAddress) {
if netAddr != nil {
log.Trace("updateAddress srcAddr(%s)\n", srcAddr.String())
}
//todo external IP is no longer added
if conf.Cfg != nil && !conf.Cfg.P2PNet.Discover {
return
}

// Filter out non-routable addresses. Note that non-routable
// also includes invalid and local addresses.
Expand Down

0 comments on commit 9785875

Please sign in to comment.