Merge pull request #116 from prysmaticlabs/fix/deposits

Only deposit when --deposit is present 

Former-commit-id: 731934362ea81e52957e95cd9b7a68e3805675cd [formerly dfef4cf8fe3d96a831b9fdbf3503db651ca67db8]
Former-commit-id: aea665ad8e8fe8642c8cd2c3868087a111c74029
This commit is contained in:
Preston Van Loon 2018-05-13 13:56:47 -04:00 committed by GitHub
commit 4bfb23a5e7
4 changed files with 32 additions and 8 deletions

View File

@ -52,6 +52,7 @@ type Client interface {
Account() *accounts.Account
SMCCaller() *contracts.SMCCaller
SMCTransactor() *contracts.SMCTransactor
DepositFlagSet() bool
}
// NewClient forms a new struct instance.
@ -239,3 +240,8 @@ func initSMC(c *shardingClient) (*contracts.SMC, error) {
}
return contract, nil
}
// DepositFlagSet returns true for cli flag --deposit.
func (c *shardingClient) DepositFlagSet() bool {
return c.ctx.GlobalBool(utils.DepositFlag.Name)
}

View File

@ -2,6 +2,7 @@ package notary
import (
"context"
"errors"
"fmt"
"math/big"
@ -131,9 +132,13 @@ func submitCollation(shardID int64) error {
return nil
}
// joinNotaryPool checks if the account is a notary in the SMC. If
// the account is not in the set, it will deposit ETH into contract.
// joinNotaryPool checks if the deposit flag is true and the account is a
// notary in the SMC. If the account is not in the set, it will deposit ETH
// into contract.
func joinNotaryPool(c client.Client) error {
if !c.DepositFlagSet() {
return errors.New("joinNotaryPool called when deposit flag was not set")
}
log.Info("Joining notary pool")
txOps, err := c.CreateTXOpts(sharding.NotaryDeposit)

View File

@ -32,8 +32,10 @@ func (c *notary) Start() error {
}
defer c.client.Close()
if err := joinNotaryPool(c.client); err != nil {
return err
if c.client.DepositFlagSet() {
if err := joinNotaryPool(c.client); err != nil {
return err
}
}
return subscribeBlockHeaders(c.client)

View File

@ -22,8 +22,9 @@ var (
// Mock client for testing notary. Should this go into sharding/client/testing?
type mockClient struct {
smc *contracts.SMC
t *testing.T
smc *contracts.SMC
t *testing.T
DepositFlag bool
}
func (m *mockClient) Account() *accounts.Account {
@ -49,6 +50,10 @@ func (m *mockClient) CreateTXOpts(value *big.Int) (*bind.TransactOpts, error) {
return txOpts, nil
}
func (m *mockClient) DepositFlagSet() bool {
return m.DepositFlag
}
// Unused mockClient methods.
func (m *mockClient) Start() error {
m.t.Fatal("Start called")
@ -102,8 +107,7 @@ func TestIsAccountInNotaryPool(t *testing.T) {
func TestJoinNotaryPool(t *testing.T) {
backend, smc := setup()
client := &mockClient{smc, t}
client := &mockClient{smc: smc, t: t, DepositFlag: false}
// There should be no notary initially.
numNotaries, err := smc.NotaryPoolLength(&bind.CallOpts{})
if err != nil {
@ -113,6 +117,13 @@ func TestJoinNotaryPool(t *testing.T) {
t.Fatalf("Unexpected number of notaries. Got %d, wanted 0.", numNotaries)
}
client.DepositFlag = false
err = joinNotaryPool(client)
if err == nil {
t.Error("Joined notary pool while --deposit was not present")
}
client.DepositFlag = true
err = joinNotaryPool(client)
if err != nil {
t.Fatal(err)