From dcdafc311122dd3ab782f89df7f8fc9eb3c77868 Mon Sep 17 00:00:00 2001 From: Terence Tsao Date: Mon, 26 Feb 2018 14:48:42 -0800 Subject: [PATCH 1/8] verify client is in validator pool before querying eligible proposer Former-commit-id: 137864983a88747e187ddf8215b5ca91b6122df2 [formerly 66faa46eb087b222292a1b0d941374601a8e9f12] Former-commit-id: c0e2a692144a3c8c530c4cb8e7b6188c1d581d17 --- sharding/collator.go | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/sharding/collator.go b/sharding/collator.go index 5bb73efd9..65ca9e3ae 100644 --- a/sharding/collator.go +++ b/sharding/collator.go @@ -40,8 +40,19 @@ func subscribeBlockHeaders(c collatorClient) error { // Query the current state to see if we are an eligible proposer log.Info(fmt.Sprintf("Received new header: %v", head.Number.String())) // TODO: Only run this code on certain periods? - if err := checkShardsForProposal(c, head); err != nil { - return fmt.Errorf("unable to watch shards. %v", err) + + // Check if we are in the validator pool before checking if we are an eligible proposer + v, err := checkForValidators(c) + if err != nil { + return fmt.Errorf("unable to verify client in validator pool", err) + } + + if (v) { + if err := checkShardsForProposal(c, head); err != nil { + return fmt.Errorf("unable to watch shards. %v", err) + } + } else { + log.Warn("client not in validator pool, re-try next block") } } @@ -82,6 +93,25 @@ func checkShardsForProposal(c collatorClient, head *types.Header) error { return nil } +// checkForValidators checks if the client is in the validator pool because +// we can't guarantee our tx for deposit will be in the next block header we receive +// The function calls IsValidatorDeposited from the VMC and returns true if +// the client is in the validator pool +func checkForValidators(c collatorClient) (bool, error) { + account, err := c.Account() + if err != nil { + return false, err + } + + // Checks if our deposit has gone through according to the VMC + b, err := c.VMCCaller().IsValidatorDeposited(&bind.CallOpts{}, account.Address) + if err != nil { + return false, err + } + + return b, nil +} + // proposeCollation interacts with the VMC directly to add a collation header func proposeCollation(shardID int64) error { // TODO: Adds a collation header to the VMC with the following fields: From b3648af9beff9fad5ab303ab3bf649a73a720bb0 Mon Sep 17 00:00:00 2001 From: Terence Tsao Date: Mon, 26 Feb 2018 17:18:49 -0800 Subject: [PATCH 2/8] updated warning msg, and made method name more descriptive Former-commit-id: 9a3da33abd9f3a4a687d3991ceff2ed1e428e871 [formerly 6d19a8f335e9307d4b10a47eb60fb9743c7db435] Former-commit-id: f6316b049ffc6f3ab94405a841bda1d2686a287f --- sharding/collator.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/sharding/collator.go b/sharding/collator.go index 65ca9e3ae..e55ddaa87 100644 --- a/sharding/collator.go +++ b/sharding/collator.go @@ -27,7 +27,12 @@ type collatorClient interface { func subscribeBlockHeaders(c collatorClient) error { headerChan := make(chan *types.Header, 16) - _, err := c.ChainReader().SubscribeNewHead(context.Background(), headerChan) + account, err := c.Account() + if err != nil { + return err + } + + _, err = c.ChainReader().SubscribeNewHead(context.Background(), headerChan) if err != nil { return fmt.Errorf("unable to subscribe to incoming headers. %v", err) } @@ -42,9 +47,9 @@ func subscribeBlockHeaders(c collatorClient) error { // TODO: Only run this code on certain periods? // Check if we are in the validator pool before checking if we are an eligible proposer - v, err := checkForValidators(c) + v, err := isAccountInValidatorSet(c) if err != nil { - return fmt.Errorf("unable to verify client in validator pool", err) + return fmt.Errorf("unable to verify client in validator pool. %v", err) } if (v) { @@ -52,7 +57,7 @@ func subscribeBlockHeaders(c collatorClient) error { return fmt.Errorf("unable to watch shards. %v", err) } } else { - log.Warn("client not in validator pool, re-try next block") + log.Warn(fmt.Sprintf("Account %s not in validator pool.", account.Address.String())) } } @@ -97,7 +102,7 @@ func checkShardsForProposal(c collatorClient, head *types.Header) error { // we can't guarantee our tx for deposit will be in the next block header we receive // The function calls IsValidatorDeposited from the VMC and returns true if // the client is in the validator pool -func checkForValidators(c collatorClient) (bool, error) { +func isAccountInValidatorSet(c collatorClient) (bool, error) { account, err := c.Account() if err != nil { return false, err From 8b91d9789763743834773f3d3d6f8a415cb1669a Mon Sep 17 00:00:00 2001 From: Terence Tsao Date: Mon, 26 Feb 2018 17:31:00 -0800 Subject: [PATCH 3/8] updated comment to reflect new method name, added punctuation Former-commit-id: e1c33f0d62cd460afc750e538a15b051be755ee2 [formerly 92a2419212708a0fa9f3ae88f9aeede7d836df4e] Former-commit-id: 6f8bd74087939390191abae2b56a6bed22280c95 --- sharding/collator.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sharding/collator.go b/sharding/collator.go index e55ddaa87..3c3ed2f08 100644 --- a/sharding/collator.go +++ b/sharding/collator.go @@ -98,8 +98,8 @@ func checkShardsForProposal(c collatorClient, head *types.Header) error { return nil } -// checkForValidators checks if the client is in the validator pool because -// we can't guarantee our tx for deposit will be in the next block header we receive +// isAccountInValidatorSet checks if the client is in the validator pool because +// we can't guarantee our tx for deposit will be in the next block header we receive. // The function calls IsValidatorDeposited from the VMC and returns true if // the client is in the validator pool func isAccountInValidatorSet(c collatorClient) (bool, error) { From 349501a815bed063a0a708fd72a27970a0fdc8e2 Mon Sep 17 00:00:00 2001 From: Terence Tsao Date: Mon, 26 Feb 2018 18:19:36 -0800 Subject: [PATCH 4/8] fixed linter issue Former-commit-id: 6f9ff695c747f337cce2d6fdbd04c48b4a36d9d5 [formerly 7397777cc91d4055558d3e32d17eec53f0b02cb5] Former-commit-id: 3eae93312088ab799c2ac5878295ab212f469ec1 --- sharding/collator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sharding/collator.go b/sharding/collator.go index 3c3ed2f08..c609d42ed 100644 --- a/sharding/collator.go +++ b/sharding/collator.go @@ -52,7 +52,7 @@ func subscribeBlockHeaders(c collatorClient) error { return fmt.Errorf("unable to verify client in validator pool. %v", err) } - if (v) { + if v { if err := checkShardsForProposal(c, head); err != nil { return fmt.Errorf("unable to watch shards. %v", err) } From 6b8f403160655ca7d8140770ec76b9eab681353d Mon Sep 17 00:00:00 2001 From: nisdas Date: Tue, 27 Feb 2018 13:14:29 +0800 Subject: [PATCH 5/8] Change from ioutil read fileto bufio scanner Former-commit-id: 8dada22f453ed3c19c0e0f9fc565ae874feb7867 [formerly 5935902039f2829c21d1d6523cfbe99403e3924e] Former-commit-id: a384213b80bc648c153941e2a8969b01f0bb0733 --- sharding/client.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/sharding/client.go b/sharding/client.go index c9e3207d7..9d50c1086 100644 --- a/sharding/client.go +++ b/sharding/client.go @@ -3,11 +3,11 @@ package sharding //go:generate abigen --sol contracts/validator_manager.sol --pkg contracts --out contracts/validator_manager.go import ( + "bufio" "context" "fmt" - "io/ioutil" "math/big" - "strings" + "os" "github.com/ethereum/go-ethereum" @@ -120,12 +120,22 @@ func (c *Client) unlockAccount(account accounts.Account) error { pass := "" if c.ctx.GlobalIsSet(utils.PasswordFileFlag.Name) { - blob, err := ioutil.ReadFile(c.ctx.GlobalString(utils.PasswordFileFlag.Name)) + file, err := os.Open(c.ctx.GlobalString(utils.PasswordFileFlag.Name)) if err != nil { - return fmt.Errorf("unable to read account password contents in file %s. %v", utils.PasswordFileFlag.Value, err) + return fmt.Errorf("unable to open file containing account password %s. %v", utils.PasswordFileFlag.Value, err) } - // TODO: Use bufio.Scanner or other reader that doesn't include a trailing newline character. - pass = strings.Trim(string(blob), "\n") // Some text files end in new line, remove with strings.Trim. + scanner := bufio.NewScanner(file) + scanner.Split(bufio.ScanWords) + passwordSuccess := scanner.Scan() + if passwordSuccess == false { + err = scanner.Err() + if err != nil { + return fmt.Errorf("unable to read contents of file %v", err) + } + return fmt.Errorf("Password Not Found in file") + } + + pass = scanner.Text() } return c.keystore.Unlock(account, pass) From 8376b28a33053aff992f6f859e4087e4ed42ee20 Mon Sep 17 00:00:00 2001 From: nisdas Date: Tue, 27 Feb 2018 13:32:27 +0800 Subject: [PATCH 6/8] Fix lint Former-commit-id: caeb71c32561fd381577cf376ec24468d9f94139 [formerly 35b9abe6cd42749d59f140feff5fca3d8b4107e6] Former-commit-id: 168f24d03bf13a544513c2f970525627b03797a8 --- sharding/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sharding/client.go b/sharding/client.go index 9d50c1086..895eec22b 100644 --- a/sharding/client.go +++ b/sharding/client.go @@ -127,7 +127,7 @@ func (c *Client) unlockAccount(account accounts.Account) error { scanner := bufio.NewScanner(file) scanner.Split(bufio.ScanWords) passwordSuccess := scanner.Scan() - if passwordSuccess == false { + if !passwordSuccess { err = scanner.Err() if err != nil { return fmt.Errorf("unable to read contents of file %v", err) From 58bfae12ca0f365c7c1592303ea0fe6d828b2953 Mon Sep 17 00:00:00 2001 From: nisdas Date: Tue, 27 Feb 2018 22:43:58 +0800 Subject: [PATCH 7/8] Remove one time variable and change capital ization Former-commit-id: 424d1f536fbc46002c37df35a582de3772405c29 [formerly 534eab56faf5379970b8018d8db9c46789df34b1] Former-commit-id: 993b362bffb8d5b3ec6019137cc324d078e989e3 --- sharding/client.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sharding/client.go b/sharding/client.go index 895eec22b..381b7daaf 100644 --- a/sharding/client.go +++ b/sharding/client.go @@ -126,13 +126,12 @@ func (c *Client) unlockAccount(account accounts.Account) error { } scanner := bufio.NewScanner(file) scanner.Split(bufio.ScanWords) - passwordSuccess := scanner.Scan() - if !passwordSuccess { + if !scanner.Scan() { err = scanner.Err() if err != nil { return fmt.Errorf("unable to read contents of file %v", err) } - return fmt.Errorf("Password Not Found in file") + return fmt.Errorf("password not found in file") } pass = scanner.Text() From 11ec5db55b56b6ed23182b3b341cee3da0992017 Mon Sep 17 00:00:00 2001 From: nisdas Date: Tue, 27 Feb 2018 23:50:56 +0800 Subject: [PATCH 8/8] Changing error Former-commit-id: a8d82f8773392a30d885b570f38160e04d819e7a [formerly 6c7b389396643110e7ec1eb2b5bb92b9ea4d723e] Former-commit-id: 6bf3998fe1e4392b7704d75e781012bb8b612095 --- sharding/client.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sharding/client.go b/sharding/client.go index 381b7daaf..d7b355792 100644 --- a/sharding/client.go +++ b/sharding/client.go @@ -5,6 +5,7 @@ package sharding import ( "bufio" "context" + "errors" "fmt" "math/big" "os" @@ -131,7 +132,7 @@ func (c *Client) unlockAccount(account accounts.Account) error { if err != nil { return fmt.Errorf("unable to read contents of file %v", err) } - return fmt.Errorf("password not found in file") + return errors.New("password not found in file") } pass = scanner.Text()