prysm-pulse/sharding/client/smc.go
nisdas 394adb4750 Refactor Code
Former-commit-id: ad6a4793064556b556a92dbed3f8a555972c07a1 [formerly 13e903a723eee255b4eb6e2bcc7be06ba73b6fa7]
Former-commit-id: b2ecf21d02170030dfc56e9d9303aeb7bee45fab
2018-03-31 12:07:42 +08:00

54 lines
1.5 KiB
Go

package client
import (
"context"
"fmt"
"math/big"
"time"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/sharding"
"github.com/ethereum/go-ethereum/sharding/contracts"
)
// initSMC initializes the sharding manager contract bindings.
// If the SMC does not exist, it will be deployed.
func initSMC(c *ShardingClient) error {
b, err := c.client.CodeAt(context.Background(), sharding.ShardingManagerAddress, nil)
if err != nil {
return fmt.Errorf("unable to get contract code at %s: %v", sharding.ShardingManagerAddress, err)
}
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))
if err != nil {
return fmt.Errorf("unable to intiate the transaction: %v", err)
}
addr, tx, contract, err := contracts.DeploySMC(txOps, c.client)
if err != nil {
return fmt.Errorf("unable to deploy sharding manager 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.Smc = contract
log.Info(fmt.Sprintf("New contract deployed at %s", addr.String()))
} else {
contract, err := contracts.NewSMC(sharding.ShardingManagerAddress, c.client)
if err != nil {
return fmt.Errorf("failed to create sharding contract: %v", err)
}
c.Smc = contract
}
return nil
}