mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-24 12:27:18 +00:00
5569a68452
* Value assigned to a variable is never read before being overwritten * The result of append is not used anywhere * Suspicious assignment of range-loop vars detected * Unused method receiver detected * Revert "Auxiliary commit to revert individual files from 54edcb445484a2e5d79612e19af8e949b8861253" This reverts commit bbd1e1beabf7b0c5cfc4f514dcc820062ad6c063. * Method modifies receiver * Fix test * Duplicate imports detected * Incorrectly formatted error string * Types of function parameters can be combined * One more "Unused method receiver detected" * Unused parameter detected in function
161 lines
5.0 KiB
Go
161 lines
5.0 KiB
Go
package depositcache
|
|
|
|
import (
|
|
"context"
|
|
"math/big"
|
|
"testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
var _ PendingDepositsFetcher = (*DepositCache)(nil)
|
|
|
|
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 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 = []*ethpb.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 = []*ethpb.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 = []*ethpb.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.DeepSSZEqual(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 = []*ethpb.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 := []*ethpb.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 = []*ethpb.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 := []*ethpb.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 = []*ethpb.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 = []*ethpb.DepositContainer{
|
|
{Eth1BlockHeight: 10, Index: 10},
|
|
{Eth1BlockHeight: 12, Index: 12},
|
|
}
|
|
|
|
assert.DeepEqual(t, expected, dc.pendingDeposits)
|
|
}
|