2018-06-03 23:13:09 +00:00
|
|
|
package mainchain
|
2018-05-20 22:47:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2018-07-20 21:31:26 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/client/contracts"
|
|
|
|
"github.com/prysmaticlabs/prysm/client/params"
|
2018-05-20 22:47:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// dialRPC endpoint to node.
|
|
|
|
func dialRPC(endpoint string) (*rpc.Client, error) {
|
|
|
|
if endpoint == "" {
|
2018-06-03 23:24:31 +00:00
|
|
|
endpoint = node.DefaultIPCEndpoint(ClientIdentifier)
|
2018-05-20 22:47:47 +00:00
|
|
|
}
|
|
|
|
return rpc.Dial(endpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
// initSMC initializes the sharding manager contract bindings.
|
|
|
|
// If the SMC does not exist, it will be deployed.
|
2018-06-03 23:13:09 +00:00
|
|
|
func initSMC(s *SMCClient) (*contracts.SMC, error) {
|
2018-06-13 17:37:23 +00:00
|
|
|
b, err := s.client.CodeAt(context.Background(), params.DefaultConfig.SMCAddress, nil)
|
2018-05-20 22:47:47 +00:00
|
|
|
if err != nil {
|
2018-06-13 17:37:23 +00:00
|
|
|
return nil, fmt.Errorf("unable to get contract code at %s: %v", params.DefaultConfig.SMCAddress.Hex(), err)
|
2018-05-20 22:47:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deploy SMC for development only.
|
2018-05-22 11:16:57 +00:00
|
|
|
// TODO: Separate contract deployment from the sharding node. It would only need to be deployed
|
|
|
|
// once on the mainnet, so this code would not need to ship with the node.
|
2018-05-20 22:47:47 +00:00
|
|
|
if len(b) == 0 {
|
2018-07-10 02:27:23 +00:00
|
|
|
log.Infof("No sharding manager contract found at %s, deploying new contract", params.DefaultConfig.SMCAddress.Hex())
|
2018-05-20 22:47:47 +00:00
|
|
|
|
2018-06-02 22:29:35 +00:00
|
|
|
txOps, err := s.CreateTXOpts(big.NewInt(0))
|
2018-05-20 22:47:47 +00:00
|
|
|
if err != nil {
|
2018-05-23 00:35:03 +00:00
|
|
|
return nil, fmt.Errorf("unable to initiate the transaction: %v", err)
|
2018-05-20 22:47:47 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 22:29:35 +00:00
|
|
|
addr, tx, contract, err := contracts.DeploySMC(txOps, s.client)
|
2018-05-20 22:47:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to deploy sharding manager contract: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-06-02 22:29:35 +00:00
|
|
|
for pending := true; pending; _, pending, err = s.client.TransactionByHash(context.Background(), tx.Hash()) {
|
2018-05-20 22:47:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to get transaction by hash: %v", err)
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
|
2018-07-10 02:27:23 +00:00
|
|
|
log.Infof("New contract deployed at %s", addr.Hex())
|
2018-05-20 22:47:47 +00:00
|
|
|
return contract, nil
|
|
|
|
}
|
|
|
|
|
2018-06-13 17:37:23 +00:00
|
|
|
return contracts.NewSMC(params.DefaultConfig.SMCAddress, s.client)
|
2018-05-20 22:47:47 +00:00
|
|
|
}
|