forked from beowulf-foundation/beowulf-go
/
trx.go
193 lines (167 loc) · 4.64 KB
/
trx.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package client
import (
"time"
"github.com/thanhxeon2470/beowulf-go/api"
"github.com/thanhxeon2470/beowulf-go/config"
"github.com/thanhxeon2470/beowulf-go/transactions"
"github.com/thanhxeon2470/beowulf-go/types"
)
var RefBlockMap = make(map[time.Time]uint32)
//BResp of response when sending a transaction.
type BResp struct {
ID string
JSONTrx string
}
//OperResp type is returned when the operation is performed.
type OperResp struct {
NameOper string
Bresp *BResp
}
//Get HeadBlockNumber from mem before getting from Blockchain
func (client *Client) GetHeadBlockNum() (uint32, error) {
if len(RefBlockMap) > 0 {
for k := range RefBlockMap {
old := k.Add(config.GET_HEAD_BLOCK_NUM_TIMEOUT_IN_MIN * time.Minute)
now := time.Now().UTC()
if old.Before(now) {
delete(RefBlockMap, k)
props, err := client.API.GetDynamicGlobalProperties()
if err != nil {
return 0, err
}
refBlockNum := props.HeadBlockNumber
if refBlockNum > config.HEAD_BLOCK_NUM_SPAN {
refBlockNum -= config.HEAD_BLOCK_NUM_SPAN
}
RefBlockMap[now] = refBlockNum
return refBlockNum, nil
}
return RefBlockMap[k], nil
}
}
props, err := client.API.GetDynamicGlobalProperties()
if err != nil {
return 0, err
}
refBlockNum := props.HeadBlockNumber
if refBlockNum > config.HEAD_BLOCK_NUM_SPAN {
refBlockNum -= config.HEAD_BLOCK_NUM_SPAN
}
now := time.Now().UTC()
RefBlockMap[now] = refBlockNum
return refBlockNum, nil
}
//SendTrx generates and sends an array of transactions to BEOWULF.
func (client *Client) SendTrx(strx []types.Operation, extension string) (*BResp, error) {
var bresp BResp
// Getting the necessary parameters
refBlockNum, err := client.GetHeadBlockNum()
if err != nil {
return nil, err
}
block, err := client.API.GetBlock(refBlockNum)
if err != nil {
return nil, err
}
refBlockId := block.BlockId
// Creating a Transaction
refBlockPrefix, err := transactions.RefBlockPrefix(refBlockId)
if err != nil {
return nil, err
}
ex := []interface{}{}
if len(extension) > 0 {
ex = make([]interface{}, 1)
as := types.ExtensionJsonType{extension}
tas := types.ExtensionType{uint8(types.ExtJsonType.Code()), as}
ex[0] = &tas
}
tx := transactions.NewSignedTransaction(&types.Transaction{
RefBlockNum: transactions.RefBlockNum(refBlockNum),
RefBlockPrefix: refBlockPrefix,
Extensions: ex, //[]interface{}{},
})
// Adding Operations to a Transaction
for _, val := range strx {
tx.PushOperation(val)
}
expTime := time.Now().Add(config.TRANSACTION_EXPIRATION_IN_MIN * time.Minute).UTC()
tm := types.Time{
Time: &expTime,
}
tx.Expiration = &tm
createdTime := time.Now().UTC()
tx.CreatedTime = types.UInt64(createdTime.Unix())
// Obtain the key required for signing
privKeys, err := client.SigningKeys(strx[0])
if err != nil {
return nil, err
}
// Sign the transaction
tx.Transaction.Signatures = []string{}
txId, err := tx.Sign(privKeys, client.chainID)
if err != nil || txId == "" {
return nil, err
}
// Sending a transaction
if client.AsyncProtocol {
var resp *api.AsyncBroadcastResponse
resp, err = client.API.BroadcastTransaction(tx.Transaction)
if resp != nil {
bresp.ID = resp.ID
}
} else {
var resp *api.BroadcastResponse
resp, err = client.API.BroadcastTransactionSynchronous(tx.Transaction)
if resp != nil {
bresp.ID = resp.ID
}
}
bresp.JSONTrx, _ = JSONTrxString(tx)
if err != nil {
return &bresp, err
}
return &bresp, nil
}
func (client *Client) GetTrx(strx []types.Operation, extension string) (*types.Transaction, error) {
// Getting the necessary parameters
refBlockNum, err := client.GetHeadBlockNum()
if err != nil {
return nil, err
}
block, err := client.API.GetBlock(refBlockNum)
if err != nil {
return nil, err
}
refBlockId := block.BlockId
// Creating a Transaction
refBlockPrefix, err := transactions.RefBlockPrefix(refBlockId)
if err != nil {
return nil, err
}
ex := []interface{}{}
if len(extension) > 0 {
ex = make([]interface{}, 1)
as := types.ExtensionJsonType{extension}
tas := types.ExtensionType{uint8(types.ExtJsonType.Code()), as}
ex[0] = &tas
}
tx := &types.Transaction{
RefBlockNum: transactions.RefBlockNum(refBlockNum),
RefBlockPrefix: refBlockPrefix,
Extensions: ex, //[]interface{}{},
}
// Adding Operations to a Transaction
for _, val := range strx {
tx.PushOperation(val)
}
expTime := time.Now().Add(config.TRANSACTION_EXPIRATION_IN_MIN * time.Minute).UTC()
//expTime := time.Now().Add(59 * time.Minute).UTC()
tm := types.Time{
Time: &expTime,
}
tx.Expiration = &tm
createdTime := time.Now().UTC()
tx.CreatedTime = types.UInt64(createdTime.Unix())
return tx, nil
}