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