add new config to set local accounts from harmony config (#4177)

* 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”>
pull/4179/head
Gheis 3 years ago committed by GitHub
parent ea96987816
commit 8bd76f14e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 0
      .hmy/locals.txt
  2. 4
      cmd/harmony/config_migrations_test.go
  3. 1
      cmd/harmony/config_test.go
  4. 7
      cmd/harmony/default.go
  5. 9
      cmd/harmony/flags.go
  6. 44
      cmd/harmony/flags_test.go
  7. 53
      cmd/harmony/main.go
  8. 7
      internal/configs/harmony/harmony.go
  9. 2
      node/node.go
  10. 6
      node/node_handler_test.go
  11. 2
      node/node_newblock_test.go
  12. 4
      node/node_test.go
  13. 1
      rosetta/infra/harmony-mainnet.conf
  14. 1
      rosetta/infra/harmony-pstn.conf

@ -71,6 +71,7 @@ Version = "1.0.2"
[TxPool] [TxPool]
BlacklistFile = "./.hmy/blacklist.txt" BlacklistFile = "./.hmy/blacklist.txt"
LocalAccountsFile = "./.hmy/locals.txt"
[WS] [WS]
Enabled = true Enabled = true
@ -140,6 +141,7 @@ Version = "1.0.3"
[TxPool] [TxPool]
BlacklistFile = "./.hmy/blacklist.txt" BlacklistFile = "./.hmy/blacklist.txt"
LocalAccountsFile = "./.hmy/locals.txt"
[WS] [WS]
Enabled = true Enabled = true
@ -221,6 +223,7 @@ Version = "1.0.4"
[TxPool] [TxPool]
BlacklistFile = "./.hmy/blacklist.txt" BlacklistFile = "./.hmy/blacklist.txt"
LocalAccountsFile = "./.hmy/locals.txt"
[WS] [WS]
Enabled = true Enabled = true
@ -309,6 +312,7 @@ Version = "1.0.4"
[TxPool] [TxPool]
BlacklistFile = "./.hmy/blacklist.txt" BlacklistFile = "./.hmy/blacklist.txt"
LocalAccountsFile = "./.hmy/locals.txt"
[WS] [WS]
Enabled = true Enabled = true

@ -83,6 +83,7 @@ Version = "1.0.4"
[TxPool] [TxPool]
BlacklistFile = "./.hmy/blacklist.txt" BlacklistFile = "./.hmy/blacklist.txt"
LocalAccountsFile = "./.hmy/locals.txt"
[Sync] [Sync]
Downloader = false Downloader = false

@ -69,9 +69,10 @@ var defaultConfig = harmonyconfig.HarmonyConfig{
KMSConfigFile: "", KMSConfigFile: "",
}, },
TxPool: harmonyconfig.TxPoolConfig{ TxPool: harmonyconfig.TxPoolConfig{
BlacklistFile: "./.hmy/blacklist.txt", BlacklistFile: "./.hmy/blacklist.txt",
RosettaFixFile: "", RosettaFixFile: "",
AccountSlots: 16, AccountSlots: 16,
LocalAccountsFile: "./.hmy/locals.txt",
}, },
Sync: getDefaultSyncConfig(defNetworkType), Sync: getDefaultSyncConfig(defNetworkType),
Pprof: harmonyconfig.PprofConfig{ Pprof: harmonyconfig.PprofConfig{

@ -134,6 +134,7 @@ var (
rosettaFixFileFlag, rosettaFixFileFlag,
tpBlacklistFileFlag, tpBlacklistFileFlag,
legacyTPBlacklistFileFlag, legacyTPBlacklistFileFlag,
localAccountsFileFlag,
} }
pprofFlags = []cli.Flag{ pprofFlags = []cli.Flag{
@ -1074,6 +1075,11 @@ var (
DefValue: defaultConfig.TxPool.BlacklistFile, DefValue: defaultConfig.TxPool.BlacklistFile,
Deprecated: "use --txpool.blacklist", 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) { 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) { } else if cli.IsFlagChanged(cmd, legacyTPBlacklistFileFlag) {
config.TxPool.BlacklistFile = cli.GetStringFlagValue(cmd, legacyTPBlacklistFileFlag) config.TxPool.BlacklistFile = cli.GetStringFlagValue(cmd, legacyTPBlacklistFileFlag)
} }
if cli.IsFlagChanged(cmd, localAccountsFileFlag) {
config.TxPool.LocalAccountsFile = cli.GetStringFlagValue(cmd, localAccountsFileFlag)
}
} }
// pprof flags // pprof flags

@ -105,9 +105,10 @@ func TestHarmonyFlags(t *testing.T) {
KMSConfigFile: "config.json", KMSConfigFile: "config.json",
}, },
TxPool: harmonyconfig.TxPoolConfig{ TxPool: harmonyconfig.TxPoolConfig{
BlacklistFile: "./.hmy/blacklist.txt", BlacklistFile: "./.hmy/blacklist.txt",
RosettaFixFile: "", RosettaFixFile: "",
AccountSlots: 16, AccountSlots: 16,
LocalAccountsFile: "./.hmy/locals.txt",
}, },
Pprof: harmonyconfig.PprofConfig{ Pprof: harmonyconfig.PprofConfig{
Enabled: false, Enabled: false,
@ -873,33 +874,46 @@ func TestTxPoolFlags(t *testing.T) {
{ {
args: []string{}, args: []string{},
expConfig: harmonyconfig.TxPoolConfig{ expConfig: harmonyconfig.TxPoolConfig{
BlacklistFile: defaultConfig.TxPool.BlacklistFile, BlacklistFile: defaultConfig.TxPool.BlacklistFile,
RosettaFixFile: defaultConfig.TxPool.RosettaFixFile, RosettaFixFile: defaultConfig.TxPool.RosettaFixFile,
AccountSlots: defaultConfig.TxPool.AccountSlots, AccountSlots: defaultConfig.TxPool.AccountSlots,
LocalAccountsFile: defaultConfig.TxPool.LocalAccountsFile,
}, },
}, },
{ {
args: []string{"--txpool.blacklist", "blacklist.file", "--txpool.rosettafixfile", "rosettafix.file"}, args: []string{"--txpool.blacklist", "blacklist.file", "--txpool.rosettafixfile", "rosettafix.file"},
expConfig: harmonyconfig.TxPoolConfig{ expConfig: harmonyconfig.TxPoolConfig{
BlacklistFile: "blacklist.file", BlacklistFile: "blacklist.file",
RosettaFixFile: "rosettafix.file", RosettaFixFile: "rosettafix.file",
AccountSlots: 16, // default AccountSlots: 16, // default
LocalAccountsFile: defaultConfig.TxPool.LocalAccountsFile,
}, },
}, },
{ {
args: []string{"--blacklist", "blacklist.file", "--txpool.rosettafixfile", "rosettafix.file"}, args: []string{"--blacklist", "blacklist.file", "--txpool.rosettafixfile", "rosettafix.file"},
expConfig: harmonyconfig.TxPoolConfig{ expConfig: harmonyconfig.TxPoolConfig{
BlacklistFile: "blacklist.file", BlacklistFile: "blacklist.file",
RosettaFixFile: "rosettafix.file", RosettaFixFile: "rosettafix.file",
AccountSlots: 16, // default AccountSlots: 16, // default
LocalAccountsFile: defaultConfig.TxPool.LocalAccountsFile,
}, },
}, },
{ {
args: []string{"--txpool.accountslots", "5", "--txpool.blacklist", "blacklist.file", "--txpool.rosettafixfile", "rosettafix.file"}, args: []string{"--txpool.accountslots", "5", "--txpool.blacklist", "blacklist.file", "--txpool.rosettafixfile", "rosettafix.file"},
expConfig: harmonyconfig.TxPoolConfig{ expConfig: harmonyconfig.TxPoolConfig{
AccountSlots: 5, AccountSlots: 5,
BlacklistFile: "blacklist.file", BlacklistFile: "blacklist.file",
RosettaFixFile: "rosettafix.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",
}, },
}, },
} }

@ -666,6 +666,11 @@ func setupConsensusAndNode(hc harmonyconfig.HarmonyConfig, nodeConfig *nodeconfi
utils.Logger().Warn().Msgf("Blacklist setup error: %s", err.Error()) 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. // Current node.
var chainDBFactory shardchain.DBFactory var chainDBFactory shardchain.DBFactory
if hc.ShardData.EnableShardData { if hc.ShardData.EnableShardData {
@ -680,7 +685,7 @@ func setupConsensusAndNode(hc harmonyconfig.HarmonyConfig, nodeConfig *nodeconfi
chainDBFactory = &shardchain.LDBFactory{RootDir: nodeConfig.DBDir} 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 { if hc.Legacy != nil && hc.Legacy.TPBroadcastInvalidTxn != nil {
currentNode.BroadcastInvalidTx = *hc.Legacy.TPBroadcastInvalidTxn currentNode.BroadcastInvalidTx = *hc.Legacy.TPBroadcastInvalidTxn
@ -845,6 +850,52 @@ func setupBlacklist(hc harmonyconfig.HarmonyConfig) (map[ethCommon.Address]struc
return addrMap, nil 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) { func listenOSSigAndShutDown(node *node.Node) {
// Prepare for graceful shutdown from os signals // Prepare for graceful shutdown from os signals
osSignal := make(chan os.Signal, 1) osSignal := make(chan os.Signal, 1)

@ -97,9 +97,10 @@ type BlsConfig struct {
} }
type TxPoolConfig struct { type TxPoolConfig struct {
BlacklistFile string BlacklistFile string
RosettaFixFile string RosettaFixFile string
AccountSlots uint64 AccountSlots uint64
LocalAccountsFile string
} }
type PprofConfig struct { type PprofConfig struct {

@ -954,6 +954,7 @@ func New(
consensusObj *consensus.Consensus, consensusObj *consensus.Consensus,
chainDBFactory shardchain.DBFactory, chainDBFactory shardchain.DBFactory,
blacklist map[common.Address]struct{}, blacklist map[common.Address]struct{},
localAccounts []common.Address,
isArchival map[uint32]bool, isArchival map[uint32]bool,
harmonyconfig *harmonyconfig.HarmonyConfig, harmonyconfig *harmonyconfig.HarmonyConfig,
) *Node { ) *Node {
@ -1024,6 +1025,7 @@ func New(
} }
if harmonyconfig != nil { if harmonyconfig != nil {
txPoolConfig.AccountSlots = harmonyconfig.TxPool.AccountSlots txPoolConfig.AccountSlots = harmonyconfig.TxPool.AccountSlots
txPoolConfig.Locals = append(txPoolConfig.Locals, localAccounts...)
} }
txPoolConfig.Blacklist = blacklist txPoolConfig.Blacklist = blacklist

@ -39,7 +39,7 @@ func TestAddNewBlock(t *testing.T) {
t.Fatalf("Cannot craeate consensus: %v", err) t.Fatalf("Cannot craeate consensus: %v", err)
} }
nodeconfig.SetNetworkType(nodeconfig.Devnet) 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) txs := make(map[common.Address]types.Transactions)
stks := staking.StakingTransactions{} stks := staking.StakingTransactions{}
@ -88,7 +88,7 @@ func TestVerifyNewBlock(t *testing.T) {
archiveMode := make(map[uint32]bool) archiveMode := make(map[uint32]bool)
archiveMode[0] = true archiveMode[0] = true
archiveMode[1] = false 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) txs := make(map[common.Address]types.Transactions)
stks := staking.StakingTransactions{} stks := staking.StakingTransactions{}
@ -134,7 +134,7 @@ func TestVerifyVRF(t *testing.T) {
archiveMode := make(map[uint32]bool) archiveMode := make(map[uint32]bool)
archiveMode[0] = true archiveMode[0] = true
archiveMode[1] = false archiveMode[1] = false
node := New(host, consensus, testDBFactory, nil, archiveMode, nil) node := New(host, consensus, testDBFactory, nil, nil, archiveMode, nil)
consensus.Blockchain = node.Blockchain() consensus.Blockchain = node.Blockchain()
txs := make(map[common.Address]types.Transactions) txs := make(map[common.Address]types.Transactions)

@ -40,7 +40,7 @@ func TestFinalizeNewBlockAsync(t *testing.T) {
t.Fatalf("Cannot craeate consensus: %v", err) t.Fatalf("Cannot craeate consensus: %v", err)
} }
var testDBFactory = &shardchain.MemDBFactory{} var testDBFactory = &shardchain.MemDBFactory{}
node := New(host, consensus, testDBFactory, nil, nil, nil) node := New(host, consensus, testDBFactory, nil, nil, nil, nil)
node.Worker.UpdateCurrent() node.Worker.UpdateCurrent()

@ -39,7 +39,7 @@ func TestNewNode(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Cannot craeate consensus: %v", err) 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 { if node.Consensus == nil {
t.Error("Consensus is not initialized for the node") t.Error("Consensus is not initialized for the node")
} }
@ -216,7 +216,7 @@ func TestAddBeaconPeer(t *testing.T) {
archiveMode := make(map[uint32]bool) archiveMode := make(map[uint32]bool)
archiveMode[0] = true archiveMode[0] = true
archiveMode[1] = false archiveMode[1] = false
node := New(host, consensus, testDBFactory, nil, archiveMode, nil) node := New(host, consensus, testDBFactory, nil, nil, archiveMode, nil)
for _, p := range peers1 { for _, p := range peers1 {
ret := node.AddBeaconPeer(p) ret := node.AddBeaconPeer(p)
if ret { if ret {

@ -96,6 +96,7 @@ Version = "2.5.2"
BlacklistFile = "./.hmy/blacklist.txt" BlacklistFile = "./.hmy/blacklist.txt"
RosettaFixFile = "./rosetta_local_fix.csv" RosettaFixFile = "./rosetta_local_fix.csv"
AccountSlots = 16 AccountSlots = 16
LocalAccountsFile = "./.hmy/locals.txt"
[ShardData] [ShardData]
EnableShardData = true EnableShardData = true

@ -95,6 +95,7 @@ Version = "2.5.2"
[TxPool] [TxPool]
BlacklistFile = "./.hmy/blacklist.txt" BlacklistFile = "./.hmy/blacklist.txt"
AccountSlots = 16 AccountSlots = 16
LocalAccountsFile = "./.hmy/locals.txt"
[ShardData] [ShardData]
EnableShardData = false EnableShardData = false

Loading…
Cancel
Save