-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
193 lines (178 loc) · 5.38 KB
/
transaction.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
/*
Copyright 2022 CESS (Cumulus Encrypted Storage System) authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package chain
import (
"cess-cacher/logger"
"cess-cacher/utils"
"math/big"
"strconv"
"strings"
"time"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/pkg/errors"
)
func (c *chainClient) SubmitExtrinsic(method string,
callback func(events CacheEventRecords) bool, args ...any) (string, error) {
defer func() {
if p := recover(); p != nil {
logger.Uld.Sugar().Error(p)
}
}()
var (
txhash string
ext types.Extrinsic
)
c.lock.Lock()
defer c.lock.Unlock()
if !c.IsChainClientOk() {
c.SetChainState(false)
return txhash, errors.Wrap(ERR_RPC_CONNECTION, "submit extrinsic error")
}
c.SetChainState(true)
if call, err := types.NewCall(c.metadata, method, args...); err != nil {
return txhash, errors.Wrap(err, "submit extrinsic error")
} else {
ext = types.NewExtrinsic(call)
}
accInfo, err := c.GetAccountInfo()
if err != nil {
return txhash, errors.Wrap(err, "submit extrinsic error")
}
o := types.SignatureOptions{
BlockHash: c.genesisHash,
Era: types.ExtrinsicEra{IsMortalEra: false},
GenesisHash: c.genesisHash,
Nonce: types.NewUCompactFromUInt(uint64(accInfo.Nonce)),
SpecVersion: c.runtimeVersion.SpecVersion,
Tip: types.NewUCompactFromUInt(0),
TransactionVersion: c.runtimeVersion.TransactionVersion,
}
// Sign the transaction
if err = ext.Sign(c.keyring, o); err != nil {
return txhash, errors.Wrap(err, "submit extrinsic error")
}
// Do the transfer and track the actual status
sub, err := c.api.RPC.Author.SubmitAndWatchExtrinsic(ext)
if err != nil {
if !strings.Contains(err.Error(), "Priority is too low") {
return txhash, errors.Wrap(err, "submit extrinsic error")
}
for i := 0; i < 20; i++ {
o.Nonce = types.NewUCompactFromUInt(uint64(accInfo.Nonce + types.NewU32(1)))
// Sign the transaction
if err = ext.Sign(c.keyring, o); err != nil {
return txhash, errors.Wrap(err, "submit extrinsic error")
}
sub, err = c.api.RPC.Author.SubmitAndWatchExtrinsic(ext)
if err == nil {
break
}
}
if err != nil {
return txhash, errors.Wrap(err, "submit extrinsic error")
}
}
defer sub.Unsubscribe()
timeout := time.After(c.timeForBlockOut)
for {
select {
case status := <-sub.Chan():
if status.IsInBlock {
events := CacheEventRecords{}
txhash, _ = types.EncodeToHex(status.AsInBlock)
h, err := c.api.RPC.State.GetStorageRaw(c.keyEvents, status.AsInBlock)
if err != nil {
return txhash, errors.Wrap(err, "submit extrinsic error")
}
types.EventRecordsRaw(*h).DecodeEventRecords(c.metadata, &events)
if callback(events) {
return txhash, nil
}
return txhash, errors.Wrap(ERR_TX_FAILED, "submit extrinsic error")
}
case err = <-sub.Err():
return txhash, errors.Wrap(err, "submit extrinsic error")
case <-timeout:
return txhash, errors.Wrap(ERR_RPC_TIMEOUT, "submit extrinsic error")
}
}
}
func (c *chainClient) Register(ip, port string, price uint64) (string, error) {
info, err := NewCacherInfo(c.IncomeAcc, ip, port, price)
if err != nil {
return "", errors.Wrap(err, "register cacher error")
}
txhash, err := c.SubmitExtrinsic(
CACHER_REGISTER,
func(events CacheEventRecords) bool {
return len(events.Cacher_Register) > 0
},
info,
)
return txhash, errors.Wrap(err, "register cacher error")
}
func (c *chainClient) Update(ip, port string, price uint64) (string, error) {
info, err := NewCacherInfo(c.IncomeAcc, ip, port, price)
if err != nil {
return "", errors.Wrap(err, "update cacher info error")
}
if _, err = c.GetMinerInfo(); err != nil {
return "", errors.Wrap(err, "update cacher info error")
}
txhash, err := c.SubmitExtrinsic(
CACHER_UPDATE,
func(events CacheEventRecords) bool {
return len(events.Cacher_Update) > 0
},
info,
)
return txhash, errors.Wrap(err, "update cacher info error")
}
func (c *chainClient) Logout() (string, error) {
txhash, err := c.SubmitExtrinsic(
CACHER_LOGOUT,
func(events CacheEventRecords) bool {
return len(events.Cacher_Logout) > 0
},
)
return txhash, errors.Wrap(err, "logout cacher error")
}
func NewCacherInfo(account, ip, port string, price uint64) (CacherInfo, error) {
var info CacherInfo
if !utils.IsIPv4(ip) {
return info, ERR_RPC_IP_FORMAT
}
var ipv4 Ipv4Type
ipv4.Index = 0
ips := strings.Split(ip, ".")
for i := 0; i < len(ipv4.Value); i++ {
tmp, err := strconv.Atoi(ips[i])
if err != nil {
return info, err
}
ipv4.Value[i] = types.U8(tmp)
}
tmp, err := strconv.Atoi(port)
if err != nil {
return info, err
}
pubkey, err := utils.DecodePublicKeyOfCessAccount(account)
if err != nil {
return info, err
}
info.Acc = types.NewAccountID(pubkey)
ipv4.Port = types.U16(tmp)
info.Ip = ipv4
info.BytePrice = types.NewU128(*big.NewInt(int64(price)))
return info, nil
}