This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
/
transaction.go
102 lines (79 loc) 路 2.37 KB
/
transaction.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
package snapshot
import (
"github.com/ethereum/go-ethereum/common"
"github.com/itzmeanjan/ette/app/data"
_db "github.com/itzmeanjan/ette/app/db"
pb "github.com/itzmeanjan/ette/app/pb"
"gorm.io/gorm"
)
// TransactionToProtoBuf - Creating proto buffer compatible data
// format for transaction data, which can be easily serialized & deserialized
// for taking snapshot and restoring from it
func TransactionToProtoBuf(tx *data.Transaction, db *gorm.DB) *pb.Transaction {
_tx := &pb.Transaction{
Hash: tx.Hash,
From: tx.From,
To: tx.To,
Contract: tx.Contract,
Value: tx.Contract,
Data: tx.Data,
Gas: tx.Gas,
GasPrice: tx.GasPrice,
Cost: tx.Cost,
Nonce: tx.Nonce,
State: tx.State,
BlockHash: tx.BlockHash,
}
events := _db.GetEventsByTransactionHash(db, common.HexToHash(tx.Hash))
if events == nil {
return _tx
}
_tx.Events = EventsToProtoBuf(events)
return _tx
}
// TransactionsToProtoBuf - Creating proto buffer compatible data
// format for transactions data, which can be easily serialized & deserialized
// for taking snapshot and restoring from it
func TransactionsToProtoBuf(txs *data.Transactions, db *gorm.DB) []*pb.Transaction {
_txs := make([]*pb.Transaction, len(txs.Transactions))
for i := 0; i < len(txs.Transactions); i++ {
_txs[i] = TransactionToProtoBuf(txs.Transactions[i], db)
}
return _txs
}
// ProtoBufToTransaction - Required while restoring from snapshot i.e. attempting to put
// whole block data into database
func ProtoBufToTransaction(tx *pb.Transaction) *_db.PackedTransaction {
_tx := &_db.Transactions{
Hash: tx.Hash,
From: tx.From,
To: tx.To,
Contract: tx.Contract,
Value: tx.Contract,
Data: tx.Data,
Gas: tx.Gas,
GasPrice: tx.GasPrice,
Cost: tx.Cost,
Nonce: tx.Nonce,
State: tx.State,
BlockHash: tx.BlockHash,
}
if tx.Events == nil {
return &_db.PackedTransaction{
Tx: _tx,
}
}
return &_db.PackedTransaction{
Tx: _tx,
Events: ProtoBufToEvents(tx.Events),
}
}
// ProtoBufToTransactions - Required while restoring from snapshot i.e. attempting to put
// whole block data into database
func ProtoBufToTransactions(txs []*pb.Transaction) []*_db.PackedTransaction {
_txs := make([]*_db.PackedTransaction, len(txs))
for k, v := range txs {
_txs[k] = ProtoBufToTransaction(v)
}
return _txs
}