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