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.

106 lines
2.3 KiB

10 years ago
10 years ago
10 years ago
  1. package blocks
  2. import (
  3. "fmt"
  4. "io"
  5. . "github.com/tendermint/tendermint/binary"
  6. )
  7. type Signable interface {
  8. Binary
  9. GetSignature() Signature
  10. SetSignature(Signature)
  11. }
  12. //-----------------------------------------------------------------------------
  13. type Signature struct {
  14. SignerId uint64
  15. Bytes []byte
  16. }
  17. func ReadSignature(r io.Reader, n *int64, err *error) Signature {
  18. return Signature{
  19. SignerId: ReadUInt64(r, n, err),
  20. Bytes: ReadByteSlice(r, n, err),
  21. }
  22. }
  23. func (sig Signature) IsZero() bool {
  24. return len(sig.Bytes) == 0
  25. }
  26. func (sig Signature) WriteTo(w io.Writer) (n int64, err error) {
  27. WriteUInt64(w, sig.SignerId, &n, &err)
  28. WriteByteSlice(w, sig.Bytes, &n, &err)
  29. return
  30. }
  31. func (sig Signature) String() string {
  32. return fmt.Sprintf("Signature{%v:%X}", sig.SignerId, sig.Bytes)
  33. }
  34. //-------------------------------------
  35. func ReadSignatures(r io.Reader, n *int64, err *error) (sigs []Signature) {
  36. length := ReadUInt32(r, n, err)
  37. for i := uint32(0); i < length; i++ {
  38. sigs = append(sigs, ReadSignature(r, n, err))
  39. }
  40. return
  41. }
  42. func WriteSignatures(w io.Writer, sigs []Signature, n *int64, err *error) {
  43. WriteUInt32(w, uint32(len(sigs)), n, err)
  44. for _, sig := range sigs {
  45. WriteBinary(w, sig, n, err)
  46. if *err != nil {
  47. return
  48. }
  49. }
  50. }
  51. //-----------------------------------------------------------------------------
  52. type RoundSignature struct {
  53. Round uint16
  54. Signature
  55. }
  56. func ReadRoundSignature(r io.Reader, n *int64, err *error) RoundSignature {
  57. return RoundSignature{
  58. ReadUInt16(r, n, err),
  59. ReadSignature(r, n, err),
  60. }
  61. }
  62. func (rsig RoundSignature) WriteTo(w io.Writer) (n int64, err error) {
  63. WriteUInt16(w, rsig.Round, &n, &err)
  64. WriteBinary(w, rsig.Signature, &n, &err)
  65. return
  66. }
  67. func (rsig RoundSignature) IsZero() bool {
  68. return rsig.Round == 0 && rsig.SignerId == 0 && len(rsig.Bytes) == 0
  69. }
  70. //-------------------------------------
  71. func ReadRoundSignatures(r io.Reader, n *int64, err *error) (rsigs []RoundSignature) {
  72. length := ReadUInt32(r, n, err)
  73. for i := uint32(0); i < length; i++ {
  74. rsigs = append(rsigs, ReadRoundSignature(r, n, err))
  75. }
  76. return
  77. }
  78. func WriteRoundSignatures(w io.Writer, rsigs []RoundSignature, n *int64, err *error) {
  79. WriteUInt32(w, uint32(len(rsigs)), n, err)
  80. for _, rsig := range rsigs {
  81. WriteBinary(w, rsig, n, err)
  82. if *err != nil {
  83. return
  84. }
  85. }
  86. }