2021-08-24 07:54:04 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2021-08-26 05:01:09 +00:00
|
|
|
v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
|
2021-08-24 07:54:04 +00:00
|
|
|
v2 "github.com/prysmaticlabs/prysm/beacon-chain/state/v2"
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2021-08-26 05:01:09 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
2021-08-24 07:54:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSyncCommitteeHeadState(t *testing.T) {
|
2021-08-26 05:01:09 +00:00
|
|
|
beaconState, err := v2.InitializeFromProto(ðpb.BeaconStateAltair{
|
|
|
|
Fork: ðpb.Fork{
|
|
|
|
PreviousVersion: params.BeaconConfig().GenesisForkVersion,
|
|
|
|
CurrentVersion: params.BeaconConfig().GenesisForkVersion,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
phase0State, err := v1.InitializeFromProto(ðpb.BeaconState{
|
|
|
|
Fork: ðpb.Fork{
|
|
|
|
PreviousVersion: params.BeaconConfig().GenesisForkVersion,
|
|
|
|
CurrentVersion: params.BeaconConfig().GenesisForkVersion,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2021-08-24 07:54:04 +00:00
|
|
|
type put struct {
|
|
|
|
slot types.Slot
|
|
|
|
state state.BeaconState
|
|
|
|
}
|
|
|
|
tests := []struct {
|
2021-08-26 05:01:09 +00:00
|
|
|
name string
|
|
|
|
key types.Slot
|
|
|
|
put *put
|
|
|
|
want state.BeaconState
|
|
|
|
wantErr bool
|
|
|
|
wantPutErr bool
|
2021-08-24 07:54:04 +00:00
|
|
|
}{
|
2021-08-26 05:01:09 +00:00
|
|
|
{
|
|
|
|
name: "putting error in",
|
|
|
|
key: types.Slot(1),
|
|
|
|
put: &put{
|
|
|
|
slot: types.Slot(1),
|
|
|
|
state: nil,
|
|
|
|
},
|
|
|
|
wantPutErr: true,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "putting invalid state in",
|
|
|
|
key: types.Slot(1),
|
|
|
|
put: &put{
|
|
|
|
slot: types.Slot(1),
|
|
|
|
state: phase0State,
|
|
|
|
},
|
|
|
|
wantPutErr: true,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2021-08-24 07:54:04 +00:00
|
|
|
{
|
|
|
|
name: "not found when empty cache",
|
|
|
|
key: types.Slot(1),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not found when non-existent key in non-empty cache",
|
|
|
|
key: types.Slot(2),
|
|
|
|
put: &put{
|
|
|
|
slot: types.Slot(1),
|
2021-08-26 05:01:09 +00:00
|
|
|
state: beaconState,
|
2021-08-24 07:54:04 +00:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "found with key",
|
|
|
|
key: types.Slot(1),
|
|
|
|
put: &put{
|
|
|
|
slot: types.Slot(1),
|
2021-08-26 05:01:09 +00:00
|
|
|
state: beaconState,
|
2021-08-24 07:54:04 +00:00
|
|
|
},
|
2021-08-26 05:01:09 +00:00
|
|
|
want: beaconState,
|
2021-08-24 07:54:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-08-26 05:01:09 +00:00
|
|
|
c := NewSyncCommitteeHeadState()
|
2021-08-24 07:54:04 +00:00
|
|
|
if tt.put != nil {
|
2021-08-26 05:01:09 +00:00
|
|
|
err := c.Put(tt.put.slot, tt.put.state)
|
|
|
|
if (err != nil) != tt.wantPutErr {
|
|
|
|
t.Fatalf("Put() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
2021-08-24 07:54:04 +00:00
|
|
|
}
|
|
|
|
got, err := c.Get(tt.key)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Fatalf("Get() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
require.DeepEqual(t, tt.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|