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