forked from elastos/Elastos.ELA
/
common.go
177 lines (155 loc) · 4.13 KB
/
common.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
package servers
import (
. "Elastos.ELA/common"
"Elastos.ELA/common/log"
"Elastos.ELA/consensus/pow"
. "Elastos.ELA/core/transaction"
tx "Elastos.ELA/core/transaction"
. "Elastos.ELA/errors"
. "Elastos.ELA/net/protocol"
"Elastos.ELA/core/asset"
"Elastos.ELA/core/transaction/payload"
)
const TlsPort = 443
var NodeForServers Noder
var LocalPow *pow.PowService
type TxAttributeInfo struct {
Usage TransactionAttributeUsage
Data string
}
type UTXOTxInputInfo struct {
ReferTxID string
ReferTxOutputIndex uint16
Sequence uint32
Address string
Value string
}
type BalanceTxInputInfo struct {
AssetID string
Value Fixed64
ProgramHash string
}
type TxoutputInfo struct {
AssetID string
Value string
Address string
OutputLock uint32
}
type TxoutputMap struct {
Key Uint256
Txout []TxoutputInfo
}
type AmountMap struct {
Key Uint256
Value Fixed64
}
type ProgramInfo struct {
Code string
Parameter string
}
type Transactions struct {
TxType TransactionType
PayloadVersion byte
Payload PayloadInfo
Attributes []TxAttributeInfo
UTXOInputs []UTXOTxInputInfo
BalanceInputs []BalanceTxInputInfo
Outputs []TxoutputInfo
LockTime uint32
Programs []ProgramInfo
AssetOutputs []TxoutputMap
AssetInputAmount []AmountMap
AssetOutputAmount []AmountMap
Timestamp uint32 `json:",omitempty"`
Confirminations uint32 `json:",omitempty"`
TxSize uint32 `json:",omitempty"`
Hash string
}
type AuxInfo struct {
Version int32
PrevBlock string
MerkleRoot string
Timestamp uint32
Bits uint32
Nonce uint32
}
type BlockHead struct {
Version uint32
PrevBlockHash string
TransactionsRoot string
Timestamp uint32
Bits uint32
Height uint32
Nonce uint32
AuxPow *AuxInfo
Difficulty string
BlockSize int
Hash string
}
type BlockInfo struct {
Hash string
BlockData *BlockHead
Transactions []*Transactions
Confirminations uint32
MinerInfo string
}
type NodeInfo struct {
State uint // NodeForServers status
Port uint16 // The nodes's port
ID uint64 // The nodes's id
Time int64
Version string // node's version
Services uint64 // The services the NodeForServers supplied
Relay bool // The relay capability of the NodeForServers (merge into capbility flag)
Height uint64 // The NodeForServers latest block height
TxnCnt uint64 // The transactions be transmit by this NodeForServers
RxTxnCnt uint64 // The transaction received by this NodeForServers
Mining bool // Is this node mining or not
}
type PayloadInfo interface{}
type CoinbaseInfo struct {
CoinbaseData string
}
type RegisterAssetInfo struct {
Asset *asset.Asset
Amount string
Controller string
}
func TransPayloadToHex(p Payload) PayloadInfo {
switch object := p.(type) {
case *payload.CoinBase:
obj := new(CoinbaseInfo)
obj.CoinbaseData = string(object.CoinbaseData)
return obj
case *payload.RegisterAsset:
obj := new(RegisterAssetInfo)
obj.Asset = object.Asset
obj.Amount = object.Amount.String()
obj.Controller = BytesToHexString(object.Controller.ToArrayReverse())
return obj
case *payload.TransferAsset:
case *payload.Record:
case *payload.DeployCode:
}
return nil
}
func VerifyAndSendTx(txn *tx.Transaction) ErrCode {
// if transaction is verified unsucessfully then will not put it into transaction pool
if errCode := NodeForServers.AppendToTxnPool(txn); errCode != Success {
log.Warn("Can NOT add the transaction to TxnPool")
log.Info("[httpjsonrpc] VerifyTransaction failed when AppendToTxnPool.")
return errCode
}
if err := NodeForServers.Xmit(txn); err != nil {
log.Error("Xmit Tx Error:Xmit transaction failed.", err)
return ErrXmitFail
}
return Success
}
func ResponsePack(errCode ErrCode, result interface{}) map[string]interface{} {
if errCode != 0 && (result == "" || result == nil) {
result = ErrMap[errCode]
}
desc := ErrMap[errCode]
return map[string]interface{}{"Result": result, "Error": errCode, "Desc": desc, "Version": "1.0.0"}
}