You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
5.0 KiB

  1. // Copyright 2017-2018 DERO Project. All rights reserved.
  2. // Use of this source code in any form is governed by RESEARCH license.
  3. // license can be found in the LICENSE file.
  4. // GPG: 0F39 E425 8C65 3947 702A 8234 08B2 0360 A03A 9DE8
  5. //
  6. //
  7. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  8. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  10. // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  13. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  14. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  15. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. package globals
  17. import "net/url"
  18. import "strconv"
  19. import "golang.org/x/net/proxy"
  20. import "github.com/sirupsen/logrus"
  21. import log "github.com/sirupsen/logrus"
  22. import "github.com/arnaucode/derosuite/config"
  23. type ChainState int // block chain can only be in 2 state, either SYNCRONISED or syncing
  24. const (
  25. SYNCRONISED ChainState = iota // 0
  26. SYNCING // 1
  27. )
  28. // all the the global variables used by the program are stored here
  29. // since the entire logic is designed around a state machine driven by external events
  30. // once the core starts nothing changes until there is a network state change
  31. var Incoming_Block = make([]byte, 100) // P2P feeds it, blockchain consumes it
  32. var Outgoing_Block = make([]byte, 100) // blockchain feeds it, P2P consumes it only if a block has been mined
  33. var Incoming_Tx = make([]byte, 100) // P2P feeds it, blockchain consumes it
  34. var Outgoing_Tx = make([]byte, 100) // blockchain feeds it, P2P consumes it only if a user has created a Tx mined
  35. var Subsystem_Active uint32 // atomic counter to show how many subsystems are active
  36. var Exit_In_Progress bool
  37. // on init this variable is updated to setup global config in 1 go
  38. var Config config.CHAIN_CONFIG
  39. // global logger all components will use it with context
  40. var Logger *logrus.Logger
  41. var Log_Level = logrus.InfoLevel // default is info level
  42. var ilog_formatter *logrus.TextFormatter // used while tracing code
  43. var Dialer proxy.Dialer = proxy.Direct // for proxy and direct connections
  44. // all outgoing connections , including DNS requests must be made using this
  45. // all program arguments are available here
  46. var Arguments map[string]interface{}
  47. func Initialize() {
  48. var err error
  49. _ = err
  50. Config = config.Mainnet // default is mainnnet
  51. if Arguments["--testnet"].(bool) == true { // setup testnet if requested
  52. Config = config.Testnet
  53. }
  54. // formatter := &logrus.TextFormatter{DisableColors : true}
  55. //Logger= &logrus.Logger{Formatter:formatter}
  56. Logger = logrus.New()
  57. //Logger.Formatter = &logrus.TextFormatter{DisableColors : true}
  58. Logger.SetLevel(logrus.InfoLevel)
  59. if Arguments["--debug"].(bool) == true { // setup debug mode if requested
  60. Log_Level = logrus.DebugLevel
  61. Logger.SetLevel(logrus.DebugLevel)
  62. }
  63. // choose socks based proxy if user requested so
  64. if Arguments["--socks-proxy"] != nil {
  65. log.Debugf("Setting up proxy using %s", Arguments["--socks-proxy"].(string))
  66. //uri, err := url.Parse("socks5://127.0.0.1:9000") // "socks5://demo:demo@192.168.99.100:1080"
  67. uri, err := url.Parse("socks5://" + Arguments["--socks-proxy"].(string)) // "socks5://demo:demo@192.168.99.100:1080"
  68. if err != nil {
  69. log.Fatalf("Error parsing socks proxy: err %s", err)
  70. }
  71. Dialer, err = proxy.FromURL(uri, proxy.Direct)
  72. if err != nil {
  73. log.Fatalf("Error creating socks proxy: err \"%s\" from data %s ", err, Arguments["--socks-proxy"].(string))
  74. }
  75. }
  76. // windows and logrus have issues while printing colored messages, so disable them right now
  77. ilog_formatter = &logrus.TextFormatter{} // this needs to be created after after top logger has been intialised
  78. ilog_formatter.DisableColors = true
  79. ilog_formatter.DisableTimestamp = true
  80. }
  81. // tells whether we are in mainnet mode
  82. // if we are not mainnet, we are a testnet,
  83. // we will only have a single mainnet ,( but we may have one or more testnets )
  84. func IsMainnet() bool {
  85. if Config.Name == "mainnet" {
  86. return true
  87. }
  88. return false
  89. }
  90. /* this function converts a logrus entry into a txt formater based entry with no colors for tracing*/
  91. func CTXString(entry *logrus.Entry) string {
  92. entry.Level = logrus.DebugLevel
  93. data, err := ilog_formatter.Format(entry)
  94. _ = err
  95. return string(data)
  96. }
  97. // never do any division operation on money
  98. // ISSUE: dividing 10 into 3 parts will give 3.33.. etc., adding result 3 times will 9.9999, so some money is lost
  99. //
  100. func FormatMoney(amount uint64) string {
  101. amountf := float64(amount) / 1000000000000.0 // float64 gives 14 char precision, we need only 12
  102. return strconv.FormatFloat(amountf, 'f', 12, 64)
  103. }