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

Replace Attestation Type Primitive With Proto Generated Type #1149

Merged
merged 19 commits into from
Dec 22, 2018
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
10 changes: 5 additions & 5 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -940,19 +940,19 @@ go_repository(

go_repository(
name = "com_github_deckarep_golang_set",
commit = "699df6a3acf6867538e50931511e9dc403da108a",
tag = "v1.7.1",
importpath = "github.com/deckarep/golang-set",
)

go_repository(
name = "com_github_go_stack_stack",
commit = "2fee6af1a9795aafbe0253a0cfbdf668e1fb8a9a",
tag = "v1.8.0",
importpath = "github.com/go-stack/stack",
)

go_repository(
name = "com_github_rs_cors",
commit = "a3460e445dd310dbefee993fe449f2ff9c08ae71",
tag = "v1.6.0",
importpath = "github.com/rs/cors",
)

Expand All @@ -964,7 +964,7 @@ go_repository(

go_repository(
name = "in_gopkg_urfave_cli_v1",
tag = "v0.1.0",
tag = "v1.20.0",
importpath = "gopkg.in/urfave/cli.v1",
)

Expand All @@ -976,6 +976,6 @@ go_repository(

go_repository(
name = "com_github_edsrzf_mmap_go",
commit = "188cc3b666ba704534fa4f96e9e61f21f1e1ba7c",
tag = "v1.0.0",
importpath = "github.com/edsrzf/mmap-go",
)
6 changes: 4 additions & 2 deletions beacon-chain/attestation/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ go_library(
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/attestation",
visibility = ["//beacon-chain:__subpackages__"],
deps = [
"//beacon-chain/core/types:go_default_library",
"//beacon-chain/db:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/event:go_default_library",
"//shared/hashutil:go_default_library",
"@com_github_golang_protobuf//proto:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],
)
Expand All @@ -18,8 +20,8 @@ go_test(
srcs = ["service_test.go"],
embed = [":go_default_library"],
deps = [
"//beacon-chain/core/types:go_default_library",
"//beacon-chain/internal:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/testutil:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_sirupsen_logrus//hooks/test:go_default_library",
Expand Down
17 changes: 10 additions & 7 deletions beacon-chain/attestation/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ package attestation
import (
"context"

"github.com/prysmaticlabs/prysm/beacon-chain/core/types"
"github.com/golang/protobuf/proto"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/event"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/sirupsen/logrus"
)

Expand All @@ -19,9 +21,9 @@ type Service struct {
cancel context.CancelFunc
beaconDB *db.BeaconDB
broadcastFeed *event.Feed
broadcastChan chan *types.Attestation
broadcastChan chan *pb.Attestation
incomingFeed *event.Feed
incomingChan chan *types.Attestation
incomingChan chan *pb.Attestation
}

// Config options for the service.
Expand All @@ -40,9 +42,9 @@ func NewAttestationService(ctx context.Context, cfg *Config) *Service {
cancel: cancel,
beaconDB: cfg.BeaconDB,
broadcastFeed: new(event.Feed),
broadcastChan: make(chan *types.Attestation, cfg.BroadcastAttestationBuf),
broadcastChan: make(chan *pb.Attestation, cfg.BroadcastAttestationBuf),
incomingFeed: new(event.Feed),
incomingChan: make(chan *types.Attestation, cfg.ReceiveAttestationBuf),
incomingChan: make(chan *pb.Attestation, cfg.ReceiveAttestationBuf),
}
}

Expand Down Expand Up @@ -78,11 +80,12 @@ func (a *Service) aggregateAttestations() {
return
// Listen for a newly received incoming attestation from the sync service.
case attestation := <-a.incomingChan:
h, err := attestation.Hash()
enc, err := proto.Marshal(attestation)
if err != nil {
log.Errorf("Could not hash incoming attestation: %v", err)
log.Errorf("Could not marshal incoming attestation to bytes: %v", err)
continue
}
h := hashutil.Hash(enc)
if err := a.beaconDB.SaveAttestation(attestation); err != nil {
log.Errorf("Could not save attestation: %v", err)
continue
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/attestation/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"testing"

"github.com/prysmaticlabs/prysm/beacon-chain/core/types"
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/sirupsen/logrus"
logTest "github.com/sirupsen/logrus/hooks/test"
Expand All @@ -27,7 +27,7 @@ func TestIncomingAttestations(t *testing.T) {
<-exitRoutine
}()

service.incomingChan <- types.NewAttestation(nil)
service.incomingChan <- &pb.Attestation{}
service.cancel()
exitRoutine <- true

Expand Down
36 changes: 22 additions & 14 deletions beacon-chain/blockchain/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,17 @@ func TestRunningChainService(t *testing.T) {
StateRootHash32: stateRoot[:],
ParentRootHash32: parentHash[:],
CandidatePowReceiptRootHash32: []byte("a"),
Attestations: []*pb.AggregatedAttestation{{
Slot: attestationSlot,
AttesterBitfield: []byte{128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Shard: shard,
JustifiedBlockHash: parentHash[:],
}},
Body: &pb.BeaconBlockBody{
Attestations: []*pb.Attestation{{
ParticipationBitfield: []byte{128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Data: &pb.AttestationData{
Slot: attestationSlot,
Shard: shard,
JustifiedBlockRootHash32: parentHash[:],
},
}},
},
}

if err := SetSlotInState(chainService, currentSlot); err != nil {
Expand Down Expand Up @@ -431,13 +435,17 @@ func TestIsBlockReadyForProcessing(t *testing.T) {
StateRootHash32: stateRoot[:],
ParentRootHash32: parentHash[:],
CandidatePowReceiptRootHash32: []byte("a"),
Attestations: []*pb.AggregatedAttestation{{
Slot: attestationSlot,
AttesterBitfield: []byte{128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Shard: shard,
JustifiedBlockHash: parentHash[:],
}},
Body: &pb.BeaconBlockBody{
Attestations: []*pb.Attestation{{
ParticipationBitfield: []byte{128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Data: &pb.AttestationData{
Slot: attestationSlot,
Shard: shard,
JustifiedBlockRootHash32: parentHash[:],
},
}},
},
}

chainService.enablePOWChain = true
Expand Down
19 changes: 19 additions & 0 deletions beacon-chain/core/attestations/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["attestation.go"],
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/core/attestations",
visibility = ["//beacon-chain:__subpackages__"],
deps = [
"//proto/beacon/p2p/v1:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/params:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = ["attestation_test.go"],
embed = [":go_default_library"],
)
69 changes: 69 additions & 0 deletions beacon-chain/core/attestations/attestation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package attestations

import (
"encoding/binary"

pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
)

// CreateAttestationMsg hashes parentHashes + shardID + slotNumber +
// shardBlockHash + justifiedSlot into a message to use for verifying
// with aggregated public key and signature.
func CreateAttestationMsg(
blockHash []byte,
slot uint64,
shardID uint64,
justifiedSlot uint64,
forkVersion uint64,
) [32]byte {
msg := make([]byte, binary.MaxVarintLen64)
binary.BigEndian.PutUint64(msg, forkVersion)
binary.PutUvarint(msg, slot%params.BeaconConfig().CycleLength)
binary.PutUvarint(msg, shardID)
msg = append(msg, blockHash...)
binary.PutUvarint(msg, justifiedSlot)
return hashutil.Hash(msg)
}

// Key generates the blake2b hash of the following attestation fields:
// slotNumber + shardID + blockHash + obliqueParentHash
// This is used for attestation table look up in localDB.
func Key(att *pb.AttestationData) [32]byte {
key := make([]byte, binary.MaxVarintLen64)
binary.PutUvarint(key, att.GetSlot())
binary.PutUvarint(key, att.GetShard())
key = append(key, att.GetShardBlockRootHash32()...)
return hashutil.Hash(key)
}

// VerifyProposerAttestation verifies the proposer's attestation of the block.
// Proposers broadcast the attestation along with the block to its peers.
func VerifyProposerAttestation(att *pb.AttestationData, pubKey [32]byte, proposerShardID uint64) error {
// Verify the attestation attached with block response.
// Get proposer index and shardID.
attestationMsg := CreateAttestationMsg(
att.GetShardBlockRootHash32(),
att.GetSlot(),
proposerShardID,
att.GetJustifiedSlot(),
params.BeaconConfig().InitialForkVersion,
)
_ = attestationMsg
_ = pubKey
// TODO(#258): use attestationMsg to verify against signature
// and public key. Return error if incorrect.
return nil
}

// ContainsValidator checks if the validator is included in the attestation.
// TODO(#569): Modify method to accept a single index rather than a bitfield.
func ContainsValidator(attesterBitfield []byte, bitfield []byte) bool {
for i := 0; i < len(bitfield); i++ {
if bitfield[i]&attesterBitfield[i] != 0 {
return true
}
}
return false
}
15 changes: 15 additions & 0 deletions beacon-chain/core/attestations/attestation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package attestations

import (
"testing"
)

func TestContainsValidator(t *testing.T) {
if !ContainsValidator([]byte{7}, []byte{4}) {
t.Error("Attestation should contain validator")
}

if ContainsValidator([]byte{7}, []byte{8}) {
t.Error("Attestation should not contain validator")
}
}
2 changes: 1 addition & 1 deletion beacon-chain/core/blocks/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestGenesisBlock(t *testing.T) {
t.Error("genesis block missing ParentHash field")
}

if b1.GetAttestations() != nil {
if b1.GetBody().GetAttestations() != nil {
t.Errorf("genesis block should have 0 attestations")
}

Expand Down
11 changes: 2 additions & 9 deletions beacon-chain/core/types/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = [
"attestation.go",
"state.go",
],
srcs = ["state.go"],
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/core/types",
visibility = ["//beacon-chain:__subpackages__"],
deps = [
Expand All @@ -16,16 +13,12 @@ go_library(
"//shared/params:go_default_library",
"@com_github_ethereum_go_ethereum//common:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library",
"@com_github_golang_protobuf//proto:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = [
"attestation_test.go",
"state_test.go",
],
srcs = ["state_test.go"],
embed = [":go_default_library"],
deps = [
"//proto/beacon/p2p/v1:go_default_library",
Expand Down
Loading