From baa95d053c40e380047ae4f7fe3f31bf3b2aed5b Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Mon, 21 Jan 2019 08:28:50 +0000 Subject: [PATCH 1/7] reduce wait to join shard Signed-off-by: Leo Chen --- cmd/client/txgen/main.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/client/txgen/main.go b/cmd/client/txgen/main.go index 2b849d681..02bb51847 100644 --- a/cmd/client/txgen/main.go +++ b/cmd/client/txgen/main.go @@ -165,14 +165,14 @@ func main() { log.Debug("Client Join Shard", "leader", leader) clientNode.GetHost().AddPeer(&leader) go clientNode.JoinShard(leader) - // wait for 3 seconds for client to send ping message to leader - time.Sleep(3 * time.Second) - clientNode.StopPing <- struct{}{} - clientNode.State = node.NodeJoinedShard } + // wait for 1 seconds for client to send ping message to leader + time.Sleep(time.Second) + clientNode.StopPing <- struct{}{} + clientNode.State = node.NodeJoinedShard // Transaction generation process - time.Sleep(5 * time.Second) // wait for nodes to be ready + time.Sleep(2 * time.Second) // wait for nodes to be ready start := time.Now() totalTime := float64(*duration) From 94a17e5b2a1ddc887e747b6bee8229c82043fc21 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Mon, 21 Jan 2019 14:48:17 -0800 Subject: [PATCH 2/7] script to run leader only for debugging --- test/leader_run.sh | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100755 test/leader_run.sh diff --git a/test/leader_run.sh b/test/leader_run.sh new file mode 100755 index 000000000..cfbef60dd --- /dev/null +++ b/test/leader_run.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +ROOT=$(dirname $0)/.. +USER=$(whoami) +DRYRUN= + +set -x +set -eo pipefail + +echo "compiling ..." +go build -o bin/harmony cmd/harmony.go +go build -o bin/beacon cmd/beaconchain/main.go + + +# Create a tmp folder for logs +t=`date +"%Y%m%d-%H%M%S"` +log_folder="tmp_log/log-$t" + +mkdir -p $log_folder +LOG_FILE=$log_folder/r.log +rm -f bc_config.json + +echo "launching beacon chain ..." +$DRYRUN $ROOT/bin/beacon -numShards 3 > $log_folder/beacon.log 2>&1 | tee -a $LOG_FILE & +sleep 2 #waiting for beaconchain +MA=$(grep "Beacon Chain Started" $log_folder/beacon.log | awk -F: ' { print $2 } ') + +if [ -n "$MA" ]; then + HMY_OPT="-bc_addr $MA" +fi + +DB='-db_supported' + +$ROOT/bin/harmony -ip 127.0.0.1 -port 9000 -log_folder $log_folder $DB -min_peers 0 $HMY_OPT 2>&1 | tee -a $LOG_FILE & From 2330504e1363ba1b7ece626741ce67cc1d2b1082 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Mon, 21 Jan 2019 14:48:54 -0800 Subject: [PATCH 3/7] clean up /tmp/harmony before running experiment --- test/deploy.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/test/deploy.sh b/test/deploy.sh index 1f9ea9305..905ba7688 100755 --- a/test/deploy.sh +++ b/test/deploy.sh @@ -24,6 +24,7 @@ function cleanup() { done # Remove bc_config.json before starting experiment. rm -f bc_config.json + rm -rf /tmp/harmony* } function killnode() { From e3f09e5a29955e33c0de13fa53866a077d5d0f98 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Mon, 21 Jan 2019 14:50:28 -0800 Subject: [PATCH 4/7] make sure db initiliazed before committing genesis block --- core/genesis.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/genesis.go b/core/genesis.go index bce27becc..827e93072 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -23,6 +23,7 @@ import ( "errors" "fmt" "math/big" + "os" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" @@ -33,6 +34,7 @@ import ( "github.com/harmony-one/harmony/core/rawdb" "github.com/harmony-one/harmony/core/state" "github.com/harmony-one/harmony/core/types" + "github.com/harmony-one/harmony/internal/utils" ) //go:generate gencodec -type Genesis -field-override genesisSpecMarshaling -out gen_genesis.go @@ -221,7 +223,9 @@ func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig { // to the given database (or discards it if nil). func (g *Genesis) ToBlock(db ethdb.Database) *types.Block { if db == nil { - db = ethdb.NewMemDatabase() + utils.GetLogInstance().Error("db should be initialized") + os.Exit(1) + // db = ethdb.NewMemDatabase() } statedb, _ := state.New(common.Hash{}, state.NewDatabase(db)) for addr, account := range g.Alloc { From 9762ab75bd7262fda0aab0668d0849200d1f303a Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Mon, 21 Jan 2019 14:53:47 -0800 Subject: [PATCH 5/7] switch using disk-based db by default. logging singleton should have context of the node (ip and port) --- cmd/harmony.go | 7 +++++-- internal/utils/singleton.go | 14 +++++++++++++- node/node.go | 13 +++++++------ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/cmd/harmony.go b/cmd/harmony.go index 329d68aea..05f00674a 100644 --- a/cmd/harmony.go +++ b/cmd/harmony.go @@ -15,10 +15,10 @@ import ( "github.com/harmony-one/harmony/internal/attack" pkg_newnode "github.com/harmony-one/harmony/internal/newnode" "github.com/harmony-one/harmony/internal/profiler" + "github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/node" "github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p/p2pimpl" - peerstore "github.com/libp2p/go-libp2p-peerstore" multiaddr "github.com/multiformats/go-multiaddr" ) @@ -82,7 +82,7 @@ func main() { port := flag.String("port", "9000", "port of the node.") logFolder := flag.String("log_folder", "latest", "the folder collecting the logs of this execution") attackedMode := flag.Int("attacked_mode", 0, "0 means not attacked, 1 means attacked, 2 means being open to be selected as attacked") - dbSupported := flag.Bool("db_supported", false, "false means not db_supported, true means db_supported") + dbSupported := flag.Bool("db_supported", true, "false means not db_supported, true means db_supported") profile := flag.Bool("profile", false, "Turn on profiling (CPU, Memory).") metricsReportURL := flag.String("metrics_report_url", "", "If set, reports metrics to this URL.") versionFlag := flag.Bool("version", false, "Output version info") @@ -102,6 +102,9 @@ func main() { printVersion(os.Args[0]) } + // Logging setup + utils.SetPortAndIP(*port, *ip) + // Add GOMAXPROCS to achieve max performance. runtime.GOMAXPROCS(1024) diff --git a/internal/utils/singleton.go b/internal/utils/singleton.go index 140d9724a..a86e242ca 100644 --- a/internal/utils/singleton.go +++ b/internal/utils/singleton.go @@ -9,6 +9,18 @@ import ( "github.com/ethereum/go-ethereum/log" ) +// Global Port and IP for logging. +var ( + Port string + IP string +) + +// SetPortAndIP used to print out loggings of node with Port and IP. +func SetPortAndIP(port, ip string) { + Port = port + IP = ip +} + // UniqueValidatorID defines the structure of unique validator ID type UniqueValidatorID struct { uniqueID uint32 @@ -37,7 +49,7 @@ func (s *UniqueValidatorID) GetUniqueID() uint32 { // GetLogInstance returns logging singleton. func GetLogInstance() log.Logger { onceForLog.Do(func() { - logInstance = log.New() + logInstance = log.New("port", Port, "ip", IP) }) return logInstance } diff --git a/node/node.go b/node/node.go index 50db3b94d..7c4e2093d 100644 --- a/node/node.go +++ b/node/node.go @@ -209,7 +209,7 @@ func DeserializeNode(d []byte) *NetworkNode { } // New creates a new node. -func New(host host.Host, consensus *bft.Consensus, db *ethdb.LDBDatabase) *Node { +func New(host host.Host, consensus *bft.Consensus, db ethdb.Database) *Node { node := Node{} if host != nil { @@ -218,15 +218,12 @@ func New(host host.Host, consensus *bft.Consensus, db *ethdb.LDBDatabase) *Node } // Logger - node.log = log.New() + node.log = log.New("IP", host.GetSelfPeer().IP, "Port", host.GetSelfPeer().Port) if host != nil && consensus != nil { // Consensus and associated channel to communicate blocks node.Consensus = consensus - // Initialize level db. - node.db = db - // Initialize genesis block and blockchain genesisAlloc := node.CreateGenesisAllocWithTestingAddresses(100) contractKey, _ := ecdsa.GenerateKey(crypto.S256(), strings.NewReader("Test contract key string stream that is fixed so that generated test key are deterministic every time")) @@ -236,7 +233,11 @@ func New(host host.Host, consensus *bft.Consensus, db *ethdb.LDBDatabase) *Node genesisAlloc[contractAddress] = core.GenesisAccount{Balance: contractFunds} node.ContractKeys = append(node.ContractKeys, contractKey) - database := ethdb.NewMemDatabase() + database := db + if database == nil { + database = ethdb.NewMemDatabase() + } + chainConfig := params.TestChainConfig chainConfig.ChainID = big.NewInt(int64(node.Consensus.ShardID)) // Use ChainID as piggybacked ShardID gspec := core.Genesis{ From f1c38ee0d535000d87c804c3abed4c3b78b8ea83 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Mon, 21 Jan 2019 17:16:28 -0800 Subject: [PATCH 6/7] change harmony db path --- cmd/harmony.go | 3 +-- core/genesis.go | 1 - test/deploy.sh | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/cmd/harmony.go b/cmd/harmony.go index 05f00674a..116054233 100644 --- a/cmd/harmony.go +++ b/cmd/harmony.go @@ -49,8 +49,7 @@ func attackDetermination(attackedMode int) bool { // InitLDBDatabase initializes a LDBDatabase. func InitLDBDatabase(ip string, port string) (*ethdb.LDBDatabase, error) { - // TODO(minhdoan): Refactor this. - dbFileName := "/tmp/harmony_" + ip + port + ".dat" + dbFileName := fmt.Sprintf("./db/harmony_%s_%s", ip, port) var err = os.RemoveAll(dbFileName) if err != nil { fmt.Println(err.Error()) diff --git a/core/genesis.go b/core/genesis.go index 827e93072..828276052 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -225,7 +225,6 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block { if db == nil { utils.GetLogInstance().Error("db should be initialized") os.Exit(1) - // db = ethdb.NewMemDatabase() } statedb, _ := state.New(common.Hash{}, state.NewDatabase(db)) for addr, account := range g.Alloc { diff --git a/test/deploy.sh b/test/deploy.sh index 905ba7688..1dee068d0 100755 --- a/test/deploy.sh +++ b/test/deploy.sh @@ -24,7 +24,7 @@ function cleanup() { done # Remove bc_config.json before starting experiment. rm -f bc_config.json - rm -rf /tmp/harmony* + rm -rf ./db/harmony_* } function killnode() { From a1c0df83aff2162869e5f30462b35923b6c94c42 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Mon, 21 Jan 2019 17:56:12 -0800 Subject: [PATCH 7/7] add flag to remove disk db before running --- cmd/harmony.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/cmd/harmony.go b/cmd/harmony.go index 116054233..eac75505d 100644 --- a/cmd/harmony.go +++ b/cmd/harmony.go @@ -48,11 +48,13 @@ func attackDetermination(attackedMode int) bool { } // InitLDBDatabase initializes a LDBDatabase. -func InitLDBDatabase(ip string, port string) (*ethdb.LDBDatabase, error) { +func InitLDBDatabase(ip string, port string, freshDB bool) (*ethdb.LDBDatabase, error) { dbFileName := fmt.Sprintf("./db/harmony_%s_%s", ip, port) - var err = os.RemoveAll(dbFileName) - if err != nil { - fmt.Println(err.Error()) + if freshDB { + var err = os.RemoveAll(dbFileName) + if err != nil { + fmt.Println(err.Error()) + } } return ethdb.NewLDBDatabase(dbFileName, 0, 0) } @@ -82,6 +84,7 @@ func main() { logFolder := flag.String("log_folder", "latest", "the folder collecting the logs of this execution") attackedMode := flag.Int("attacked_mode", 0, "0 means not attacked, 1 means attacked, 2 means being open to be selected as attacked") dbSupported := flag.Bool("db_supported", true, "false means not db_supported, true means db_supported") + freshDB := flag.Bool("fresh_db", false, "true means the existing disk based db will be removed") profile := flag.Bool("profile", false, "Turn on profiling (CPU, Memory).") metricsReportURL := flag.String("metrics_report_url", "", "If set, reports metrics to this URL.") versionFlag := flag.Bool("version", false, "Output version info") @@ -166,7 +169,7 @@ func main() { var ldb *ethdb.LDBDatabase if *dbSupported { - ldb, _ = InitLDBDatabase(*ip, *port) + ldb, _ = InitLDBDatabase(*ip, *port, *freshDB) } host, err := p2pimpl.NewHost(&selfPeer)