forked from coinbase/mesh-bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
529 lines (446 loc) · 13.7 KB
/
types.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// Copyright 2020 Coinbase, Inc.
//
// 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 syscoin
import (
"fmt"
"strings"
"github.com/btcsuite/btcd/chaincfg"
"github.com/coinbase/rosetta-sdk-go/types"
)
const (
// Blockchain is Syscoin.
Blockchain string = "Syscoin"
// MainnetNetwork is the value of the network
// in MainnetNetworkIdentifier.
MainnetNetwork string = "Mainnet"
// TestnetNetwork is the value of the network
// in TestnetNetworkIdentifier.
TestnetNetwork string = "Testnet3"
// Decimals is the decimals value
// used in Currency.
Decimals = 8
// SatoshisInSyscoin is the number of
// Satoshis in 1 SYS (10^8).
SatoshisInSyscoin = 100000000
// InputOpType is used to describe
// INPUT.
InputOpType = "INPUT"
// OutputOpType is used to describe
// OUTPUT.
OutputOpType = "OUTPUT"
// CoinbaseOpType is used to describe
// Coinbase.
CoinbaseOpType = "COINBASE"
// SuccessStatus is the status of all
// Syscoin operations because anything
// on-chain is considered successful.
SuccessStatus = "SUCCESS"
// SkippedStatus is the status of all
// operations that are skipped because
// of BIP-30. You can read more about these
// types of operations in BIP-30.
SkippedStatus = "SKIPPED"
// TransactionHashLength is the length
// of any transaction hash in Syscoin.
TransactionHashLength = 64
// NullData is returned by syscoind
// as the ScriptPubKey.Type for OP_RETURN
// locking scripts.
NullData = "nulldata"
)
// Fee estimate constants
// Source: https://syscoinops.org/en/tools/calc-size/
const (
MinFeeRate = float64(0.00001) // nolint:gomnd
TransactionOverhead = 12 // 4 version, 2 segwit flag, 1 vin, 1 vout, 4 lock time
InputSize = 68 // 4 prev index, 32 prev hash, 4 sequence, 1 script size, ~27 script witness
OutputOverhead = 9 // 8 value, 1 script size
P2PKHScriptPubkeySize = 25 // P2PKH size
)
// CreateMainNetParams is a function to override default mainnet settings with address prefixes
func CreateMainNetParams() (*chaincfg.Params) {
chaincfg.MainNetParams.PubKeyHashAddrID = 63
chaincfg.MainNetParams.ScriptHashAddrID = 5
chaincfg.MainNetParams.Bech32HRPSegwit = "sys"
return &chaincfg.MainNetParams
}
// CreateTestNet3Params is a function to override default testnet settings with address prefixes
func CreateTestNet3Params() (*chaincfg.Params) {
chaincfg.TestNet3Params.PubKeyHashAddrID = 65
chaincfg.TestNet3Params.ScriptHashAddrID = 196
chaincfg.TestNet3Params.Bech32HRPSegwit = "tsys"
return &chaincfg.TestNet3Params
}
var (
// MainnetGenesisBlockIdentifier is the genesis block for mainnet.
MainnetGenesisBlockIdentifier = &types.BlockIdentifier{
Hash: "0000022642db0346b6e01c2a397471f4f12e65d4f4251ec96c1f85367a61a7ab",
}
// MainnetParams are the params for mainnet.
MainnetParams = CreateMainNetParams()
// MainnetCurrency is the *types.Currency for mainnet.
MainnetCurrency = &types.Currency{
Symbol: "SYS",
Decimals: Decimals,
}
// TestnetGenesisBlockIdentifier is the genesis block for testnet.
TestnetGenesisBlockIdentifier = &types.BlockIdentifier{
Hash: "00000e92e80d01cb6f46ae338127a3c1e9f9adf199aaffedf247758045886efb",
}
// TestnetParams are the params for testnet.
TestnetParams = CreateTestNet3Params()
// chain parameters
// TestnetCurrency is the *types.Currency for testnet.
TestnetCurrency = &types.Currency{
Symbol: "tSYS",
Decimals: Decimals,
}
// OperationTypes are all supported operation.Types.
OperationTypes = []string{
InputOpType,
OutputOpType,
CoinbaseOpType,
}
// OperationStatuses are all supported operation.Status.
OperationStatuses = []*types.OperationStatus{
{
Status: SuccessStatus,
Successful: true,
},
{
Status: SkippedStatus,
Successful: false,
},
}
)
// ScriptPubKey is a script placed on the output operations
// of a Syscoin transaction that must be satisfied to spend
// the output.
type ScriptPubKey struct {
ASM string `json:"asm"`
Hex string `json:"hex"`
RequiredSigs int64 `json:"reqSigs,omitempty"`
Type string `json:"type"`
Addresses []string `json:"addresses,omitempty"`
}
// ScriptSig is a script on the input operations of a
// Syscoin transaction that satisfies the ScriptPubKey
// on an output being spent.
type ScriptSig struct {
ASM string `json:"asm"`
Hex string `json:"hex"`
}
// BlockchainInfo is information about the Syscoin network.
// This struct only contains the information necessary for
// this implementation.
type BlockchainInfo struct {
Chain string `json:"chain"`
Blocks int64 `json:"blocks"`
BestBlockHash string `json:"bestblockhash"`
}
// PeerInfo is a collection of relevant info about a particular peer.
type PeerInfo struct {
Addr string `json:"addr"`
Version int64 `json:"version"`
SubVer string `json:"subver"`
StartingHeight int64 `json:"startingheight"`
RelayTxes bool `json:"relaytxes"`
LastSend int64 `json:"lastsend"`
LastRecv int64 `json:"lastrecv"`
BanScore int64 `json:"banscore"`
SyncedBlocks int64 `json:"synced_blocks"`
SyncedHeaders int64 `json:"synced_headers"`
}
// Block is a raw Syscoin block (with verbosity == 2).
type Block struct {
Hash string `json:"hash"`
Height int64 `json:"height"`
PreviousBlockHash string `json:"previousblockhash"`
Time int64 `json:"time"`
MedianTime int64 `json:"mediantime"`
Nonce int64 `json:"nonce"`
MerkleRoot string `json:"merkleroot"`
Version int32 `json:"version"`
Size int64 `json:"size"`
Weight int64 `json:"weight"`
Bits string `json:"bits"`
Difficulty float64 `json:"difficulty"`
Txs []*Transaction `json:"tx"`
}
// Metadata returns the metadata for a block.
func (b Block) Metadata() (map[string]interface{}, error) {
m := &BlockMetadata{
Nonce: b.Nonce,
MerkleRoot: b.MerkleRoot,
Version: b.Version,
Size: b.Size,
Weight: b.Weight,
MedianTime: b.MedianTime,
Bits: b.Bits,
Difficulty: b.Difficulty,
}
return types.MarshalMap(m)
}
// BlockMetadata is a collection of useful
// metadata in a block.
type BlockMetadata struct {
Nonce int64 `json:"nonce,omitempty"`
MerkleRoot string `json:"merkleroot,omitempty"`
Version int32 `json:"version,omitempty"`
Size int64 `json:"size,omitempty"`
Weight int64 `json:"weight,omitempty"`
MedianTime int64 `json:"mediantime,omitempty"`
Bits string `json:"bits,omitempty"`
Difficulty float64 `json:"difficulty,omitempty"`
}
// Transaction is a raw Syscoin transaction.
type Transaction struct {
Hex string `json:"hex"`
Hash string `json:"txid"`
Size int64 `json:"size"`
Vsize int64 `json:"vsize"`
Version int32 `json:"version"`
Locktime int64 `json:"locktime"`
Weight int64 `json:"weight"`
Inputs []*Input `json:"vin"`
Outputs []*Output `json:"vout"`
}
// Metadata returns the metadata for a transaction.
func (t Transaction) Metadata() (map[string]interface{}, error) {
m := &TransactionMetadata{
Size: t.Size,
Vsize: t.Vsize,
Version: t.Version,
Locktime: t.Locktime,
Weight: t.Weight,
}
return types.MarshalMap(m)
}
// TransactionMetadata is a collection of useful
// metadata in a transaction.
type TransactionMetadata struct {
Size int64 `json:"size,omitempty"`
Vsize int64 `json:"vsize,omitempty"`
Version int32 `json:"version,omitempty"`
Locktime int64 `json:"locktime,omitempty"`
Weight int64 `json:"weight,omitempty"`
}
// Input is a raw input in a Syscoin transaction.
type Input struct {
TxHash string `json:"txid"`
Vout int64 `json:"vout"`
ScriptSig *ScriptSig `json:"scriptSig"`
Sequence int64 `json:"sequence"`
TxInWitness []string `json:"txinwitness"`
// Relevant when the input is the coinbase input
Coinbase string `json:"coinbase"`
}
// Metadata returns the metadata for an input.
func (i Input) Metadata() (map[string]interface{}, error) {
m := &OperationMetadata{
ScriptSig: i.ScriptSig,
Sequence: i.Sequence,
TxInWitness: i.TxInWitness,
Coinbase: i.Coinbase,
}
return types.MarshalMap(m)
}
// Output is a raw output in a Syscoin transaction.
type Output struct {
Value float64 `json:"value"`
Index int64 `json:"n"`
ScriptPubKey *ScriptPubKey `json:"scriptPubKey"`
}
// Metadata returns the metadata for an output.
func (o Output) Metadata() (map[string]interface{}, error) {
m := &OperationMetadata{
ScriptPubKey: o.ScriptPubKey,
}
return types.MarshalMap(m)
}
// OperationMetadata is a collection of useful
// metadata from Syscoin inputs and outputs.
type OperationMetadata struct {
// Coinbase Metadata
Coinbase string `json:"coinbase,omitempty"`
// Input Metadata
ScriptSig *ScriptSig `json:"scriptsig,omitempty"`
Sequence int64 `json:"sequence,omitempty"`
TxInWitness []string `json:"txinwitness,omitempty"`
// Output Metadata
ScriptPubKey *ScriptPubKey `json:"scriptPubKey,omitempty"`
}
// request represents the JSON-RPC request body
type request struct {
JSONRPC string `json:"jsonrpc"`
ID int `json:"id"`
Method string `json:"method"`
Params []interface{} `json:"params"`
}
func (r request) GetVersion() string { return r.JSONRPC }
func (r request) GetID() int { return r.ID }
func (r request) GetMethod() string { return r.Method }
func (r request) GetParams() []interface{} { return r.Params }
// Responses
// jSONRPCResponse represents an interface for generic JSON-RPC responses
type jSONRPCResponse interface {
Err() error
}
type responseError struct {
Code int64 `json:"code"`
Message string `json:"message"`
}
// BlockResponse is the response body for `getblock` requests
type blockResponse struct {
Result *Block `json:"result"`
Error *responseError `json:"error"`
}
func (b blockResponse) Err() error {
if b.Error == nil {
return nil
}
if b.Error.Code == blockNotFoundErrCode {
return ErrBlockNotFound
}
return fmt.Errorf(
"%w: error JSON RPC response, code: %d, message: %s",
ErrJSONRPCError,
b.Error.Code,
b.Error.Message,
)
}
type pruneBlockchainResponse struct {
Result int64 `json:"result"`
Error *responseError `json:"error"`
}
func (p pruneBlockchainResponse) Err() error {
if p.Error == nil {
return nil
}
return fmt.Errorf(
"%w: error JSON RPC response, code: %d, message: %s",
ErrJSONRPCError,
p.Error.Code,
p.Error.Message,
)
}
type blockchainInfoResponse struct {
Result *BlockchainInfo `json:"result"`
Error *responseError `json:"error"`
}
func (b blockchainInfoResponse) Err() error {
if b.Error == nil {
return nil
}
return fmt.Errorf(
"%w: error JSON RPC response, code: %d, message: %s",
ErrJSONRPCError,
b.Error.Code,
b.Error.Message,
)
}
type peerInfoResponse struct {
Result []*PeerInfo `json:"result"`
Error *responseError `json:"error"`
}
func (p peerInfoResponse) Err() error {
if p.Error == nil {
return nil
}
return fmt.Errorf(
"%w: error JSON RPC response, code: %d, message: %s",
ErrJSONRPCError,
p.Error.Code,
p.Error.Message,
)
}
// blockHashResponse is the response body for `getblockhash` requests
type blockHashResponse struct {
Result string `json:"result"`
Error *responseError `json:"error"`
}
func (b blockHashResponse) Err() error {
if b.Error == nil {
return nil
}
return fmt.Errorf(
"%w: error JSON RPC response, code: %d, message: %s",
ErrJSONRPCError,
b.Error.Code,
b.Error.Message,
)
}
// sendRawTransactionResponse is the response body for `sendrawtransaction` requests
type sendRawTransactionResponse struct {
Result string `json:"result"`
Error *responseError `json:"error"`
}
func (s sendRawTransactionResponse) Err() error {
if s.Error == nil {
return nil
}
return fmt.Errorf(
"%w: error JSON RPC response, code: %d, message: %s",
ErrJSONRPCError,
s.Error.Code,
s.Error.Message,
)
}
type suggestedFeeRate struct {
FeeRate float64 `json:"feerate"`
}
// suggestedFeeRateResponse is the response body for `estimatesmartfee` requests
type suggestedFeeRateResponse struct {
Result *suggestedFeeRate `json:"result"`
Error *responseError `json:"error"`
}
func (s suggestedFeeRateResponse) Err() error {
if s.Error == nil {
return nil
}
return fmt.Errorf(
"%w: error JSON RPC response, code: %d, message: %s",
ErrJSONRPCError,
s.Error.Code,
s.Error.Message,
)
}
// rawMempoolResponse is the response body for `getrawmempool` requests.
type rawMempoolResponse struct {
Result []string `json:"result"`
Error *responseError `json:"error"`
}
func (r rawMempoolResponse) Err() error {
if r.Error == nil {
return nil
}
return fmt.Errorf(
"%w: error JSON RPC response, code: %d, message: %s",
ErrJSONRPCError,
r.Error.Code,
r.Error.Message,
)
}
// CoinIdentifier converts a tx hash and vout into
// the canonical CoinIdentifier.Identifier used in
// rosetta-syscoin.
func CoinIdentifier(hash string, vout int64) string {
return fmt.Sprintf("%s:%d", hash, vout)
}
// TransactionHash extracts the transaction hash
// from a CoinIdentifier.Identifier.
func TransactionHash(identifier string) string {
vals := strings.Split(identifier, ":")
return vals[0]
}