// Copyright 2018 The dexon-consensus Authors
// This file is part of the dexon-consensus library.
//
// The dexon-consensus library is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The dexon-consensus library is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the dexon-consensus library. If not, see
// <http://www.gnu.org/licenses/>.

package main

import (
	"flag"
	"fmt"
	"log"
	"math/rand"
	"net/http"
	_ "net/http/pprof"
	"os"
	"runtime"
	"runtime/pprof"
	"time"

	"github.com/dexon-foundation/dexon-consensus/simulation"
	"github.com/dexon-foundation/dexon-consensus/simulation/config"
)

var initialize = flag.Bool("init", false, "initialize config file")
var configFile = flag.String("config", "", "path to simulation config file")
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
var logfile = flag.String("log", "", "write log to `file`-nodeID.log")

func main() {
	flag.Parse()
	rand.Seed(time.Now().UnixNano())
	// Supports runtime pprof monitoring.
	go func() {
		log.Println(http.ListenAndServe("localhost:6060", nil))
	}()
	if *configFile == "" {
		fmt.Fprintln(os.Stderr, "error: no configuration file specified")
		os.Exit(1)
	}
	if *initialize {
		if err := config.GenerateDefault(*configFile); err != nil {
			fmt.Fprintf(os.Stderr, "error: %s", err)
			os.Exit(1)
		}
		//os.Exit(0)
	}

	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Fatal("could not create CPU profile: ", err)
		}
		if err := pprof.StartCPUProfile(f); err != nil {
			log.Fatal("could not start CPU profile: ", err)
		}
		defer pprof.StopCPUProfile()
	}

	cfg, err := config.Read(*configFile)
	if err != nil {
		panic(err)
	}
	simulation.Run(cfg, *logfile)

	if *memprofile != "" {
		f, err := os.Create(*memprofile)
		if err != nil {
			log.Fatal("could not create memory profile: ", err)
		}
		runtime.GC() // get up-to-date statistics
		if err := pprof.WriteHeapProfile(f); err != nil {
			log.Fatal("could not write memory profile: ", err)
		}
		// #nosec G104
		f.Close()
	}
}
</option>
<option value='2022Q2'>2022Q2</option>
<option value='2022Q3'>2022Q3</option>
<option value='2022Q4'>2022Q4</option>
<option value='2023Q1'>2023Q1</option>
<option value='2023Q2'>2023Q2</option>
<option value='2023Q3'>2023Q3</option>
<option value='2023Q4'>2023Q4</option>
<option value='2024Q1'>2024Q1</option>
<option value='2024Q2'>2024Q2</option>
<option value='2024Q3'>2024Q3</option>
<option value='2024Q4'>2024Q4</option>
<option value='2025Q1'>2025Q1</option>
<option value='2025Q2'>2025Q2</option>
<option value='2025Q3'>2025Q3</option>
<option value='branches/2014Q1'>branches/2014Q1</option>
<option value='branches/2014Q2'>branches/2014Q2</option>
<option value='branches/2014Q3'>branches/2014Q3</option>
<option value='branches/2014Q4'>branches/2014Q4</option>
<option value='branches/2015Q1'>branches/2015Q1</option>
<option value='branches/2015Q2'>branches/2015Q2</option>
<option value='branches/2015Q3'>branches/2015Q3</option>
<option value='branches/2015Q4'>branches/2015Q4</option>
<option value='branches/2016Q1'>branches/2016Q1</option>
<option value='branches/2016Q2'>branches/2016Q2</option>
<option value='branches/2016Q3'>branches/2016Q3</option>
<option value='branches/2016Q4'>branches/2016Q4</option>
<option value='branches/2017Q1'>branches/2017Q1</option>
<option value='branches/2017Q2'>branches/2017Q2</option>
<option value='branches/2017Q3'>branches/2017Q3</option>
<option value='branches/2017Q4'>branches/2017Q4</option>
<option value='branches/2018Q1'>branches/2018Q1</option>
<option value='branches/2018Q2'>branches/2018Q2</option>
<option value='branches/2018Q3'>branches/2018Q3</option>
<option value='branches/2018Q4'>branches/2018Q4</option>
<option value='branches/2019Q1'>branches/2019Q1</option>
<option value='branches/2019Q2'>branches/2019Q2</option>
<option value='branches/2019Q3'>branches/2019Q3</option>
<option value='branches/2019Q4'>branches/2019Q4</option>
<option value='branches/2020Q1'>branches/2020Q1</option>
<option value='branches/2020Q2'>branches/2020Q2</option>
<option value='branches/2020Q3'>branches/2020Q3</option>
<option value='branches/2020Q4'>branches/2020Q4</option>
<option value='branches/2021Q1'>branches/2021Q1</option>
<option value='branches/RELEASE_8_4_0'>branches/RELEASE_8_4_0</option>
<option value='branches/RELENG_2_1_0'>branches/RELENG_2_1_0</option>
<option value='branches/RELENG_2_2'>branches/RELENG_2_2</option>
<option value='branches/RELENG_9_1_0'>branches/RELENG_9_1_0</option>
<option value='branches/RELENG_9_2_0'>branches/RELENG_9_2_0</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3'>dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/lodash-4.17.15'>dependabot/npm_and_yarn/devel/electron4/files/lodash-4.17.15</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/lodash.merge-4.6.2'>dependabot/npm_and_yarn/devel/electron4/files/lodash.merge-4.6.2</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/lodash.template-4.5.0'>dependabot/npm_and_yarn/devel/electron4/files/lodash.template-4.5.0</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/minimist-1.2.2'>dependabot/npm_and_yarn/devel/electron4/files/minimist-1.2.2</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2'>dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2</option>
<option value='main' selected='selected'>main</option>
<option value='master'>master</option>
<option value='svn_head'>svn_head</option>
</select> <input type='submit' value='switch'/></form></td></tr>
<tr><td class='sub'>FreeBSD Ports (https://github.com/freebsd/freebsd-ports)</td><td class='sub right'></td></tr></table>
<table class='tabs'><tr><td>
<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/about/'>about</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/'>summary</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/refs/?id=8fd27c4e3e4c3bade33746320a49d9900cafcf17'>refs</a><a class='active' href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/net/wifibox-alpine'>log</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/tree/net/wifibox-alpine?id=8fd27c4e3e4c3bade33746320a49d9900cafcf17'>tree</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/commit/net/wifibox-alpine?id=8fd27c4e3e4c3bade33746320a49d9900cafcf17'>commit</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/diff/net/wifibox-alpine?id=8fd27c4e3e4c3bade33746320a49d9900cafcf17'>diff</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/stats/net/wifibox-alpine'>stats</a></td><td class='form'><form class='right' method='get' action='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/net/wifibox-alpine'>
<input type='hidden' name='id' value='8fd27c4e3e4c3bade33746320a49d9900cafcf17'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/?id=8fd27c4e3e4c3bade33746320a49d9900cafcf17'>root</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/net?id=8fd27c4e3e4c3bade33746320a49d9900cafcf17'>net</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/net/wifibox-alpine?id=8fd27c4e3e4c3bade33746320a49d9900cafcf17'>wifibox-alpine</a></div><div class='content'><table class='list nowrap'><tr class='nohover'><th></th><th class='left'>Commit message (<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/net/wifibox-alpine?id=8fd27c4e3e4c3bade33746320a49d9900cafcf17&amp;showmsg=1'>Expand</a>)</th><th class='left'>Author</th><th class='left'>Age</th><th class='left'>Files</th><th class='left'>Lines</th></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/commit/net/wifibox-alpine?id=23aeb02e858c0f9fdb0cc219439c2abc6481b179'>net/wifibox: Update to 1.2.2</a></td><td>PÁLI Gábor János</td><td><span title='2023-03-28 17:07:10 +0800'>2023-03-28</span></td><td>3</td><td><span class='deletions'>-48</span>/<span class='insertions'>+83</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/commit/net/wifibox-alpine?id=1b4f77b589d3a1be9164cf465f60f9b6eb7933f3'>net/wifibox: Update to 1.2.1</a></td><td>PÁLI Gábor János</td><td><span title='2023-02-21 08:51:21 +0800'>2023-02-21</span></td><td>2</td><td><span class='deletions'>-33</span>/<span class='insertions'>+33</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/commit/net/wifibox-alpine?id=e18273038049070e2f249bd40e81f231bd288d24'>net/wifibox: Update to 1.2.0</a></td><td>PÁLI Gábor János</td><td><span title='2023-02-08 01:29:44 +0800'>2023-02-08</span></td><td>4</td><td><span class='deletions'>-102</span>/<span class='insertions'>+152</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/commit/net/wifibox-alpine?id=fb16dfecae4a6efac9f3a78e0b759fb7a3c53de4'>Remove WWW entries moved into port Makefiles</a></td><td>Stefan Eßer</td><td><span title='2022-09-08 05:58:51 +0800'>2022-09-08</span></td><td>1</td><td><span class='deletions'>-2</span>/<span class='insertions'>+0</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/commit/net/wifibox-alpine?id=b7f05445c00f2625aa19b4154ebcbce5ed2daa52'>Add WWW entries to port Makefiles</a></td><td>Stefan Eßer</td><td><span title='2022-09-08 05:10:59 +0800'>2022-09-08</span></td><td>1</td><td><span class='deletions'>-0</span>/<span class='insertions'>+1</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/commit/net/wifibox-alpine?id=69e65b73a5bcbbaf08a5db2c4260043069626395'>net/wifibox: Update to 1.1.1</a></td><td>PÁLI Gábor János</td><td><span title='2022-07-20 19:51:54 +0800'>2022-07-20</span></td><td>2</td><td><span class='deletions'>-21</span>/<span class='insertions'>+29</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/commit/net/wifibox-alpine?id=466b6f0c645fe1cc208bb15c77ba24ebb2813b77'>net/wifibox*: Update to 1.1.0</a></td><td>PÁLI Gábor János</td><td><span title='2022-06-27 19:32:32 +0800'>2022-06-27</span></td><td>3</td><td><span class='deletions'>-77</span>/<span class='insertions'>+151</span></td></tr>