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.

94 lines
2.3 KiB

  1. package coordinator
  2. import (
  3. "time"
  4. "github.com/hermeznetwork/hermez-node/common"
  5. "github.com/hermeznetwork/hermez-node/log"
  6. )
  7. // ServerProofInterface is the interface to a ServerProof that calculates zk proofs
  8. type ServerProofInterface interface {
  9. CalculateProof(zkInputs *common.ZKInputs) error
  10. GetProof(stopCh chan bool) (*Proof, error)
  11. }
  12. // ServerProof contains the data related to a ServerProof
  13. type ServerProof struct {
  14. // TODO
  15. URL string
  16. Available bool
  17. }
  18. // NewServerProof creates a new ServerProof
  19. func NewServerProof(URL string) *ServerProof {
  20. return &ServerProof{URL: URL}
  21. }
  22. // CalculateProof sends the *common.ZKInputs to the ServerProof to compute the
  23. // Proof
  24. func (p *ServerProof) CalculateProof(zkInputs *common.ZKInputs) error {
  25. log.Error("TODO")
  26. return errTODO
  27. }
  28. // GetProof retreives the Proof from the ServerProof
  29. func (p *ServerProof) GetProof(stopCh chan bool) (*Proof, error) {
  30. log.Error("TODO")
  31. return nil, errTODO
  32. }
  33. // ServerProofMock is a mock ServerProof to be used in tests. It doesn't calculate anything
  34. type ServerProofMock struct {
  35. }
  36. // CalculateProof sends the *common.ZKInputs to the ServerProof to compute the
  37. // Proof
  38. func (p *ServerProofMock) CalculateProof(zkInputs *common.ZKInputs) error {
  39. return nil
  40. }
  41. // GetProof retreives the Proof from the ServerProof
  42. func (p *ServerProofMock) GetProof(stopCh chan bool) (*Proof, error) {
  43. // Simulate a delay
  44. select {
  45. case <-time.After(200 * time.Millisecond): //nolint:gomnd
  46. return &Proof{}, nil
  47. case <-stopCh:
  48. return nil, ErrStop
  49. }
  50. }
  51. // ServerProofPool contains the multiple ServerProof
  52. type ServerProofPool struct {
  53. pool chan ServerProofInterface
  54. }
  55. // NewServerProofPool creates a new pool of ServerProofs.
  56. func NewServerProofPool(maxServerProofs int) *ServerProofPool {
  57. return &ServerProofPool{
  58. pool: make(chan ServerProofInterface, maxServerProofs),
  59. }
  60. }
  61. // Add a ServerProof to the pool
  62. func (p *ServerProofPool) Add(serverProof ServerProofInterface) {
  63. p.pool <- serverProof
  64. }
  65. // Get returns the next available ServerProof
  66. func (p *ServerProofPool) Get(stopCh chan bool) (ServerProofInterface, error) {
  67. select {
  68. case <-stopCh:
  69. log.Info("ServerProofPool.Get stopped")
  70. return nil, ErrStop
  71. default:
  72. select {
  73. case <-stopCh:
  74. log.Info("ServerProofPool.Get stopped")
  75. return nil, ErrStop
  76. case serverProof := <-p.pool:
  77. return serverProof, nil
  78. }
  79. }
  80. }