This repository has been archived by the owner on Jun 17, 2022. It is now read-only.
/
record.go
116 lines (98 loc) · 2.26 KB
/
record.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
package topicsdb
import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
)
type (
logrecBuilder struct {
types.Log
ID ID
conditions uint8
topicsCount uint8
ok chan struct{}
ready chan error
}
)
func newLogrecBuilder(logrec ID, conditions uint8, topicCount uint8) *logrecBuilder {
rec := &logrecBuilder{
Log: types.Log{
BlockNumber: logrec.BlockNumber(),
TxHash: logrec.TxHash(),
Index: logrec.Index(),
Topics: make([]common.Hash, topicCount),
},
ID: logrec,
conditions: conditions,
topicsCount: topicCount,
}
return rec
}
func (rec *logrecBuilder) Build() (r *types.Log, err error) {
if rec.ready != nil {
var complete bool
err, complete = <-rec.ready
if !complete {
return
}
}
r = &rec.Log
return
}
// MatchedWith count of conditions.
func (rec *logrecBuilder) MatchedWith(count uint8) {
if rec.conditions > count {
rec.conditions -= count
return
}
rec.conditions = 0
if rec.ok != nil {
rec.ok <- struct{}{}
}
}
// IsMatched if it is matched with the all conditions.
func (rec *logrecBuilder) IsMatched() bool {
return rec.conditions == 0
}
// SetOtherTopic appends topic.
func (rec *logrecBuilder) SetOtherTopic(pos uint8, topic common.Hash) {
if pos >= rec.topicsCount {
log.Crit("inconsistent table.Others", "param", "topicN")
}
var empty common.Hash
if rec.Topics[pos] != empty {
return
}
rec.Topics[pos] = topic
}
// Fetch log record's data.
func (rec *logrecBuilder) Fetch(
othersTable ethdb.Iteratee,
logrecTable ethdb.KeyValueReader,
) (err error) {
// others
it := othersTable.NewIterator(rec.ID.Bytes(), nil)
for it.Next() {
pos := extractTopicPos(it.Key())
topic := common.BytesToHash(it.Value())
rec.SetOtherTopic(pos, topic)
}
err = it.Error()
if err != nil {
return
}
it.Release()
// fields
buf, err := logrecTable.Get(rec.ID.Bytes())
if err != nil {
return
}
offset := 0
rec.Address = common.BytesToAddress(buf[offset : offset+common.AddressLength])
offset += common.AddressLength
rec.BlockHash = common.BytesToHash(buf[offset : offset+common.HashLength])
offset += common.HashLength
rec.Data = buf[offset:]
return
}