aboutsummaryrefslogtreecommitdiffstats
path: root/rpc/api/parsing.go
blob: 8e25ffffbae029bca2b4352cd9e7e29152f28985 (plain) (blame)
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
package api

import (
    "bytes"
    "encoding/binary"
    "encoding/hex"
    "encoding/json"
    "math/big"
    "strings"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/rpc/shared"
)

type hexdata struct {
    data  []byte
    isNil bool
}

func (d *hexdata) String() string {
    return "0x" + common.Bytes2Hex(d.data)
}

func (d *hexdata) MarshalJSON() ([]byte, error) {
    if d.isNil {
        return json.Marshal(nil)
    }
    return json.Marshal(d.String())
}

func newHexData(input interface{}) *hexdata {
    d := new(hexdata)

    if input == nil {
        d.isNil = true
        return d
    }
    switch input := input.(type) {
    case []byte:
        d.data = input
    case common.Hash:
        d.data = input.Bytes()
    case *common.Hash:
        if input == nil {
            d.isNil = true
        } else {
            d.data = input.Bytes()
        }
    case common.Address:
        d.data = input.Bytes()
    case *common.Address:
        if input == nil {
            d.isNil = true
        } else {
            d.data = input.Bytes()
        }
    case types.Bloom:
        d.data = input.Bytes()
    case *types.Bloom:
        if input == nil {
            d.isNil = true
        } else {
            d.data = input.Bytes()
        }
    case *big.Int:
        if input == nil {
            d.isNil = true
        } else {
            d.data = input.Bytes()
        }
    case int64:
        d.data = big.NewInt(input).Bytes()
    case uint64:
        buff := make([]byte, 8)
        binary.BigEndian.PutUint64(buff, input)
        d.data = buff
    case int:
        d.data = big.NewInt(int64(input)).Bytes()
    case uint:
        d.data = big.NewInt(int64(input)).Bytes()
    case int8:
        d.data = big.NewInt(int64(input)).Bytes()
    case uint8:
        d.data = big.NewInt(int64(input)).Bytes()
    case int16:
        d.data = big.NewInt(int64(input)).Bytes()
    case uint16:
        buff := make([]byte, 2)
        binary.BigEndian.PutUint16(buff, input)
        d.data = buff
    case int32:
        d.data = big.NewInt(int64(input)).Bytes()
    case uint32:
        buff := make([]byte, 4)
        binary.BigEndian.PutUint32(buff, input)
        d.data = buff
    case string: // hexstring
        // aaargh ffs TODO: avoid back-and-forth hex encodings where unneeded
        bytes, err := hex.DecodeString(strings.TrimPrefix(input, "0x"))
        if err != nil {
            d.isNil = true
        } else {
            d.data = bytes
        }
    default:
        d.isNil = true
    }

    return d
}

type hexnum struct {
    data  []byte
    isNil bool
}

func (d *hexnum) String() string {
    // Get hex string from bytes
    out := common.Bytes2Hex(d.data)
    // Trim leading 0s
    out = strings.TrimLeft(out, "0")
    // Output "0x0" when value is 0
    if len(out) == 0 {
        out = "0"
    }
    return "0x" + out
}

func (d *hexnum) MarshalJSON() ([]byte, error) {
    if d.isNil {
        return json.Marshal(nil)
    }
    return json.Marshal(d.String())
}

func newHexNum(input interface{}) *hexnum {
    d := new(hexnum)

    d.data = newHexData(input).data

    return d
}

type BlockRes struct {
    fullTx bool

    BlockNumber     *hexnum           `json:"number"`
    BlockHash       *hexdata          `json:"hash"`
    ParentHash      *hexdata          `json:"parentHash"`
    Nonce           *hexdata          `json:"nonce"`
    Sha3Uncles      *hexdata          `json:"sha3Uncles"`
    LogsBloom       *hexdata          `json:"logsBloom"`
    TransactionRoot *hexdata          `json:"transactionsRoot"`
    StateRoot       *hexdata          `json:"stateRoot"`
    Miner           *hexdata          `json:"miner"`
    Difficulty      *hexnum           `json:"difficulty"`
    TotalDifficulty *hexnum           `json:"totalDifficulty"`
    Size            *hexnum           `json:"size"`
    ExtraData       *hexdata          `json:"extraData"`
    GasLimit        *hexnum           `json:"gasLimit"`
    GasUsed         *hexnum           `json:"gasUsed"`
    UnixTimestamp   *hexnum           `json:"timestamp"`
    Transactions    []*TransactionRes `json:"transactions"`
    Uncles          []*UncleRes       `json:"uncles"`
}

func (b *BlockRes) MarshalJSON() ([]byte, error) {
    if b.fullTx {
        var ext struct {
            BlockNumber     *hexnum           `json:"number"`
            BlockHash       *hexdata          `json:"hash"`
            ParentHash      *hexdata          `json:"parentHash"`
            Nonce           *hexdata          `json:"nonce"`
            Sha3Uncles      *hexdata          `json:"sha3Uncles"`
            LogsBloom       *hexdata          `json:"logsBloom"`
            TransactionRoot *hexdata          `json:"transactionsRoot"`
            StateRoot       *hexdata          `json:"stateRoot"`
            Miner           *hexdata          `json:"miner"`
            Difficulty      *hexnum           `json:"difficulty"`
            TotalDifficulty *hexnum           `json:"totalDifficulty"`
            Size            *hexnum           `json:"size"`
            ExtraData       *hexdata          `json:"extraData"`
            GasLimit        *hexnum           `json:"gasLimit"`
            GasUsed         *hexnum           `json:"gasUsed"`
            UnixTimestamp   *hexnum           `json:"timestamp"`
            Transactions    []*TransactionRes `json:"transactions"`
            Uncles          []*hexdata        `json:"uncles"`
        }

        ext.BlockNumber = b.BlockNumber
        ext.BlockHash = b.BlockHash
        ext.ParentHash = b.ParentHash
        ext.Nonce = b.Nonce
        ext.Sha3Uncles = b.Sha3Uncles
        ext.LogsBloom = b.LogsBloom
        ext.TransactionRoot = b.TransactionRoot
        ext.StateRoot = b.StateRoot
        ext.Miner = b.Miner
        ext.Difficulty = b.Difficulty
        ext.TotalDifficulty = b.TotalDifficulty
        ext.Size = b.Size
        ext.ExtraData = b.ExtraData
        ext.GasLimit = b.GasLimit
        ext.GasUsed = b.GasUsed
        ext.UnixTimestamp = b.UnixTimestamp
        ext.Transactions = b.Transactions
        ext.Uncles = make([]*hexdata, len(b.Uncles))
        for i, u := range b.Uncles {
            ext.Uncles[i] = u.BlockHash
        }
        return json.Marshal(ext)
    } else {
        var ext struct {
            BlockNumber     *hexnum    `json:"number"`
            BlockHash       *hexdata   `json:"hash"`
            ParentHash      *hexdata   `json:"parentHash"`
            Nonce           *hexdata   `json:"nonce"`
            Sha3Uncles      *hexdata   `json:"sha3Uncles"`
            LogsBloom       *hexdata   `json:"logsBloom"`
            TransactionRoot *hexdata   `json:"transactionsRoot"`
            StateRoot       *hexdata   `json:"stateRoot"`
            Miner           *hexdata   `json:"miner"`
            Difficulty      *hexnum    `json:"difficulty"`
            TotalDifficulty *hexnum    `json:"totalDifficulty"`
            Size            *hexnum    `json:"size"`
            ExtraData       *hexdata   `json:"extraData"`
            GasLimit        *hexnum    `json:"gasLimit"`
            GasUsed         *hexnum    `json:"gasUsed"`
            UnixTimestamp   *hexnum    `json:"timestamp"`
            Transactions    []*hexdata `json:"transactions"`
            Uncles          []*hexdata `json:"uncles"`
        }

        ext.BlockNumber = b.BlockNumber
        ext.BlockHash = b.BlockHash
        ext.ParentHash = b.ParentHash
        ext.Nonce = b.Nonce
        ext.Sha3Uncles = b.Sha3Uncles
        ext.LogsBloom = b.LogsBloom
        ext.TransactionRoot = b.TransactionRoot
        ext.StateRoot = b.StateRoot
        ext.Miner = b.Miner
        ext.Difficulty = b.Difficulty
        ext.TotalDifficulty = b.TotalDifficulty
        ext.Size = b.Size
        ext.ExtraData = b.ExtraData
        ext.GasLimit = b.GasLimit
        ext.GasUsed = b.GasUsed
        ext.UnixTimestamp = b.UnixTimestamp
        ext.Transactions = make([]*hexdata, len(b.Transactions))
        for i, tx := range b.Transactions {
            ext.Transactions[i] = tx.Hash
        }
        ext.Uncles = make([]*hexdata, len(b.Uncles))
        for i, u := range b.Uncles {
            ext.Uncles[i] = u.BlockHash
        }
        return json.Marshal(ext)
    }
}

func NewBlockRes(block *types.Block, fullTx bool) *BlockRes {
    if block == nil {
        return nil
    }

    res := new(BlockRes)
    res.fullTx = fullTx
    res.BlockNumber = newHexNum(block.Number())
    res.BlockHash = newHexData(block.Hash())
    res.ParentHash = newHexData(block.ParentHash())
    res.Nonce = newHexData(block.Nonce())
    res.Sha3Uncles = newHexData(block.UncleHash())
    res.LogsBloom = newHexData(block.Bloom())
    res.TransactionRoot = newHexData(block.TxHash())
    res.StateRoot = newHexData(block.Root())
    res.Miner = newHexData(block.Coinbase())
    res.Difficulty = newHexNum(block.Difficulty())
    res.TotalDifficulty = newHexNum(block.Td)
    res.Size = newHexNum(block.Size().Int64())
    res.ExtraData = newHexData(block.Extra())
    res.GasLimit = newHexNum(block.GasLimit())
    res.GasUsed = newHexNum(block.GasUsed())
    res.UnixTimestamp = newHexNum(block.Time())

    txs := block.Transactions()
    res.Transactions = make([]*TransactionRes, len(txs))
    for i, tx := range txs {
        res.Transactions[i] = NewTransactionRes(tx)
        res.Transactions[i].BlockHash = res.BlockHash
        res.Transactions[i].BlockNumber = res.BlockNumber
        res.Transactions[i].TxIndex = newHexNum(i)
    }

    uncles := block.Uncles()
    res.Uncles = make([]*UncleRes, len(uncles))
    for i, uncle := range uncles {
        res.Uncles[i] = NewUncleRes(uncle)
    }

    return res
}

type TransactionRes struct {
    Hash        *hexdata `json:"hash"`
    Nonce       *hexnum  `json:"nonce"`
    BlockHash   *hexdata `json:"blockHash"`
    BlockNumber *hexnum  `json:"blockNumber"`
    TxIndex     *hexnum  `json:"transactionIndex"`
    From        *hexdata `json:"from"`
    To          *hexdata `json:"to"`
    Value       *hexnum  `json:"value"`
    Gas         *hexnum  `json:"gas"`
    GasPrice    *hexnum  `json:"gasPrice"`
    Input       *hexdata `json:"input"`
}

func NewTransactionRes(tx *types.Transaction) *TransactionRes {
    if tx == nil {
        return nil
    }

    var v = new(TransactionRes)
    v.Hash = newHexData(tx.Hash())
    v.Nonce = newHexNum(tx.Nonce())
    // v.BlockHash =
    // v.BlockNumber =
    // v.TxIndex =
    from, _ := tx.From()
    v.From = newHexData(from)
    v.To = newHexData(tx.To())
    v.Value = newHexNum(tx.Value())
    v.Gas = newHexNum(tx.Gas())
    v.GasPrice = newHexNum(tx.GasPrice())
    v.Input = newHexData(tx.Data())
    return v
}

type UncleRes struct {
    BlockNumber     *hexnum  `json:"number"`
    BlockHash       *hexdata `json:"hash"`
    ParentHash      *hexdata `json:"parentHash"`
    Nonce           *hexdata `json:"nonce"`
    Sha3Uncles      *hexdata `json:"sha3Uncles"`
    ReceiptHash     *hexdata `json:"receiptHash"`
    LogsBloom       *hexdata `json:"logsBloom"`
    TransactionRoot *hexdata `json:"transactionsRoot"`
    StateRoot       *hexdata `json:"stateRoot"`
    Miner           *hexdata `json:"miner"`
    Difficulty      *hexnum  `json:"difficulty"`
    ExtraData       *hexdata `json:"extraData"`
    GasLimit        *hexnum  `json:"gasLimit"`
    GasUsed         *hexnum  `json:"gasUsed"`
    UnixTimestamp   *hexnum  `json:"timestamp"`
}

func NewUncleRes(h *types.Header) *UncleRes {
    if h == nil {
        return nil
    }

    var v = new(UncleRes)
    v.BlockNumber = newHexNum(h.Number)
    v.BlockHash = newHexData(h.Hash())
    v.ParentHash = newHexData(h.ParentHash)
    v.Sha3Uncles = newHexData(h.UncleHash)
    v.Nonce = newHexData(h.Nonce[:])
    v.LogsBloom = newHexData(h.Bloom)
    v.TransactionRoot = newHexData(h.TxHash)
    v.StateRoot = newHexData(h.Root)
    v.Miner = newHexData(h.Coinbase)
    v.Difficulty = newHexNum(h.Difficulty)
    v.ExtraData = newHexData(h.Extra)
    v.GasLimit = newHexNum(h.GasLimit)
    v.GasUsed = newHexNum(h.GasUsed)
    v.UnixTimestamp = newHexNum(h.Time)
    v.ReceiptHash = newHexData(h.ReceiptHash)

    return v
}

// type FilterLogRes struct {
//  Hash             string `json:"hash"`
//  Address          string `json:"address"`
//  Data             string `json:"data"`
//  BlockNumber      string `json:"blockNumber"`
//  TransactionHash  string `json:"transactionHash"`
//  BlockHash        string `json:"blockHash"`
//  TransactionIndex string `json:"transactionIndex"`
//  LogIndex         string `json:"logIndex"`
// }

// type FilterWhisperRes struct {
//  Hash       string `json:"hash"`
//  From       string `json:"from"`
//  To         string `json:"to"`
//  Expiry     string `json:"expiry"`
//  Sent       string `json:"sent"`
//  Ttl        string `json:"ttl"`
//  Topics     string `json:"topics"`
//  Payload    string `json:"payload"`
//  WorkProved string `json:"workProved"`
// }

type ReceiptRes struct {
    TransactionHash   *hexdata       `json:"transactionHash"`
    TransactionIndex  *hexnum        `json:"transactionIndex"`
    BlockNumber       *hexnum        `json:"blockNumber"`
    BlockHash         *hexdata       `json:"blockHash"`
    CumulativeGasUsed *hexnum        `json:"cumulativeGasUsed"`
    GasUsed           *hexnum        `json:"gasUsed"`
    ContractAddress   *hexdata       `json:"contractAddress"`
    Logs              *[]interface{} `json:"logs"`
}

func NewReceiptRes(rec *types.Receipt) *ReceiptRes {
    if rec == nil {
        return nil
    }

    var v = new(ReceiptRes)
    v.TransactionHash = newHexData(rec.TxHash)
    // v.TransactionIndex = newHexNum(input)
    // v.BlockNumber = newHexNum(input)
    // v.BlockHash = newHexData(input)
    v.CumulativeGasUsed = newHexNum(rec.CumulativeGasUsed)
    // v.GasUsed = newHexNum(input)
    // If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation
    if bytes.Compare(rec.ContractAddress.Bytes(), bytes.Repeat([]byte{0}, 20)) != 0 {
        v.ContractAddress = newHexData(rec.ContractAddress)
    }

    logs := make([]interface{}, len(rec.Logs()))
    for i, log := range rec.Logs() {
        logs[i] = NewLogRes(log)
    }
    v.Logs = &logs

    return v
}

func numString(raw interface{}) (*big.Int, error) {
    var number *big.Int
    // Parse as integer
    num, ok := raw.(float64)
    if ok {
        number = big.NewInt(int64(num))
        return number, nil
    }

    // Parse as string/hexstring
    str, ok := raw.(string)
    if ok {
        number = common.String2Big(str)
        return number, nil
    }

    return nil, shared.NewInvalidTypeError("", "not a number or string")
}

func blockHeight(raw interface{}, number *int64) error {
    // Parse as integer
    num, ok := raw.(float64)
    if ok {
        *number = int64(num)
        return nil
    }

    // Parse as string/hexstring
    str, ok := raw.(string)
    if !ok {
        return shared.NewInvalidTypeError("", "not a number or string")
    }

    switch str {
    case "earliest":
        *number = 0
    case "latest":
        *number = -1
    case "pending":
        *number = -2
    default:
        if common.HasHexPrefix(str) {
            *number = common.String2Big(str).Int64()
        } else {
            return shared.NewInvalidTypeError("blockNumber", "is not a valid string")
        }
    }

    return nil
}

func blockHeightFromJson(msg json.RawMessage, number *int64) error {
    var raw interface{}
    if err := json.Unmarshal(msg, &raw); err != nil {
        return shared.NewDecodeParamError(err.Error())
    }
    return blockHeight(raw, number)
}