prysm-pulse/contracts/deposit/deposit_tree_test.go
terencechain d17996f8b0
Update to V4 🚀 (#12134)
* Update V3 from V4

* Fix build v3 -> v4

* Update ssz

* Update beacon_chain.pb.go

* Fix formatter import

* Update update-mockgen.sh comment to v4

* Fix conflicts. Pass build and tests

* Fix test
2023-03-17 18:52:56 +00:00

103 lines
3.5 KiB
Go

package deposit_test
import (
"strconv"
"testing"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/prysmaticlabs/prysm/v4/config/params"
"github.com/prysmaticlabs/prysm/v4/container/trie"
depositcontract "github.com/prysmaticlabs/prysm/v4/contracts/deposit/mock"
"github.com/prysmaticlabs/prysm/v4/runtime/interop"
"github.com/prysmaticlabs/prysm/v4/testing/assert"
"github.com/prysmaticlabs/prysm/v4/testing/require"
)
func TestDepositTrieRoot_OK(t *testing.T) {
testAcc, err := depositcontract.Setup()
require.NoError(t, err)
localTrie, err := trie.NewTrie(params.BeaconConfig().DepositContractTreeDepth)
require.NoError(t, err)
depRoot, err := testAcc.Contract.GetDepositRoot(&bind.CallOpts{})
require.NoError(t, err)
localRoot, err := localTrie.HashTreeRoot()
require.NoError(t, err)
assert.Equal(t, depRoot, localRoot, "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]
var 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)
assert.NoError(t, localTrie.Insert(item[:], i))
depRoot, err = testAcc.Contract.GetDepositRoot(&bind.CallOpts{})
require.NoError(t, err)
localRoot, err := localTrie.HashTreeRoot()
require.NoError(t, err)
assert.Equal(t, depRoot, localRoot, "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 := trie.NewTrie(params.BeaconConfig().DepositContractTreeDepth)
require.NoError(t, err)
depRoot, err := testAcc.Contract.GetDepositRoot(&bind.CallOpts{})
require.NoError(t, err)
localRoot, err := localTrie.HashTreeRoot()
require.NoError(t, err)
assert.Equal(t, depRoot, localRoot, "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]
var 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)
assert.NoError(t, localTrie.Insert(item[:], i))
depRoot, err = testAcc.Contract.GetDepositRoot(&bind.CallOpts{})
require.NoError(t, err)
localRoot, err := localTrie.HashTreeRoot()
require.NoError(t, err)
assert.NotEqual(t, depRoot, localRoot, "Local deposit trie root and contract deposit trie root are equal for index %d", i)
}
}