2018-01-15 00:10:02 +00:00
|
|
|
package sharding
|
|
|
|
|
2018-01-15 21:39:00 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-02-06 20:22:19 +00:00
|
|
|
"math/big"
|
2018-01-15 21:39:00 +00:00
|
|
|
"time"
|
|
|
|
|
2018-02-26 02:20:42 +00:00
|
|
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
2018-01-15 21:39:00 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-01-27 19:24:13 +00:00
|
|
|
"github.com/ethereum/go-ethereum/sharding/contracts"
|
2018-01-15 21:39:00 +00:00
|
|
|
)
|
|
|
|
|
2018-03-08 17:34:15 +00:00
|
|
|
// initSMC initializes the sharding manager contract bindings.
|
|
|
|
// If the SMC does not exist, it will be deployed.
|
|
|
|
func initSMC(c *Client) error {
|
|
|
|
b, err := c.client.CodeAt(context.Background(), shardingManagerAddress, nil)
|
2018-01-15 21:39:00 +00:00
|
|
|
if err != nil {
|
2018-03-08 17:34:15 +00:00
|
|
|
return fmt.Errorf("unable to get contract code at %s: %v", shardingManagerAddress, err)
|
2018-01-15 21:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(b) == 0 {
|
2018-03-08 17:34:15 +00:00
|
|
|
log.Info(fmt.Sprintf("No sharding manager contract found at %s. Deploying new contract.", shardingManagerAddress.String()))
|
2018-01-15 21:39:00 +00:00
|
|
|
|
2018-02-06 20:22:19 +00:00
|
|
|
txOps, err := c.createTXOps(big.NewInt(0))
|
2018-02-06 20:04:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to intiate the transaction: %v", err)
|
2018-01-24 03:25:32 +00:00
|
|
|
}
|
2018-01-27 19:24:13 +00:00
|
|
|
|
2018-03-08 17:34:15 +00:00
|
|
|
addr, tx, contract, err := contracts.DeploySMC(txOps, c.client)
|
2018-01-24 03:25:32 +00:00
|
|
|
if err != nil {
|
2018-03-08 17:34:15 +00:00
|
|
|
return fmt.Errorf("unable to deploy sharding manager contract: %v", err)
|
2018-01-24 03:25:32 +00:00
|
|
|
}
|
2018-01-18 02:34:03 +00:00
|
|
|
|
2018-01-27 19:24:13 +00:00
|
|
|
for pending := true; pending; _, pending, err = c.client.TransactionByHash(context.Background(), tx.Hash()) {
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get transaction by hash: %v", err)
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
2018-01-15 21:39:00 +00:00
|
|
|
}
|
2018-01-22 05:13:20 +00:00
|
|
|
|
2018-03-08 17:34:15 +00:00
|
|
|
c.smc = contract
|
2018-01-27 19:24:13 +00:00
|
|
|
log.Info(fmt.Sprintf("New contract deployed at %s", addr.String()))
|
|
|
|
} else {
|
2018-03-08 17:34:15 +00:00
|
|
|
contract, err := contracts.NewSMC(shardingManagerAddress, c.client)
|
2018-01-17 03:59:35 +00:00
|
|
|
if err != nil {
|
2018-03-08 17:34:15 +00:00
|
|
|
return fmt.Errorf("failed to create sharding contract: %v", err)
|
2018-01-15 21:39:00 +00:00
|
|
|
}
|
2018-03-08 17:34:15 +00:00
|
|
|
c.smc = contract
|
2018-01-17 03:59:35 +00:00
|
|
|
}
|
2018-01-15 21:39:00 +00:00
|
|
|
|
2018-01-27 19:24:13 +00:00
|
|
|
return nil
|
2018-01-17 03:59:35 +00:00
|
|
|
}
|
2018-01-28 22:21:46 +00:00
|
|
|
|
2018-03-08 17:34:15 +00:00
|
|
|
// joinCollatorPool checks if the account is a collator in the SMC. If
|
2018-01-28 22:21:46 +00:00
|
|
|
// the account is not in the set, it will deposit 100ETH into contract.
|
2018-03-08 17:34:15 +00:00
|
|
|
func joinCollatorPool(c *Client) error {
|
2018-01-28 22:21:46 +00:00
|
|
|
|
2018-03-06 04:17:55 +00:00
|
|
|
if c.ctx.GlobalBool(utils.DepositFlag.Name) {
|
2018-02-26 02:20:42 +00:00
|
|
|
|
2018-03-08 17:34:15 +00:00
|
|
|
log.Info("Joining collator pool")
|
2018-02-26 02:20:42 +00:00
|
|
|
txOps, err := c.createTXOps(depositSize)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to intiate the deposit transaction: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-08 17:34:15 +00:00
|
|
|
tx, err := c.smc.SMCTransactor.Deposit(txOps)
|
2018-02-26 02:20:42 +00:00
|
|
|
if err != nil {
|
2018-03-08 17:34:15 +00:00
|
|
|
return fmt.Errorf("unable to deposit eth and become a collator: %v", err)
|
2018-02-26 02:20:42 +00:00
|
|
|
}
|
2018-02-27 02:07:09 +00:00
|
|
|
log.Info(fmt.Sprintf("Deposited %dETH into contract with transaction hash: %s", depositSize, tx.Hash().String()))
|
2018-02-26 02:20:42 +00:00
|
|
|
|
|
|
|
} else {
|
2018-03-08 17:34:15 +00:00
|
|
|
log.Info("Not joining collator set")
|
2018-01-29 05:35:15 +00:00
|
|
|
|
2018-01-29 05:41:40 +00:00
|
|
|
}
|
2018-01-29 05:35:15 +00:00
|
|
|
return nil
|
2018-01-28 22:21:46 +00:00
|
|
|
}
|