-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathutils.go
121 lines (111 loc) · 4.16 KB
/
utils.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
// Copyright 2018 The Fractal Team Authors
// This file is part of the fractal project.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package rpcapi
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/log"
"github.com/fractalplatform/fractal/common"
"github.com/fractalplatform/fractal/types"
)
// submitTransaction is a helper function that submits tx to txPool and logs a message.
func submitTransaction(ctx context.Context, b Backend, tx *types.Transaction) (common.Hash, error) {
if err := b.SendTx(ctx, tx); err != nil {
return common.Hash{}, err
}
log.Info("Submitted transaction", "fullhash", tx.Hash().Hex())
return tx.Hash(), nil
}
// RPCMarshalBlock converts the given block to the RPC output which depends on fullTx. If inclTx is true transactions are
// returned. When fullTx is true the returned block contains full transaction details, otherwise it will only contain
// transaction hashes.
func RPCMarshalBlock(chainID *big.Int, b *types.Block, inclTx bool, fullTx bool) map[string]interface{} {
head := b.Header() // copies the header once
fields := map[string]interface{}{
"number": head.Number,
"hash": b.Hash(),
"proposedIrreversible": head.ProposedIrreversible,
"parentHash": head.ParentHash,
"logsBloom": head.Bloom,
"stateRoot": head.Root,
"miner": head.Coinbase,
"difficulty": head.Difficulty,
"extraData": hexutil.Bytes(head.Extra),
"size": b.Size(),
"gasLimit": head.GasLimit,
"gasUsed": head.GasUsed,
"timestamp": head.Time,
"transactionsRoot": head.TxsRoot,
"receiptsRoot": head.ReceiptsRoot,
"forkID": head.ForkID,
}
if inclTx {
formatTx := func(tx *types.Transaction, index uint64) interface{} {
return tx.Hash()
}
if fullTx {
formatTx = func(tx *types.Transaction, index uint64) interface{} {
return tx.NewRPCTransaction(b.Hash(), b.NumberU64(), index)
}
}
txs := b.Transactions()
transactions := make([]interface{}, len(txs))
for i, tx := range txs {
transactions[i] = formatTx(tx, uint64(i))
}
fields["transactions"] = transactions
}
return fields
}
func RPCMarshalBlockWithPayer(chainID *big.Int, b *types.Block, inclTx bool, fullTx bool) map[string]interface{} {
head := b.Header() // copies the header once
fields := map[string]interface{}{
"number": head.Number,
"hash": b.Hash(),
"proposedIrreversible": head.ProposedIrreversible,
"parentHash": head.ParentHash,
"logsBloom": head.Bloom,
"stateRoot": head.Root,
"miner": head.Coinbase,
"difficulty": head.Difficulty,
"extraData": hexutil.Bytes(head.Extra),
"size": b.Size(),
"gasLimit": head.GasLimit,
"gasUsed": head.GasUsed,
"timestamp": head.Time,
"transactionsRoot": head.TxsRoot,
"receiptsRoot": head.ReceiptsRoot,
"forkID": head.ForkID,
}
if inclTx {
formatTx := func(tx *types.Transaction, index uint64) interface{} {
return tx.Hash()
}
if fullTx {
formatTx = func(tx *types.Transaction, index uint64) interface{} {
return tx.NewRPCTransactionWithPayer(b.Hash(), b.NumberU64(), index)
}
}
txs := b.Transactions()
transactions := make([]interface{}, len(txs))
for i, tx := range txs {
transactions[i] = formatTx(tx, uint64(i))
}
fields["transactions"] = transactions
}
return fields
}