prysm-pulse/contracts/deposit-contract/deposit_tree_test.go
Radosław Kapka b742511193
Numeric conversion cleanup (#7465)
* validator count

* fix build errors

* fix test error

* randao mixes

* active indices

* lastReceivedMerkleIndex

* redundant conversions

* eth1FollowDistance

* refs

* fix compile error

* slasher

* validator

* revert changes to lastReceivedMerkleIndex

* remove todo

* fix references test

* fix committee test

* Revert "validator count"

This reverts commit 19b376e39914b88b620a113215daa539e3e66758.

# Conflicts:
#	beacon-chain/rpc/beacon/validators.go

* Revert "fix build errors"

This reverts commit f4acd6e9776c899a0523df10b64e55783b44c451.

* Revert "fix test error"

This reverts commit 2a5c9eec63e4546139220c71fe3c07ad92bed3b6.

* Revert "randao mixes"

This reverts commit 2863f9c24dd8cfc44ffce16321f68ef3b793e9a2.

* Revert "active indices"

This reverts commit 6e8385f3956a08ef9c8980b5343365a1bee48542.

* Revert "refs"

This reverts commit c64a153f67cd26daa58c3bf5f911da05d41cfb5d.

* Revert "fix references test"

This reverts commit fe773b55a6f8cc38c6c5c1f96615fdb8bbd4397d.

* Revert "fix committee test"

This reverts commit 7a0931c4487707e3eaf75a592415577f04253772.

* fix compilation errors

* Revert "slasher"

This reverts commit 8b34137931cff1e6cdffeaf65e1ef07eefdea647.

* trieutil

* some int() simplifications

* fix deepsource issues

* removed redundant fmt.Sprint

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
2020-10-13 12:43:49 +00:00

95 lines
3.3 KiB
Go

package depositcontract_test
import (
"strconv"
"testing"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
depositcontract "github.com/prysmaticlabs/prysm/contracts/deposit-contract"
"github.com/prysmaticlabs/prysm/shared/interop"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
"github.com/prysmaticlabs/prysm/shared/trieutil"
)
func TestDepositTrieRoot_OK(t *testing.T) {
testAcc, err := depositcontract.Setup()
require.NoError(t, err)
localTrie, err := trieutil.NewTrie(params.BeaconConfig().DepositContractTreeDepth)
require.NoError(t, err)
depRoot, err := testAcc.Contract.GetDepositRoot(&bind.CallOpts{})
require.NoError(t, err)
assert.Equal(t, depRoot, localTrie.HashTreeRoot(), "Local deposit trie root and contract deposit trie root are not equal")
privKeys, pubKeys, err := interop.DeterministicallyGenerateKeys(0 /*startIndex*/, 101)
require.NoError(t, err)
depositDataItems, depositDataRoots, err := interop.DepositDataFromKeys(privKeys, pubKeys)
require.NoError(t, err)
testAcc.TxOpts.Value = depositcontract.Amount32Eth()
for i := 0; i < 100; i++ {
data := depositDataItems[i]
dataRoot := [32]byte{}
copy(dataRoot[:], depositDataRoots[i])
_, err := testAcc.Contract.Deposit(testAcc.TxOpts, data.PublicKey, data.WithdrawalCredentials, data.Signature, dataRoot)
require.NoError(t, err, "Could not deposit to deposit contract")
testAcc.Backend.Commit()
item, err := data.HashTreeRoot()
require.NoError(t, err)
localTrie.Insert(item[:], i)
depRoot, err = testAcc.Contract.GetDepositRoot(&bind.CallOpts{})
require.NoError(t, err)
assert.Equal(t, depRoot, localTrie.HashTreeRoot(), "Local deposit trie root and contract deposit trie root are not equal for index %d", i)
}
}
func TestDepositTrieRoot_Fail(t *testing.T) {
testAcc, err := depositcontract.Setup()
require.NoError(t, err)
localTrie, err := trieutil.NewTrie(params.BeaconConfig().DepositContractTreeDepth)
require.NoError(t, err)
depRoot, err := testAcc.Contract.GetDepositRoot(&bind.CallOpts{})
require.NoError(t, err)
assert.Equal(t, depRoot, localTrie.HashTreeRoot(), "Local deposit trie root and contract deposit trie root are not equal")
privKeys, pubKeys, err := interop.DeterministicallyGenerateKeys(0 /*startIndex*/, 101)
require.NoError(t, err)
depositDataItems, depositDataRoots, err := interop.DepositDataFromKeys(privKeys, pubKeys)
require.NoError(t, err)
testAcc.TxOpts.Value = depositcontract.Amount32Eth()
for i := 0; i < 100; i++ {
data := depositDataItems[i]
dataRoot := [32]byte{}
copy(dataRoot[:], depositDataRoots[i])
_, err := testAcc.Contract.Deposit(testAcc.TxOpts, data.PublicKey, data.WithdrawalCredentials, data.Signature, dataRoot)
require.NoError(t, err, "Could not deposit to deposit contract")
// Change an element in the data when storing locally
copy(data.PublicKey, strconv.Itoa(i+10))
testAcc.Backend.Commit()
item, err := data.HashTreeRoot()
require.NoError(t, err)
localTrie.Insert(item[:], i)
depRoot, err = testAcc.Contract.GetDepositRoot(&bind.CallOpts{})
require.NoError(t, err)
assert.NotEqual(t, depRoot, localTrie.HashTreeRoot(), "Local deposit trie root and contract deposit trie root are equal for index %d", i)
}
}