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.

98 lines
3.3 KiB

  1. package globals
  2. import "net/url"
  3. import "golang.org/x/net/proxy"
  4. import "github.com/sirupsen/logrus"
  5. import log "github.com/sirupsen/logrus"
  6. import "github.com/deroproject/derosuite/config"
  7. type ChainState int // block chain can only be in 2 state, either SYNCRONISED or syncing
  8. const (
  9. SYNCRONISED ChainState = iota // 0
  10. SYNCING // 1
  11. )
  12. // all the the global variables used by the program are stored here
  13. // since the entire logic is designed around a state machine driven by external events
  14. // once the core starts nothing changes until there is a network state change
  15. var Incoming_Block = make([]byte, 100) // P2P feeds it, blockchain consumes it
  16. var Outgoing_Block = make([]byte, 100) // blockchain feeds it, P2P consumes it only if a block has been mined
  17. var Incoming_Tx = make([]byte, 100) // P2P feeds it, blockchain consumes it
  18. var Outgoing_Tx = make([]byte, 100) // blockchain feeds it, P2P consumes it only if a user has created a Tx mined
  19. var Subsystem_Active uint32 // atomic counter to show how many subsystems are active
  20. var Exit_In_Progress bool
  21. // on init this variable is updated to setup global config in 1 go
  22. var Config config.CHAIN_CONFIG
  23. // global logger all components will use it with context
  24. var Logger *logrus.Logger
  25. var Log_Level = logrus.InfoLevel // default is info level
  26. var ilog_formatter *logrus.TextFormatter // used while tracing code
  27. var Dialer proxy.Dialer = proxy.Direct // for proxy and direct connections
  28. // all outgoing connections , including DNS requests must be made using this
  29. // all program arguments are available here
  30. var Arguments map[string]interface{}
  31. func Initialize() {
  32. var err error
  33. _ = err
  34. Config = config.Mainnet // default is mainnnet
  35. if Arguments["--testnet"].(bool) == true { // setup testnet if requested
  36. Config = config.Testnet
  37. }
  38. // formatter := &logrus.TextFormatter{DisableColors : true}
  39. //Logger= &logrus.Logger{Formatter:formatter}
  40. Logger = logrus.New()
  41. //Logger.Formatter = &logrus.TextFormatter{DisableColors : true}
  42. Logger.SetLevel(logrus.InfoLevel)
  43. if Arguments["--debug"].(bool) == true { // setup debug mode if requested
  44. Log_Level = logrus.DebugLevel
  45. Logger.SetLevel(logrus.DebugLevel)
  46. }
  47. // choose socks based proxy if user requested so
  48. if Arguments["--socks-proxy"] != nil {
  49. log.Debugf("Setting up proxy using %s", Arguments["--socks-proxy"].(string))
  50. //uri, err := url.Parse("socks5://127.0.0.1:9000") // "socks5://demo:demo@192.168.99.100:1080"
  51. uri, err := url.Parse("socks5://" + Arguments["--socks-proxy"].(string)) // "socks5://demo:demo@192.168.99.100:1080"
  52. if err != nil {
  53. log.Fatalf("Error parsing socks proxy: err %s", err)
  54. }
  55. Dialer, err = proxy.FromURL(uri, proxy.Direct)
  56. if err != nil {
  57. log.Fatalf("Error creating socks proxy: err \"%s\" from data %s ", err, Arguments["--socks-proxy"].(string))
  58. }
  59. }
  60. // windows and logrus have issues while printing colored messages, so disable them right now
  61. ilog_formatter = &logrus.TextFormatter{} // this needs to be created after after top logger has been intialised
  62. ilog_formatter.DisableColors = true
  63. ilog_formatter.DisableTimestamp = true
  64. }
  65. /* this function converts a logrus entry into a txt formater based entry with no colors for tracing*/
  66. func CTXString(entry *logrus.Entry) string {
  67. entry.Level = logrus.DebugLevel
  68. data, err := ilog_formatter.Format(entry)
  69. _ = err
  70. return string(data)
  71. }