From a9afdd90ef8f33262242eee17c0a6f1e701baa05 Mon Sep 17 00:00:00 2001 From: Raul Jordan Date: Mon, 5 Feb 2018 22:41:40 -0600 Subject: [PATCH] propose collation called on geteligibleproposer Former-commit-id: 0a23d54aa3006d103bb7123bcfe0ac2ca3a2b027 [formerly 7266cc53ca4d6826b4a54d30391f9c5e5b0b7d10] Former-commit-id: 6b33ca471ede8696fc83285ae426645308a4c6fe --- sharding/collator.go | 29 +++++++++++++---------------- sharding/collator_test.go | 16 ++++++++++++---- sharding/vmc.go | 2 +- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/sharding/collator.go b/sharding/collator.go index 77aa121dfe..7d6f41f8d1 100644 --- a/sharding/collator.go +++ b/sharding/collator.go @@ -6,6 +6,7 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/log" + "math/big" ) // subscribeBlockHeaders checks incoming block headers and determines if @@ -51,27 +52,23 @@ func watchShards(c *Client, head *types.Header) error { return fmt.Errorf("cannot unlock account. %v", err) } - ops := bind.CallOpts{} - count, err := c.vmc.VMCCaller.ShardCount(&ops) - if err != nil { - return fmt.Errorf("unable to fetch shard count. %v", err) - } - + log.Info(fmt.Sprint("watching shards...")) s := 0 - for s < int(count.Int64()) { + for s < shardCount { // Checks if we are an eligible proposer according to the VMC - addr, err := c.vmc.VMCCaller.GetEligibleProposer(&ops, big.NewInt(s)) - if err != nil { - return fmt.Errorf("cannot fetch eligible collation proposer. %v", err) - } - // if the address is the coinbase addr (current node running the sharding - // clint, then we propose a new collation) - if addr == accounts[0].Address { - err := proposeCollation() + ops := bind.CallOpts{} + period := head.Number.Div(head.Number, big.NewInt(int64(periodLength))) + addr, err := c.vmc.VMCCaller.GetEligibleProposer(&ops, big.NewInt(int64(s)), period) + + // If output is non-empty and the addr == coinbase + if err == nil && addr == accounts[0].Address { + log.Info(fmt.Sprintf("selected as collator on shard %d", s)) + err := proposeCollation(s) if err != nil { return fmt.Errorf("could not propose collation. %v", err) } } + s++ } @@ -104,6 +101,6 @@ func proposeCollation(shardID int) error { // This functions will fetch the transactions in the txpool and and apply // them to finish up the collation. It will then need to broadcast the // collation to the main chain using JSON-RPC. - + log.Info(fmt.Sprint("propose collation called")) return nil } diff --git a/sharding/collator_test.go b/sharding/collator_test.go index 29ca48f1ec..274ebc2d4d 100644 --- a/sharding/collator_test.go +++ b/sharding/collator_test.go @@ -1,11 +1,19 @@ package sharding import ( + "github.com/ethereum/go-ethereum/common/hexutil" + "sync" "testing" ) -func TestDeposit(t *testing.T) { - if 0 != 0 { - t.Errorf("test incorrect") - } +// FakeEthService based on implementation of internal/ethapi.Client +type FakeEthService struct { + mu sync.Mutex + + getCodeResp hexutil.Bytes + getCodeErr error +} + +func TestSubscribeHeaders(t *testing.T) { + } diff --git a/sharding/vmc.go b/sharding/vmc.go index 594e767b69..5184ff8e45 100644 --- a/sharding/vmc.go +++ b/sharding/vmc.go @@ -97,7 +97,7 @@ func initVMCValidator(c *Client) error { }, } - _, err := c.vmc.VMCTransactor.Deposit(&ops, accounts[0].Address) + _, err := c.vmc.VMCTransactor.Deposit(&ops) if err != nil { return fmt.Errorf("unable to deposit eth and become a validator: %v", err) }