-
Notifications
You must be signed in to change notification settings - Fork 290
/
utils.go
205 lines (187 loc) · 6.64 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
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
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/
package utils
import (
"math/big"
"reflect"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
types2 "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/trie"
oComm "github.com/ontio/ontology/common"
sysconfig "github.com/ontio/ontology/common/config"
"github.com/ontio/ontology/core/types"
types3 "github.com/ontio/ontology/http/ethrpc/types"
)
func EthBlockFromOntology(block *types.Block, fullTx bool, bloom types2.Bloom) map[string]interface{} {
if block == nil {
return nil
}
hash := block.Hash()
transactions, gasUsed, ethTxs := EthTransactionsFromOntology(block.Transactions, common.BytesToHash(hash.ToArray()), uint64(block.Header.Height))
var blockTxs interface{}
if fullTx {
blockTxs = ethTxs
} else {
blockTxs = transactions
}
return FormatBlock(*block, 0, gasUsed, blockTxs, bloom)
}
func RawEthBlockFromOntology(block *types.Block, bloom types2.Bloom) *types2.Block {
if block == nil {
return nil
}
hash := block.Hash()
gasUsed, ethTxs := RawEthTransactionsFromOntology(block.Transactions, common.BytesToHash(hash.ToArray()), uint64(block.Header.Height))
h := &types2.Header{
ParentHash: common.Hash(block.Header.PrevBlockHash),
UncleHash: common.Hash{},
Coinbase: common.Address{},
Root: common.Hash{},
TxHash: common.Hash(block.Header.TransactionsRoot),
ReceiptHash: common.Hash{},
Bloom: bloom,
Difficulty: big.NewInt(0),
Number: big.NewInt(int64(block.Header.Height)),
GasLimit: 0,
GasUsed: gasUsed.Uint64(),
Time: uint64(block.Header.Timestamp),
Extra: []byte{},
MixDigest: common.Hash{},
Nonce: types2.BlockNonce{},
}
return types2.NewBlock(h, ethTxs, nil, nil, new(trie.Trie))
}
func EthTransactionsFromOntology(txs []*types.Transaction, blockHash common.Hash, blockNumber uint64) ([]common.Hash, *big.Int, []*types3.Transaction) {
var transactionHashes []common.Hash
var transactions []*types3.Transaction
gasUsed := big.NewInt(0)
for idx, tx := range txs {
hash := tx.Hash()
rpcTx, err := OntTxToEthTx(*tx, blockHash, blockNumber, uint64(idx))
if err != nil {
continue
}
gasUsed.Add(gasUsed, big.NewInt(int64(rpcTx.Gas)))
transactionHashes = append(transactionHashes, common.BytesToHash(hash.ToArray()))
transactions = append(transactions, rpcTx)
}
return transactionHashes, gasUsed, transactions
}
func RawEthTransactionsFromOntology(txs []*types.Transaction, blockHash common.Hash, blockNumber uint64) (*big.Int, []*types2.Transaction) {
var transactions []*types2.Transaction
gasUsed := big.NewInt(0)
for _, tx := range txs {
if tx.IsEipTx() {
eipTx, err := tx.GetEIP155Tx()
if err != nil {
continue
}
gasUsed.Add(gasUsed, big.NewInt(int64(eipTx.Gas())))
transactions = append(transactions, eipTx)
}
}
return gasUsed, transactions
}
func OntTxToEthTx(tx types.Transaction, blockHash common.Hash, blockNumber, index uint64) (*types3.Transaction, error) {
eip155Tx, err := tx.GetEIP155Tx()
if err != nil {
return nil, err
}
return NewTransaction(eip155Tx, common.Hash(tx.Hash()), blockHash, blockNumber, index)
}
func FormatBlock(block types.Block, gasLimit uint64, gasUsed *big.Int, transactions interface{}, bloom types2.Bloom) map[string]interface{} {
size := len(block.ToArray())
header := block.Header
hash := header.Hash()
transactionsRoot := types2.EmptyRootHash
if oComm.UINT256_EMPTY != header.TransactionsRoot {
transactionsRoot = common.BytesToHash(header.TransactionsRoot[:])
}
ret := map[string]interface{}{
"number": hexutil.Uint64(header.Height),
"hash": common.BytesToHash(hash[:]),
"parentHash": common.BytesToHash(header.PrevBlockHash[:]),
"nonce": types2.BlockNonce{}, // PoW specific
"sha3Uncles": types2.EmptyUncleHash,
"logsBloom": bloom,
"transactionsRoot": transactionsRoot,
"stateRoot": common.Hash{},
"miner": common.Address{},
"mixHash": common.Hash{},
"difficulty": hexutil.Uint64(0),
"totalDifficulty": hexutil.Uint64(0),
"extraData": hexutil.Bytes{},
"size": hexutil.Uint64(size),
"gasLimit": hexutil.Uint64(gasLimit),
"gasUsed": (*hexutil.Big)(gasUsed),
"timestamp": hexutil.Uint64(header.Timestamp),
"uncles": []common.Hash{},
"receiptsRoot": types2.EmptyRootHash,
}
if !reflect.ValueOf(transactions).IsNil() {
switch transactions.(type) {
case []common.Hash:
ret["transactions"] = transactions.([]common.Hash)
case []*types3.Transaction:
ret["transactions"] = transactions.([]*types3.Transaction)
}
} else {
ret["transactions"] = []common.Hash{}
}
return ret
}
func EthToOntAddr(address common.Address) oComm.Address {
return oComm.Address(address)
}
func EthToOntHash(txHash common.Hash) oComm.Uint256 {
return oComm.Uint256(txHash)
}
func OntToEthHash(txHash oComm.Uint256) common.Hash {
return common.Hash(txHash)
}
func NewTransaction(tx *types2.Transaction, txHash, blockHash common.Hash, blockNumber, index uint64) (*types3.Transaction, error) {
signer := types2.NewEIP155Signer(big.NewInt(int64(GetChainId())))
from, err := signer.Sender(tx)
if err != nil {
return nil, err
}
v, r, s := tx.RawSignatureValues()
rpcTx := &types3.Transaction{
From: from,
Gas: hexutil.Uint64(tx.Gas()),
GasPrice: (*hexutil.Big)(tx.GasPrice()),
Hash: txHash,
Input: hexutil.Bytes(tx.Data()),
Nonce: hexutil.Uint64(tx.Nonce()),
To: tx.To(),
Value: (*hexutil.Big)(tx.Value()),
V: (*hexutil.Big)(v),
R: (*hexutil.Big)(r),
S: (*hexutil.Big)(s),
}
if blockHash != (common.Hash{}) {
rpcTx.BlockHash = &blockHash
rpcTx.BlockNumber = (*hexutil.Big)(new(big.Int).SetUint64(blockNumber))
rpcTx.TransactionIndex = (*hexutil.Uint64)(&index)
}
return rpcTx, nil
}
func GetChainId() uint32 {
return sysconfig.DefConfig.P2PNode.EVMChainId
}