2021-04-22 16:34:00 +00:00
|
|
|
package epoch_processing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/bazelbuild/rules_go/go/tools/bazel"
|
|
|
|
"github.com/golang/snappy"
|
2021-07-23 16:11:21 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2021-07-22 17:13:18 +00:00
|
|
|
v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
|
2021-07-29 21:45:17 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/util"
|
2021-05-17 18:32:04 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2021-04-22 16:34:00 +00:00
|
|
|
)
|
|
|
|
|
2021-07-23 16:11:21 +00:00
|
|
|
type epochOperation func(*testing.T, state.BeaconState) (state.BeaconState, error)
|
2021-04-22 16:34:00 +00:00
|
|
|
|
|
|
|
// RunEpochOperationTest takes in the prestate and processes it through the
|
|
|
|
// passed in epoch operation function and checks the post state with the expected post state.
|
|
|
|
func RunEpochOperationTest(
|
|
|
|
t *testing.T,
|
|
|
|
testFolderPath string,
|
|
|
|
operationFn epochOperation,
|
|
|
|
) {
|
2021-09-23 18:53:46 +00:00
|
|
|
preBeaconStateFile, err := util.BazelFileBytes(path.Join(testFolderPath, "pre.ssz_snappy"))
|
2021-04-22 16:34:00 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
preBeaconStateSSZ, err := snappy.Decode(nil /* dst */, preBeaconStateFile)
|
|
|
|
require.NoError(t, err, "Failed to decompress")
|
2021-07-29 21:45:17 +00:00
|
|
|
preBeaconStateBase := ðpb.BeaconState{}
|
2021-04-22 16:34:00 +00:00
|
|
|
if err := preBeaconStateBase.UnmarshalSSZ(preBeaconStateSSZ); err != nil {
|
|
|
|
t.Fatalf("Failed to unmarshal: %v", err)
|
|
|
|
}
|
2021-06-30 15:06:19 +00:00
|
|
|
preBeaconState, err := v1.InitializeFromProto(preBeaconStateBase)
|
2021-04-22 16:34:00 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// If the post.ssz is not present, it means the test should fail on our end.
|
|
|
|
postSSZFilepath, err := bazel.Runfile(path.Join(testFolderPath, "post.ssz_snappy"))
|
|
|
|
postSSZExists := true
|
|
|
|
if err != nil && strings.Contains(err.Error(), "could not locate file") {
|
|
|
|
postSSZExists = false
|
|
|
|
} else if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
beaconState, err := operationFn(t, preBeaconState)
|
|
|
|
if postSSZExists {
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-08-15 15:24:13 +00:00
|
|
|
postBeaconStateFile, err := ioutil.ReadFile(postSSZFilepath) // #nosec G304
|
2021-04-22 16:34:00 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
postBeaconStateSSZ, err := snappy.Decode(nil /* dst */, postBeaconStateFile)
|
|
|
|
require.NoError(t, err, "Failed to decompress")
|
2021-07-29 21:45:17 +00:00
|
|
|
postBeaconState := ðpb.BeaconState{}
|
2021-04-22 16:34:00 +00:00
|
|
|
if err := postBeaconState.UnmarshalSSZ(postBeaconStateSSZ); err != nil {
|
|
|
|
t.Fatalf("Failed to unmarshal: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-06-30 15:06:19 +00:00
|
|
|
pbState, err := v1.ProtobufBeaconState(beaconState.InnerStateUnsafe())
|
2021-04-22 16:34:00 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
if !proto.Equal(pbState, postBeaconState) {
|
|
|
|
t.Fatal("Post state does not match expected")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Note: This doesn't test anything worthwhile. It essentially tests
|
|
|
|
// that *any* error has occurred, not any specific error.
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Did not fail when expected")
|
|
|
|
}
|
|
|
|
t.Logf("Expected failure; failure reason = %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|