Replace Deposit Hash with HashTreeRoot (#3102)

* change to hashTreeRoot

* remove function and run gaz

* fix panic
This commit is contained in:
Nishant Das
2019-07-29 19:13:24 +05:30
committed by Raul Jordan
parent 3fe0933936
commit f72f7677b3
16 changed files with 21 additions and 71 deletions

View File

@@ -945,7 +945,7 @@ func ProcessDeposit(beaconState *pb.BeaconState, deposit *ethpb.Deposit, valInde
func verifyDeposit(beaconState *pb.BeaconState, deposit *ethpb.Deposit) error {
// Verify Merkle proof of deposit and deposit trie root.
receiptRoot := beaconState.Eth1Data.DepositRoot
leaf, err := hashutil.DepositHash(deposit.Data)
leaf, err := ssz.HashTreeRoot(deposit.Data)
if err != nil {
return fmt.Errorf("could not tree hash deposit data: %v", err)
}

View File

@@ -1464,7 +1464,7 @@ func TestProcessDeposits_RepeatedDeposit_IncreasesValidatorBalance(t *testing.T)
}
sig := sk.Sign(sr[:], 3)
deposit.Data.Signature = sig.Marshal()
leaf, err := hashutil.DepositHash(deposit.Data)
leaf, err := ssz.HashTreeRoot(deposit.Data)
if err != nil {
t.Fatal(err)
}
@@ -1612,6 +1612,7 @@ func TestProcessDeposit_SkipsDepositWithUncompressedSignature(t *testing.T) {
a, _ := blsintern.DecompressG2(bytesutil.ToBytes96(dep[0].Data.Signature))
uncompressedSignature := a.SerializeBytes()
dep[0].Data.Signature = uncompressedSignature[:]
ssz.ToggleCache(false)
eth1Data := testutil.GenerateEth1Data(t, dep)
testutil.ResetCache() // Can't have an uncompressed signature in the cache.

View File

@@ -12,7 +12,6 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/mathutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/trieutil"
@@ -163,7 +162,7 @@ func GenesisBeaconState(deposits []*ethpb.Deposit, genesisTime uint64, eth1Data
validatorMap := make(map[[32]byte]int)
leaves := [][]byte{}
for _, deposit := range deposits {
hash, err := hashutil.DepositHash(deposit.Data)
hash, err := ssz.HashTreeRoot(deposit.Data)
if err != nil {
return nil, err
}

View File

@@ -55,7 +55,6 @@ go_test(
"//shared/bls:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/event:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/params:go_default_library",
"//shared/testutil:go_default_library",
"//shared/trieutil:go_default_library",

View File

@@ -7,7 +7,6 @@ import (
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/trieutil"
)
@@ -63,7 +62,7 @@ func (w *Web3Service) processDeposit(
func verifyDeposit(eth1Data *ethpb.Eth1Data, deposit *ethpb.Deposit) error {
// Verify Merkle proof of deposit and deposit trie root.
receiptRoot := eth1Data.DepositRoot
leaf, err := hashutil.DepositHash(deposit.Data)
leaf, err := ssz.HashTreeRoot(deposit.Data)
if err != nil {
return fmt.Errorf("could not tree hash deposit data: %v", err)
}

View File

@@ -12,7 +12,6 @@ import (
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/trieutil"
@@ -35,7 +34,7 @@ func TestProcessDeposit_OK(t *testing.T) {
deposits, _ := testutil.SetupInitialDeposits(t, 1)
leaf, err := hashutil.DepositHash(deposits[0].Data)
leaf, err := ssz.HashTreeRoot(deposits[0].Data)
if err != nil {
t.Fatalf("Could not hash deposit %v", err)
}
@@ -76,7 +75,7 @@ func TestProcessDeposit_InvalidMerkleBranch(t *testing.T) {
deposits, _ := testutil.SetupInitialDeposits(t, 1)
leaf, err := hashutil.DepositHash(deposits[0].Data)
leaf, err := ssz.HashTreeRoot(deposits[0].Data)
if err != nil {
t.Fatalf("Could not hash deposit %v", err)
}
@@ -124,7 +123,7 @@ func TestProcessDeposit_InvalidPublicKey(t *testing.T) {
deposits, _ := testutil.SetupInitialDeposits(t, 1)
deposits[0].Data.PublicKey = []byte("junk")
leaf, err := hashutil.DepositHash(deposits[0].Data)
leaf, err := ssz.HashTreeRoot(deposits[0].Data)
if err != nil {
t.Fatalf("Could not hash deposit %v", err)
}
@@ -170,7 +169,7 @@ func TestProcessDeposit_InvalidSignature(t *testing.T) {
copy(fakeSig[:], []byte{'F', 'A', 'K', 'E'})
deposits[0].Data.Signature = fakeSig[:]
leaf, err := hashutil.DepositHash(deposits[0].Data)
leaf, err := ssz.HashTreeRoot(deposits[0].Data)
if err != nil {
t.Fatalf("Could not hash deposit %v", err)
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
gethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/prysmaticlabs/go-ssz"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
contracts "github.com/prysmaticlabs/prysm/contracts/deposit-contract"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
@@ -93,7 +94,7 @@ func (w *Web3Service) ProcessDepositLog(depositLog gethTypes.Log) {
WithdrawalCredentials: withdrawalCredentials,
}
depositHash, err := hashutil.DepositHash(depositData)
depositHash, err := ssz.HashTreeRoot(depositData)
if err != nil {
log.Errorf("Unable to determine hashed value of deposit %v", err)
return
@@ -276,7 +277,7 @@ func (w *Web3Service) requestBatchedLogs() error {
func (w *Web3Service) ChainStartDepositHashes() ([][]byte, error) {
hashes := make([][]byte, len(w.chainStartDeposits))
for i, dep := range w.chainStartDeposits {
hash, err := hashutil.DepositHash(dep.Data)
hash, err := ssz.HashTreeRoot(dep.Data)
if err != nil {
return nil, err
}

View File

@@ -78,7 +78,6 @@ go_test(
"//shared/bls:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/event:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/params:go_default_library",
"//shared/testutil:go_default_library",
"//shared/trieutil:go_default_library",

View File

@@ -15,7 +15,6 @@ import (
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/trieutil"
"github.com/sirupsen/logrus"
@@ -292,7 +291,7 @@ func (ps *ProposerServer) deposits(ctx context.Context, currentVote *ethpb.Eth1D
}
depositData := [][]byte{}
for _, dep := range upToEth1DataDeposits {
depHash, err := hashutil.DepositHash(dep.Data)
depHash, err := ssz.HashTreeRoot(dep.Data)
if err != nil {
return nil, fmt.Errorf("coulf not hash deposit data %v", err)
}

View File

@@ -19,7 +19,6 @@ import (
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/trieutil"
@@ -543,7 +542,7 @@ func TestPendingDeposits_OutsideEth1FollowWindow(t *testing.T) {
t.Fatalf("could not setup deposit trie: %v", err)
}
for _, dp := range append(readyDeposits, recentDeposits...) {
depositHash, err := hashutil.DepositHash(dp.Deposit.Data)
depositHash, err := ssz.HashTreeRoot(dp.Deposit.Data)
if err != nil {
t.Fatalf("Unable to determine hashed value of deposit %v", err)
}
@@ -650,7 +649,7 @@ func TestPendingDeposits_CantReturnBelowStateEth1DepositIndex(t *testing.T) {
t.Fatalf("could not setup deposit trie: %v", err)
}
for _, dp := range append(readyDeposits, recentDeposits...) {
depositHash, err := hashutil.DepositHash(dp.Deposit.Data)
depositHash, err := ssz.HashTreeRoot(dp.Deposit.Data)
if err != nil {
t.Fatalf("Unable to determine hashed value of deposit %v", err)
}
@@ -750,7 +749,7 @@ func TestPendingDeposits_CantReturnMoreThanMax(t *testing.T) {
t.Fatalf("could not setup deposit trie: %v", err)
}
for _, dp := range append(readyDeposits, recentDeposits...) {
depositHash, err := hashutil.DepositHash(dp.Deposit.Data)
depositHash, err := ssz.HashTreeRoot(dp.Deposit.Data)
if err != nil {
t.Fatalf("Unable to determine hashed value of deposit %v", err)
}

View File

@@ -3,15 +3,12 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = [
"deposit_hash.go",
"hash.go",
"merkleRoot.go",
],
importpath = "github.com/prysmaticlabs/prysm/shared/hashutil",
visibility = ["//visibility:public"],
deps = [
"//proto/eth/v1alpha1:go_default_library",
"//shared/bytesutil:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library",
"@org_golang_x_crypto//sha3:go_default_library",
],

View File

@@ -1,42 +0,0 @@
package hashutil
import (
"reflect"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
)
// DepositHash returns the sha256 of the information contained in the deposit
// data as specified in the deposit contract.
// Spec:
// pubkey_root: bytes32 = sha256(concat(pubkey, slice(zero_bytes_32, start=0, len=16)))
// signature_root: bytes32 = sha256(concat(
// sha256(slice(signature, start=0, len=64)),
// sha256(concat(slice(signature, start=64, len=32), zero_bytes_32))
// ))
// value: bytes32 = sha256(concat(
// sha256(concat(pubkey_root, withdrawal_credentials)),
// sha256(concat(
// amount,
// slice(zero_bytes_32, start=0, len=24),
// signature_root,
// ))
// ))
func DepositHash(dep *ethpb.Deposit_Data) ([32]byte, error) {
if dep == nil || reflect.ValueOf(dep).IsNil() {
return [32]byte{}, ErrNilProto
}
var zeroBytes [32]byte
pubkeyRoot := Hash(append(dep.PublicKey, zeroBytes[:16]...))
sigHash := Hash(dep.Signature[:64])
sigZeroBytesHash := Hash(append(dep.Signature[64:96], zeroBytes[:]...))
sigRoot := Hash(append(sigHash[:], sigZeroBytesHash[:]...))
pubRootWCredsHash := Hash(append(pubkeyRoot[:], dep.WithdrawalCredentials...))
amountSigHash := Hash(append(append(bytesutil.Bytes8(dep.Amount), zeroBytes[:24]...), sigRoot[:]...))
return Hash(append(pubRootWCredsHash[:], amountSigHash[:]...)), nil
}

View File

@@ -18,7 +18,6 @@ go_library(
"//proto/beacon/p2p/v1:go_default_library",
"//proto/eth/v1alpha1:go_default_library",
"//shared/bls:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/params:go_default_library",
"//shared/trieutil:go_default_library",
"@com_github_ghodss_yaml//:go_default_library",

View File

@@ -12,7 +12,6 @@ import (
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/trieutil"
)
@@ -75,7 +74,7 @@ func SetupInitialDeposits(t testing.TB, numDeposits uint64) ([]*ethpb.Deposit, [
func GenerateDepositProof(t testing.TB, deposits []*ethpb.Deposit) ([]*ethpb.Deposit, [32]byte) {
encodedDeposits := make([][]byte, len(deposits))
for i := 0; i < len(encodedDeposits); i++ {
hashedDeposit, err := hashutil.DepositHash(deposits[i].Data)
hashedDeposit, err := ssz.HashTreeRoot(deposits[i].Data)
if err != nil {
t.Fatalf("could not tree hash deposit data: %v", err)
}

View File

@@ -25,5 +25,6 @@ go_test(
"//shared/hashutil:go_default_library",
"//shared/params:go_default_library",
"@com_github_ethereum_go_ethereum//accounts/abi/bind:go_default_library",
"@com_github_prysmaticlabs_go_ssz//:go_default_library",
],
)

View File

@@ -6,6 +6,7 @@ import (
"testing"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/prysmaticlabs/go-ssz"
contracts "github.com/prysmaticlabs/prysm/contracts/deposit-contract"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/hashutil"
@@ -214,7 +215,7 @@ func TestDepositTrieRoot_OK(t *testing.T) {
}
testAcc.Backend.Commit()
item, err := hashutil.DepositHash(data)
item, err := ssz.HashTreeRoot(data)
if err != nil {
t.Fatal(err)
}
@@ -283,7 +284,7 @@ func TestDepositTrieRoot_Fail(t *testing.T) {
copy(data.Signature, []byte(strconv.Itoa(i+10)))
testAcc.Backend.Commit()
item, err := hashutil.DepositHash(data)
item, err := ssz.HashTreeRoot(data)
if err != nil {
t.Fatal(err)
}