From 248f245f9f86758b78337f4edb693f974d3cc24d Mon Sep 17 00:00:00 2001 From: Terence Tsao Date: Sun, 13 May 2018 11:28:23 -0700 Subject: [PATCH 1/3] sharding/contracts: added regression test Former-commit-id: 01dbc764a0a616d1ed4c21232d5fc886052c3c74 [formerly 5509cecf26e39948254f55d2d2cca820d3ed5b1b] Former-commit-id: 3830de45de328b24d3d2c81559fac3a1e9d0a76a --- sharding/contracts/sharding_manager_test.go | 31 +++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/sharding/contracts/sharding_manager_test.go b/sharding/contracts/sharding_manager_test.go index 72a27b98f..25267971b 100644 --- a/sharding/contracts/sharding_manager_test.go +++ b/sharding/contracts/sharding_manager_test.go @@ -516,6 +516,37 @@ func TestGetCommitteeWithNonMember(t *testing.T) { } +// TestGetCommitteeWithinSamePeriod tests notary registers and samples within the same period +func TestGetCommitteeWithinSamePeriod(t *testing.T) { + addr := crypto.PubkeyToAddress(mainKey.PublicKey) + backend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: accountBalance2000Eth}}) + txOpts := bind.NewKeyedTransactor(mainKey) + txOpts.Value = notaryDeposit + _, _, smc, _ := deploySMCContract(backend, mainKey) + + // Notary 0 registers. + smc.RegisterNotary(txOpts) + backend.Commit() + + notary, _ := smc.NotaryRegistry(&bind.CallOpts{}, addr) + numNotaries, _ := smc.NotaryPoolLength(&bind.CallOpts{}) + + if !notary.Deposited { + t.Errorf("Notary has not registered. Got deposited flag: %v", notary.Deposited) + } + + if numNotaries.Cmp(big.NewInt(1)) != 0 { + t.Errorf("Incorrect count from notary pool. Want: 1, Got: %v", numNotaries) + } + + // Notary 0 samples for itself within the same period after registration + sampledAddr, _ := smc.GetNotaryInCommittee(&bind.CallOpts{}, big.NewInt(0)) + + if addr != sampledAddr { + t.Errorf("Unable to sample notary address within same period of registration, got addr: %v", sampledAddr) + } +} + // TestGetCommitteeAfterDeregisters tests notary tries to be in committee after deregistered. func TestGetCommitteeAfterDeregisters(t *testing.T) { const notaryCount = 10 From 16c5158cb913223848c11b9278e7205a07c9b1c9 Mon Sep 17 00:00:00 2001 From: Terence Tsao Date: Sun, 13 May 2018 15:49:06 -0700 Subject: [PATCH 2/3] sharding/contracts: collator -> notary Former-commit-id: baa704eda89c6f51e8a8419550bd1f40ef378625 [formerly 277726a6ee2d899ae2f9cc428213205531356d0a] Former-commit-id: 8ef9b8fe4bdbf4e0f7591c458b4cb7f37b81db61 --- sharding/notary/notary.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sharding/notary/notary.go b/sharding/notary/notary.go index bd7ca3fc5..52062fe82 100644 --- a/sharding/notary/notary.go +++ b/sharding/notary/notary.go @@ -72,9 +72,9 @@ func checkSMCForNotary(c client.Client, head *types.Header) error { return err } - // If the account is selected as collator, submit collation. + // If the account is selected as notary, submit collation. if addr == c.Account().Address { - log.Info(fmt.Sprintf("Selected as collator on shard: %d", s)) + log.Info(fmt.Sprintf("Selected as notary on shard: %d", s)) err := submitCollation(s) if err != nil { return fmt.Errorf("could not add collation. %v", err) From 8bff6099c62615da0bfc8165375e8e1d4131941b Mon Sep 17 00:00:00 2001 From: Terence Tsao Date: Sun, 13 May 2018 15:53:52 -0700 Subject: [PATCH 3/3] sharding/contracts: merge with master Former-commit-id: 251eebf6cb46497e3afda66e0496ebd04593a741 [formerly 3399601d0fd7263f99ad2a6c0e9935e1ad0139a9] Former-commit-id: e92aa0f01cd90b73684d5f3b3d8b127ae5130b1b --- cmd/geth/usage.go | 6 ++++++ cmd/utils/flags.go | 3 ++- sharding/client/client.go | 6 ++++++ sharding/notary/notary.go | 13 ++++++++++-- sharding/notary/notary_client.go | 6 ++++-- sharding/notary/notary_test.go | 35 ++++++++++++++++++++++++++++---- 6 files changed, 60 insertions(+), 9 deletions(-) diff --git a/cmd/geth/usage.go b/cmd/geth/usage.go index a1558c233..83b596db5 100644 --- a/cmd/geth/usage.go +++ b/cmd/geth/usage.go @@ -83,6 +83,12 @@ var AppHelpFlagGroups = []flagGroup{ utils.LightKDFFlag, }, }, + { + Name: "SHARDING", + Flags: []cli.Flag{ + utils.DepositFlag, + }, + }, {Name: "DEVELOPER CHAIN", Flags: []cli.Flag{ utils.DeveloperFlag, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 968a92aca..a4eadcf09 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -55,6 +55,7 @@ import ( "github.com/ethereum/go-ethereum/p2p/nat" "github.com/ethereum/go-ethereum/p2p/netutil" "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/sharding" whisper "github.com/ethereum/go-ethereum/whisper/whisperv6" "gopkg.in/urfave/cli.v1" ) @@ -536,7 +537,7 @@ var ( //Sharding Settings DepositFlag = cli.BoolFlag{ Name: "deposit", - Usage: "To become a notary with your sharding client, 100 ETH will be deposited from user's account into SMC ", + Usage: "To become a notary with your sharding client, " + new(big.Int).Div(sharding.NotaryDeposit, new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)).String() + " ETH will be deposited into SMC", } ) diff --git a/sharding/client/client.go b/sharding/client/client.go index 58c3e189e..769ce50dd 100644 --- a/sharding/client/client.go +++ b/sharding/client/client.go @@ -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) +} diff --git a/sharding/notary/notary.go b/sharding/notary/notary.go index 52062fe82..9d115b488 100644 --- a/sharding/notary/notary.go +++ b/sharding/notary/notary.go @@ -2,6 +2,7 @@ package notary import ( "context" + "errors" "fmt" "math/big" @@ -130,9 +131,17 @@ 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") + } + + if b, err := isAccountInNotaryPool(c); b || err != nil { + return err + } log.Info("Joining notary pool") txOps, err := c.CreateTXOpts(sharding.NotaryDeposit) diff --git a/sharding/notary/notary_client.go b/sharding/notary/notary_client.go index a33a53795..029909b22 100644 --- a/sharding/notary/notary_client.go +++ b/sharding/notary/notary_client.go @@ -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) diff --git a/sharding/notary/notary_test.go b/sharding/notary/notary_test.go index 099b43758..21fa7d6fd 100644 --- a/sharding/notary/notary_test.go +++ b/sharding/notary/notary_test.go @@ -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) @@ -127,4 +138,20 @@ func TestJoinNotaryPool(t *testing.T) { if big.NewInt(1).Cmp(numNotaries) != 0 { t.Fatalf("Unexpected number of notaries. Got %d, wanted 1.", numNotaries) } + + // Trying to join while deposited should do nothing + err = joinNotaryPool(client) + if err != nil { + t.Error(err) + } + backend.Commit() + + numNotaries, err = smc.NotaryPoolLength(&bind.CallOpts{}) + if err != nil { + t.Fatal(err) + } + if big.NewInt(1).Cmp(numNotaries) != 0 { + t.Fatalf("Unexpected number of notaries. Got %d, wanted 1.", numNotaries) + } + }