prysm-pulse/contracts/deposit-contract/deposit_tree_test.go
Nishant Das c41140e15a Optimize Insertion in Deposit Trie (#4299)
* current changes
* change algorithm for tree insert
* almost done with getting this to pass
* unit test passes
* tests now pass
* fix in repo
* Merge branch 'master' into optimizeDepositLogs
* fix build
* Merge branch 'optimizeDepositLogs' of github.com:prysmaticlabs/prysm into optimizeDepositLogs
* remove tautology
* fix tautology
* fix up sparsity
* Merge branch 'master' into optimizeDepositLogs
* further fixes
* Merge branch 'optimizeDepositLogs' of github.com:prysmaticlabs/prysm into optimizeDepositLogs
* Update shared/trieutil/sparse_merkle.go
* comments
* Merge branch 'optimizeDepositLogs' of github.com:prysmaticlabs/prysm into optimizeDepositLogs
* add bench for optimized
* gaz
* Merge refs/heads/master into optimizeDepositLogs
2019-12-17 02:19:12 +00:00

132 lines
3.6 KiB
Go

package depositcontract_test
import (
"strconv"
"testing"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/prysmaticlabs/go-ssz"
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/trieutil"
)
func TestDepositTrieRoot_OK(t *testing.T) {
testAcc, err := depositcontract.Setup()
if err != nil {
t.Fatal(err)
}
localTrie, err := trieutil.NewTrie(int(params.BeaconConfig().DepositContractTreeDepth))
if err != nil {
t.Fatal(err)
}
depRoot, err := testAcc.Contract.GetDepositRoot(&bind.CallOpts{})
if err != nil {
t.Fatal(err)
}
if depRoot != localTrie.HashTreeRoot() {
t.Errorf("Local deposit trie root and contract deposit trie root are not equal. Expected %#x , Got %#x", depRoot, localTrie.Root())
}
privKeys, pubKeys, err := interop.DeterministicallyGenerateKeys(0 /*startIndex*/, 101)
if err != nil {
t.Fatal(err)
}
depositDataItems, depositDataRoots, err := interop.DepositDataFromKeys(privKeys, pubKeys)
if err != nil {
t.Fatal(err)
}
testAcc.TxOpts.Value = depositcontract.Amount32Eth()
for i := 0; i < 100; i++ {
data := depositDataItems[i]
dataRoot := [32]byte{}
copy(dataRoot[:], depositDataRoots[i])
if _, err := testAcc.Contract.Deposit(testAcc.TxOpts, data.PublicKey, data.WithdrawalCredentials, data.Signature, dataRoot); err != nil {
t.Fatalf("Could not deposit to deposit contract %v", err)
}
testAcc.Backend.Commit()
item, err := ssz.HashTreeRoot(data)
if err != nil {
t.Fatal(err)
}
localTrie.Insert(item[:], i)
depRoot, err = testAcc.Contract.GetDepositRoot(&bind.CallOpts{})
if err != nil {
t.Fatal(err)
}
if depRoot != localTrie.HashTreeRoot() {
t.Errorf("Local deposit trie root and contract deposit trie root are not equal for index %d. Expected %#x , Got %#x", i, depRoot, localTrie.Root())
}
}
}
func TestDepositTrieRoot_Fail(t *testing.T) {
testAcc, err := depositcontract.Setup()
if err != nil {
t.Fatal(err)
}
localTrie, err := trieutil.NewTrie(int(params.BeaconConfig().DepositContractTreeDepth))
if err != nil {
t.Fatal(err)
}
depRoot, err := testAcc.Contract.GetDepositRoot(&bind.CallOpts{})
if err != nil {
t.Fatal(err)
}
if depRoot != localTrie.HashTreeRoot() {
t.Errorf("Local deposit trie root and contract deposit trie root are not equal. Expected %#x , Got %#x", depRoot, localTrie.Root())
}
privKeys, pubKeys, err := interop.DeterministicallyGenerateKeys(0 /*startIndex*/, 101)
if err != nil {
t.Fatal(err)
}
depositDataItems, depositDataRoots, err := interop.DepositDataFromKeys(privKeys, pubKeys)
if err != nil {
t.Fatal(err)
}
testAcc.TxOpts.Value = depositcontract.Amount32Eth()
for i := 0; i < 100; i++ {
data := depositDataItems[i]
dataRoot := [32]byte{}
copy(dataRoot[:], depositDataRoots[i])
if _, err := testAcc.Contract.Deposit(testAcc.TxOpts, data.PublicKey, data.WithdrawalCredentials, data.Signature, dataRoot); err != nil {
t.Fatalf("Could not deposit to deposit contract %v", err)
}
// Change an element in the data when storing locally
copy(data.PublicKey, []byte(strconv.Itoa(i+10)))
testAcc.Backend.Commit()
item, err := ssz.HashTreeRoot(data)
if err != nil {
t.Fatal(err)
}
localTrie.Insert(item[:], i)
depRoot, err = testAcc.Contract.GetDepositRoot(&bind.CallOpts{})
if err != nil {
t.Fatal(err)
}
if depRoot == localTrie.HashTreeRoot() {
t.Errorf("Local deposit trie root and contract deposit trie root are equal for index %d when they were expected to be not equal", i)
}
}
}