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

@@ -5,8 +5,8 @@ go_library(
srcs = [
"contract.go",
"deposit.go",
"helper.go",
"logs.go",
"mock.go",
],
importpath = "github.com/prysmaticlabs/prysm/contracts/deposit",
visibility = ["//visibility:public"],
@@ -20,11 +20,8 @@ go_library(
"@com_github_ethereum_go_ethereum//:go_default_library",
"@com_github_ethereum_go_ethereum//accounts/abi:go_default_library",
"@com_github_ethereum_go_ethereum//accounts/abi/bind:go_default_library",
"@com_github_ethereum_go_ethereum//accounts/abi/bind/backends:go_default_library",
"@com_github_ethereum_go_ethereum//common:go_default_library",
"@com_github_ethereum_go_ethereum//core:go_default_library",
"@com_github_ethereum_go_ethereum//core/types:go_default_library",
"@com_github_ethereum_go_ethereum//crypto:go_default_library",
"@com_github_ethereum_go_ethereum//event:go_default_library",
"@com_github_pkg_errors//:go_default_library",
],
@@ -43,6 +40,7 @@ go_test(
"//beacon-chain/core/signing:go_default_library",
"//config/params:go_default_library",
"//container/trie:go_default_library",
"//contracts/deposit/mock:go_default_library",
"//crypto/bls:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"//runtime/interop:go_default_library",

View File

@@ -8,19 +8,20 @@ import (
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
depositcontract "github.com/prysmaticlabs/prysm/contracts/deposit"
"github.com/prysmaticlabs/prysm/contracts/deposit/mock"
"github.com/prysmaticlabs/prysm/runtime/interop"
"github.com/prysmaticlabs/prysm/testing/assert"
"github.com/prysmaticlabs/prysm/testing/require"
)
func TestSetupRegistrationContract_OK(t *testing.T) {
_, err := depositcontract.Setup()
_, err := mock.Setup()
assert.NoError(t, err, "Can not deploy validator registration contract")
}
// negative test case, deposit with less than 1 ETH which is less than the top off amount.
func TestRegister_Below1ETH(t *testing.T) {
testAccount, err := depositcontract.Setup()
testAccount, err := mock.Setup()
require.NoError(t, err)
// Generate deposit data
@@ -31,16 +32,16 @@ func TestRegister_Below1ETH(t *testing.T) {
var depositDataRoot [32]byte
copy(depositDataRoot[:], depositDataRoots[0])
testAccount.TxOpts.Value = depositcontract.LessThan1Eth()
testAccount.TxOpts.Value = mock.LessThan1Eth()
_, err = testAccount.Contract.Deposit(testAccount.TxOpts, pubKeys[0].Marshal(), depositDataItems[0].WithdrawalCredentials, depositDataItems[0].Signature, depositDataRoot)
assert.ErrorContains(t, "execution reverted", err, "Validator registration should have failed with insufficient deposit")
}
// normal test case, test depositing 32 ETH and verify HashChainValue event is correctly emitted.
func TestValidatorRegister_OK(t *testing.T) {
testAccount, err := depositcontract.Setup()
testAccount, err := mock.Setup()
require.NoError(t, err)
testAccount.TxOpts.Value = depositcontract.Amount32Eth()
testAccount.TxOpts.Value = mock.Amount32Eth()
// Generate deposit data
privKeys, pubKeys, err := interop.DeterministicallyGenerateKeys(0 /*startIndex*/, 1)

View File

@@ -7,7 +7,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/prysmaticlabs/prysm/config/params"
"github.com/prysmaticlabs/prysm/container/trie"
depositcontract "github.com/prysmaticlabs/prysm/contracts/deposit"
depositcontract "github.com/prysmaticlabs/prysm/contracts/deposit/mock"
"github.com/prysmaticlabs/prysm/runtime/interop"
"github.com/prysmaticlabs/prysm/testing/assert"
"github.com/prysmaticlabs/prysm/testing/require"

View File

@@ -0,0 +1,23 @@
package deposit
import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
)
// NewDepositContractcallFromBoundContract creates a new instance of DepositContractCaller, bound to
// a specific deployed contract.
func NewDepositContractCallerFromBoundContract(contract *bind.BoundContract) DepositContractCaller {
return DepositContractCaller{contract: contract}
}
// NewDepositContractTransactorFromBoundContract creates a new instance of
// DepositContractTransactor, bound to a specific deployed contract.
func NewDepositContractTransactorFromBoundContract(contract *bind.BoundContract) DepositContractTransactor {
return DepositContractTransactor{contract: contract}
}
// NewDepositContractFiltererFromBoundContract creates a new instance of
// DepositContractFilterer, bound to a specific deployed contract.
func NewDepositContractFiltererFromBoundContract(contract *bind.BoundContract) DepositContractFilterer {
return DepositContractFilterer{contract: contract}
}

View File

@@ -0,0 +1,19 @@
load("@prysm//tools/go:def.bzl", "go_library")
go_library(
name = "go_default_library",
testonly = True,
srcs = ["mock.go"],
importpath = "github.com/prysmaticlabs/prysm/contracts/deposit/mock",
visibility = ["//visibility:public"],
deps = [
"//contracts/deposit:go_default_library",
"@com_github_ethereum_go_ethereum//accounts/abi:go_default_library",
"@com_github_ethereum_go_ethereum//accounts/abi/bind:go_default_library",
"@com_github_ethereum_go_ethereum//accounts/abi/bind/backends:go_default_library",
"@com_github_ethereum_go_ethereum//common:go_default_library",
"@com_github_ethereum_go_ethereum//core:go_default_library",
"@com_github_ethereum_go_ethereum//core/types:go_default_library",
"@com_github_ethereum_go_ethereum//crypto:go_default_library",
],
)

View File

@@ -1,4 +1,4 @@
package deposit
package mock
import (
"crypto/ecdsa"
@@ -13,6 +13,7 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/prysmaticlabs/prysm/contracts/deposit"
)
var (
@@ -26,7 +27,7 @@ var (
type TestAccount struct {
Addr common.Address
ContractAddr common.Address
Contract *DepositContract
Contract *deposit.DepositContract
Backend *backends.SimulatedBackend
TxOpts *bind.TransactOpts
}
@@ -79,8 +80,8 @@ func LessThan1Eth() *big.Int {
}
// DeployDepositContract deploys a new Ethereum contract, binding an instance of DepositContract to it.
func DeployDepositContract(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *DepositContract, error) {
parsed, err := abi.JSON(strings.NewReader(DepositContractABI))
func DeployDepositContract(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *deposit.DepositContract, error) {
parsed, err := abi.JSON(strings.NewReader(deposit.DepositContractABI))
if err != nil {
return common.Address{}, nil, nil, err
}
@@ -89,5 +90,9 @@ func DeployDepositContract(auth *bind.TransactOpts, backend bind.ContractBackend
if err != nil {
return common.Address{}, nil, nil, err
}
return address, tx, &DepositContract{DepositContractCaller: DepositContractCaller{contract: contract}, DepositContractTransactor: DepositContractTransactor{contract: contract}, DepositContractFilterer: DepositContractFilterer{contract: contract}}, nil
return address, tx, &deposit.DepositContract{
DepositContractCaller: deposit.NewDepositContractCallerFromBoundContract(contract),
DepositContractTransactor: deposit.NewDepositContractTransactorFromBoundContract(contract),
DepositContractFilterer: deposit.NewDepositContractFiltererFromBoundContract(contract),
}, nil
}