-
Notifications
You must be signed in to change notification settings - Fork 0
/
order_keeper.go
177 lines (162 loc) · 4.6 KB
/
order_keeper.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
package keeper
import (
"errors"
"fmt"
sdk "github.com/ci123chain/ci123chain/pkg/abci/types"
"github.com/ci123chain/ci123chain/pkg/account"
"github.com/ci123chain/ci123chain/pkg/order/types"
"github.com/ci123chain/ci123chain/pkg/redis"
"github.com/tendermint/tendermint/libs/log"
)
type OrderKeeper struct {
Cdb *redis.RedisDB//*couchdb.GoCouchDB
StoreKey sdk.StoreKey
AccountKeeper account.AccountKeeper
}
func NewOrderKeeper(cdb *redis.RedisDB, key sdk.StoreKey, ak account.AccountKeeper) OrderKeeper {
return OrderKeeper{
Cdb: cdb,
StoreKey: key,
AccountKeeper:ak,
}
}
func (k OrderKeeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
func (ok *OrderKeeper) WaitForReady(ctx sdk.Context) {
for {
//ok.Logger(ctx).Info("Stucking on waitForReady")
orderbook, err := ok.GetOrderBook(ctx)
if err != nil {
if err.Error() != types.NoOrderBookErr {
panic(err)
} else {
//time.Sleep(types.SleepTime)
continue
}
}
if ok.isReady(ctx, orderbook, ctx.ChainID(), ctx.BlockHeight()) {
ok.UpdateOrderBook(ctx, orderbook, nil)
return
}
//time.Sleep(types.SleepTime)
if err != nil {
panic(err)
}
}
}
func (ok *OrderKeeper) UpdateOrderBook(ctx sdk.Context, orderbook types.OrderBook, actions *types.Actions) {
if actions != nil {
name := actions.Name
for _,v := range orderbook.Lists{
if v.Name == name{
return
}
}
for _,v := range orderbook.Actions{
if v.Name == name{
return
}
}
orderbook.Actions = append(orderbook.Actions, *actions)
}
for i := 0; i < len(orderbook.Lists); i++ {
if orderbook.Lists[i].Name == ctx.ChainID(){
orderbook.Lists[i].Height = ctx.BlockHeight()
orderbook.Current.Index = i
orderbook.Current.State = types.StateCommitting
break
}
}
//handler actions
var deleteIndex []int
if orderbook.Current.Index == 0 && orderbook.Actions != nil {
for k, v := range orderbook.Actions {
if v.Type == types.OpADD && ctx.BlockHeight() == v.Height {
list := types.Lists{
Name: v.Name,
Height: 0,
}
orderbook.Lists = append(orderbook.Lists, list)
deleteIndex = append(deleteIndex, k)
}
}
}
for k, v := range deleteIndex{
length := len(orderbook.Actions)
if length - 1 > 0 {
orderbook.Actions = append(orderbook.Actions[:v-k],orderbook.Actions[v-k+1:]...)
} else {
orderbook.Actions = nil
}
}
ok.SetOrderBook(ctx, orderbook)
return
}
func (ok *OrderKeeper) GetOrderBook(ctx sdk.Context) (types.OrderBook, error) {
//store := ctx.KVStore(ok.StoreKey).Latest([]string{types.OrderBookKey})
store := ctx.KVStore(ok.StoreKey)
var orderbook types.OrderBook
isExist := ok.ExistOrderBook(ctx)
if !isExist {
return orderbook, errors.New(types.NoOrderBookErr)
}
bz := store.Get([]byte(types.OrderBookKey))
err := types.ModuleCdc.UnmarshalJSON(bz, &orderbook)
return orderbook, err
}
func (ok *OrderKeeper) ExistOrderBook(ctx sdk.Context) bool {
//store := ctx.KVStore(ok.StoreKey).Latest([]string{types.OrderBookKey})
store := ctx.KVStore(ok.StoreKey)
bz := store.Get([]byte(types.OrderBookKey))
if len(bz) > 0 {
return true
}
return false
}
func (ok *OrderKeeper) SetOrderBook(ctx sdk.Context, orderbook types.OrderBook) {
store := ctx.KVStore(ok.StoreKey)
bz, err := types.ModuleCdc.MarshalJSON(orderbook)
if err != nil {
panic(err)
}
store.Set([]byte(types.OrderBookKey), bz)
}
func (ok *OrderKeeper) isReady(ctx sdk.Context, orderbook types.OrderBook, shardID string, height int64) bool {
if orderbook.Current.State == types.StateInit {
if orderbook.Lists[0].Name == shardID {
return true
} else {
return false
}
}
//handle crash
if orderbook.Lists[orderbook.Current.Index].Name == shardID &&
orderbook.Current.State == types.StateCommitting {
if orderbook.Lists[orderbook.Current.Index].Height + 1 == height {
orderbook.Current.State = types.StateDone
//orderBytes, _ := types.ModuleCdc.MarshalJSON(orderbook)
//cdb := dbm.NewPrefixDB(ok.Cdb, []byte("s/k:"+ok.StoreKey.Name()+"/"))
//cdb.Set([]byte(types.OrderBookKey), orderBytes)
ok.SetOrderBook(ctx, orderbook)
return false
}
}
var nextIndex int
if orderbook.Current.Index == len(orderbook.Lists) - 1 {
nextIndex = 0
} else {
nextIndex = orderbook.Current.Index + 1
}
if orderbook.Lists[nextIndex].Height + 1 == height &&
orderbook.Current.State == types.StateDone &&
orderbook.Lists[nextIndex].Name == shardID {
return true
}else if orderbook.Lists[nextIndex].Height == 0 &&
orderbook.Current.State == types.StateDone &&
orderbook.Lists[nextIndex].Name == shardID {
return true
}else {
return false
}
}