-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.go
68 lines (55 loc) · 1.22 KB
/
backend.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package kowalad
import (
"context"
"fmt"
"time"
"github.com/kowala-tech/kcoin/kcoinclient"
"github.com/kowala-tech/kcoin/log"
)
const (
timeout = 300 * time.Second
)
type Backend interface {
StartNode(config *Config) error
StopNode() error
SendRawTransaction(data []byte) error
}
type backend struct {
config *Config
node Node
client *kcoinclient.Client
log log.Logger
rpcTimeout time.Duration
}
func NewBackend() *backend {
b := &backend{
node: NewLightNode(),
log: log.New("package", "kowalad/backend"),
}
return b
}
func (b *backend) StartNode(config *Config) error {
b.config = config
return b.startNode(config)
}
func (b *backend) startNode(config *Config) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("node crashed on start: %v", err)
}
}()
b.client, err = b.node.Client()
if err != nil {
log.Error("Failed to get the client", "err", err)
return err
}
return b.node.Start(config)
}
func (b *backend) StopNode() error {
return b.node.Stop()
}
func (b *backend) SendRawTransaction(data []byte) error {
ctx, cancel := context.WithTimeout(context.Background(), b.rpcTimeout)
defer cancel()
return b.client.SendRawTransaction(ctx, data)
}