From 8bd76f14e18b79ad0f33a570c258f178eef47742 Mon Sep 17 00:00:00 2001 From: Gheis <36589218+GheisMohammadi@users.noreply.github.com> Date: Wed, 25 May 2022 23:55:40 +0800 Subject: [PATCH] add new config to set local accounts from harmony config (#4177) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add new config to set local accounts from harmony config * removed extra if for checking flag change and fix format Co-authored-by: “GheisMohammadi” <“Gheis.Mohammadi@gmail.com”> --- .hmy/locals.txt | 0 cmd/harmony/config_migrations_test.go | 4 ++ cmd/harmony/config_test.go | 1 + cmd/harmony/default.go | 7 ++-- cmd/harmony/flags.go | 9 +++++ cmd/harmony/flags_test.go | 44 ++++++++++++++-------- cmd/harmony/main.go | 53 ++++++++++++++++++++++++++- internal/configs/harmony/harmony.go | 7 ++-- node/node.go | 2 + node/node_handler_test.go | 6 +-- node/node_newblock_test.go | 2 +- node/node_test.go | 4 +- rosetta/infra/harmony-mainnet.conf | 1 + rosetta/infra/harmony-pstn.conf | 1 + 14 files changed, 113 insertions(+), 28 deletions(-) create mode 100644 .hmy/locals.txt diff --git a/.hmy/locals.txt b/.hmy/locals.txt new file mode 100644 index 000000000..e69de29bb diff --git a/cmd/harmony/config_migrations_test.go b/cmd/harmony/config_migrations_test.go index 6f7047c7d..031978448 100644 --- a/cmd/harmony/config_migrations_test.go +++ b/cmd/harmony/config_migrations_test.go @@ -71,6 +71,7 @@ Version = "1.0.2" [TxPool] BlacklistFile = "./.hmy/blacklist.txt" + LocalAccountsFile = "./.hmy/locals.txt" [WS] Enabled = true @@ -140,6 +141,7 @@ Version = "1.0.3" [TxPool] BlacklistFile = "./.hmy/blacklist.txt" + LocalAccountsFile = "./.hmy/locals.txt" [WS] Enabled = true @@ -221,6 +223,7 @@ Version = "1.0.4" [TxPool] BlacklistFile = "./.hmy/blacklist.txt" + LocalAccountsFile = "./.hmy/locals.txt" [WS] Enabled = true @@ -309,6 +312,7 @@ Version = "1.0.4" [TxPool] BlacklistFile = "./.hmy/blacklist.txt" + LocalAccountsFile = "./.hmy/locals.txt" [WS] Enabled = true diff --git a/cmd/harmony/config_test.go b/cmd/harmony/config_test.go index e5ae11435..1f669fef5 100644 --- a/cmd/harmony/config_test.go +++ b/cmd/harmony/config_test.go @@ -83,6 +83,7 @@ Version = "1.0.4" [TxPool] BlacklistFile = "./.hmy/blacklist.txt" + LocalAccountsFile = "./.hmy/locals.txt" [Sync] Downloader = false diff --git a/cmd/harmony/default.go b/cmd/harmony/default.go index 0440789e4..d6527f11f 100644 --- a/cmd/harmony/default.go +++ b/cmd/harmony/default.go @@ -69,9 +69,10 @@ var defaultConfig = harmonyconfig.HarmonyConfig{ KMSConfigFile: "", }, TxPool: harmonyconfig.TxPoolConfig{ - BlacklistFile: "./.hmy/blacklist.txt", - RosettaFixFile: "", - AccountSlots: 16, + BlacklistFile: "./.hmy/blacklist.txt", + RosettaFixFile: "", + AccountSlots: 16, + LocalAccountsFile: "./.hmy/locals.txt", }, Sync: getDefaultSyncConfig(defNetworkType), Pprof: harmonyconfig.PprofConfig{ diff --git a/cmd/harmony/flags.go b/cmd/harmony/flags.go index 9ec6386d1..c7f07b104 100644 --- a/cmd/harmony/flags.go +++ b/cmd/harmony/flags.go @@ -134,6 +134,7 @@ var ( rosettaFixFileFlag, tpBlacklistFileFlag, legacyTPBlacklistFileFlag, + localAccountsFileFlag, } pprofFlags = []cli.Flag{ @@ -1074,6 +1075,11 @@ var ( DefValue: defaultConfig.TxPool.BlacklistFile, Deprecated: "use --txpool.blacklist", } + localAccountsFileFlag = cli.StringFlag{ + Name: "txpool.locals", + Usage: "file of local wallet addresses", + DefValue: defaultConfig.TxPool.LocalAccountsFile, + } ) func applyTxPoolFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfig) { @@ -1092,6 +1098,9 @@ func applyTxPoolFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfig) { } else if cli.IsFlagChanged(cmd, legacyTPBlacklistFileFlag) { config.TxPool.BlacklistFile = cli.GetStringFlagValue(cmd, legacyTPBlacklistFileFlag) } + if cli.IsFlagChanged(cmd, localAccountsFileFlag) { + config.TxPool.LocalAccountsFile = cli.GetStringFlagValue(cmd, localAccountsFileFlag) + } } // pprof flags diff --git a/cmd/harmony/flags_test.go b/cmd/harmony/flags_test.go index 0b0f9efea..a3c9a3dcc 100644 --- a/cmd/harmony/flags_test.go +++ b/cmd/harmony/flags_test.go @@ -105,9 +105,10 @@ func TestHarmonyFlags(t *testing.T) { KMSConfigFile: "config.json", }, TxPool: harmonyconfig.TxPoolConfig{ - BlacklistFile: "./.hmy/blacklist.txt", - RosettaFixFile: "", - AccountSlots: 16, + BlacklistFile: "./.hmy/blacklist.txt", + RosettaFixFile: "", + AccountSlots: 16, + LocalAccountsFile: "./.hmy/locals.txt", }, Pprof: harmonyconfig.PprofConfig{ Enabled: false, @@ -873,33 +874,46 @@ func TestTxPoolFlags(t *testing.T) { { args: []string{}, expConfig: harmonyconfig.TxPoolConfig{ - BlacklistFile: defaultConfig.TxPool.BlacklistFile, - RosettaFixFile: defaultConfig.TxPool.RosettaFixFile, - AccountSlots: defaultConfig.TxPool.AccountSlots, + BlacklistFile: defaultConfig.TxPool.BlacklistFile, + RosettaFixFile: defaultConfig.TxPool.RosettaFixFile, + AccountSlots: defaultConfig.TxPool.AccountSlots, + LocalAccountsFile: defaultConfig.TxPool.LocalAccountsFile, }, }, { args: []string{"--txpool.blacklist", "blacklist.file", "--txpool.rosettafixfile", "rosettafix.file"}, expConfig: harmonyconfig.TxPoolConfig{ - BlacklistFile: "blacklist.file", - RosettaFixFile: "rosettafix.file", - AccountSlots: 16, // default + BlacklistFile: "blacklist.file", + RosettaFixFile: "rosettafix.file", + AccountSlots: 16, // default + LocalAccountsFile: defaultConfig.TxPool.LocalAccountsFile, }, }, { args: []string{"--blacklist", "blacklist.file", "--txpool.rosettafixfile", "rosettafix.file"}, expConfig: harmonyconfig.TxPoolConfig{ - BlacklistFile: "blacklist.file", - RosettaFixFile: "rosettafix.file", - AccountSlots: 16, // default + BlacklistFile: "blacklist.file", + RosettaFixFile: "rosettafix.file", + AccountSlots: 16, // default + LocalAccountsFile: defaultConfig.TxPool.LocalAccountsFile, }, }, { args: []string{"--txpool.accountslots", "5", "--txpool.blacklist", "blacklist.file", "--txpool.rosettafixfile", "rosettafix.file"}, expConfig: harmonyconfig.TxPoolConfig{ - AccountSlots: 5, - BlacklistFile: "blacklist.file", - RosettaFixFile: "rosettafix.file", + AccountSlots: 5, + BlacklistFile: "blacklist.file", + RosettaFixFile: "rosettafix.file", + LocalAccountsFile: defaultConfig.TxPool.LocalAccountsFile, + }, + }, + { + args: []string{"--txpool.locals", "locals.txt"}, + expConfig: harmonyconfig.TxPoolConfig{ + BlacklistFile: defaultConfig.TxPool.BlacklistFile, + RosettaFixFile: defaultConfig.TxPool.RosettaFixFile, + AccountSlots: defaultConfig.TxPool.AccountSlots, + LocalAccountsFile: "locals.txt", }, }, } diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index 455db3277..c30df7ed5 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -666,6 +666,11 @@ func setupConsensusAndNode(hc harmonyconfig.HarmonyConfig, nodeConfig *nodeconfi utils.Logger().Warn().Msgf("Blacklist setup error: %s", err.Error()) } + localAccounts, err := setupLocalAccounts(hc, blacklist) + if err != nil { + utils.Logger().Warn().Msgf("local accounts setup error: %s", err.Error()) + } + // Current node. var chainDBFactory shardchain.DBFactory if hc.ShardData.EnableShardData { @@ -680,7 +685,7 @@ func setupConsensusAndNode(hc harmonyconfig.HarmonyConfig, nodeConfig *nodeconfi chainDBFactory = &shardchain.LDBFactory{RootDir: nodeConfig.DBDir} } - currentNode := node.New(myHost, currentConsensus, chainDBFactory, blacklist, nodeConfig.ArchiveModes(), &hc) + currentNode := node.New(myHost, currentConsensus, chainDBFactory, blacklist, localAccounts, nodeConfig.ArchiveModes(), &hc) if hc.Legacy != nil && hc.Legacy.TPBroadcastInvalidTxn != nil { currentNode.BroadcastInvalidTx = *hc.Legacy.TPBroadcastInvalidTxn @@ -845,6 +850,52 @@ func setupBlacklist(hc harmonyconfig.HarmonyConfig) (map[ethCommon.Address]struc return addrMap, nil } +func setupLocalAccounts(hc harmonyconfig.HarmonyConfig, blacklist map[ethCommon.Address]struct{}) ([]ethCommon.Address, error) { + file := hc.TxPool.LocalAccountsFile + // check if file exist + var fileData string + if _, err := os.Stat(file); err == nil { + b, err := ioutil.ReadFile(file) + if err != nil { + return nil, err + } + fileData = string(b) + } else if errors.Is(err, os.ErrNotExist) { + // file path does not exist + return []ethCommon.Address{}, nil + } else { + // some other errors happened + return nil, err + } + + localAccounts := make(map[ethCommon.Address]struct{}) + lines := strings.Split(fileData, "\n") + for _, line := range lines { + if len(line) != 0 { // the file may have trailing empty string line + trimmedLine := strings.TrimSpace(line) + if strings.HasPrefix(trimmedLine, "#") { //check the line is not commented + continue + } + addr, err := common.Bech32ToAddress(trimmedLine) + if err != nil { + return nil, err + } + // skip the blacklisted addresses + if _, exists := blacklist[addr]; exists { + utils.Logger().Warn().Msgf("local account with address %s is blacklisted", addr.String()) + continue + } + localAccounts[addr] = struct{}{} + } + } + uniqueAddresses := make([]ethCommon.Address, 0, len(localAccounts)) + for addr := range localAccounts { + uniqueAddresses = append(uniqueAddresses, addr) + } + + return uniqueAddresses, nil +} + func listenOSSigAndShutDown(node *node.Node) { // Prepare for graceful shutdown from os signals osSignal := make(chan os.Signal, 1) diff --git a/internal/configs/harmony/harmony.go b/internal/configs/harmony/harmony.go index 75daef9a8..7e83a7ec1 100644 --- a/internal/configs/harmony/harmony.go +++ b/internal/configs/harmony/harmony.go @@ -97,9 +97,10 @@ type BlsConfig struct { } type TxPoolConfig struct { - BlacklistFile string - RosettaFixFile string - AccountSlots uint64 + BlacklistFile string + RosettaFixFile string + AccountSlots uint64 + LocalAccountsFile string } type PprofConfig struct { diff --git a/node/node.go b/node/node.go index 0868f499a..0997ce5a6 100644 --- a/node/node.go +++ b/node/node.go @@ -954,6 +954,7 @@ func New( consensusObj *consensus.Consensus, chainDBFactory shardchain.DBFactory, blacklist map[common.Address]struct{}, + localAccounts []common.Address, isArchival map[uint32]bool, harmonyconfig *harmonyconfig.HarmonyConfig, ) *Node { @@ -1024,6 +1025,7 @@ func New( } if harmonyconfig != nil { txPoolConfig.AccountSlots = harmonyconfig.TxPool.AccountSlots + txPoolConfig.Locals = append(txPoolConfig.Locals, localAccounts...) } txPoolConfig.Blacklist = blacklist diff --git a/node/node_handler_test.go b/node/node_handler_test.go index 535087d07..4158ca88d 100644 --- a/node/node_handler_test.go +++ b/node/node_handler_test.go @@ -39,7 +39,7 @@ func TestAddNewBlock(t *testing.T) { t.Fatalf("Cannot craeate consensus: %v", err) } nodeconfig.SetNetworkType(nodeconfig.Devnet) - node := New(host, consensus, testDBFactory, nil, nil, nil) + node := New(host, consensus, testDBFactory, nil, nil, nil, nil) txs := make(map[common.Address]types.Transactions) stks := staking.StakingTransactions{} @@ -88,7 +88,7 @@ func TestVerifyNewBlock(t *testing.T) { archiveMode := make(map[uint32]bool) archiveMode[0] = true archiveMode[1] = false - node := New(host, consensus, testDBFactory, nil, archiveMode, nil) + node := New(host, consensus, testDBFactory, nil, nil, archiveMode, nil) txs := make(map[common.Address]types.Transactions) stks := staking.StakingTransactions{} @@ -134,7 +134,7 @@ func TestVerifyVRF(t *testing.T) { archiveMode := make(map[uint32]bool) archiveMode[0] = true archiveMode[1] = false - node := New(host, consensus, testDBFactory, nil, archiveMode, nil) + node := New(host, consensus, testDBFactory, nil, nil, archiveMode, nil) consensus.Blockchain = node.Blockchain() txs := make(map[common.Address]types.Transactions) diff --git a/node/node_newblock_test.go b/node/node_newblock_test.go index 8fba8be69..9d1f2c57f 100644 --- a/node/node_newblock_test.go +++ b/node/node_newblock_test.go @@ -40,7 +40,7 @@ func TestFinalizeNewBlockAsync(t *testing.T) { t.Fatalf("Cannot craeate consensus: %v", err) } var testDBFactory = &shardchain.MemDBFactory{} - node := New(host, consensus, testDBFactory, nil, nil, nil) + node := New(host, consensus, testDBFactory, nil, nil, nil, nil) node.Worker.UpdateCurrent() diff --git a/node/node_test.go b/node/node_test.go index 677858284..0160e3122 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -39,7 +39,7 @@ func TestNewNode(t *testing.T) { if err != nil { t.Fatalf("Cannot craeate consensus: %v", err) } - node := New(host, consensus, testDBFactory, nil, nil, nil) + node := New(host, consensus, testDBFactory, nil, nil, nil, nil) if node.Consensus == nil { t.Error("Consensus is not initialized for the node") } @@ -216,7 +216,7 @@ func TestAddBeaconPeer(t *testing.T) { archiveMode := make(map[uint32]bool) archiveMode[0] = true archiveMode[1] = false - node := New(host, consensus, testDBFactory, nil, archiveMode, nil) + node := New(host, consensus, testDBFactory, nil, nil, archiveMode, nil) for _, p := range peers1 { ret := node.AddBeaconPeer(p) if ret { diff --git a/rosetta/infra/harmony-mainnet.conf b/rosetta/infra/harmony-mainnet.conf index e1b96226e..cca238ca0 100644 --- a/rosetta/infra/harmony-mainnet.conf +++ b/rosetta/infra/harmony-mainnet.conf @@ -96,6 +96,7 @@ Version = "2.5.2" BlacklistFile = "./.hmy/blacklist.txt" RosettaFixFile = "./rosetta_local_fix.csv" AccountSlots = 16 + LocalAccountsFile = "./.hmy/locals.txt" [ShardData] EnableShardData = true diff --git a/rosetta/infra/harmony-pstn.conf b/rosetta/infra/harmony-pstn.conf index fc6c12c94..e61f36d9c 100644 --- a/rosetta/infra/harmony-pstn.conf +++ b/rosetta/infra/harmony-pstn.conf @@ -95,6 +95,7 @@ Version = "2.5.2" [TxPool] BlacklistFile = "./.hmy/blacklist.txt" AccountSlots = 16 + LocalAccountsFile = "./.hmy/locals.txt" [ShardData] EnableShardData = false