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.

56 lines
1.7 KiB

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