Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions sharding/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type shardingClient struct {
type Client interface {
Start() error
Close()
CreateTXOps(*big.Int) (*bind.TransactOpts, error)
CreateTXOpts(*big.Int) (*bind.TransactOpts, error)
ChainReader() ethereum.ChainReader
Account() *accounts.Account
SMCCaller() *contracts.SMCCaller
Expand Down Expand Up @@ -145,8 +145,8 @@ func (c *shardingClient) unlockAccount(account accounts.Account) error {
return c.keystore.Unlock(account, pass)
}

// CreateTXOps creates a *TransactOpts with a signer using the default account on the keystore.
func (c *shardingClient) CreateTXOps(value *big.Int) (*bind.TransactOpts, error) {
// CreateTXOpts creates a *TransactOpts with a signer using the default account on the keystore.
func (c *shardingClient) CreateTXOpts(value *big.Int) (*bind.TransactOpts, error) {
account := c.Account()

return &bind.TransactOpts{
Expand Down Expand Up @@ -210,7 +210,7 @@ func initSMC(c *shardingClient) (*contracts.SMC, error) {
if len(b) == 0 {
log.Info(fmt.Sprintf("No sharding manager contract found at %s. Deploying new contract.", sharding.ShardingManagerAddress.String()))

txOps, err := c.CreateTXOps(big.NewInt(0))
txOps, err := c.CreateTXOpts(big.NewInt(0))
if err != nil {
return nil, fmt.Errorf("unable to intiate the transaction: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions sharding/collator/collator.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ func submitCollation(shardID int64) error {
}

// joinCollatorPool checks if the account is a collator in the SMC. If
// the account is not in the set, it will deposit 100ETH into contract.
// the account is not in the set, it will deposit ETH into contract.
func joinCollatorPool(c client.Client) error {

log.Info("Joining collator pool")
txOps, err := c.CreateTXOps(sharding.CollatorDeposit)
txOps, err := c.CreateTXOpts(sharding.CollatorDeposit)
if err != nil {
return fmt.Errorf("unable to intiate the deposit transaction: %v", err)
}
Expand Down
57 changes: 48 additions & 9 deletions sharding/collator/collator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ func (m *mockClient) SMCTransactor() *contracts.SMCTransactor {
return &m.smc.SMCTransactor
}

func (m *mockClient) CreateTXOps(value *big.Int) (*bind.TransactOpts, error) {
m.t.Fatal("CreateTXOps not implemented")
return nil, nil
func (m *mockClient) CreateTXOpts(value *big.Int) (*bind.TransactOpts, error) {
txOpts := transactOpts()
txOpts.Value = value
return txOpts, nil
}

// Unused mockClient methods
Expand All @@ -57,13 +58,21 @@ func (m *mockClient) Close() {
m.t.Fatal("Close called")
}

func TestIsAccountInCollatorPool(t *testing.T) {
// Test setup (should this go to sharding/client/testing?)
// Helper/setup methods
// TODO: consider moving these to common sharding testing package as the collator and smc tests
// use them.
func transactOpts() *bind.TransactOpts {
return bind.NewKeyedTransactor(key)
}
func setup() (*backends.SimulatedBackend, *contracts.SMC) {
backend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: accountBalance1001Eth}})
transactOpts := bind.NewKeyedTransactor(key)
_, _, smc, _ := contracts.DeploySMC(transactOpts, backend)
_, _, smc, _ := contracts.DeploySMC(transactOpts(), backend)
backend.Commit()
return backend, smc
}

func TestIsAccountInCollatorPool(t *testing.T) {
backend, smc := setup()
client := &mockClient{smc: smc, t: t}

// address should not be in pool initially
Expand All @@ -75,9 +84,10 @@ func TestIsAccountInCollatorPool(t *testing.T) {
t.Fatal("Account unexpectedly in collator pool")
}

txOpts := transactOpts()
// deposit in collator pool, then it should return true
transactOpts.Value = sharding.CollatorDeposit
if _, err := smc.Deposit(transactOpts); err != nil {
txOpts.Value = sharding.CollatorDeposit
if _, err := smc.Deposit(txOpts); err != nil {
t.Fatalf("Failed to deposit: %v", err)
}
backend.Commit()
Expand All @@ -89,3 +99,32 @@ func TestIsAccountInCollatorPool(t *testing.T) {
t.Fatal("Account not in collator pool when expected to be")
}
}

func TestJoinCollatorPool(t *testing.T) {
backend, smc := setup()

@rauljordan rauljordan Apr 3, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How extensible is this approach to future testing? Do we have to initialize it like the setup like this within each testing method?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think so. I added a TODO to consider moving this setup to some common testing package since the proposer would likely want the same setup methods and mockClient.

client := &mockClient{smc, t}

// There should be no collators initially
numCollators, err := smc.NumCollators(&bind.CallOpts{})
if err != nil {
t.Fatal(err)
}
if big.NewInt(0).Cmp(numCollators) != 0 {
t.Fatalf("Unexpected number of collators. Got %d, wanted 0.", numCollators)
}

err = joinCollatorPool(client)
if err != nil {
t.Fatal(err)
}
backend.Commit()

// Now there should be one collator
numCollators, err = smc.NumCollators(&bind.CallOpts{})
if err != nil {
t.Fatal(err)
}
if big.NewInt(1).Cmp(numCollators) != 0 {
t.Fatalf("Unexpected number of collators. Got %d, wanted 1.", numCollators)
}
}