-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathfork.go
66 lines (53 loc) · 1.23 KB
/
fork.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
package loomchain
import (
"sort"
)
type forkRoute struct {
TxHandler
Height int64
}
type forkList []forkRoute
func (s forkList) Len() int {
return len(s)
}
func (s forkList) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s forkList) Less(i, j int) bool {
return s[i].Height < s[j].Height
}
type ForkRouter struct {
routes map[string]forkList
}
func NewForkRouter() *ForkRouter {
return &ForkRouter{
routes: make(map[string]forkList),
}
}
func (r *ForkRouter) Handle(chainID string, height int64, handler TxHandler) {
routes := r.routes[chainID]
found := sort.Search(len(routes), func(i int) bool {
return routes[i].Height >= height
})
if found < len(routes) && routes[found].Height == height {
panic("route already exists for given chain and height")
}
routes = append(routes, forkRoute{
TxHandler: handler,
Height: height,
})
sort.Sort(routes)
r.routes[chainID] = routes
}
func (r *ForkRouter) ProcessTx(state State, txBytes []byte, isCheckTx bool) (TxHandlerResult, error) {
block := state.Block()
routes := r.routes[block.ChainID]
var found TxHandler
for _, route := range routes {
if route.Height > block.Height {
break
}
found = route
}
return found.ProcessTx(state, txBytes, isCheckTx)
}