Skip to content

Commit 86661de

Browse files
committed
Fixed tests and bloom
1 parent c21293c commit 86661de

3 files changed

Lines changed: 35 additions & 19 deletions

File tree

‎core/types/bloom9.go‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,40 @@ func CreateBloom(receipts Receipts) Bloom {
2020
func LogsBloom(logs state.Logs) *big.Int {
2121
bin := new(big.Int)
2222
for _, log := range logs {
23-
data := make([]common.Hash, len(log.Topics())+1)
24-
data[0] = log.Address().Hash()
23+
data := make([]common.Hash, len(log.Topics()))
24+
bin.Or(bin, bloom9(log.Address().Bytes()))
2525

2626
for i, topic := range log.Topics() {
27-
data[i+1] = topic
27+
data[i] = topic
2828
}
2929

3030
for _, b := range data {
31-
bin.Or(bin, bloom9(crypto.Sha3(b[:])))
31+
bin.Or(bin, bloom9(b[:]))
3232
}
3333
}
3434

3535
return bin
3636
}
3737

3838
func bloom9(b []byte) *big.Int {
39+
b = crypto.Sha3(b[:])
40+
3941
r := new(big.Int)
4042

4143
for i := 0; i < 6; i += 2 {
4244
t := big.NewInt(1)
43-
//b := uint(b[i+1]) + 512*(uint(b[i])&1)
44-
b := (uint(b[i+1]) + (uint(b[i]) << 8)) & 511
45+
b := (uint(b[i+1]) + (uint(b[i]) << 8)) & 2047
4546
r.Or(r, t.Lsh(t, b))
4647
}
4748

4849
return r
4950
}
5051

52+
var Bloom9 = bloom9
53+
5154
func BloomLookup(bin Bloom, topic common.Hash) bool {
5255
bloom := bin.Big()
53-
cmp := bloom9(crypto.Sha3(topic[:]))
56+
cmp := bloom9(topic[:])
5457

5558
return bloom.And(bloom, cmp).Cmp(cmp) == 0
5659
}

‎core/types/common.go‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package types
22

33
import (
4+
"fmt"
45
"math/big"
56

67
"github.com/ethereum/go-ethereum/common"
@@ -10,7 +11,9 @@ type BlockProcessor interface {
1011
Process(*Block) (*big.Int, error)
1112
}
1213

13-
type Bloom [256]byte
14+
const bloomLength = 256
15+
16+
type Bloom [bloomLength]byte
1417

1518
func BytesToBloom(b []byte) Bloom {
1619
var bloom Bloom
@@ -19,13 +22,13 @@ func BytesToBloom(b []byte) Bloom {
1922
}
2023

2124
func (b *Bloom) SetBytes(d []byte) {
22-
if len(b) > len(d) {
23-
panic("bloom bytes too big")
25+
if len(b) < len(d) {
26+
panic(fmt.Sprintf("bloom bytes too big %d %d", len(b), len(d)))
2427
}
2528

2629
// reverse loop
2730
for i := len(d) - 1; i >= 0; i-- {
28-
b[i] = b[i]
31+
b[bloomLength-len(d)+i] = b[i]
2932
}
3033
}
3134

‎tests/vm/gh_test.go‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package vm
22

33
import (
44
"bytes"
5-
"fmt"
65
"math/big"
76
"strconv"
87
"testing"
@@ -82,9 +81,6 @@ func RunVmTest(p string, t *testing.T) {
8281
helper.CreateFileTests(t, p, &tests)
8382

8483
for name, test := range tests {
85-
if name != "log2_nonEmptyMem" {
86-
continue
87-
}
8884
db, _ := ethdb.NewMemDatabase()
8985
statedb := state.New(common.Hash{}, db)
9086
for addr, account := range test.Pre {
@@ -172,12 +168,26 @@ func RunVmTest(p string, t *testing.T) {
172168
if len(test.Logs) != len(logs) {
173169
t.Errorf("log length mismatch. Expected %d, got %d", len(test.Logs), len(logs))
174170
} else {
175-
fmt.Println("A", test.Logs)
176-
fmt.Println("B", logs)
177171
for i, log := range test.Logs {
172+
if common.HexToAddress(log.AddressF) != logs[i].Address() {
173+
t.Errorf("'%s' log address expected %v got %x", name, log.AddressF, logs[i].Address())
174+
}
175+
176+
if !bytes.Equal(logs[i].Data(), helper.FromHex(log.DataF)) {
177+
t.Errorf("'%s' log data expected %v got %x", name, log.DataF, logs[i].Data())
178+
}
179+
180+
if len(log.TopicsF) != len(logs[i].Topics()) {
181+
t.Errorf("'%s' log topics length expected %d got %d", name, len(log.TopicsF), logs[i].Topics())
182+
} else {
183+
for j, topic := range log.TopicsF {
184+
if common.HexToHash(topic) != logs[i].Topics()[j] {
185+
t.Errorf("'%s' log topic[%d] expected %v got %x", name, j, topic, logs[i].Topics()[j])
186+
}
187+
}
188+
}
178189
genBloom := common.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 256)
179-
fmt.Println("A BLOOM", log.BloomF)
180-
fmt.Printf("B BLOOM %x\n", genBloom)
190+
181191
if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) {
182192
t.Errorf("'%s' bloom mismatch", name)
183193
}

0 commit comments

Comments
 (0)