-
Notifications
You must be signed in to change notification settings - Fork 156
/
websocket.go
129 lines (112 loc) · 2.63 KB
/
websocket.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
package websocket
import (
"bytes"
"encoding/json"
"github.com/nknorg/nkn/api/common"
"github.com/nknorg/nkn/api/websocket/server"
. "github.com/nknorg/nkn/common"
"github.com/nknorg/nkn/core/ledger"
"github.com/nknorg/nkn/events"
"github.com/nknorg/nkn/net/node"
. "github.com/nknorg/nkn/util/config"
"github.com/nknorg/nkn/vault"
)
var ws *server.WsServer
var (
pushBlockFlag bool = true
pushRawBlockFlag bool = false
pushBlockTxsFlag bool = false
)
func NewServer(localNode *node.LocalNode, w vault.Wallet) *server.WsServer {
// common.SetNode(n)
ledger.DefaultLedger.Blockchain.BCEvents.Subscribe(events.EventBlockPersistCompleted, SendBlock2WSclient)
ws = server.InitWsServer(localNode, w)
return ws
}
func SendBlock2WSclient(v interface{}) {
go func() {
PushSigChainBlockHash(v)
}()
if Parameters.HttpWsPort != 0 && pushBlockFlag {
go func() {
PushBlock(v)
}()
}
if Parameters.HttpWsPort != 0 && pushBlockTxsFlag {
go func() {
PushBlockTransactions(v)
}()
}
}
func GetWsPushBlockFlag() bool {
return pushBlockFlag
}
func SetWsPushBlockFlag(b bool) {
pushBlockFlag = b
}
func GetPushRawBlockFlag() bool {
return pushRawBlockFlag
}
func SetPushRawBlockFlag(b bool) {
pushRawBlockFlag = b
}
func GetPushBlockTxsFlag() bool {
return pushBlockTxsFlag
}
func SetPushBlockTxsFlag(b bool) {
pushBlockTxsFlag = b
}
func SetTxHashMap(txhash string, sessionid string) {
if ws == nil {
return
}
ws.SetTxHashMap(txhash, sessionid)
}
func PushBlock(v interface{}) {
if ws == nil {
return
}
resp := common.ResponsePack(common.SUCCESS)
if block, ok := v.(*ledger.Block); ok {
if pushRawBlockFlag {
w := bytes.NewBuffer(nil)
block.Serialize(w)
resp["Result"] = BytesToHexString(w.Bytes())
} else {
info, _ := block.MarshalJson()
var x interface{}
json.Unmarshal(info, &x)
resp["Result"] = x
}
resp["Action"] = "sendRawBlock"
ws.PushResult(resp)
}
}
func PushBlockTransactions(v interface{}) {
if ws == nil {
return
}
resp := common.ResponsePack(common.SUCCESS)
if block, ok := v.(*ledger.Block); ok {
if pushBlockTxsFlag {
resp["Result"] = common.GetBlockTransactions(block)
}
resp["Action"] = "sendblocktransactions"
ws.PushResult(resp)
}
}
func PushSigChainBlockHash(v interface{}) {
if ws == nil {
return
}
resp := common.ResponsePack(common.SUCCESS)
if block, ok := v.(*ledger.Block); ok {
resp["Action"] = "updateSigChainBlockHash"
//resp["Result"] = common.GetBlockInfo(block).BlockData.PrevBlockHash
resp["Result"] = BytesToHexString(block.Header.PrevBlockHash.ToArrayReverse())
ws.PushResult(resp)
}
}
func GetServer() *server.WsServer {
return ws
}