prysm-pulse/sharding/vmc.go
nisdas e028d5d8f0 Add Flag to main.go , and use flag in vmc.go
Former-commit-id: 53b5ad5831de51e345854a38b1f7928e31ffae16 [formerly 4ee90d9a47c09f436f50f079ea4a8be2e9b86036]
Former-commit-id: ba696db5822ec0293597d68e6945062f477e8f12
2018-02-26 10:20:42 +08:00

81 lines
2.4 KiB
Go

package sharding
import (
"context"
"fmt"
"math/big"
"time"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/sharding/contracts"
)
// initVMC initializes the validator management contract bindings.
// If the VMC does not exist, it will be deployed.
func initVMC(c *Client) error {
b, err := c.client.CodeAt(context.Background(), validatorManagerAddress, nil)
if err != nil {
return fmt.Errorf("unable to get contract code at %s: %v", validatorManagerAddress, err)
}
if len(b) == 0 {
log.Info(fmt.Sprintf("No validator management contract found at %s. Deploying new contract.", validatorManagerAddress.String()))
txOps, err := c.createTXOps(big.NewInt(0))
if err != nil {
return fmt.Errorf("unable to intiate the transaction: %v", err)
}
addr, tx, contract, err := contracts.DeployVMC(txOps, c.client)
if err != nil {
return fmt.Errorf("unable to deploy validator management contract: %v", err)
}
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)
}
c.vmc = contract
log.Info(fmt.Sprintf("New contract deployed at %s", addr.String()))
} else {
contract, err := contracts.NewVMC(validatorManagerAddress, c.client)
if err != nil {
return fmt.Errorf("failed to create validator contract: %v", err)
}
c.vmc = contract
}
return nil
}
// joinValidatorSet checks if the account is a validator in the VMC. If
// the account is not in the set, it will deposit 100ETH into contract.
func joinValidatorSet(c *Client) error {
// TODO: Check if account is already in validator set. Fetch this From
// the VMC contract's validator set
if c.ctx.GlobalBool(utils.ValidatorSetFlag.Name) {
log.Info(fmt.Sprintf("Joining Validator Set"))
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)
}
log.Info(fmt.Sprintf("Deposited 100ETH into contract with transaction hash: %s", tx.Hash().String()))
} else {
log.Info(fmt.Sprintf("Not Joining Validator Set"))
}
return nil
}