-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpylons.go
136 lines (114 loc) · 3.82 KB
/
pylons.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package pylons
import (
"errors"
"fmt"
"strconv"
"github.com/saveio/pylons/common"
"github.com/saveio/pylons/common/constants"
"github.com/saveio/pylons/network"
"github.com/saveio/pylons/service"
"github.com/saveio/pylons/transfer"
"github.com/saveio/themis/account"
"github.com/saveio/themis/common/log"
"github.com/saveio/themis/smartcontract/service/native/utils"
)
var Version = "0.1"
type ChannelConfig struct {
ClientType string
ChainNodeURLs []string
DBPath string
BlockDelay string
SettleTimeout string
RevealTimeout string
}
type Channel struct {
Config *ChannelConfig
Service *service.ChannelService
}
func DefaultChannelConfig() *ChannelConfig {
config := &ChannelConfig{
ClientType: "rpc",
ChainNodeURLs: []string{"http://localhost:20336"},
DBPath: ".",
BlockDelay: "3",
}
return config
}
func NewChannelService(channelConfig *ChannelConfig, account *account.Account) (*Channel, error) {
settleTimeout, revealTimeout, err := getTimeout(channelConfig)
if err != nil {
log.Fatal(err)
return nil, err
}
blockChainService := network.NewBlockChainService(channelConfig.ClientType, channelConfig.ChainNodeURLs, account)
if blockChainService == nil {
log.Fatal("creating blockChain service failed")
return nil, errors.New("creating blockChain service failed")
}
startBlock, err := blockChainService.ChainClient.GetCurrentBlockHeight()
if err != nil {
log.Fatal("can not get current block height from blockChain service")
return nil, fmt.Errorf("GetCurrentBlockHeight error:%s", err)
}
// construct the option map
option := map[string]string{
"database_path": channelConfig.DBPath,
"block_delay": channelConfig.BlockDelay,
"settle_timeout": strconv.Itoa(settleTimeout),
"reveal_timeout": strconv.Itoa(revealTimeout),
}
if channelConfig.BlockDelay != "" {
blockDelay, err := strconv.Atoi(channelConfig.BlockDelay)
if err != nil {
log.Fatal("Invalid BlockDelay")
return nil, fmt.Errorf("invalid BlockDelay error:%s", err.Error())
}
common.SetMaxBlockDelay(blockDelay)
log.Infof("[NewChannelService] SetMaxBlockDelay blockDelay: %d", blockDelay)
}
service, err := service.NewChannelService(blockChainService, common.BlockHeight(startBlock),
common.Address(utils.MicroPayContractAddress), new(service.MessageHandler), option)
log.Info("channel service created, use account ", blockChainService.GetAccount().Address.ToBase58())
if err != nil {
return nil, fmt.Errorf("[NewChannelService] NewChannelService error: %s", err.Error())
}
channel := &Channel{Config: channelConfig, Service: service}
return channel, nil
}
func getTimeout(config *ChannelConfig) (settle int, reveal int, err error) {
settleTimeout := constants.DefaultSettleTimeout
if config.SettleTimeout != "" {
settleTimeout, err = strconv.Atoi(config.SettleTimeout)
if err != nil {
log.Fatal("invalid settle timeout")
return 0, 0, err
}
}
revealTimeout := constants.DefaultRevealTimeout
if config.RevealTimeout != "" {
revealTimeout, err = strconv.Atoi(config.RevealTimeout)
if err != nil {
log.Fatal("invalid reveal timeout")
return 0, 0, err
}
}
if settleTimeout < 2*revealTimeout {
log.Fatalf("settle timeout(%d) should be at least double of revealTimeout(%d)",
settleTimeout, revealTimeout)
return 0, 0, fmt.Errorf("settle timeout(%d) should be at least double of revealTimeout(%d)",
settleTimeout, revealTimeout)
}
return settleTimeout, revealTimeout, nil
}
func (this *Channel) StartPylons() error {
return this.Service.StartService()
}
func (this *Channel) StopPylons() {
this.Service.StopService()
}
func (this *Channel) RegisterReceiveNotification(notificationChannel chan *transfer.EventPaymentReceivedSuccess) {
this.Service.ReceiveNotificationChannels[notificationChannel] = struct{}{}
}
func (this *Channel) GetVersion() string {
return Version
}