forked from ethereum/ecp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimporter.go
More file actions
114 lines (102 loc) · 3.66 KB
/
importer.go
File metadata and controls
114 lines (102 loc) · 3.66 KB
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
package main
import (
"github.com/codegangsta/cli"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type ImportMaster struct {
session *mgo.Session
ethereum *eth.Ethereum
txCollection *mgo.Collection
blockCollection *mgo.Collection
}
type Transaction struct {
TxHash string `bson:"tx_hash"`
Recipient string
From string
Amount string
Price string
GasLimit string `bson:"gas_limit"`
Payload []byte
BlockId *bson.ObjectId `bson:"block_id,omitempty"`
}
func (self *ImportMaster) parseTx(tx *types.Transaction, blockId *bson.ObjectId) *Transaction {
hash := tx.Hash().Hex()
from, err := tx.From()
if err != nil {
utils.Fatalf("Could not parse from address: %v", err)
}
var recipient string
if tx.Recipient != nil {
recipient = tx.Recipient.Hex()
}
txx := &Transaction{hash, recipient, from.Hex(), tx.Amount.String(), tx.Price.String(), tx.GasLimit.String(), tx.Payload, blockId}
return txx
}
type Block struct {
BlockHash string `bson:"block_hash"`
ParentHash string `bson:"parent_hash"`
UncleHash string `bson:"uncle_hash"`
Coinbase string `bson:"coin_base"`
Root string `bson:"root"`
TxHash string `bson:"tx_hash"`
ReceiptHash string `bson:"receipt_hash"`
Number string
Difficulty string
GasLimit string `bson:"gas_limit"`
GasUsed string `bson:"gas_used"`
Time uint64
TxAmount uint64 `bson:"tx_amount"`
Extra string `bson:"extra"`
Nonce string
StorageSize string `bson:"storage_size"`
MixDigest string `bson:"mix_digest"`
Processed bool `bson:"processed"`
Id *bson.ObjectId `bson:"_id,omitempty"`
}
func NewImporter(ctx *cli.Context) *ImportMaster {
importer := new(ImportMaster)
mongoUrl := ctx.String("mongo-url")
db := ctx.String("mongo-database")
glog.V(logger.Info).Infoln("Connecting to MongoDB db '", db, "'using", mongoUrl)
session, err := mgo.Dial(mongoUrl)
if err != nil {
panic(err)
}
importer.session = session
importer.txCollection = session.DB(db).C("transactions")
importer.blockCollection = session.DB(db).C("blocks")
return importer
}
func (self *ImportMaster) importBlock(block *types.Block) {
blockHash := block.Header().Hash().Hex()
txAmount := uint64(len(block.Transactions()))
glog.V(logger.Info).Infoln("Importing block", blockHash, "Hash with ", txAmount, "transactions")
extData := string(block.Header().Extra[:])
err := self.blockCollection.Insert(&Block{blockHash, block.ParentHash().Hex(), block.Header().UncleHash.Hex(), block.Header().Coinbase.Hex(), block.Header().Root.Hex(), block.Header().TxHash.Hex(), block.Header().ReceiptHash.Hex(), block.Header().Number.String(), block.Header().Difficulty.String(), block.Header().GasLimit.String(), block.Header().GasUsed.String(), block.Header().Time, txAmount, extData, string(block.Nonce()), block.Size().String(), block.Header().MixDigest.Hex(), false, nil})
if err != nil {
clilogger.Infoln(err)
}
result := Block{}
err = self.blockCollection.Find(bson.M{"block_hash": blockHash}).One(&result)
if err != nil {
utils.Fatalf("Could not find the block we just added, saving faild: %v", err)
}
for _, tx := range block.Transactions() {
self.importTx(tx, result.Id)
}
}
func (self *ImportMaster) importTx(tx *types.Transaction, blockId *bson.ObjectId) {
if glog.V(logger.Info) {
glog.Infoln("Importing tx", tx.Hash().Hex())
}
err := self.txCollection.Insert(self.parseTx(tx, blockId))
if err != nil {
clilogger.Infoln(err)
}
}