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.

92 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. return errTODO
  26. }
  27. // GetProof retreives the Proof from the ServerProof
  28. func (p *ServerProof) GetProof(stopCh chan bool) (*Proof, error) {
  29. return nil, errTODO
  30. }
  31. // ServerProofMock is a mock ServerProof to be used in tests. It doesn't calculate anything
  32. type ServerProofMock struct {
  33. }
  34. // CalculateProof sends the *common.ZKInputs to the ServerProof to compute the
  35. // Proof
  36. func (p *ServerProofMock) CalculateProof(zkInputs *common.ZKInputs) error {
  37. return nil
  38. }
  39. // GetProof retreives the Proof from the ServerProof
  40. func (p *ServerProofMock) GetProof(stopCh chan bool) (*Proof, error) {
  41. // Simulate a delay
  42. select {
  43. case <-time.After(200 * time.Millisecond): //nolint:gomnd
  44. return &Proof{}, nil
  45. case <-stopCh:
  46. return nil, ErrStop
  47. }
  48. }
  49. // ServerProofPool contains the multiple ServerProof
  50. type ServerProofPool struct {
  51. pool chan ServerProofInterface
  52. }
  53. // NewServerProofPool creates a new pool of ServerProofs.
  54. func NewServerProofPool(maxServerProofs int) *ServerProofPool {
  55. return &ServerProofPool{
  56. pool: make(chan ServerProofInterface, maxServerProofs),
  57. }
  58. }
  59. // Add a ServerProof to the pool
  60. func (p *ServerProofPool) Add(serverProof ServerProofInterface) {
  61. p.pool <- serverProof
  62. }
  63. // Get returns the next available ServerProof
  64. func (p *ServerProofPool) Get(stopCh chan bool) (ServerProofInterface, error) {
  65. select {
  66. case <-stopCh:
  67. log.Info("ServerProofPool.Get stopped")
  68. return nil, ErrStop
  69. default:
  70. select {
  71. case <-stopCh:
  72. log.Info("ServerProofPool.Get stopped")
  73. return nil, ErrStop
  74. case serverProof := <-p.pool:
  75. return serverProof, nil
  76. }
  77. }
  78. }