forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaddr_txs.go
207 lines (180 loc) · 5.4 KB
/
addr_txs.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
// Copyright 2015 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
package eth
import (
"database/sql"
"fmt"
"math/big"
"sync"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
_ "github.com/mattn/go-sqlite3"
)
const sqliteDBName = "addr_tx.db"
type AddrTxSyncer struct {
txDB *sql.DB
chainDB ethdb.Database
bc *core.BlockChain
shutdownChan chan bool
wg sync.WaitGroup
}
func NewAddrTxSyncer(datadir string, chainDB ethdb.Database, bc *core.BlockChain) (*AddrTxSyncer, error) {
s := AddrTxSyncer{}
s.chainDB = chainDB
s.bc = bc
s.shutdownChan = make(chan bool)
db, err := sql.Open("sqlite3", datadir+"/"+sqliteDBName)
if err != nil {
return nil, err
}
createStmt := `
CREATE TABLE IF NOT EXISTS txs (hash CHARACTER(40) PRIMARY KEY NOT NULL, blocknumber INT4, sender CHARACTER(40), recipient CHARACTER(40));
CREATE INDEX IF NOT EXISTS from_index ON txs (sender);
CREATE INDEX IF NOT EXISTS to_index ON txs (recipient);
`
_, err = db.Exec(createStmt)
if err != nil {
glog.V(logger.Error).Infof("Could not create sqlite db: %v", err)
return nil, err
}
s.txDB = db
// TODO: try out multiple sync threads and benchmark performance
return &s, nil
}
func (s *AddrTxSyncer) Stop() {
close(s.shutdownChan)
s.wg.Wait()
glog.V(logger.Info).Infof("Address Txs Syncer Stopped.\n")
}
func (s *AddrTxSyncer) ListTransactions(addr common.Address) ([]common.Hash, error) {
addrStr := common.Bytes2Hex(addr[:])
t0 := time.Now()
sqlStmt := fmt.Sprintf("SELECT hash FROM txs WHERE sender = '%s' OR recipient = '%s'", addrStr)
rows, err := s.txDB.Query(sqlStmt)
fmt.Printf("FUNKY: select: %v\n", time.Since(t0).String())
t0 = time.Now()
if err != nil {
return nil, err
}
var txHashes []common.Hash
for rows.Next() {
var txHash string
rows.Scan(&txHash)
//fmt.Printf("FUNKY: txHash: %v\n", txHash)
txHashes = append(txHashes, common.HexToHash(txHash))
}
rows.Close()
fmt.Printf("FUNKY: rows proc: %v\n", time.Since(t0).String())
//fmt.Printf("FUNKY: txHashes: %v\n", txHashes)
return txHashes, nil
}
func (s *AddrTxSyncer) SyncAddrTxs() error {
s.wg.Add(1)
defer s.wg.Done()
rows, err := s.txDB.Query("SELECT blocknumber FROM txs ORDER BY blocknumber LIMIT 1")
if err != nil {
return err
}
lastBlockNum := uint64(0)
if rows.Next() {
rows.Scan(&lastBlockNum)
}
rows.Close()
var headNumber *big.Int
var blockHash common.Hash
var block *types.Block
var bn *big.Int
blockHash = core.GetHeadBlockHash(s.chainDB)
headBlock := core.GetBlock(s.chainDB, blockHash)
headNumber = headBlock.Number()
if lastBlockNum == 0 {
block = headBlock
bn = headNumber
} else {
block = s.bc.GetBlockByNumber(lastBlockNum)
bn = new(big.Int).SetUint64(lastBlockNum)
}
glog.V(logger.Info).Infof("Loading addr_txs db, starting backwards traversal at block %v\n", bn)
t0 := time.Now()
progress := func() {
t0 = time.Now()
hnf := float64(headNumber.Uint64())
bnf := float64(bn.Uint64())
p := ((hnf - bnf) * 100) / hnf
glog.V(logger.Info).Infof("Loading addr_txs db... %.3f%c\n", p, '%')
}
progress()
for {
select {
case <-s.shutdownChan:
return nil
default:
}
if block == nil || bn.Cmp(common.Big0) == 0 {
glog.V(logger.Info).Infof("Loading addr_txs db... done.\n")
return nil
}
for _, tx := range block.Transactions() {
from, _ := tx.From() // already validated
h := tx.Hash()
err := insertTx(s.txDB, &h, bn, &from, tx.To())
if err != nil {
return err
}
}
if headNumber == nil {
headNumber = bn
}
//t0 := time.Now()
blockHash = block.ParentHash()
block = core.GetBlock(s.chainDB, blockHash)
bn = block.Number()
//fmt.Printf("FUNKY: GetBlock: %v\n", time.Since(t0).String())
if time.Since(t0) > 10*time.Second {
progress()
}
}
return nil
}
func insertTx(db *sql.DB, hash *common.Hash, blockNumber *big.Int, from, to *common.Address) error {
// no to addr in contract deployment txs
toStr := "NULL"
if to != nil {
toStr = common.Bytes2Hex(to[:])
}
// primary key collisions are ignored, can happen if interrupting
// sync - then all txs in the last block are re-inserted
sqlStmt :=
fmt.Sprintf("INSERT OR IGNORE INTO txs(hash, blocknumber, sender, recipient) VALUES('%s', '%v', '%s', '%s');",
common.Bytes2Hex(hash[:]),
blockNumber,
common.Bytes2Hex(from[:]),
toStr)
//fmt.Printf("FUNKY: sqlStmt:\n%s\n", sqlStmt)
//t0 := time.Now()
_, err := db.Exec(sqlStmt)
//fmt.Printf("FUNKY: INSERT: %v\n", time.Since(t0).String())
if err != nil {
glog.V(logger.Error).Infof("Could not insert tx into addr_txs db: %v", err)
return err
}
return nil
}