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

feat: add hpc validator #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.13

require (
github.com/Shopify/sarama v1.27.0 // indirect
github.com/cbergoon/merkletree v0.2.0
github.com/gogo/protobuf v1.3.2
github.com/golang/mock v1.4.3
github.com/hashicorp/go-version v1.2.1 // indirect
Expand Down
92 changes: 92 additions & 0 deletions validator/validatorlib/hpc_validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package validatorlib

import (
"bytes"
"crypto/sha256"
"encoding/json"

"github.com/cbergoon/merkletree"
"github.com/meshplus/bitxhub-kit/crypto"
"github.com/meshplus/bitxhub-kit/crypto/asym"
"github.com/meshplus/bitxhub-kit/types"
)

type HpcProof struct {
Signatures [][]byte `json:"signatures"`
SpvHash []byte `json:"spv_hash"`
PreHashes [][]byte `json:"pre_hashes"`
PostHashes [][]byte `json:"post_hashes"`
RootHash []byte `json:"root_hash"`
}

type HpcValidator struct {
Total int `json:"total"`
Validators [][]byte `json:"validators"`
}

func ValidateHpc(proof []byte, validators []byte, payload []byte) (bool, error) {
hpcProof := &HpcProof{}
hpcValidator := &HpcValidator{}
if err := json.Unmarshal(proof, hpcProof); err != nil {
return false, err
}
if err := json.Unmarshal(validators, hpcValidator); err != nil {
return false, err
}
count := 0
for _, sig := range hpcProof.Signatures {
validSig := false
for index, validator := range hpcValidator.Validators {
address := types.NewAddress(validator)
ok, err := asym.Verify(crypto.Secp256k1, sig, hpcProof.SpvHash, *address)
if err != nil {
return false, err
}
if ok {
validSig = true
hpcValidator.Validators = append(hpcValidator.Validators[:index], hpcValidator.Validators[index+1:]...)
break
}
}
if validSig {
count += 1
if count == (hpcValidator.Total-1)/3+1 {
return VerifyMerkle(hpcProof, payload)
}
}
}
return false, nil
}

func VerifyMerkle(hpcProof *HpcProof, payload []byte) (bool, error) {
h := sha256.Sum256(payload)
if !bytes.Equal(h[:], hpcProof.SpvHash) {
return false, nil
}
hashes := make([]merkletree.Content, 0, len(hpcProof.PreHashes)+len(hpcProof.PostHashes)+1)
for _, hash := range hpcProof.PreHashes {
hashes = append(hashes, types.NewHash(hash))
}
hashes = append(hashes, types.NewHash(hpcProof.SpvHash))
for _, hash := range hpcProof.PostHashes {
hashes = append(hashes, types.NewHash(hash))
}
root, err := calcMerkleRoot(hashes)
if err != nil {
return false, err
}
return root.Equals(types.NewHash(hpcProof.RootHash))
}

func calcMerkleRoot(contents []merkletree.Content) (*types.Hash, error) {
if len(contents) == 0 {
return &types.Hash{}, nil
}

tree, err := merkletree.NewTree(contents)
if err != nil {
return nil, err
}

return types.NewHash(tree.MerkleRoot()), nil
}