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
This commit is contained in:
Nishant Das
2020-02-14 14:35:16 +08:00
committed by GitHub
parent ecfd7bdfa1
commit b263efefeb
4 changed files with 15 additions and 8 deletions

View File

@@ -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)

View File

@@ -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)))

View File

@@ -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)))
}

View File

@@ -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)