mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-08 21:08:10 -05:00
* Update fastssz * Merge branch 'master' of github.com:prysmaticlabs/prysm into update-fssz * fmt * gaz * Merge refs/heads/master into update-fssz * goimports * Merge refs/heads/master into update-fssz * Merge refs/heads/master into update-fssz * Merge refs/heads/master into update-fssz * Merge refs/heads/master into update-fssz * Merge refs/heads/master into update-fssz * Fix * fix ethereumapis * fix again * kafka * fix gen file * fix compute signing root * gofmt * checkpoint progress * progress * checkpoint * progress * Fix build * checkpoint * helpers * Another test fixed * gaz * another test fix * gofmt * some fixes * Merge branch 'master' of github.com:prysmaticlabs/prysm into update-fssz * fix one test * Merge branch 'master' of github.com:prysmaticlabs/prysm into update-fssz * fill empty checkpoint roots * more padding * more padding * Fix //beacon-chain/rpc/debug:go_default_test * fix //beacon-chain/core/state:go_default_test * fix //beacon-chain/core/state:go_default_test * fix some htr errors * fix //slasher/rpc:go_default_test * Progress on //beacon-chain/core/blocks:go_default_test * Progress on //beacon-chain/core/blocks:go_default_test * Progress on //beacon-chain/core/blocks:go_default_test * fix //slasher/db/kv:go_default_test * progress * fix //beacon-chain/sync/initial-sync:go_raceon_test * gofmt and gaz * fix one more test, taking a break * Fix //beacon-chain/core/blocks:go_default_test * Complete beacon-chain/powchain * Do most of beacon-chain/rpc/beacon/ * Do most of beacon-chain/blockchain * fix //beacon-chain/operations/attestations/kv:go_default_test * Fix //beacon-chain/cache/depositcache:go_default_test * Fix //slasher/detection:go_default_test * Progress * fix //beacon-chain/rpc/validator:go_default_test * gofmt * fix //validator/client:go_default_test * fix * fix //beacon-chain/blockchain:go_raceoff_test * fix //beacon-chain/rpc/beacon:go_default_test * fix 1 of 4 shards in //beacon-chain/sync:go_default_test * Fix //beacon-chain/sync:go_default_test and gofmt * prevent panic * fix //beacon-chain/state/stategen:go_default_test * fix * Merge branch 'master' of github.com:prysmaticlabs/prysm into update-fssz * fix most tests * Self review, go mod tidy, run regen scripts * fix slasher * Update ethereumapis * disable spawn strategy override * Merge refs/heads/master into update-fssz * Merge refs/heads/master into update-fssz * Remove extra line in imports * Remove extra line in imports * Gofmt * PR feedback from @nisdas
171 lines
5.2 KiB
Go
171 lines
5.2 KiB
Go
package depositcache
|
|
|
|
import (
|
|
"context"
|
|
"math/big"
|
|
"testing"
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
dbpb "github.com/prysmaticlabs/prysm/proto/beacon/db"
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
)
|
|
|
|
var _ = PendingDepositsFetcher(&DepositCache{})
|
|
|
|
func TestInsertPendingDeposit_OK(t *testing.T) {
|
|
dc := DepositCache{}
|
|
dc.InsertPendingDeposit(context.Background(), ðpb.Deposit{}, 111, 100, [32]byte{})
|
|
|
|
assert.Equal(t, 1, len(dc.pendingDeposits), "Deposit not inserted")
|
|
}
|
|
|
|
func TestInsertPendingDeposit_ignoresNilDeposit(t *testing.T) {
|
|
dc := DepositCache{}
|
|
dc.InsertPendingDeposit(context.Background(), nil /*deposit*/, 0 /*blockNum*/, 0, [32]byte{})
|
|
|
|
assert.Equal(t, 0, len(dc.pendingDeposits))
|
|
}
|
|
|
|
func makeDepositProof() [][]byte {
|
|
proof := make([][]byte, int(params.BeaconConfig().DepositContractTreeDepth)+1)
|
|
for i := range proof {
|
|
proof[i] = make([]byte, 32)
|
|
}
|
|
return proof
|
|
}
|
|
|
|
func TestRemovePendingDeposit_OK(t *testing.T) {
|
|
db := DepositCache{}
|
|
proof1 := makeDepositProof()
|
|
proof1[0] = bytesutil.PadTo([]byte{'A'}, 32)
|
|
proof2 := makeDepositProof()
|
|
proof2[0] = bytesutil.PadTo([]byte{'A'}, 32)
|
|
data := ðpb.Deposit_Data{
|
|
PublicKey: make([]byte, 48),
|
|
WithdrawalCredentials: make([]byte, 32),
|
|
Amount: 0,
|
|
Signature: make([]byte, 96),
|
|
}
|
|
depToRemove := ðpb.Deposit{Proof: proof1, Data: data}
|
|
otherDep := ðpb.Deposit{Proof: proof2, Data: data}
|
|
db.pendingDeposits = []*dbpb.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 = []*dbpb.DepositContainer{{Deposit: ðpb.Deposit{}}}
|
|
dc.RemovePendingDeposit(context.Background(), nil /*deposit*/)
|
|
assert.Equal(t, 1, len(dc.pendingDeposits), "Deposit unexpectedly removed")
|
|
}
|
|
|
|
func TestPendingDeposit_RoundTrip(t *testing.T) {
|
|
dc := DepositCache{}
|
|
proof := makeDepositProof()
|
|
proof[0] = bytesutil.PadTo([]byte{'A'}, 32)
|
|
data := ðpb.Deposit_Data{
|
|
PublicKey: make([]byte, 48),
|
|
WithdrawalCredentials: make([]byte, 32),
|
|
Amount: 0,
|
|
Signature: make([]byte, 96),
|
|
}
|
|
dep := ðpb.Deposit{Proof: proof, Data: data}
|
|
dc.InsertPendingDeposit(context.Background(), dep, 111, 100, [32]byte{})
|
|
dc.RemovePendingDeposit(context.Background(), dep)
|
|
assert.Equal(t, 0, len(dc.pendingDeposits), "Failed to insert & delete a pending deposit")
|
|
}
|
|
|
|
func TestPendingDeposits_OK(t *testing.T) {
|
|
dc := DepositCache{}
|
|
|
|
dc.pendingDeposits = []*dbpb.DepositContainer{
|
|
{Eth1BlockHeight: 2, Deposit: ðpb.Deposit{Proof: [][]byte{[]byte("A")}}},
|
|
{Eth1BlockHeight: 4, Deposit: ðpb.Deposit{Proof: [][]byte{[]byte("B")}}},
|
|
{Eth1BlockHeight: 6, Deposit: ðpb.Deposit{Proof: [][]byte{[]byte("c")}}},
|
|
}
|
|
|
|
deposits := dc.PendingDeposits(context.Background(), big.NewInt(4))
|
|
expected := []*ethpb.Deposit{
|
|
{Proof: [][]byte{[]byte("A")}},
|
|
{Proof: [][]byte{[]byte("B")}},
|
|
}
|
|
assert.DeepEqual(t, expected, deposits)
|
|
|
|
all := dc.PendingDeposits(context.Background(), nil)
|
|
assert.Equal(t, len(dc.pendingDeposits), len(all), "PendingDeposits(ctx, nil) did not return all deposits")
|
|
}
|
|
|
|
func TestPrunePendingDeposits_ZeroMerkleIndex(t *testing.T) {
|
|
dc := DepositCache{}
|
|
|
|
dc.pendingDeposits = []*dbpb.DepositContainer{
|
|
{Eth1BlockHeight: 2, Index: 2},
|
|
{Eth1BlockHeight: 4, Index: 4},
|
|
{Eth1BlockHeight: 6, Index: 6},
|
|
{Eth1BlockHeight: 8, Index: 8},
|
|
{Eth1BlockHeight: 10, Index: 10},
|
|
{Eth1BlockHeight: 12, Index: 12},
|
|
}
|
|
|
|
dc.PrunePendingDeposits(context.Background(), 0)
|
|
expected := []*dbpb.DepositContainer{
|
|
{Eth1BlockHeight: 2, Index: 2},
|
|
{Eth1BlockHeight: 4, Index: 4},
|
|
{Eth1BlockHeight: 6, Index: 6},
|
|
{Eth1BlockHeight: 8, Index: 8},
|
|
{Eth1BlockHeight: 10, Index: 10},
|
|
{Eth1BlockHeight: 12, Index: 12},
|
|
}
|
|
assert.DeepEqual(t, expected, dc.pendingDeposits)
|
|
}
|
|
|
|
func TestPrunePendingDeposits_OK(t *testing.T) {
|
|
dc := DepositCache{}
|
|
|
|
dc.pendingDeposits = []*dbpb.DepositContainer{
|
|
{Eth1BlockHeight: 2, Index: 2},
|
|
{Eth1BlockHeight: 4, Index: 4},
|
|
{Eth1BlockHeight: 6, Index: 6},
|
|
{Eth1BlockHeight: 8, Index: 8},
|
|
{Eth1BlockHeight: 10, Index: 10},
|
|
{Eth1BlockHeight: 12, Index: 12},
|
|
}
|
|
|
|
dc.PrunePendingDeposits(context.Background(), 6)
|
|
expected := []*dbpb.DepositContainer{
|
|
{Eth1BlockHeight: 6, Index: 6},
|
|
{Eth1BlockHeight: 8, Index: 8},
|
|
{Eth1BlockHeight: 10, Index: 10},
|
|
{Eth1BlockHeight: 12, Index: 12},
|
|
}
|
|
|
|
assert.DeepEqual(t, expected, dc.pendingDeposits)
|
|
|
|
dc.pendingDeposits = []*dbpb.DepositContainer{
|
|
{Eth1BlockHeight: 2, Index: 2},
|
|
{Eth1BlockHeight: 4, Index: 4},
|
|
{Eth1BlockHeight: 6, Index: 6},
|
|
{Eth1BlockHeight: 8, Index: 8},
|
|
{Eth1BlockHeight: 10, Index: 10},
|
|
{Eth1BlockHeight: 12, Index: 12},
|
|
}
|
|
|
|
dc.PrunePendingDeposits(context.Background(), 10)
|
|
expected = []*dbpb.DepositContainer{
|
|
{Eth1BlockHeight: 10, Index: 10},
|
|
{Eth1BlockHeight: 12, Index: 12},
|
|
}
|
|
|
|
assert.DeepEqual(t, expected, dc.pendingDeposits)
|
|
}
|