Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
0015ce1e
Commit
0015ce1e
authored
Oct 07, 2014
by
obscuren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
kick of bad peers
parent
677836cb
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
54 additions
and
77 deletions
+54
-77
block_pool.go
block_pool.go
+36
-52
filter.go
ethchain/filter.go
+1
-1
js_types.go
ethpipe/js_types.go
+2
-2
list.go
ethutil/list.go
+9
-2
peer.go
peer.go
+6
-20
No files found.
block_pool.go
View file @
0015ce1e
...
@@ -3,6 +3,7 @@ package eth
...
@@ -3,6 +3,7 @@ package eth
import
(
import
(
"bytes"
"bytes"
"container/list"
"container/list"
"fmt"
"math"
"math"
"math/big"
"math/big"
"sync"
"sync"
...
@@ -29,8 +30,8 @@ type BlockPool struct {
...
@@ -29,8 +30,8 @@ type BlockPool struct {
eth
*
Ethereum
eth
*
Ethereum
hash
Pool
[][]
byte
hash
es
[][]
byte
pool
map
[
string
]
*
block
pool
map
[
string
]
*
block
td
*
big
.
Int
td
*
big
.
Int
quit
chan
bool
quit
chan
bool
...
@@ -53,12 +54,12 @@ func NewBlockPool(eth *Ethereum) *BlockPool {
...
@@ -53,12 +54,12 @@ func NewBlockPool(eth *Ethereum) *BlockPool {
}
}
func
(
self
*
BlockPool
)
Len
()
int
{
func
(
self
*
BlockPool
)
Len
()
int
{
return
len
(
self
.
hash
Pool
)
return
len
(
self
.
hash
es
)
}
}
func
(
self
*
BlockPool
)
Reset
()
{
func
(
self
*
BlockPool
)
Reset
()
{
self
.
pool
=
make
(
map
[
string
]
*
block
)
self
.
pool
=
make
(
map
[
string
]
*
block
)
self
.
hash
Pool
=
nil
self
.
hash
es
=
nil
}
}
func
(
self
*
BlockPool
)
HasLatestHash
()
bool
{
func
(
self
*
BlockPool
)
HasLatestHash
()
bool
{
...
@@ -88,6 +89,10 @@ func (self *BlockPool) FetchHashes(peer *Peer) bool {
...
@@ -88,6 +89,10 @@ func (self *BlockPool) FetchHashes(peer *Peer) bool {
if
(
self
.
peer
==
nil
&&
peer
.
td
.
Cmp
(
highestTd
)
>=
0
)
||
(
self
.
peer
!=
nil
&&
peer
.
td
.
Cmp
(
self
.
peer
.
td
)
>=
0
)
||
self
.
peer
==
peer
{
if
(
self
.
peer
==
nil
&&
peer
.
td
.
Cmp
(
highestTd
)
>=
0
)
||
(
self
.
peer
!=
nil
&&
peer
.
td
.
Cmp
(
self
.
peer
.
td
)
>=
0
)
||
self
.
peer
==
peer
{
if
self
.
peer
!=
peer
{
if
self
.
peer
!=
peer
{
poollogger
.
Debugf
(
"Found better suitable peer (%v vs %v)
\n
"
,
self
.
td
,
peer
.
td
)
poollogger
.
Debugf
(
"Found better suitable peer (%v vs %v)
\n
"
,
self
.
td
,
peer
.
td
)
if
self
.
peer
!=
nil
{
self
.
peer
.
doneFetchingHashes
=
true
}
}
}
self
.
peer
=
peer
self
.
peer
=
peer
...
@@ -114,7 +119,7 @@ func (self *BlockPool) AddHash(hash []byte, peer *Peer) {
...
@@ -114,7 +119,7 @@ func (self *BlockPool) AddHash(hash []byte, peer *Peer) {
if
self
.
pool
[
string
(
hash
)]
==
nil
{
if
self
.
pool
[
string
(
hash
)]
==
nil
{
self
.
pool
[
string
(
hash
)]
=
&
block
{
peer
,
nil
,
nil
,
time
.
Now
(),
0
}
self
.
pool
[
string
(
hash
)]
=
&
block
{
peer
,
nil
,
nil
,
time
.
Now
(),
0
}
self
.
hash
Pool
=
append
([][]
byte
{
hash
},
self
.
hashPool
...
)
self
.
hash
es
=
append
([][]
byte
{
hash
},
self
.
hashes
...
)
}
}
}
}
...
@@ -127,9 +132,12 @@ func (self *BlockPool) Add(b *ethchain.Block, peer *Peer) {
...
@@ -127,9 +132,12 @@ func (self *BlockPool) Add(b *ethchain.Block, peer *Peer) {
if
self
.
pool
[
hash
]
==
nil
&&
!
self
.
eth
.
BlockChain
()
.
HasBlock
(
b
.
Hash
())
{
if
self
.
pool
[
hash
]
==
nil
&&
!
self
.
eth
.
BlockChain
()
.
HasBlock
(
b
.
Hash
())
{
poollogger
.
Infof
(
"Got unrequested block (%x...)
\n
"
,
hash
[
0
:
4
])
poollogger
.
Infof
(
"Got unrequested block (%x...)
\n
"
,
hash
[
0
:
4
])
self
.
hash
Pool
=
append
(
self
.
hashPool
,
b
.
Hash
())
self
.
hash
es
=
append
(
self
.
hashes
,
b
.
Hash
())
self
.
pool
[
hash
]
=
&
block
{
peer
,
peer
,
b
,
time
.
Now
(),
0
}
self
.
pool
[
hash
]
=
&
block
{
peer
,
peer
,
b
,
time
.
Now
(),
0
}
fmt
.
Println
(
"1."
,
!
self
.
eth
.
BlockChain
()
.
HasBlock
(
b
.
PrevHash
),
ethutil
.
Bytes2Hex
(
b
.
Hash
()[
0
:
4
]),
ethutil
.
Bytes2Hex
(
b
.
PrevHash
[
0
:
4
]))
fmt
.
Println
(
"2."
,
self
.
pool
[
string
(
b
.
PrevHash
)]
==
nil
)
fmt
.
Println
(
"3."
,
!
self
.
fetchingHashes
)
if
!
self
.
eth
.
BlockChain
()
.
HasBlock
(
b
.
PrevHash
)
&&
self
.
pool
[
string
(
b
.
PrevHash
)]
==
nil
&&
!
self
.
fetchingHashes
{
if
!
self
.
eth
.
BlockChain
()
.
HasBlock
(
b
.
PrevHash
)
&&
self
.
pool
[
string
(
b
.
PrevHash
)]
==
nil
&&
!
self
.
fetchingHashes
{
poollogger
.
Infof
(
"Unknown chain, requesting (%x...)
\n
"
,
b
.
PrevHash
[
0
:
4
])
poollogger
.
Infof
(
"Unknown chain, requesting (%x...)
\n
"
,
b
.
PrevHash
[
0
:
4
])
peer
.
QueueMessage
(
ethwire
.
NewMessage
(
ethwire
.
MsgGetBlockHashesTy
,
[]
interface
{}{
b
.
Hash
(),
uint32
(
256
)}))
peer
.
QueueMessage
(
ethwire
.
NewMessage
(
ethwire
.
MsgGetBlockHashesTy
,
[]
interface
{}{
b
.
Hash
(),
uint32
(
256
)}))
...
@@ -145,28 +153,10 @@ func (self *BlockPool) Remove(hash []byte) {
...
@@ -145,28 +153,10 @@ func (self *BlockPool) Remove(hash []byte) {
self
.
mut
.
Lock
()
self
.
mut
.
Lock
()
defer
self
.
mut
.
Unlock
()
defer
self
.
mut
.
Unlock
()
self
.
hash
Pool
=
ethutil
.
DeleteFromByteSlice
(
self
.
hashPool
,
hash
)
self
.
hash
es
=
ethutil
.
DeleteFromByteSlice
(
self
.
hashes
,
hash
)
delete
(
self
.
pool
,
string
(
hash
))
delete
(
self
.
pool
,
string
(
hash
))
}
}
func
(
self
*
BlockPool
)
ProcessCanonical
(
f
func
(
block
*
ethchain
.
Block
))
(
procAmount
int
)
{
blocks
:=
self
.
Blocks
()
ethchain
.
BlockBy
(
ethchain
.
Number
)
.
Sort
(
blocks
)
for
_
,
block
:=
range
blocks
{
if
self
.
eth
.
BlockChain
()
.
HasBlock
(
block
.
PrevHash
)
{
procAmount
++
f
(
block
)
self
.
Remove
(
block
.
Hash
())
}
}
return
}
func
(
self
*
BlockPool
)
DistributeHashes
()
{
func
(
self
*
BlockPool
)
DistributeHashes
()
{
self
.
mut
.
Lock
()
self
.
mut
.
Lock
()
defer
self
.
mut
.
Unlock
()
defer
self
.
mut
.
Unlock
()
...
@@ -178,8 +168,8 @@ func (self *BlockPool) DistributeHashes() {
...
@@ -178,8 +168,8 @@ func (self *BlockPool) DistributeHashes() {
)
)
num
:=
int
(
math
.
Min
(
float64
(
amount
),
float64
(
len
(
self
.
pool
))))
num
:=
int
(
math
.
Min
(
float64
(
amount
),
float64
(
len
(
self
.
pool
))))
for
i
,
j
:=
0
,
0
;
i
<
len
(
self
.
hash
Pool
)
&&
j
<
num
;
i
++
{
for
i
,
j
:=
0
,
0
;
i
<
len
(
self
.
hash
es
)
&&
j
<
num
;
i
++
{
hash
:=
self
.
hash
Pool
[
i
]
hash
:=
self
.
hash
es
[
i
]
item
:=
self
.
pool
[
string
(
hash
)]
item
:=
self
.
pool
[
string
(
hash
)]
if
item
!=
nil
&&
item
.
block
==
nil
{
if
item
!=
nil
&&
item
.
block
==
nil
{
...
@@ -193,7 +183,7 @@ func (self *BlockPool) DistributeHashes() {
...
@@ -193,7 +183,7 @@ func (self *BlockPool) DistributeHashes() {
peer
=
item
.
from
peer
=
item
.
from
}
else
{
}
else
{
// Remove it
// Remove it
self
.
hash
Pool
=
ethutil
.
DeleteFromByteSlice
(
self
.
hashPool
,
hash
)
self
.
hash
es
=
ethutil
.
DeleteFromByteSlice
(
self
.
hashes
,
hash
)
delete
(
self
.
pool
,
string
(
hash
))
delete
(
self
.
pool
,
string
(
hash
))
}
}
}
else
if
lastFetchFailed
||
item
.
peer
==
nil
{
}
else
if
lastFetchFailed
||
item
.
peer
==
nil
{
...
@@ -250,17 +240,31 @@ out:
...
@@ -250,17 +240,31 @@ out:
}
}
})
})
self
.
DistributeHashes
()
if
len
(
self
.
hashes
)
>
0
{
self
.
DistributeHashes
()
}
if
self
.
ChainLength
<
len
(
self
.
hash
Pool
)
{
if
self
.
ChainLength
<
len
(
self
.
hash
es
)
{
self
.
ChainLength
=
len
(
self
.
hash
Pool
)
self
.
ChainLength
=
len
(
self
.
hash
es
)
}
}
/*
if !self.fetchingHashes {
blocks := self.Blocks()
ethchain.BlockBy(ethchain.Number).Sort(blocks)
if len(blocks) > 0 {
if !self.eth.BlockChain().HasBlock(b.PrevHash) && self.pool[string(b.PrevHash)] == nil && !self.fetchingHashes {
}
}
}
*/
}
}
}
}
}
}
func
(
self
*
BlockPool
)
chainThread
()
{
func
(
self
*
BlockPool
)
chainThread
()
{
procTimer
:=
time
.
NewTicker
(
10
00
*
time
.
Millisecond
)
procTimer
:=
time
.
NewTicker
(
5
00
*
time
.
Millisecond
)
out
:
out
:
for
{
for
{
select
{
select
{
...
@@ -294,14 +298,8 @@ out:
...
@@ -294,14 +298,8 @@ out:
}
}
}
}
if
len
(
blocks
)
>
0
{
self
.
eth
.
Eventer
()
.
Post
(
"blocks"
,
blocks
)
}
var
err
error
var
err
error
for
i
,
block
:=
range
blocks
{
for
i
,
block
:=
range
blocks
{
//self.eth.Eventer().Post("block", block)
err
=
self
.
eth
.
StateManager
()
.
Process
(
block
,
false
)
err
=
self
.
eth
.
StateManager
()
.
Process
(
block
,
false
)
if
err
!=
nil
{
if
err
!=
nil
{
poollogger
.
Infoln
(
err
)
poollogger
.
Infoln
(
err
)
...
@@ -318,10 +316,6 @@ out:
...
@@ -318,10 +316,6 @@ out:
if
err
!=
nil
{
if
err
!=
nil
{
self
.
Reset
()
self
.
Reset
()
// Remove this bad chain
for
_
,
block
:=
range
blocks
{
self
.
Remove
(
block
.
Hash
())
}
poollogger
.
Debugf
(
"Punishing peer for supplying bad chain (%v)
\n
"
,
self
.
peer
.
conn
.
RemoteAddr
())
poollogger
.
Debugf
(
"Punishing peer for supplying bad chain (%v)
\n
"
,
self
.
peer
.
conn
.
RemoteAddr
())
// This peer gave us bad hashes and made us fetch a bad chain, therefor he shall be punished.
// This peer gave us bad hashes and made us fetch a bad chain, therefor he shall be punished.
...
@@ -330,16 +324,6 @@ out:
...
@@ -330,16 +324,6 @@ out:
self
.
td
=
ethutil
.
Big0
self
.
td
=
ethutil
.
Big0
self
.
peer
=
nil
self
.
peer
=
nil
}
}
/*
// Handle in batches of 4k
//max := int(math.Min(4000, float64(len(blocks))))
for _, block := range blocks {
self.eth.Eventer().Post("block", block)
self.Remove(block.Hash())
}
*/
}
}
}
}
}
}
ethchain/filter.go
View file @
0015ce1e
...
@@ -216,7 +216,7 @@ func (self *Filter) bloomFilter(block *Block) bool {
...
@@ -216,7 +216,7 @@ func (self *Filter) bloomFilter(block *Block) bool {
fk
:=
append
([]
byte
(
"bloom"
),
block
.
Hash
()
...
)
fk
:=
append
([]
byte
(
"bloom"
),
block
.
Hash
()
...
)
bin
,
err
:=
self
.
eth
.
Db
()
.
Get
(
fk
)
bin
,
err
:=
self
.
eth
.
Db
()
.
Get
(
fk
)
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
err
)
fmt
.
Println
(
err
)
}
}
bloom
:=
NewBloomFilter
(
bin
)
bloom
:=
NewBloomFilter
(
bin
)
...
...
ethpipe/js_types.go
View file @
0015ce1e
...
@@ -33,9 +33,9 @@ func NewJSBlock(block *ethchain.Block) *JSBlock {
...
@@ -33,9 +33,9 @@ func NewJSBlock(block *ethchain.Block) *JSBlock {
return
&
JSBlock
{}
return
&
JSBlock
{}
}
}
var
ptxs
[]
JSTransaction
var
ptxs
[]
*
JSTransaction
for
_
,
tx
:=
range
block
.
Transactions
()
{
for
_
,
tx
:=
range
block
.
Transactions
()
{
ptxs
=
append
(
ptxs
,
*
NewJSTx
(
tx
,
block
.
State
()))
ptxs
=
append
(
ptxs
,
NewJSTx
(
tx
,
block
.
State
()))
}
}
list
:=
ethutil
.
NewList
(
ptxs
)
list
:=
ethutil
.
NewList
(
ptxs
)
...
...
ethutil/list.go
View file @
0015ce1e
...
@@ -3,12 +3,14 @@ package ethutil
...
@@ -3,12 +3,14 @@ package ethutil
import
(
import
(
"encoding/json"
"encoding/json"
"reflect"
"reflect"
"sync"
)
)
// The list type is an anonymous slice handler which can be used
// The list type is an anonymous slice handler which can be used
// for containing any slice type to use in an environment which
// for containing any slice type to use in an environment which
// does not support slice types (e.g., JavaScript, QML)
// does not support slice types (e.g., JavaScript, QML)
type
List
struct
{
type
List
struct
{
mut
sync
.
Mutex
val
interface
{}
val
interface
{}
list
reflect
.
Value
list
reflect
.
Value
Length
int
Length
int
...
@@ -21,7 +23,7 @@ func NewList(t interface{}) *List {
...
@@ -21,7 +23,7 @@ func NewList(t interface{}) *List {
panic
(
"list container initialized with a non-slice type"
)
panic
(
"list container initialized with a non-slice type"
)
}
}
return
&
List
{
t
,
list
,
list
.
Len
()}
return
&
List
{
sync
.
Mutex
{},
t
,
list
,
list
.
Len
()}
}
}
func
EmptyList
()
*
List
{
func
EmptyList
()
*
List
{
...
@@ -30,8 +32,10 @@ func EmptyList() *List {
...
@@ -30,8 +32,10 @@ func EmptyList() *List {
// Get N element from the embedded slice. Returns nil if OOB.
// Get N element from the embedded slice. Returns nil if OOB.
func
(
self
*
List
)
Get
(
i
int
)
interface
{}
{
func
(
self
*
List
)
Get
(
i
int
)
interface
{}
{
if
self
.
list
.
Len
()
>
i
{
if
self
.
list
.
Len
()
>
i
{
self
.
mut
.
Lock
()
defer
self
.
mut
.
Unlock
()
i
:=
self
.
list
.
Index
(
i
)
.
Interface
()
i
:=
self
.
list
.
Index
(
i
)
.
Interface
()
return
i
return
i
...
@@ -51,6 +55,9 @@ func (self *List) GetAsJson(i int) interface{} {
...
@@ -51,6 +55,9 @@ func (self *List) GetAsJson(i int) interface{} {
// Appends value at the end of the slice. Panics when incompatible value
// Appends value at the end of the slice. Panics when incompatible value
// is given.
// is given.
func
(
self
*
List
)
Append
(
v
interface
{})
{
func
(
self
*
List
)
Append
(
v
interface
{})
{
self
.
mut
.
Lock
()
defer
self
.
mut
.
Unlock
()
self
.
list
=
reflect
.
Append
(
self
.
list
,
reflect
.
ValueOf
(
v
))
self
.
list
=
reflect
.
Append
(
self
.
list
,
reflect
.
ValueOf
(
v
))
self
.
Length
=
self
.
list
.
Len
()
self
.
Length
=
self
.
list
.
Len
()
}
}
...
...
peer.go
View file @
0015ce1e
...
@@ -516,10 +516,11 @@ func (p *Peer) HandleInbound() {
...
@@ -516,10 +516,11 @@ func (p *Peer) HandleInbound() {
blockPool
.
AddHash
(
hash
,
p
)
blockPool
.
AddHash
(
hash
,
p
)
}
}
if
!
foundCommonHash
&&
msg
.
Data
.
Len
()
!=
0
{
if
!
foundCommonHash
{
if
!
p
.
FetchHashes
()
{
//if !p.FetchHashes() {
p
.
doneFetchingHashes
=
true
// p.doneFetchingHashes = true
}
//}
p
.
FetchHashes
()
}
else
{
}
else
{
peerlogger
.
Infof
(
"Found common hash (%x...)
\n
"
,
p
.
lastReceivedHash
[
0
:
4
])
peerlogger
.
Infof
(
"Found common hash (%x...)
\n
"
,
p
.
lastReceivedHash
[
0
:
4
])
p
.
doneFetchingHashes
=
true
p
.
doneFetchingHashes
=
true
...
@@ -533,8 +534,6 @@ func (p *Peer) HandleInbound() {
...
@@ -533,8 +534,6 @@ func (p *Peer) HandleInbound() {
it
:=
msg
.
Data
.
NewIterator
()
it
:=
msg
.
Data
.
NewIterator
()
for
it
.
Next
()
{
for
it
.
Next
()
{
block
:=
ethchain
.
NewBlockFromRlpValue
(
it
.
Value
())
block
:=
ethchain
.
NewBlockFromRlpValue
(
it
.
Value
())
//fmt.Printf("%v %x - %x\n", block.Number, block.Hash()[0:4], block.PrevHash[0:4])
blockPool
.
Add
(
block
,
p
)
blockPool
.
Add
(
block
,
p
)
p
.
lastBlockReceived
=
time
.
Now
()
p
.
lastBlockReceived
=
time
.
Now
()
...
@@ -557,21 +556,8 @@ func (self *Peer) FetchBlocks(hashes [][]byte) {
...
@@ -557,21 +556,8 @@ func (self *Peer) FetchBlocks(hashes [][]byte) {
func
(
self
*
Peer
)
FetchHashes
()
bool
{
func
(
self
*
Peer
)
FetchHashes
()
bool
{
blockPool
:=
self
.
ethereum
.
blockPool
blockPool
:=
self
.
ethereum
.
blockPool
return
blockPool
.
FetchHashes
(
self
)
/*
if self.td.Cmp(self.ethereum.HighestTDPeer()) >= 0 {
blockPool.td = self.td
if !blockPool.HasLatestHash() {
return
blockPool
.
FetchHashes
(
self
)
self.doneFetchingHashes = false
const amount = 256
peerlogger.Debugf("Fetching hashes (%d)\n", amount)
self.QueueMessage(ethwire.NewMessage(ethwire.MsgGetBlockHashesTy, []interface{}{self.lastReceivedHash, uint32(amount)}))
}
}
*/
}
}
func
(
self
*
Peer
)
FetchingHashes
()
bool
{
func
(
self
*
Peer
)
FetchingHashes
()
bool
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment