-
Notifications
You must be signed in to change notification settings - Fork 189
/
tx_record.go
206 lines (169 loc) · 5.65 KB
/
tx_record.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
// Copyright 2019 The klaytn Authors
// This file is part of the klaytn library.
//
// The klaytn library 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 klaytn library 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 klaytn library. If not, see <http://www.gnu.org/licenses/>.
package dbsyncer
import (
"github.com/klaytn/klaytn/blockchain/types"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/common/hexutil"
"strings"
)
type SummaryArguments struct {
from string
txHash string
to string
contractAddress string
tx *types.Transaction
}
type TxMapArguments struct {
senderTxHash string
txHash string
}
type TxReceiptArgument struct {
tx *types.Transaction
receipt *types.Receipt
}
type BulkInsertQuery struct {
parameters string
vals []interface{}
blockNumber uint64
insertCount int
}
func MakeTxDBRow(block *types.Block, txKey uint64, tx *types.Transaction, receipt *types.Receipt) (string, []interface{}, TxMapArguments, SummaryArguments, error) {
cols := ""
vals := []interface{}{}
blockHash := block.Hash().Hex()
blockNumber := block.Number().Uint64()
contractAddress := ""
if (receipt.ContractAddress != common.Address{}) {
contractAddress = strings.ToLower(receipt.ContractAddress.Hex())
}
from := ""
if tx.IsLegacyTransaction() {
signer := types.NewEIP155Signer(tx.ChainId())
addr, err := types.Sender(signer, tx)
if err != nil {
logger.Error("fail to tx.From", "err", err)
return "", []interface{}{}, TxMapArguments{}, SummaryArguments{}, err
}
from = strings.ToLower(addr.Hex())
} else {
addr, err := tx.From()
if err != nil {
logger.Error("fail to tx.From", "err", err)
return "", []interface{}{}, TxMapArguments{}, SummaryArguments{}, err
}
from = strings.ToLower(addr.Hex())
}
gas := tx.Gas()
txGasPrice := tx.GasPrice().String()
txGasUsed := receipt.GasUsed
input := hexutil.Bytes(tx.Data()).String()
if input == "0x" {
input = ""
}
nonce := tx.Nonce()
status := receipt.Status
to := "" // '' means that address doesn't exist
if tx.To() != nil {
to = strings.ToLower(tx.To().Hex())
}
timestamp := block.Time().Uint64()
txHash := tx.Hash().Hex()
txtype := tx.Type().String()
value := tx.Value().String()
feePayer := ""
feeRatio := uint8(0)
if tx.IsFeeDelegatedTransaction() {
feeAddr, err := tx.FeePayer()
if err != nil {
logger.Error("fail to tx.FeePayer", "err", err)
return "", []interface{}{}, TxMapArguments{}, SummaryArguments{}, err
}
ratio, _ := tx.FeeRatio()
// ok is false in TxTypeFeeDelegatedValueTransfer
// ok is true in TxTypeFeeDelegatedValueTransferWithRatio
feePayer = strings.ToLower(feeAddr.Hex())
feeRatio = uint8(ratio)
}
senderHash := ""
if senderTxHash, ok := tx.SenderTxHash(); ok {
senderHash = senderTxHash.Hex()
}
cols = "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
vals = append(vals, txKey, blockHash, blockNumber, contractAddress, from, gas, txGasPrice, txGasUsed, input, nonce, status, to,
timestamp, txHash, txtype, value, feePayer, feeRatio, senderHash)
TxMapArg := TxMapArguments{senderHash, txHash}
summaryArg := SummaryArguments{from, txHash, to, contractAddress, tx}
return cols, vals, TxMapArg, summaryArg, nil
}
func MakeSummaryDBRow(sa SummaryArguments) (cols string, vals []interface{}, count int, err error) {
// insert account summary for creation and deploy
if !sa.tx.IsLegacyTransaction() {
if sa.tx.Type().IsAccountCreation() {
accountType := 0
creator := sa.from
createdTx := sa.txHash
hra := true
//TODO-Klaytn need to use humanreable field
internalTx, ok := sa.tx.GetTxInternalData().(*types.TxInternalDataAccountCreation)
if !ok {
logger.Error("fail to convert TxInternalDataAccountCreation", "txhash", sa.tx.Hash().Hex())
hra = false
} else {
hra = internalTx.HumanReadable
}
cols = "(?,?,?,?,?)"
vals = append(vals, sa.to, accountType, creator, createdTx, hra)
count = 1
} else if sa.tx.Type().IsContractDeploy() {
accountType := 1
creator := sa.from
createdTx := sa.txHash
//TODO-Klaytn need to use humanreable field
hra := false
switch internalTx := sa.tx.GetTxInternalData().(type) {
case *types.TxInternalDataSmartContractDeploy:
hra = internalTx.HumanReadable
case *types.TxInternalDataFeeDelegatedSmartContractDeploy:
hra = internalTx.HumanReadable
case *types.TxInternalDataFeeDelegatedSmartContractDeployWithRatio:
hra = internalTx.HumanReadable
default:
logger.Error("fail to convert ", "type", internalTx, "txhash", sa.tx.Hash().Hex())
}
cols = "(?,?,?,?,?)"
vals = append(vals, sa.contractAddress, accountType, creator, createdTx, hra)
count = 1
}
} else if sa.contractAddress != "" {
// AccountCreation for LegacyTransaction
accountType := 1
creator := sa.from
createdTx := sa.txHash
cols = "(?,?,?,?,?)"
vals = append(vals, sa.contractAddress, accountType, creator, createdTx, false)
count = 1
}
return cols, vals, count, nil
}
func MakeTxMappingRow(tm TxMapArguments) (cols string, vals []interface{}, count int, err error) {
if tm.senderTxHash != "" {
cols = "(?,?)"
vals = append(vals, tm.senderTxHash, tm.txHash)
count = 1
}
return cols, vals, count, nil
}