From b263efefebeb20b06ca790a6159e00e6bb21a73e Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Fri, 14 Feb 2020 14:35:16 +0800 Subject: [PATCH] Copy Checkpoint Root Properly (#4862) * change to custom hashing * Merge branch 'master' into minorOpt * goimports * Merge branch 'minorOpt' of https://github.com/prysmaticlabs/geth-sharding into minorOpt * gaz * pad to 32 bytes * one more case * Merge branch 'master' of https://github.com/prysmaticlabs/geth-sharding into minorOpt * one more case * more cases * some more cases * do it better --- shared/stateutil/attestations.go | 3 ++- shared/stateutil/blocks.go | 6 ++++-- shared/stateutil/state_root.go | 3 ++- shared/stateutil/validators.go | 11 +++++++---- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/shared/stateutil/attestations.go b/shared/stateutil/attestations.go index 33b7326248..59c437335f 100644 --- a/shared/stateutil/attestations.go +++ b/shared/stateutil/attestations.go @@ -75,7 +75,8 @@ func attestationDataRoot(data *ethpb.AttestationData) ([32]byte, error) { fieldRoots[1] = interRoot[:] // Beacon block root. - fieldRoots[2] = data.BeaconBlockRoot + blockRoot := bytesutil.ToBytes32(data.BeaconBlockRoot) + fieldRoots[2] = blockRoot[:] // Source sourceRoot, err := CheckpointRoot(data.Source) diff --git a/shared/stateutil/blocks.go b/shared/stateutil/blocks.go index f0eadccfd9..3870b73acc 100644 --- a/shared/stateutil/blocks.go +++ b/shared/stateutil/blocks.go @@ -40,14 +40,16 @@ func Eth1Root(eth1Data *ethpb.Eth1Data) ([32]byte, error) { } if eth1Data != nil { if len(eth1Data.DepositRoot) > 0 { - fieldRoots[0] = eth1Data.DepositRoot + depRoot := bytesutil.ToBytes32(eth1Data.DepositRoot) + fieldRoots[0] = depRoot[:] } eth1DataCountBuf := make([]byte, 8) binary.LittleEndian.PutUint64(eth1DataCountBuf, eth1Data.DepositCount) eth1CountRoot := bytesutil.ToBytes32(eth1DataCountBuf) fieldRoots[1] = eth1CountRoot[:] if len(eth1Data.BlockHash) > 0 { - fieldRoots[2] = eth1Data.BlockHash + blockHash := bytesutil.ToBytes32(eth1Data.BlockHash) + fieldRoots[2] = blockHash[:] } } return bitwiseMerkleize(fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots))) diff --git a/shared/stateutil/state_root.go b/shared/stateutil/state_root.go index b385dac683..124d8a717e 100644 --- a/shared/stateutil/state_root.go +++ b/shared/stateutil/state_root.go @@ -250,7 +250,8 @@ func CheckpointRoot(checkpoint *ethpb.Checkpoint) ([32]byte, error) { binary.LittleEndian.PutUint64(epochBuf, checkpoint.Epoch) epochRoot := bytesutil.ToBytes32(epochBuf) fieldRoots[0] = epochRoot[:] - fieldRoots[1] = checkpoint.Root + ckpRoot := bytesutil.ToBytes32(checkpoint.Root) + fieldRoots[1] = ckpRoot[:] } return bitwiseMerkleize(fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots))) } diff --git a/shared/stateutil/validators.go b/shared/stateutil/validators.go index 3b0d5b4147..bd13182b76 100644 --- a/shared/stateutil/validators.go +++ b/shared/stateutil/validators.go @@ -6,6 +6,7 @@ import ( "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" + "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/hashutil" "github.com/prysmaticlabs/prysm/shared/params" @@ -104,8 +105,10 @@ func (h *stateRootHasher) validatorRoot(validator *ethpb.Validator) ([32]byte, e fieldRoots := make([][32]byte, 2, 8) if validator != nil { - copy(enc[0:48], validator.PublicKey) - copy(enc[48:80], validator.WithdrawalCredentials) + pubkey := bytesutil.ToBytes48(validator.PublicKey) + copy(enc[0:48], pubkey[:]) + withdrawCreds := bytesutil.ToBytes32(validator.WithdrawalCredentials) + copy(enc[48:80], withdrawCreds[:]) effectiveBalanceBuf := [32]byte{} binary.LittleEndian.PutUint64(effectiveBalanceBuf[:8], validator.EffectiveBalance) copy(enc[80:88], effectiveBalanceBuf[:8]) @@ -138,7 +141,7 @@ func (h *stateRootHasher) validatorRoot(validator *ethpb.Validator) ([32]byte, e } // Public key. - pubKeyChunks, err := pack([][]byte{validator.PublicKey}) + pubKeyChunks, err := pack([][]byte{pubkey[:]}) if err != nil { return [32]byte{}, err } @@ -149,7 +152,7 @@ func (h *stateRootHasher) validatorRoot(validator *ethpb.Validator) ([32]byte, e fieldRoots[0] = pubKeyRoot // Withdrawal credentials. - copy(fieldRoots[1][:], validator.WithdrawalCredentials) + copy(fieldRoots[1][:], withdrawCreds[:]) // Effective balance. fieldRoots = append(fieldRoots, effectiveBalanceBuf)