cranberry

Форк
0
/
client.go 
54 строки · 1.3 Кб
1
package client
2

3
import (
4
	"context"
5
	"fmt"
6

7
	pbapi "gitverse.ru/IvanTimofeev/cranberry/pkg/grpc"
8
	grpc "google.golang.org/grpc"
9
	"google.golang.org/grpc/credentials/insecure"
10
)
11

12
type CranberryClient struct {
13
	host string
14
	port int
15
}
16

17
func NewCranberryClient(host string, port int) *CranberryClient {
18
	return &CranberryClient{
19
		host: host,
20
		port: port,
21
	}
22
}
23

24
func (crc *CranberryClient) SendTx(tx *pbapi.Tx, pubKey []byte, signature *pbapi.Signature) error {
25
	conn, err := grpc.NewClient(fmt.Sprintf("%s:%d", crc.host, crc.port),
26
		grpc.WithTransportCredentials(insecure.NewCredentials()))
27
	if err != nil {
28
		return err
29
	}
30
	defer conn.Close()
31
	client := pbapi.NewCranberryServiceClient(conn)
32
	_, err = client.SendTx(context.Background(), &pbapi.SendTxRequest{
33
		Tx:        tx,
34
		Signature: signature,
35
		PubKey:    pubKey,
36
	})
37
	if err != nil {
38
		return err
39
	}
40
	return nil
41
}
42

43
func (crc *CranberryClient) GetBlockByHeight(height uint64) (*pbapi.GetBlockByHeightResponse, error) {
44
	conn, err := grpc.NewClient(fmt.Sprintf("%s:%d", crc.host, crc.port),
45
		grpc.WithTransportCredentials(insecure.NewCredentials()))
46
	if err != nil {
47
		return nil, err
48
	}
49
	defer conn.Close()
50
	client := pbapi.NewCranberryServiceClient(conn)
51
	return client.GetBlockByHeight(context.Background(), &pbapi.GetBlockByHeightRequest{
52
		Height: height,
53
	})
54
}
55

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.