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-01-27 19:25:07 +00:00
|
|
|
// initVMC initializes the validator management contract bindings.
|
2018-01-27 19:24:13 +00:00
|
|
|
// If the VMC does not exist, it will be deployed.
|
|
|
|
func initVMC(c *Client) error {
|
2018-01-15 21:39:00 +00:00
|
|
|
b, err := c.client.CodeAt(context.Background(), validatorManagerAddress, nil)
|
|
|
|
if err != nil {
|
2018-02-06 20:04:45 +00:00
|
|
|
return fmt.Errorf("unable to get contract code at %s: %v", validatorManagerAddress, err)
|
2018-01-15 21:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(b) == 0 {
|
2018-01-27 19:24:13 +00:00
|
|
|
log.Info(fmt.Sprintf("No validator management contract found at %s. Deploying new contract.", validatorManagerAddress.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-02-10 00:37:30 +00:00
|
|
|
addr, tx, contract, err := contracts.DeployVMC(txOps, c.client)
|
2018-01-24 03:25:32 +00:00
|
|
|
if err != nil {
|
2018-01-27 19:24:13 +00:00
|
|
|
return fmt.Errorf("unable to deploy validator management 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-01-27 19:24:13 +00:00
|
|
|
c.vmc = contract
|
|
|
|
log.Info(fmt.Sprintf("New contract deployed at %s", addr.String()))
|
|
|
|
} else {
|
2018-01-27 19:57:37 +00:00
|
|
|
contract, err := contracts.NewVMC(validatorManagerAddress, c.client)
|
2018-01-17 03:59:35 +00:00
|
|
|
if err != nil {
|
2018-01-27 19:24:13 +00:00
|
|
|
return fmt.Errorf("failed to create validator contract: %v", err)
|
2018-01-15 21:39:00 +00:00
|
|
|
}
|
2018-01-27 19:24:13 +00:00
|
|
|
c.vmc = 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-02-06 20:04:45 +00:00
|
|
|
// joinValidatorSet checks if the account is a validator in the VMC. If
|
2018-01-28 22:21:46 +00:00
|
|
|
// the account is not in the set, it will deposit 100ETH into contract.
|
2018-02-06 20:04:45 +00:00
|
|
|
func joinValidatorSet(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-02-27 02:07:09 +00:00
|
|
|
log.Info("Joining validator set")
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := c.vmc.VMCTransactor.Deposit(txOps)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to deposit eth and become a validator: %v", err)
|
|
|
|
}
|
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-02-27 02:07:09 +00:00
|
|
|
log.Info("Not joining validator 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
|
|
|
}
|