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