Refactor mock test helpers (#10133)

* Delete deploy contract tool. Move mock to its own package as testonly with some helper functions

* gofmt contracts/deposit/mock/mock.go

* move stategen mock.go to its on testonly pkg

* move password_reader_mock.go to mock testonly package

* move mock keymanager to its own testonly package

* move attestations mock

* move voluntaryexits mock

* Move slashings mock to mock pkg

* move the slasher mock

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
Preston Van Loon
2022-01-26 08:48:20 -06:00
committed by GitHub
parent d9799e6b6c
commit 943dec525c
64 changed files with 281 additions and 367 deletions

View File

@@ -1,27 +0,0 @@
load("@prysm//tools/go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_binary")
go_library(
name = "go_default_library",
srcs = ["deployContract.go"],
importpath = "github.com/prysmaticlabs/prysm/tools/deployContract",
visibility = ["//visibility:private"],
deps = [
"//contracts/deposit:go_default_library",
"//runtime/version:go_default_library",
"@com_github_ethereum_go_ethereum//accounts/abi/bind:go_default_library",
"@com_github_ethereum_go_ethereum//accounts/keystore:go_default_library",
"@com_github_ethereum_go_ethereum//crypto:go_default_library",
"@com_github_ethereum_go_ethereum//ethclient:go_default_library",
"@com_github_ethereum_go_ethereum//rpc:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
"@com_github_x_cray_logrus_prefixed_formatter//:go_default_library",
],
)
go_binary(
name = "deployContract",
embed = [":go_default_library"],
visibility = ["//visibility:public"],
)

View File

@@ -1,35 +0,0 @@
## Utility to Deploy Deposit Contract
This is a utility to help users deploy deposit contract for running their own beacon chain node in a local containerized set up. To run the utility, it assumes there is a running geth node as a separate process attached to proof-of-work main chain. The utility will deploy the validator registration contract and print out the contract address. Users will pass the contract address to the beacon chain node to monitor when they have been conducted to become an active validator.
### Usage
*Name:*
**deployContract** - this is a util to deploy deposit contract
*Usage:*
deployContract [global options] command [command options] [arguments...]
*Flags:*
- --ipcPath value Filename for IPC socket/pipe within the datadir
- --httpPath value HTTP-RPC server listening interface (default: "http://localhost:8545/")
- --passwordFile value Password file for unlock account (default: "./password.txt")
- --privKey value Private key to unlock account
- --k8sConfig value Name of kubernetes config map to update with the contract address
- --help, -h show help
- --version, -v print the version
### Example
To use private key with default RPC:
```
bazel run //contracts/deposit-contract/deployContract -- --httpPath=https://goerli.prylabs.net --privKey=$(echo /path/to/private/key/file)
```
### Output
```
INFO[0001] New contract deployed at 0x5275C2220C574330E230bFB7e4a0b96f60a18f02
```

View File

@@ -1,171 +0,0 @@
package main
import (
"bufio"
"context"
"io/ioutil"
"math/big"
"os"
"time"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
contracts "github.com/prysmaticlabs/prysm/contracts/deposit"
"github.com/prysmaticlabs/prysm/runtime/version"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
)
func main() {
var keystoreUTCPath string
var ipcPath string
var passwordFile string
var httpPath string
var privKeyString string
var drainAddress string
customFormatter := new(prefixed.TextFormatter)
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
customFormatter.FullTimestamp = true
logrus.SetFormatter(customFormatter)
log := logrus.WithField("prefix", "main")
app := cli.App{}
app.Name = "deployDepositContract"
app.Usage = "this is a util to deploy deposit contract"
app.Version = version.Version()
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "keystoreUTCPath",
Usage: "Location of keystore",
Destination: &keystoreUTCPath,
},
&cli.StringFlag{
Name: "ipcPath",
Usage: "Filename for IPC socket/pipe within the datadir",
Destination: &ipcPath,
},
&cli.StringFlag{
Name: "httpPath",
Value: "http://localhost:8545/",
Usage: "HTTP-RPC server listening interface",
Destination: &httpPath,
},
&cli.StringFlag{
Name: "passwordFile",
Value: "./password.txt",
Usage: "Password file for unlock account",
Destination: &passwordFile,
},
&cli.StringFlag{
Name: "privKey",
Usage: "Private key to unlock account",
Destination: &privKeyString,
},
&cli.StringFlag{
Name: "drainAddress",
Value: "",
Usage: "The drain address to specify in the contract. The default will be msg.sender",
Destination: &drainAddress,
},
}
app.Action = func(c *cli.Context) error {
// Set up RPC client
var rpcClient *rpc.Client
var err error
var txOps *bind.TransactOpts
// Uses HTTP-RPC if IPC is not set
if ipcPath == "" {
rpcClient, err = rpc.Dial(httpPath)
} else {
rpcClient, err = rpc.Dial(ipcPath)
}
if err != nil {
return err
}
client := ethclient.NewClient(rpcClient)
// User inputs private key, sign tx with private key
if privKeyString != "" {
privKey, err := crypto.HexToECDSA(privKeyString)
if err != nil {
log.Fatal(err)
}
txOps, err = bind.NewKeyedTransactorWithChainID(privKey, big.NewInt(1337))
if err != nil {
log.Fatal(err)
}
txOps.Value = big.NewInt(0)
txOps.GasLimit = 4000000
txOps.Context = context.Background()
// User inputs keystore json file, sign tx with keystore json
} else {
// #nosec - Inclusion of file via variable is OK for this tool.
file, err := os.Open(passwordFile)
if err != nil {
return err
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanWords)
scanner.Scan()
password := scanner.Text()
// #nosec - Inclusion of file via variable is OK for this tool.
keyJSON, err := ioutil.ReadFile(keystoreUTCPath)
if err != nil {
return err
}
privKey, err := keystore.DecryptKey(keyJSON, password)
if err != nil {
return err
}
txOps, err = bind.NewKeyedTransactorWithChainID(privKey.PrivateKey, big.NewInt(1337))
if err != nil {
log.Fatal(err)
}
txOps.Value = big.NewInt(0)
txOps.GasLimit = 4000000
txOps.Context = context.Background()
}
txOps.GasPrice = big.NewInt(10 * 1e9 /* 10 gwei */)
// Deploy validator registration contract
addr, tx, _, err := contracts.DeployDepositContract(
txOps,
client,
)
if err != nil {
return err
}
// Wait for contract to mine
for pending := true; pending; _, pending, err = client.TransactionByHash(context.Background(), tx.Hash()) {
if err != nil {
return err
}
time.Sleep(1 * time.Second)
}
log.WithFields(logrus.Fields{
"address": addr.Hex(),
}).Info("New contract deployed")
return nil
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}