2021-08-24 07:54:04 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2024-02-15 05:46:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v5/beacon-chain/state"
|
|
|
|
state_native "github.com/prysmaticlabs/prysm/v5/beacon-chain/state/state-native"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/require"
|
2021-08-24 07:54:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSyncCommitteeHeadState(t *testing.T) {
|
2022-09-16 22:17:46 +00:00
|
|
|
beaconState, err := state_native.InitializeFromProtoAltair(ðpb.BeaconStateAltair{
|
2021-08-26 05:01:09 +00:00
|
|
|
Fork: ðpb.Fork{
|
|
|
|
PreviousVersion: params.BeaconConfig().GenesisForkVersion,
|
|
|
|
CurrentVersion: params.BeaconConfig().GenesisForkVersion,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2022-09-16 22:17:46 +00:00
|
|
|
phase0State, err := state_native.InitializeFromProtoPhase0(ðpb.BeaconState{
|
2021-08-26 05:01:09 +00:00
|
|
|
Fork: ðpb.Fork{
|
|
|
|
PreviousVersion: params.BeaconConfig().GenesisForkVersion,
|
|
|
|
CurrentVersion: params.BeaconConfig().GenesisForkVersion,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2022-09-16 22:17:46 +00:00
|
|
|
bellatrixState, err := state_native.InitializeFromProtoBellatrix(ðpb.BeaconStateBellatrix{
|
2021-12-13 18:04:37 +00:00
|
|
|
Fork: ðpb.Fork{
|
|
|
|
PreviousVersion: params.BeaconConfig().GenesisForkVersion,
|
|
|
|
CurrentVersion: params.BeaconConfig().GenesisForkVersion,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2022-12-16 00:42:43 +00:00
|
|
|
capellaState, err := state_native.InitializeFromProtoCapella(ðpb.BeaconStateCapella{
|
|
|
|
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 {
|
2023-01-26 14:40:12 +00:00
|
|
|
slot primitives.Slot
|
2021-08-24 07:54:04 +00:00
|
|
|
state state.BeaconState
|
|
|
|
}
|
|
|
|
tests := []struct {
|
2021-08-26 05:01:09 +00:00
|
|
|
name string
|
2023-01-26 14:40:12 +00:00
|
|
|
key primitives.Slot
|
2021-08-26 05:01:09 +00:00
|
|
|
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",
|
2023-01-26 14:40:12 +00:00
|
|
|
key: primitives.Slot(1),
|
2021-08-26 05:01:09 +00:00
|
|
|
put: &put{
|
2023-01-26 14:40:12 +00:00
|
|
|
slot: primitives.Slot(1),
|
2021-08-26 05:01:09 +00:00
|
|
|
state: nil,
|
|
|
|
},
|
|
|
|
wantPutErr: true,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "putting invalid state in",
|
2023-01-26 14:40:12 +00:00
|
|
|
key: primitives.Slot(1),
|
2021-08-26 05:01:09 +00:00
|
|
|
put: &put{
|
2023-01-26 14:40:12 +00:00
|
|
|
slot: primitives.Slot(1),
|
2021-08-26 05:01:09 +00:00
|
|
|
state: phase0State,
|
|
|
|
},
|
|
|
|
wantPutErr: true,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2021-08-24 07:54:04 +00:00
|
|
|
{
|
|
|
|
name: "not found when empty cache",
|
2023-01-26 14:40:12 +00:00
|
|
|
key: primitives.Slot(1),
|
2021-08-24 07:54:04 +00:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not found when non-existent key in non-empty cache",
|
2023-01-26 14:40:12 +00:00
|
|
|
key: primitives.Slot(2),
|
2021-08-24 07:54:04 +00:00
|
|
|
put: &put{
|
2023-01-26 14:40:12 +00:00
|
|
|
slot: primitives.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",
|
2023-01-26 14:40:12 +00:00
|
|
|
key: primitives.Slot(1),
|
2021-08-24 07:54:04 +00:00
|
|
|
put: &put{
|
2023-01-26 14:40:12 +00:00
|
|
|
slot: primitives.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
|
|
|
},
|
2021-12-13 18:04:37 +00:00
|
|
|
{
|
2022-01-10 16:47:30 +00:00
|
|
|
name: "not found when non-existent key in non-empty cache (bellatrix state)",
|
2023-01-26 14:40:12 +00:00
|
|
|
key: primitives.Slot(2),
|
2021-12-13 18:04:37 +00:00
|
|
|
put: &put{
|
2023-01-26 14:40:12 +00:00
|
|
|
slot: primitives.Slot(1),
|
2022-01-10 16:47:30 +00:00
|
|
|
state: bellatrixState,
|
2021-12-13 18:04:37 +00:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
2022-01-10 16:47:30 +00:00
|
|
|
name: "found with key (bellatrix state)",
|
2023-01-26 14:40:12 +00:00
|
|
|
key: primitives.Slot(100),
|
2021-12-13 18:04:37 +00:00
|
|
|
put: &put{
|
2023-01-26 14:40:12 +00:00
|
|
|
slot: primitives.Slot(100),
|
2022-01-10 16:47:30 +00:00
|
|
|
state: bellatrixState,
|
2021-12-13 18:04:37 +00:00
|
|
|
},
|
2022-01-10 16:47:30 +00:00
|
|
|
want: bellatrixState,
|
2021-12-13 18:04:37 +00:00
|
|
|
},
|
2022-12-16 00:42:43 +00:00
|
|
|
{
|
|
|
|
name: "found with key (capella state)",
|
2023-01-26 14:40:12 +00:00
|
|
|
key: primitives.Slot(200),
|
2022-12-16 00:42:43 +00:00
|
|
|
put: &put{
|
2023-01-26 14:40:12 +00:00
|
|
|
slot: primitives.Slot(200),
|
2022-12-16 00:42:43 +00:00
|
|
|
state: capellaState,
|
|
|
|
},
|
|
|
|
want: capellaState,
|
|
|
|
},
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|