You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.7 KiB

  1. package main
  2. import acm "github.com/tendermint/tendermint/account"
  3. type Validator struct {
  4. VotingPower uint64
  5. PubKey acm.PubKey
  6. }
  7. func validate(signBytes []byte, validators []Validator, signatures []acm.Signature) bool {
  8. var signedPower uint64
  9. var totalPower uint64
  10. for i, val := range validators {
  11. if val.PubKey.VerifyBytes(signBytes, signatures[i]) {
  12. signedPower += val.VotingPower
  13. totalPower += val.VotingPower
  14. } else {
  15. totalPower += val.VotingPower
  16. }
  17. }
  18. return signedPower > totalPower*2/3
  19. }
  20. /*
  21. NOTE: Not used, just here in case we want it later.
  22. func ValidateHandler(handler http.Handler, validators []Validator) http.Handler {
  23. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  24. sigStrs := r.Header[http.CanonicalHeaderKey("signatures")]
  25. log.Debug("Woot", "sigstrs", sigStrs, "len", len(sigStrs))
  26. // from https://medium.com/@xoen/golang-read-from-an-io-readwriter-without-loosing-its-content-2c6911805361
  27. // Read the content
  28. var bodyBytes []byte
  29. if r.Body != nil {
  30. bodyBytes, _ = ioutil.ReadAll(r.Body)
  31. }
  32. // Restore the io.ReadCloser to its original state
  33. r.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
  34. // Get body string
  35. bodyString := string(bodyBytes)
  36. // Also read the path+"?"+query
  37. pathQuery := fmt.Sprintf("%v?%v", r.URL.Path, r.URL.RawQuery)
  38. // Concatenate into tuple of two strings.
  39. tuple := struct {
  40. Body string
  41. Path string
  42. }{bodyString, pathQuery}
  43. // Get sign bytes
  44. signBytes := binary.BinaryBytes(tuple)
  45. // Validate the sign bytes.
  46. //if validate(signBytes, validators,
  47. log.Debug("Should sign", "bytes", signBytes)
  48. // If validation fails
  49. // XXX
  50. // If validation passes
  51. handler.ServeHTTP(w, r)
  52. })
  53. }
  54. */