code.vegaprotocol.io/vega@v0.79.0/datanode/sqlsubscribers/notary.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package sqlsubscribers
    17  
    18  import (
    19  	"context"
    20  
    21  	"code.vegaprotocol.io/vega/core/events"
    22  	"code.vegaprotocol.io/vega/datanode/entities"
    23  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    24  
    25  	"github.com/pkg/errors"
    26  )
    27  
    28  var ErrNoSignaturesForID = errors.New("no signatures for id")
    29  
    30  type NodeSignatureEvent interface {
    31  	events.Event
    32  	NodeSignature() commandspb.NodeSignature
    33  }
    34  
    35  type NotaryStore interface {
    36  	Add(context.Context, *entities.NodeSignature) error
    37  }
    38  
    39  type Notary struct {
    40  	subscriber
    41  	store NotaryStore
    42  }
    43  
    44  func NewNotary(store NotaryStore) *Notary {
    45  	return &Notary{
    46  		store: store,
    47  	}
    48  }
    49  
    50  func (n *Notary) Push(ctx context.Context, evt events.Event) error {
    51  	return n.consume(ctx, evt.(NodeSignatureEvent))
    52  }
    53  
    54  func (n *Notary) consume(ctx context.Context, event NodeSignatureEvent) error {
    55  	ns := event.NodeSignature()
    56  	record, err := entities.NodeSignatureFromProto(&ns, entities.TxHash(event.TxHash()), n.vegaTime)
    57  	if err != nil {
    58  		return errors.Wrap(err, "converting node-signature proto to database entity failed")
    59  	}
    60  
    61  	return errors.Wrap(n.store.Add(ctx, record), "inserting node-signature to SQL store failed")
    62  }
    63  
    64  func (n *Notary) Types() []events.Type {
    65  	return []events.Type{
    66  		events.NodeSignatureEvent,
    67  	}
    68  }
    69  
    70  func (n *Notary) Name() string {
    71  	return "Notary"
    72  }