prysm-pulse/beacon-chain/db/pending_deposits_test.go
Raul Jordan c1a9937760
Post ChainStart Validator Activation (#1934)
* implemented all the merkle func signatures

* branch indices complete

* check for index out of range in generating merkle proof

* completed full tests for sparse merkle trie impl

* lint

* formatting

* gazelle

* commentary

* ivan comments

* fmt

* get rid of the deposit trie

* recalculate trie when eth1 data changes

* default data response recalculates tree

* update merkle trie to use raw bytes

* use the new verify merkle root func

* builds

* if default data response historical deposits are empty, return the deposit root at the contract at the time

* work on trie

* trying again with more logging

* keep track of merkle trie indices, use correct big int ops

* use uint for merkle idx

* add todo

* update ticker correctly

* fix config and remove unnecessary logs

* readd plus one fix

* clarify some details

* weird imports spacing

* gazelle, lint

* fix tests using the new deposit trie

* builds but tests still fail

* rpc tests

* lint

* tests pass

* bazel lint

* rem commented block

* revert att slot fix

* preston comments

* comments

* fix build

* address last comment

* use counter

* imports
2019-03-12 00:05:55 -04:00

89 lines
2.4 KiB
Go

package db
import (
"context"
"math/big"
"reflect"
"testing"
"github.com/gogo/protobuf/proto"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
)
func TestInsertPendingDeposit_OK(t *testing.T) {
db := BeaconDB{}
db.InsertPendingDeposit(context.Background(), &pb.Deposit{}, big.NewInt(111))
if len(db.pendingDeposits) != 1 {
t.Error("Deposit not inserted")
}
}
func TestInsertPendingDeposit_ignoresNilDeposit(t *testing.T) {
db := BeaconDB{}
db.InsertPendingDeposit(context.Background(), nil /*deposit*/, nil /*blockNum*/)
if len(db.pendingDeposits) > 0 {
t.Error("Unexpected deposit insertion")
}
}
func TestRemovePendingDeposit_OK(t *testing.T) {
db := BeaconDB{}
depToRemove := &pb.Deposit{MerkleTreeIndex: 1}
otherDep := &pb.Deposit{MerkleTreeIndex: 5}
db.pendingDeposits = []*depositContainer{
{deposit: depToRemove},
{deposit: otherDep},
}
db.RemovePendingDeposit(context.Background(), depToRemove)
if len(db.pendingDeposits) != 1 || !proto.Equal(db.pendingDeposits[0].deposit, otherDep) {
t.Error("Failed to remove deposit")
}
}
func TestRemovePendingDeposit_IgnoresNilDeposit(t *testing.T) {
db := BeaconDB{}
db.pendingDeposits = []*depositContainer{{deposit: &pb.Deposit{}}}
db.RemovePendingDeposit(context.Background(), nil /*deposit*/)
if len(db.pendingDeposits) != 1 {
t.Errorf("Deposit unexpectedly removed")
}
}
func TestPendingDeposit_RoundTrip(t *testing.T) {
db := BeaconDB{}
dep := &pb.Deposit{MerkleTreeIndex: 123}
db.InsertPendingDeposit(context.Background(), dep, big.NewInt(111))
db.RemovePendingDeposit(context.Background(), dep)
if len(db.pendingDeposits) != 0 {
t.Error("Failed to insert & delete a pending deposit")
}
}
func TestPendingDeposits_OK(t *testing.T) {
db := BeaconDB{}
db.pendingDeposits = []*depositContainer{
{block: big.NewInt(2), deposit: &pb.Deposit{MerkleTreeIndex: 2}},
{block: big.NewInt(4), deposit: &pb.Deposit{MerkleTreeIndex: 4}},
{block: big.NewInt(6), deposit: &pb.Deposit{MerkleTreeIndex: 6}},
}
deposits := db.PendingDeposits(context.Background(), big.NewInt(4))
expected := []*pb.Deposit{
{MerkleTreeIndex: 2},
{MerkleTreeIndex: 4},
}
if !reflect.DeepEqual(deposits, expected) {
t.Errorf("Unexpected deposits. got=%+v want=%+v", deposits, expected)
}
all := db.PendingDeposits(context.Background(), nil)
if len(all) != len(db.pendingDeposits) {
t.Error("PendingDeposits(ctx, nil) did not return all deposits")
}
}