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]
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

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

@ -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{

@ -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

@ -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",
},
},
}

@ -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)

@ -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 {

@ -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

@ -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)

@ -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()

@ -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 {

@ -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

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

Loading…
Cancel
Save