Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use FastSSZ Everywhere Applicable #6135

Merged
merged 11 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions beacon-chain/core/blocks/spectest/attestation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/shared/params/spectest"
"github.com/prysmaticlabs/prysm/shared/testutil"
Expand All @@ -25,7 +24,7 @@ func runAttestationTest(t *testing.T, config string) {
t.Fatal(err)
}
att := &ethpb.Attestation{}
if err := ssz.Unmarshal(attestationFile, att); err != nil {
if err := att.UnmarshalSSZ(attestationFile); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/shared/params/spectest"
"github.com/prysmaticlabs/prysm/shared/testutil"
Expand All @@ -25,7 +24,7 @@ func runAttesterSlashingTest(t *testing.T, config string) {
t.Fatal(err)
}
attSlashing := &ethpb.AttesterSlashing{}
if err := ssz.Unmarshal(attSlashingFile, attSlashing); err != nil {
if err := attSlashing.UnmarshalSSZ(attSlashingFile); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}

Expand Down
7 changes: 3 additions & 4 deletions beacon-chain/core/blocks/spectest/block_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/bazelbuild/rules_go/go/tools/bazel"
"github.com/gogo/protobuf/proto"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
Expand All @@ -31,7 +30,7 @@ func runBlockHeaderTest(t *testing.T, config string) {
t.Fatal(err)
}
block := &ethpb.BeaconBlock{}
if err := ssz.Unmarshal(blockFile, block); err != nil {
if err := block.UnmarshalSSZ(blockFile); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}

Expand All @@ -40,7 +39,7 @@ func runBlockHeaderTest(t *testing.T, config string) {
t.Fatal(err)
}
preBeaconStateBase := &pb.BeaconState{}
if err := ssz.Unmarshal(preBeaconStateFile, preBeaconStateBase); err != nil {
if err := preBeaconStateBase.UnmarshalSSZ(preBeaconStateFile); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}
preBeaconState, err := stateTrie.InitializeFromProto(preBeaconStateBase)
Expand Down Expand Up @@ -70,7 +69,7 @@ func runBlockHeaderTest(t *testing.T, config string) {
}

postBeaconState := &pb.BeaconState{}
if err := ssz.Unmarshal(postBeaconStateFile, postBeaconState); err != nil {
if err := postBeaconState.UnmarshalSSZ(postBeaconStateFile); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}
if !proto.Equal(beaconState.CloneInnerState(), postBeaconState) {
Expand Down
7 changes: 3 additions & 4 deletions beacon-chain/core/blocks/spectest/block_processing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/bazelbuild/rules_go/go/tools/bazel"
"github.com/gogo/protobuf/proto"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
Expand Down Expand Up @@ -41,7 +40,7 @@ func runBlockProcessingTest(t *testing.T, config string) {
t.Fatal(err)
}
beaconStateBase := &pb.BeaconState{}
if err := ssz.Unmarshal(preBeaconStateFile, beaconStateBase); err != nil {
if err := beaconStateBase.UnmarshalSSZ(preBeaconStateFile); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}
beaconState, err := stateTrie.InitializeFromProto(beaconStateBase)
Expand All @@ -67,7 +66,7 @@ func runBlockProcessingTest(t *testing.T, config string) {
t.Fatal(err)
}
block := &ethpb.SignedBeaconBlock{}
if err := ssz.Unmarshal(blockFile, block); err != nil {
if err := block.UnmarshalSSZ(blockFile); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}
beaconState, transitionError = state.ExecuteStateTransition(context.Background(), beaconState, block)
Expand Down Expand Up @@ -96,7 +95,7 @@ func runBlockProcessingTest(t *testing.T, config string) {
}

postBeaconState := &pb.BeaconState{}
if err := ssz.Unmarshal(postBeaconStateFile, postBeaconState); err != nil {
if err := postBeaconState.UnmarshalSSZ(postBeaconStateFile); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}

Expand Down
3 changes: 1 addition & 2 deletions beacon-chain/core/blocks/spectest/deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/shared/params/spectest"
"github.com/prysmaticlabs/prysm/shared/testutil"
Expand All @@ -25,7 +24,7 @@ func runDepositTest(t *testing.T, config string) {
t.Fatal(err)
}
deposit := &ethpb.Deposit{}
if err := ssz.Unmarshal(depositFile, deposit); err != nil {
if err := deposit.UnmarshalSSZ(depositFile); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/shared/params/spectest"
"github.com/prysmaticlabs/prysm/shared/testutil"
Expand All @@ -25,7 +24,7 @@ func runProposerSlashingTest(t *testing.T, config string) {
t.Fatal(err)
}
proposerSlashing := &ethpb.ProposerSlashing{}
if err := ssz.Unmarshal(proposerSlashingFile, proposerSlashing); err != nil {
if err := proposerSlashing.UnmarshalSSZ(proposerSlashingFile); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}

Expand Down
3 changes: 1 addition & 2 deletions beacon-chain/core/blocks/spectest/voluntary_exit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/shared/params/spectest"
"github.com/prysmaticlabs/prysm/shared/testutil"
Expand All @@ -25,7 +24,7 @@ func runVoluntaryExitTest(t *testing.T, config string) {
t.Fatal(err)
}
voluntaryExit := &ethpb.SignedVoluntaryExit{}
if err := ssz.Unmarshal(exitFile, voluntaryExit); err != nil {
if err := voluntaryExit.UnmarshalSSZ(exitFile); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}

Expand Down
1 change: 0 additions & 1 deletion beacon-chain/core/state/interop/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ go_library(
"//beacon-chain/state:go_default_library",
"//shared/featureconfig:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
"@com_github_prysmaticlabs_go_ssz//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],
)
3 changes: 1 addition & 2 deletions beacon-chain/core/state/interop/write_block_to_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"path"

ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
)

Expand All @@ -23,7 +22,7 @@ func WriteBlockToDisk(block *ethpb.SignedBeaconBlock, failed bool) {
}
fp := path.Join(os.TempDir(), filename)
log.Warnf("Writing block to disk at %s", fp)
enc, err := ssz.Marshal(block)
enc, err := block.MarshalSSZ()
if err != nil {
log.WithError(err).Error("Failed to ssz encode block")
return
Expand Down
3 changes: 1 addition & 2 deletions beacon-chain/core/state/interop/write_state_to_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"path"

"github.com/prysmaticlabs/go-ssz"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
)
Expand All @@ -18,7 +17,7 @@ func WriteStateToDisk(state *stateTrie.BeaconState) {
}
fp := path.Join(os.TempDir(), fmt.Sprintf("beacon_state_%d.ssz", state.Slot()))
log.Warnf("Writing state to disk at %s", fp)
enc, err := ssz.Marshal(state.InnerStateUnsafe())
enc, err := state.InnerStateUnsafe().MarshalSSZ()
if err != nil {
log.WithError(err).Error("Failed to ssz encode state")
return
Expand Down
5 changes: 2 additions & 3 deletions beacon-chain/core/state/spectest/slot_processing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

"github.com/gogo/protobuf/proto"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
beaconstate "github.com/prysmaticlabs/prysm/beacon-chain/state"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
Expand All @@ -33,7 +32,7 @@ func runSlotProcessingTests(t *testing.T, config string) {
t.Fatal(err)
}
base := &pb.BeaconState{}
if err := ssz.Unmarshal(preBeaconStateFile, base); err != nil {
if err := base.UnmarshalSSZ(preBeaconStateFile); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}
beaconState, err := beaconstate.InitializeFromProto(base)
Expand All @@ -56,7 +55,7 @@ func runSlotProcessingTests(t *testing.T, config string) {
t.Fatal(err)
}
postBeaconState := &pb.BeaconState{}
if err := ssz.Unmarshal(postBeaconStateFile, postBeaconState); err != nil {
if err := postBeaconState.UnmarshalSSZ(postBeaconStateFile); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}
postState, err := state.ProcessSlots(context.Background(), beaconState, beaconState.Slot()+uint64(slotsCount))
Expand Down
1 change: 0 additions & 1 deletion beacon-chain/interop-cold-start/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ go_library(
"//shared/interop:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
"@com_github_prysmaticlabs_go_ssz//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],
)
3 changes: 1 addition & 2 deletions beacon-chain/interop-cold-start/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache"
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
Expand Down Expand Up @@ -72,7 +71,7 @@ func NewColdStartService(ctx context.Context, cfg *Config) *Service {
log.Fatalf("Could not read pre-loaded state: %v", err)
}
genesisState := &pb.BeaconState{}
if err := ssz.Unmarshal(data, genesisState); err != nil {
if err := genesisState.UnmarshalSSZ(data); err != nil {
log.Fatalf("Could not unmarshal pre-loaded state: %v", err)
}
genesisTrie, err := stateTrie.InitializeFromProto(genesisState)
Expand Down
2 changes: 0 additions & 2 deletions beacon-chain/p2p/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ go_library(
"@com_github_prometheus_client_golang//prometheus/promauto:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
"@com_github_prysmaticlabs_go_bitfield//:go_default_library",
"@com_github_prysmaticlabs_go_ssz//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@io_opencensus_go//trace:go_default_library",
],
Expand Down Expand Up @@ -134,7 +133,6 @@ go_test(
"@com_github_multiformats_go_multiaddr//:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
"@com_github_prysmaticlabs_go_bitfield//:go_default_library",
"@com_github_prysmaticlabs_go_ssz//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_sirupsen_logrus//hooks/test:go_default_library",
],
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/p2p/encoder/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ go_library(
],
deps = [
"//shared/params:go_default_library",
"@com_github_ferranbt_fastssz//:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library",
"@com_github_golang_snappy//:go_default_library",
"@com_github_pkg_errors//:go_default_library",
Expand Down
7 changes: 7 additions & 0 deletions beacon-chain/p2p/encoder/ssz.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"sync"

fastssz "github.com/ferranbt/fastssz"
"github.com/gogo/protobuf/proto"
"github.com/golang/snappy"
"github.com/pkg/errors"
Expand Down Expand Up @@ -36,6 +37,9 @@ type SszNetworkEncoder struct {
}

func (e SszNetworkEncoder) doEncode(msg interface{}) ([]byte, error) {
if v, ok := msg.(fastssz.Marshaler); ok {
return v.MarshalSSZ()
}
return ssz.Marshal(msg)
}

Expand Down Expand Up @@ -118,6 +122,9 @@ func (e SszNetworkEncoder) EncodeWithMaxLength(w io.Writer, msg interface{}, max
}

func (e SszNetworkEncoder) doDecode(b []byte, to interface{}) error {
if v, ok := to.(fastssz.Unmarshaler); ok {
return v.UnmarshalSSZ(b)
}
return ssz.Unmarshal(b, to)
}

Expand Down
5 changes: 2 additions & 3 deletions beacon-chain/p2p/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/pkg/errors"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/p2putils"
Expand Down Expand Up @@ -110,7 +109,7 @@ func addForkEntry(
NextForkVersion: nextForkVersion,
NextForkEpoch: nextForkEpoch,
}
enc, err := ssz.Marshal(enrForkID)
enc, err := enrForkID.MarshalSSZ()
if err != nil {
return nil, err
}
Expand All @@ -129,7 +128,7 @@ func retrieveForkEntry(record *enr.Record) (*pb.ENRForkID, error) {
return nil, err
}
forkEntry := &pb.ENRForkID{}
if err := ssz.Unmarshal(sszEncodedForkEntry, forkEntry); err != nil {
if err := forkEntry.UnmarshalSSZ(sszEncodedForkEntry); err != nil {
return nil, err
}
return forkEntry, nil
Expand Down
3 changes: 1 addition & 2 deletions beacon-chain/p2p/fork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/p2putils"
Expand Down Expand Up @@ -215,7 +214,7 @@ func TestDiscv5_AddRetrieveForkEntryENR(t *testing.T) {
NextForkVersion: nextForkVersion,
NextForkEpoch: nextForkEpoch,
}
enc, err := ssz.Marshal(enrForkID)
enc, err := enrForkID.MarshalSSZ()
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 0 additions & 2 deletions beacon-chain/rpc/debug/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ go_library(
"@com_github_ethereum_go_ethereum//log:go_default_library",
"@com_github_gogo_protobuf//types:go_default_library",
"@com_github_ipfs_go_log_v2//:go_default_library",
"@com_github_prysmaticlabs_go_ssz//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@org_golang_google_grpc//codes:go_default_library",
"@org_golang_google_grpc//status:go_default_library",
Expand All @@ -48,6 +47,5 @@ go_test(
"//shared/testutil:go_default_library",
"@com_github_gogo_protobuf//types:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
"@com_github_prysmaticlabs_go_ssz//:go_default_library",
],
)
3 changes: 1 addition & 2 deletions beacon-chain/rpc/debug/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package debug
import (
"context"

"github.com/prysmaticlabs/go-ssz"
pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"google.golang.org/grpc/codes"
Expand All @@ -23,7 +22,7 @@ func (ds *Server) GetBlock(
if signedBlock == nil {
return &pbrpc.SSZResponse{Encoded: make([]byte, 0)}, nil
}
encoded, err := ssz.Marshal(signedBlock)
encoded, err := signedBlock.MarshalSSZ()
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not marshal block: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions beacon-chain/rpc/debug/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
Expand Down Expand Up @@ -34,7 +33,7 @@ func TestServer_GetBlock(t *testing.T) {
if err != nil {
t.Fatal(err)
}
wanted, err := ssz.Marshal(b)
wanted, err := b.MarshalSSZ()
if err != nil {
t.Fatal(err)
}
Expand Down
Loading