-
Notifications
You must be signed in to change notification settings - Fork 16
/
transactions.go
337 lines (290 loc) · 10.5 KB
/
transactions.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
package eth
import (
"errors"
"fmt"
"math/big"
"github.com/Oneledger/protocol/serialize"
"github.com/Oneledger/protocol/utils"
"github.com/Oneledger/protocol/vm"
rpctypes "github.com/Oneledger/protocol/web3/types"
rpcutils "github.com/Oneledger/protocol/web3/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rpc"
abci "github.com/tendermint/tendermint/abci/types"
mempl "github.com/tendermint/tendermint/mempool"
tmrpccore "github.com/tendermint/tendermint/rpc/core"
tmtypes "github.com/tendermint/tendermint/types"
)
var (
jsonSerializer = serialize.GetSerializer(serialize.NETWORK)
)
// TODO: Move to the config
const (
RPCTxFeeCap = 1 // olt
)
// GetTransactionCount returns the number of transactions at the given address up to the given block number.
func (svc *Service) GetTransactionCount(address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (*hexutil.Uint64, error) {
height, err := rpctypes.StateAndHeaderByNumberOrHash(svc.GetBlockStore(), blockNrOrHash)
if err != nil {
return nil, nil
}
svc.logger.Debug("eth_getTransactionCount", "address", address, "height", height)
// getting actual block
blockNum := svc.getStateHeight(height)
var txLen uint64
ethAcc, err := svc.ctx.GetAccountKeeper().GetVersionedAccount(address.Bytes(), blockNum)
if err == nil {
txLen = ethAcc.Sequence
}
// for pending
if height == rpctypes.PendingBlockNumber {
txLen += rpcutils.GetPendingTxCountByAddress(svc.GetMempool(), address)
}
n := hexutil.Uint64(txLen)
return &n, nil
}
// GetTransactionByHash returns the transaction identified by hash.
func (svc *Service) GetTransactionByHash(hash common.Hash) (*rpctypes.Transaction, error) {
chainID, err := svc.ChainId()
if err != nil {
svc.logger.Debug("eth_getTransactionByHash", "hash", hash, "failed to get chainId")
return nil, err
}
svc.logger.Debug("eth_getTransactionByHash", "hash", hash)
tx, err := tmrpccore.Tx(nil, hash.Bytes(), false)
if err != nil {
// Try to get pending
pendingTx, err := rpcutils.GetPendingTx(svc.GetMempool(), hash, (*big.Int)(&chainID))
if err != nil {
svc.logger.Debug("eth_getTransactionByHash", "hash", hash, "tx not found")
return nil, nil
}
return pendingTx, nil
}
block := svc.GetBlockStore().LoadBlock(tx.Height)
if block == nil {
svc.logger.Debug("eth_getTransactionByHash", "hash", hash, "block not found")
return nil, err
}
txIndex := hexutil.Uint64(tx.Index)
return rpctypes.LegacyRawBlockAndTxToEthTx(block, &tx.Tx, (*big.Int)(&chainID), &txIndex)
}
// GetTransactionReceipt returns the transaction receipt identified by hash.
func (svc *Service) GetTransactionReceipt(hash common.Hash) (*rpctypes.TransactionReceipt, error) {
svc.logger.Debug("eth_getTransactionReceipt", "hash", hash)
tx, err := tmrpccore.Tx(nil, hash.Bytes(), false)
if err != nil {
return nil, nil
}
block := svc.GetBlockStore().LoadBlock(tx.Height)
if block == nil {
svc.logger.Debug("eth_getTransactionReceipt", "hash", hash, "block not found")
return nil, nil
}
chainID := utils.HashToBigInt(block.ChainID)
txIndex := hexutil.Uint64(tx.Index)
oneTx, err := rpctypes.LegacyRawBlockAndTxToEthTx(block, &tx.Tx, chainID, &txIndex)
if err != nil {
return nil, err
}
cumulativeGasUsed := uint64(tx.TxResult.GasUsed)
if tx.Index != 0 {
cumulativeGasUsed += rpctypes.GetBlockCumulativeGas(block, int(tx.Index))
}
var (
contractAddress *common.Address
bloom = vm.BytesToBloom(make([]byte, 6))
logs = make([]*ethtypes.Log, 0)
)
// Set status codes based on tx result
status := ethtypes.ReceiptStatusSuccessful
if tx.TxResult.GetCode() == 1 {
status = ethtypes.ReceiptStatusFailed
} else {
logReceipt := rpctypes.GetTxEthLogs(&tx.TxResult, tx.Index)
status = logReceipt.Status
if status == ethtypes.ReceiptStatusSuccessful {
if oneTx.To == nil {
contractAddress = logReceipt.ContractAddress
}
logs = logReceipt.Logs
bloom = logReceipt.Bloom
}
}
receipt := &rpctypes.TransactionReceipt{
Status: hexutil.Uint64(status),
CumulativeGasUsed: hexutil.Uint64(cumulativeGasUsed),
LogsBloom: bloom,
Logs: logs,
TransactionHash: oneTx.Hash,
ContractAddress: contractAddress,
GasUsed: hexutil.Uint64(tx.TxResult.GasUsed),
BlockHash: *oneTx.BlockHash,
BlockNumber: *oneTx.BlockNumber,
TransactionIndex: *oneTx.TransactionIndex,
From: oneTx.From,
To: oneTx.To,
}
return receipt, nil
}
// GetTransactionByBlockHashAndIndex returns the transaction identified by block hash and index.
func (svc *Service) GetTransactionByBlockHashAndIndex(hash common.Hash, idx hexutil.Uint64) (*rpctypes.Transaction, error) {
svc.logger.Debug("eth_getTransactionByBlockHashAndIndex", "hash", hash, "idx", idx)
block := svc.GetBlockStore().LoadBlockByHash(hash.Bytes())
if block == nil {
svc.logger.Debug("eth_getTransactionByBlockHashAndIndex", "hash", hash, "idx", idx, "block not found")
return nil, nil
}
return svc.getTransactionByBlockAndIndex(block, idx)
}
// GetTransactionByBlockNumberAndIndex returns the transaction identified by number and index.
func (svc *Service) GetTransactionByBlockNumberAndIndex(blockNrOrHash rpc.BlockNumberOrHash, idx hexutil.Uint64) (*rpctypes.Transaction, error) {
height, err := rpctypes.StateAndHeaderByNumberOrHash(svc.GetBlockStore(), blockNrOrHash)
if err != nil {
return nil, nil
}
svc.logger.Debug("eth_getTransactionByBlockNumberAndIndex", "height", height, "idx", idx)
var (
blockNum int64
)
switch height {
case rpctypes.PendingBlockNumber:
blockNum = svc.getStateHeight(height)
svc.logger.Debug("eth_getTransactionByBlockNumberAndIndex", "height", blockNum, "idx", idx, "for pending txs")
block := svc.GetBlockStore().LoadBlock(blockNum)
if block == nil {
svc.logger.Debug("eth_getTransactionByBlockNumberAndIndex", "height", blockNum, "idx", idx, "block not found with height")
return nil, nil
}
txs := svc.GetMempool().ReapMaxTxs(50)
// return if index out of bounds
if uint64(idx) >= uint64(len(txs)) {
return nil, nil
}
chainID := utils.HashToBigInt(block.ChainID)
return rpctypes.LegacyRawBlockAndTxToEthTx(block, &txs[idx], chainID, &idx)
case rpctypes.EarliestBlockNumber:
blockNum = rpctypes.InitialBlockNumber
case rpctypes.LatestBlockNumber:
blockNum = svc.getState().Version()
default:
blockNum = height
}
block := svc.GetBlockStore().LoadBlock(blockNum)
if block == nil {
return nil, nil
}
return svc.getTransactionByBlockAndIndex(block, idx)
}
func (svc *Service) getTransactionByBlockAndIndex(block *tmtypes.Block, idx hexutil.Uint64) (*rpctypes.Transaction, error) {
if block == nil {
svc.logger.Debug("getTransactionByBlockAndIndex", "block not found")
return nil, nil
}
// return if index out of bounds
if uint64(idx) >= uint64(len(block.Txs)) {
return nil, nil
}
chainID := utils.HashToBigInt(block.ChainID)
return rpctypes.LegacyRawBlockAndTxToEthTx(block, &block.Txs[idx], chainID, &idx)
}
// SendRawTransaction will add the signed transaction to the transaction pool.
// The sender is responsible for signing the transaction and using the correct nonce.
func (svc *Service) SendRawTransaction(input hexutil.Bytes) (common.Hash, error) {
tx := new(ethtypes.Transaction)
if err := tx.UnmarshalBinary(input); err != nil {
return common.Hash{}, err
}
return svc.submitTransaction(tx)
}
// submitTransaction is a helper function that submits tx to tendermint pool and logs a message.
func (svc *Service) submitTransaction(tx *ethtypes.Transaction) (common.Hash, error) {
// If the transaction fee cap is already specified, ensure the
// fee of the given transaction is _reasonable_.
if err := rpcutils.CheckTxFee(tx.GasPrice(), tx.Gas(), RPCTxFeeCap); err != nil {
return common.Hash{}, err
}
if tx.Type() != ethtypes.LegacyTxType {
return common.Hash{}, errors.New("only legacy transactions allowed over RPC")
}
if !tx.Protected() {
// Ensure only eip155 signed transactions are submitted if EIP155Required is set.
return common.Hash{}, errors.New("only replay-protected (EIP-155) transactions allowed over RPC")
}
// Print a log with full tx details for manual investigations and interventions
signer := ethtypes.NewEIP155Signer(tx.ChainId())
from, err := ethtypes.Sender(signer, tx)
if err != nil {
return common.Hash{}, err
}
chainID, err := svc.ChainId()
if err != nil {
return common.Hash{}, err
}
if signer.ChainID().Cmp(chainID.ToInt()) != 0 {
return common.Hash{}, ethtypes.ErrInvalidChainId
}
txHash, err := svc.sendTx(tx)
if err != nil {
return common.Hash{}, err
}
if tx.To() == nil {
addr := crypto.CreateAddress(from, tx.Nonce())
svc.logger.Info("Submitted contract creation", "hash", txHash.Hex(), "from", from, "nonce", tx.Nonce(), "contract", addr.Hex(), "value", tx.Value())
} else {
svc.logger.Info("Submitted transaction", "hash", txHash.Hex(), "from", from, "nonce", tx.Nonce(), "recipient", tx.To(), "value", tx.Value())
}
return txHash, nil
}
// sendTx directly to the pool, all validation steps was done before
func (svc *Service) sendTx(ethTx *ethtypes.Transaction) (common.Hash, error) {
config := svc.ctx.GetConfig()
signedTx, err := rpcutils.EthToOLSignedTx(ethTx)
if err != nil {
return common.Hash{}, err
}
packet, err := jsonSerializer.Serialize(signedTx)
if err != nil {
return common.Hash{}, err
}
// support two scenarios
// - for fast fullnode send throughtput (async)
// - for web3 wallets to show an errors from validation (sync)
mempool := svc.GetMempool()
txHash := tmtypes.Tx(packet).Hash()
if config.Node.UseAsync {
svc.logger.Info("Use async mode to propagate tx", txHash)
err = mempool.CheckTx(packet, nil, mempl.TxInfo{})
if err != nil {
return common.Hash{}, err
}
} else {
svc.logger.Info("Use sync mode to propagate tx", txHash)
resCh := make(chan *abci.Response, 1)
err := mempool.CheckTx(packet, func(res *abci.Response) {
resCh <- res
}, mempl.TxInfo{})
if err != nil {
return common.Hash{}, deserializeError(err.Error())
}
res := <-resCh
resBrodTx := res.GetCheckTx()
if resBrodTx.Code != 0 {
return common.Hash{}, deserializeError(resBrodTx.Log)
}
}
return common.BytesToHash(txHash), nil
}
func deserializeError(errMsg string) error {
unpackedData := struct {
Msg string `json:"msg"`
}{}
err := jsonSerializer.Deserialize([]byte(errMsg), &unpackedData)
if err != nil {
return fmt.Errorf(errMsg)
}
return fmt.Errorf(unpackedData.Msg)
}