prysm-pulse/beacon-chain/cache/depositcache/pending_deposits_test.go
Raul Jordan 0326be86b5 Apply Patch Rules to Use EthereumAPIs Generated Protos in Prysm (#4112)
* starting on patch
* finish determining all required patches
* properly redefine the patch rules
* new patch
* rem double semicolon
* fix patch file
* Merge branch 'master' of github.com:prysmaticlabs/prysm into deprecate-eth-protos
* building the deps
* test target passes using ethereumapis
* compile gateway
* attempting to build everything
* e2e use ethereumapis
* more fixes for slasher
* other item
* getting closer to compiling slasher
* build slasher package
* Merge branch 'master' into deprecate-eth-protos
* Merge branch 'master' into deprecate-eth-protos
* fix benches
* lint gazelle
* Merge branch 'deprecate-eth-protos' of github.com:prysmaticlabs/prysm into deprecate-eth-protos
* proper gateway
* lint
* Merge branch 'master' into deprecate-eth-protos
* fix build
* Merge branch 'deprecate-eth-protos' of github.com:prysmaticlabs/prysm into deprecate-eth-protos
* use swag
* resolve
* ignore change
* include new patch changes
* fix test
* builds
* fix e2e
* gaz
2019-11-27 05:08:18 +00:00

163 lines
4.7 KiB
Go

package depositcache
import (
"context"
"math/big"
"reflect"
"testing"
"github.com/gogo/protobuf/proto"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
)
var _ = PendingDepositsFetcher(&DepositCache{})
func TestInsertPendingDeposit_OK(t *testing.T) {
dc := DepositCache{}
dc.InsertPendingDeposit(context.Background(), &ethpb.Deposit{}, big.NewInt(111), 100, [32]byte{})
if len(dc.pendingDeposits) != 1 {
t.Error("Deposit not inserted")
}
}
func TestInsertPendingDeposit_ignoresNilDeposit(t *testing.T) {
dc := DepositCache{}
dc.InsertPendingDeposit(context.Background(), nil /*deposit*/, nil /*blockNum*/, 0, [32]byte{})
if len(dc.pendingDeposits) > 0 {
t.Error("Unexpected deposit insertion")
}
}
func TestRemovePendingDeposit_OK(t *testing.T) {
db := DepositCache{}
depToRemove := &ethpb.Deposit{Proof: [][]byte{[]byte("A")}}
otherDep := &ethpb.Deposit{Proof: [][]byte{[]byte("B")}}
db.pendingDeposits = []*DepositContainer{
{Deposit: depToRemove, Index: 1},
{Deposit: otherDep, Index: 5},
}
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) {
dc := DepositCache{}
dc.pendingDeposits = []*DepositContainer{{Deposit: &ethpb.Deposit{}}}
dc.RemovePendingDeposit(context.Background(), nil /*deposit*/)
if len(dc.pendingDeposits) != 1 {
t.Errorf("Deposit unexpectedly removed")
}
}
func TestPendingDeposit_RoundTrip(t *testing.T) {
dc := DepositCache{}
dep := &ethpb.Deposit{Proof: [][]byte{[]byte("A")}}
dc.InsertPendingDeposit(context.Background(), dep, big.NewInt(111), 100, [32]byte{})
dc.RemovePendingDeposit(context.Background(), dep)
if len(dc.pendingDeposits) != 0 {
t.Error("Failed to insert & delete a pending deposit")
}
}
func TestPendingDeposits_OK(t *testing.T) {
dc := DepositCache{}
dc.pendingDeposits = []*DepositContainer{
{Block: big.NewInt(2), Deposit: &ethpb.Deposit{Proof: [][]byte{[]byte("A")}}},
{Block: big.NewInt(4), Deposit: &ethpb.Deposit{Proof: [][]byte{[]byte("B")}}},
{Block: big.NewInt(6), Deposit: &ethpb.Deposit{Proof: [][]byte{[]byte("c")}}},
}
deposits := dc.PendingDeposits(context.Background(), big.NewInt(4))
expected := []*ethpb.Deposit{
{Proof: [][]byte{[]byte("A")}},
{Proof: [][]byte{[]byte("B")}},
}
if !reflect.DeepEqual(deposits, expected) {
t.Errorf("Unexpected deposits. got=%+v want=%+v", deposits, expected)
}
all := dc.PendingDeposits(context.Background(), nil)
if len(all) != len(dc.pendingDeposits) {
t.Error("PendingDeposits(ctx, nil) did not return all deposits")
}
}
func TestPrunePendingDeposits_ZeroMerkleIndex(t *testing.T) {
dc := DepositCache{}
dc.pendingDeposits = []*DepositContainer{
{Block: big.NewInt(2), Index: 2},
{Block: big.NewInt(4), Index: 4},
{Block: big.NewInt(6), Index: 6},
{Block: big.NewInt(8), Index: 8},
{Block: big.NewInt(10), Index: 10},
{Block: big.NewInt(12), Index: 12},
}
dc.PrunePendingDeposits(context.Background(), 0)
expected := []*DepositContainer{
{Block: big.NewInt(2), Index: 2},
{Block: big.NewInt(4), Index: 4},
{Block: big.NewInt(6), Index: 6},
{Block: big.NewInt(8), Index: 8},
{Block: big.NewInt(10), Index: 10},
{Block: big.NewInt(12), Index: 12},
}
if !reflect.DeepEqual(dc.pendingDeposits, expected) {
t.Errorf("Unexpected deposits. got=%+v want=%+v", dc.pendingDeposits, expected)
}
}
func TestPrunePendingDeposits_OK(t *testing.T) {
dc := DepositCache{}
dc.pendingDeposits = []*DepositContainer{
{Block: big.NewInt(2), Index: 2},
{Block: big.NewInt(4), Index: 4},
{Block: big.NewInt(6), Index: 6},
{Block: big.NewInt(8), Index: 8},
{Block: big.NewInt(10), Index: 10},
{Block: big.NewInt(12), Index: 12},
}
dc.PrunePendingDeposits(context.Background(), 6)
expected := []*DepositContainer{
{Block: big.NewInt(6), Index: 6},
{Block: big.NewInt(8), Index: 8},
{Block: big.NewInt(10), Index: 10},
{Block: big.NewInt(12), Index: 12},
}
if !reflect.DeepEqual(dc.pendingDeposits, expected) {
t.Errorf("Unexpected deposits. got=%+v want=%+v", dc.pendingDeposits, expected)
}
dc.pendingDeposits = []*DepositContainer{
{Block: big.NewInt(2), Index: 2},
{Block: big.NewInt(4), Index: 4},
{Block: big.NewInt(6), Index: 6},
{Block: big.NewInt(8), Index: 8},
{Block: big.NewInt(10), Index: 10},
{Block: big.NewInt(12), Index: 12},
}
dc.PrunePendingDeposits(context.Background(), 10)
expected = []*DepositContainer{
{Block: big.NewInt(10), Index: 10},
{Block: big.NewInt(12), Index: 12},
}
if !reflect.DeepEqual(dc.pendingDeposits, expected) {
t.Errorf("Unexpected deposits. got=%+v want=%+v", dc.pendingDeposits, expected)
}
}