Implement Process Block Receipt Root Records (#1105)

* process receipt root records

* complete process pow roots

* gazelle

* remote print
This commit is contained in:
Raul Jordan 2018-12-15 17:53:02 -06:00 committed by GitHub
parent 1828537dd5
commit 58549b95e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 0 deletions

View File

@ -13,6 +13,37 @@ import (
"github.com/prysmaticlabs/prysm/shared/slices"
)
// ProcessPOWReceiptRoots processes the proof-of-work chain's receipts
// contained in a beacon block and appends them as candidate receipt roots
// in the beacon state.
//
// Official spec definition for processing pow receipt roots:
// If block.candidate_pow_receipt_root is x.candidate_pow_receipt_root
// for some x in state.candidate_pow_receipt_roots, set x.vote_count += 1.
// Otherwise, append to state.candidate_pow_receipt_roots a
// new CandidatePoWReceiptRootRecord(
// candidate_pow_receipt_root=block.candidate_pow_receipt_root,
// vote_count=1
// )
func ProcessPOWReceiptRoots(
beaconState *types.BeaconState,
block *pb.BeaconBlock,
) []*pb.CandidatePoWReceiptRootRecord {
var newCandidateReceiptRoots []*pb.CandidatePoWReceiptRootRecord
currentCandidateReceiptRoots := beaconState.CandidatePowReceiptRoots()
for idx, root := range currentCandidateReceiptRoots {
if bytes.Equal(block.GetCandidatePowReceiptRootHash32(), root.GetCandidatePowReceiptRootHash32()) {
currentCandidateReceiptRoots[idx].Votes++
} else {
newCandidateReceiptRoots = append(newCandidateReceiptRoots, &pb.CandidatePoWReceiptRootRecord{
CandidatePowReceiptRootHash32: block.GetCandidatePowReceiptRootHash32(),
Votes: 1,
})
}
}
return append(currentCandidateReceiptRoots, newCandidateReceiptRoots...)
}
// ProcessProposerSlashings is one of the operations performed
// on each processed beacon block to penalize proposers based on
// slashing conditions if any slashable events occurred.

View File

@ -1,6 +1,7 @@
package blocks
import (
"bytes"
"fmt"
"reflect"
"strings"
@ -11,6 +12,55 @@ import (
"github.com/prysmaticlabs/prysm/shared/params"
)
func TestProcessPOWReceiptRoots_SameRootHash(t *testing.T) {
beaconState := types.NewBeaconState(&pb.BeaconState{
CandidatePowReceiptRoots: []*pb.CandidatePoWReceiptRootRecord{
{
CandidatePowReceiptRootHash32: []byte{1},
Votes: 5,
},
},
})
block := &pb.BeaconBlock{
CandidatePowReceiptRootHash32: []byte{1},
}
newReceiptRoots := ProcessPOWReceiptRoots(beaconState, block)
if newReceiptRoots[0].Votes != 6 {
t.Errorf("expected votes to increase from 5 to 6, received %v", newReceiptRoots[0].Votes)
}
}
func TestProcessPOWReceiptRoots_NewCandidateRecord(t *testing.T) {
beaconState := types.NewBeaconState(&pb.BeaconState{
CandidatePowReceiptRoots: []*pb.CandidatePoWReceiptRootRecord{
{
CandidatePowReceiptRootHash32: []byte{0},
Votes: 5,
},
},
})
block := &pb.BeaconBlock{
CandidatePowReceiptRootHash32: []byte{1},
}
newReceiptRoots := ProcessPOWReceiptRoots(beaconState, block)
if len(newReceiptRoots) == 1 {
t.Error("expected new receipt roots to have length > 1")
}
if newReceiptRoots[1].Votes != 1 {
t.Errorf(
"expected new receipt roots to have a new element with votes = 1, received votes = %d",
newReceiptRoots[1].Votes,
)
}
if !bytes.Equal(newReceiptRoots[1].CandidatePowReceiptRootHash32, []byte{1}) {
t.Errorf(
"expected new receipt roots to have a new element with root = %#x, received root = %#x",
[]byte{1},
newReceiptRoots[1].CandidatePowReceiptRootHash32,
)
}
}
func TestProcessProposerSlashings_ThresholdReached(t *testing.T) {
slashings := make([]*pb.ProposerSlashing, params.BeaconConfig().MaxProposerSlashings+1)
registry := []*pb.ValidatorRecord{}