code.vegaprotocol.io/vega@v0.79.0/wallet/api/node/adapters/grpc_adapter.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 adapters
    17  
    18  import (
    19  	"context"
    20  	"sort"
    21  	"strings"
    22  
    23  	apipb "code.vegaprotocol.io/vega/protos/vega/api/v1"
    24  	nodetypes "code.vegaprotocol.io/vega/wallet/api/node/types"
    25  
    26  	"google.golang.org/grpc"
    27  	"google.golang.org/grpc/credentials"
    28  	"google.golang.org/grpc/credentials/insecure"
    29  )
    30  
    31  type GRPCAdapter struct {
    32  	client     apipb.CoreServiceClient
    33  	connection *grpc.ClientConn
    34  
    35  	host string
    36  }
    37  
    38  func (c *GRPCAdapter) Host() string {
    39  	return c.host
    40  }
    41  
    42  func (c *GRPCAdapter) SpamStatistics(ctx context.Context, party string) (nodetypes.SpamStatistics, error) {
    43  	r, err := c.client.GetSpamStatistics(ctx,
    44  		&apipb.GetSpamStatisticsRequest{
    45  			PartyId: party,
    46  		})
    47  	if err != nil {
    48  		return nodetypes.SpamStatistics{}, err
    49  	}
    50  
    51  	proposals := map[string]uint64{}
    52  	for _, st := range r.Statistics.Votes.Statistics {
    53  		proposals[st.Proposal] = st.CountForEpoch
    54  	}
    55  
    56  	blockStates := []nodetypes.PoWBlockState{}
    57  	for _, b := range r.Statistics.Pow.BlockStates {
    58  		blockStates = append(blockStates, nodetypes.PoWBlockState{
    59  			BlockHeight:          b.BlockHeight,
    60  			BlockHash:            b.BlockHash,
    61  			TransactionsSeen:     b.TransactionsSeen,
    62  			ExpectedDifficulty:   b.ExpectedDifficulty,
    63  			HashFunction:         b.HashFunction,
    64  			Difficulty:           b.Difficulty,
    65  			TxPerBlock:           b.TxPerBlock,
    66  			IncreasingDifficulty: b.IncreasingDifficulty,
    67  		})
    68  	}
    69  
    70  	// sort by block-height so latest block is first
    71  	sort.Slice(blockStates, func(i int, j int) bool {
    72  		return blockStates[i].BlockHeight > blockStates[j].BlockHeight
    73  	})
    74  
    75  	var lastBlockHeight uint64
    76  	if len(blockStates) > 0 {
    77  		lastBlockHeight = blockStates[0].BlockHeight
    78  	}
    79  	return nodetypes.SpamStatistics{
    80  		Proposals:         toSpamStatistic(r.Statistics.Proposals),
    81  		Delegations:       toSpamStatistic(r.Statistics.Delegations),
    82  		Transfers:         toSpamStatistic(r.Statistics.Transfers),
    83  		NodeAnnouncements: toSpamStatistic(r.Statistics.NodeAnnouncements),
    84  		IssuesSignatures:  toSpamStatistic(r.Statistics.IssueSignatures),
    85  		CreateReferralSet: toSpamStatistic(r.Statistics.CreateReferralSet),
    86  		UpdateReferralSet: toSpamStatistic(r.Statistics.UpdateReferralSet),
    87  		ApplyReferralCode: toSpamStatistic(r.Statistics.ApplyReferralCode),
    88  		Votes: &nodetypes.VoteSpamStatistics{
    89  			Proposals:   proposals,
    90  			MaxForEpoch: r.Statistics.Votes.MaxForEpoch,
    91  			BannedUntil: r.Statistics.Votes.BannedUntil,
    92  		},
    93  		PoW: &nodetypes.PoWStatistics{
    94  			PowBlockStates: blockStates,
    95  			BannedUntil:    r.Statistics.Pow.BannedUntil,
    96  			PastBlocks:     r.Statistics.Pow.NumberOfPastBlocks,
    97  		},
    98  		ChainID:         r.ChainId,
    99  		EpochSeq:        r.Statistics.EpochSeq,
   100  		LastBlockHeight: lastBlockHeight,
   101  	}, nil
   102  }
   103  
   104  func (c *GRPCAdapter) Statistics(ctx context.Context) (nodetypes.Statistics, error) {
   105  	statistics, err := c.client.Statistics(ctx, &apipb.StatisticsRequest{})
   106  	if err != nil {
   107  		return nodetypes.Statistics{}, err
   108  	}
   109  
   110  	return nodetypes.Statistics{
   111  		BlockHeight: statistics.Statistics.BlockHeight,
   112  		BlockHash:   statistics.Statistics.BlockHash,
   113  		ChainID:     statistics.Statistics.ChainId,
   114  		VegaTime:    statistics.Statistics.VegaTime,
   115  	}, nil
   116  }
   117  
   118  func (c *GRPCAdapter) CheckTransaction(ctx context.Context, req *apipb.CheckTransactionRequest) (*apipb.CheckTransactionResponse, error) {
   119  	return c.client.CheckTransaction(ctx, req)
   120  }
   121  
   122  func (c *GRPCAdapter) SubmitTransaction(ctx context.Context, req *apipb.SubmitTransactionRequest) (*apipb.SubmitTransactionResponse, error) {
   123  	return c.client.SubmitTransaction(ctx, req)
   124  }
   125  
   126  func (c *GRPCAdapter) LastBlock(ctx context.Context) (nodetypes.LastBlock, error) {
   127  	lastBlock, err := c.client.LastBlockHeight(ctx, &apipb.LastBlockHeightRequest{})
   128  	if err != nil {
   129  		return nodetypes.LastBlock{}, err
   130  	}
   131  
   132  	return nodetypes.LastBlock{
   133  		ChainID:                         lastBlock.ChainId,
   134  		BlockHeight:                     lastBlock.Height,
   135  		BlockHash:                       lastBlock.Hash,
   136  		ProofOfWorkHashFunction:         lastBlock.SpamPowHashFunction,
   137  		ProofOfWorkDifficulty:           lastBlock.SpamPowDifficulty,
   138  		ProofOfWorkPastBlocks:           lastBlock.SpamPowNumberOfPastBlocks,
   139  		ProofOfWorkIncreasingDifficulty: lastBlock.SpamPowIncreasingDifficulty,
   140  		ProofOfWorkTxPerBlock:           lastBlock.SpamPowNumberOfTxPerBlock,
   141  	}, nil
   142  }
   143  
   144  func (c *GRPCAdapter) Stop() error {
   145  	return c.connection.Close()
   146  }
   147  
   148  func NewGRPCAdapter(host string) (*GRPCAdapter, error) {
   149  	useTLS := strings.HasPrefix(host, "tls://")
   150  
   151  	var creds credentials.TransportCredentials
   152  	if useTLS {
   153  		host = host[6:]
   154  		creds = credentials.NewClientTLSFromCert(nil, "")
   155  	} else {
   156  		creds = insecure.NewCredentials()
   157  	}
   158  
   159  	connection, err := grpc.Dial(host, grpc.WithTransportCredentials(creds))
   160  	if err != nil {
   161  		return nil, err
   162  	}
   163  
   164  	return &GRPCAdapter{
   165  		client:     apipb.NewCoreServiceClient(connection),
   166  		connection: connection,
   167  		host:       host,
   168  	}, nil
   169  }