prysm-pulse/contracts/deposit/contract_test.go
Raul Jordan d077483577
Add V3 Suffix to All Prysm Packages (#11083)
* v3 import renamings

* tidy

* fmt

* rev

* Update beacon-chain/core/epoch/precompute/reward_penalty_test.go

* Update beacon-chain/core/helpers/validators_test.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/iface/BUILD.bazel

* Update beacon-chain/db/kv/kv.go

* Update beacon-chain/db/kv/state.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/sync/initial-sync/service.go

* fix deps

* fix bad replacements

* fix bad replacements

* change back

* gohashtree version

* fix deps

Co-authored-by: Nishant Das <nishdas93@gmail.com>
Co-authored-by: Potuz <potuz@prysmaticlabs.com>
2022-08-16 12:20:13 +00:00

85 lines
3.5 KiB
Go

package deposit_test
import (
"context"
"encoding/binary"
"testing"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
depositcontract "github.com/prysmaticlabs/prysm/v3/contracts/deposit"
"github.com/prysmaticlabs/prysm/v3/contracts/deposit/mock"
"github.com/prysmaticlabs/prysm/v3/runtime/interop"
"github.com/prysmaticlabs/prysm/v3/testing/assert"
"github.com/prysmaticlabs/prysm/v3/testing/require"
)
func TestSetupRegistrationContract_OK(t *testing.T) {
_, 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 := mock.Setup()
require.NoError(t, err)
// Generate deposit data
privKeys, pubKeys, err := interop.DeterministicallyGenerateKeys(0 /*startIndex*/, 1)
require.NoError(t, err)
depositDataItems, depositDataRoots, err := interop.DepositDataFromKeys(privKeys, pubKeys)
require.NoError(t, err)
var depositDataRoot [32]byte
copy(depositDataRoot[:], depositDataRoots[0])
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 := mock.Setup()
require.NoError(t, err)
testAccount.TxOpts.Value = mock.Amount32Eth()
// Generate deposit data
privKeys, pubKeys, err := interop.DeterministicallyGenerateKeys(0 /*startIndex*/, 1)
require.NoError(t, err)
depositDataItems, depositDataRoots, err := interop.DepositDataFromKeys(privKeys, pubKeys)
require.NoError(t, err)
var depositDataRoot [32]byte
copy(depositDataRoot[:], depositDataRoots[0])
_, err = testAccount.Contract.Deposit(testAccount.TxOpts, pubKeys[0].Marshal(), depositDataItems[0].WithdrawalCredentials, depositDataItems[0].Signature, depositDataRoot)
testAccount.Backend.Commit()
require.NoError(t, err, "Validator registration failed")
_, err = testAccount.Contract.Deposit(testAccount.TxOpts, pubKeys[0].Marshal(), depositDataItems[0].WithdrawalCredentials, depositDataItems[0].Signature, depositDataRoot)
testAccount.Backend.Commit()
assert.NoError(t, err, "Validator registration failed")
_, err = testAccount.Contract.Deposit(testAccount.TxOpts, pubKeys[0].Marshal(), depositDataItems[0].WithdrawalCredentials, depositDataItems[0].Signature, depositDataRoot)
testAccount.Backend.Commit()
assert.NoError(t, err, "Validator registration failed")
query := ethereum.FilterQuery{
Addresses: []common.Address{
testAccount.ContractAddr,
},
}
logs, err := testAccount.Backend.FilterLogs(context.Background(), query)
assert.NoError(t, err, "Unable to get logs of deposit contract")
merkleTreeIndex := make([]uint64, 5)
for i, log := range logs {
_, _, _, _, idx, err := depositcontract.UnpackDepositLogData(log.Data)
require.NoError(t, err, "Unable to unpack log data")
merkleTreeIndex[i] = binary.LittleEndian.Uint64(idx)
}
assert.Equal(t, uint64(0), merkleTreeIndex[0], "Deposit event total desposit count miss matched")
assert.Equal(t, uint64(1), merkleTreeIndex[1], "Deposit event total desposit count miss matched")
assert.Equal(t, uint64(2), merkleTreeIndex[2], "Deposit event total desposit count miss matched")
}