/
keeper.go
194 lines (159 loc) · 6.15 KB
/
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package timelock
import (
"fmt"
"sort"
"time"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/tendermint/tendermint/crypto"
tmlog "github.com/tendermint/tendermint/libs/log"
bnclog "github.com/bnb-chain/node/common/log"
)
const InitialRecordId = 1
var (
// bnb prefix address: bnb1hn8ym9xht925jkncjpf7lhjnax6z8nv24fv2yq
// tbnb prefix address: tbnb1hn8ym9xht925jkncjpf7lhjnax6z8nv2mu9wy3
TimeLockCoinsAccAddr = sdk.AccAddress(crypto.AddressHash([]byte("BinanceChainTimeLockCoins")))
)
type Keeper struct {
ck bank.Keeper
ak auth.AccountKeeper
storeKey sdk.StoreKey // The key used to access the store from the Context.
codespace sdk.CodespaceType
cdc *codec.Codec
logger tmlog.Logger
}
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, ck bank.Keeper, ak auth.AccountKeeper, codespace sdk.CodespaceType) Keeper {
logger := bnclog.With("module", "timelock")
return Keeper{
ck: ck,
ak: ak,
storeKey: key,
codespace: codespace,
cdc: cdc,
logger: logger,
}
}
func (keeper Keeper) setTimeLockRecord(ctx sdk.Context, addr sdk.AccAddress, record TimeLockRecord) {
store := ctx.KVStore(keeper.storeKey)
bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(record)
store.Set(KeyRecord(addr, record.Id), bz)
}
func (keeper Keeper) deleteTimeLockRecord(ctx sdk.Context, addr sdk.AccAddress, recordId int64) {
store := ctx.KVStore(keeper.storeKey)
store.Delete(KeyRecord(addr, recordId))
}
func (keeper Keeper) getTimeLockRecordsIterator(ctx sdk.Context, addr sdk.AccAddress) sdk.Iterator {
store := ctx.KVStore(keeper.storeKey)
return sdk.KVStorePrefixIterator(store, KeyRecordSubSpace(addr))
}
func (keeper Keeper) GetTimeLockRecord(ctx sdk.Context, addr sdk.AccAddress, recordId int64) (TimeLockRecord, bool) {
store := ctx.KVStore(keeper.storeKey)
bz := store.Get(KeyRecord(addr, recordId))
if bz == nil {
return TimeLockRecord{}, false
}
var record TimeLockRecord
keeper.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &record)
return record, true
}
func (keeper Keeper) GetTimeLockRecords(ctx sdk.Context, addr sdk.AccAddress) []TimeLockRecord {
var records []TimeLockRecord
iterator := keeper.getTimeLockRecordsIterator(ctx, addr)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
record := TimeLockRecord{}
keeper.cdc.MustUnmarshalBinaryLengthPrefixed(iterator.Value(), &record)
records = append(records, record)
}
sort.Sort(TimeLockRecords(records))
return records
}
func (kp *Keeper) GetTimeLockRecordIterator(ctx sdk.Context) (iterator store.Iterator) {
kvStore := ctx.KVStore(kp.storeKey)
return sdk.KVStorePrefixIterator(kvStore, []byte{})
}
func (keeper Keeper) getTimeLockId(ctx sdk.Context, from sdk.AccAddress) int64 {
acc := keeper.ak.GetAccount(ctx, from)
return acc.GetSequence()
}
func (keeper Keeper) TimeLock(ctx sdk.Context, from sdk.AccAddress, description string, amount sdk.Coins, lockTime time.Time) (TimeLockRecord, sdk.Error) {
if !lockTime.After(ctx.BlockHeader().Time.Add(MinLockTime)) {
return TimeLockRecord{}, ErrInvalidLockTime(DefaultCodespace,
fmt.Sprintf("lock time(%s) should be %d minute(s) after now(%s)", lockTime.UTC().String(),
MinLockTime/time.Minute, ctx.BlockHeader().Time.Add(MinLockTime).UTC().String()))
}
_, err := keeper.ck.SendCoins(ctx, from, TimeLockCoinsAccAddr, amount)
if err != nil {
return TimeLockRecord{}, err
}
recordId := keeper.getTimeLockId(ctx, from)
_, found := keeper.GetTimeLockRecord(ctx, from, recordId)
if found {
return TimeLockRecord{}, ErrTimeLockRecordAlreadyExist(DefaultCodespace, from, recordId)
}
record := TimeLockRecord{
Id: recordId,
Description: description,
Amount: amount,
LockTime: lockTime,
}
keeper.setTimeLockRecord(ctx, from, record)
return record, nil
}
func (keeper Keeper) TimeUnlock(ctx sdk.Context, from sdk.AccAddress, recordId int64, isBCFusionRefund bool) sdk.Error {
record, found := keeper.GetTimeLockRecord(ctx, from, recordId)
if !found {
return ErrTimeLockRecordDoesNotExist(DefaultCodespace, from, recordId)
}
if !isBCFusionRefund && ctx.BlockHeader().Time.Before(record.LockTime) {
return ErrCanNotUnlock(DefaultCodespace, fmt.Sprintf("lock time(%s) is after now(%s)",
record.LockTime.UTC().String(), ctx.BlockHeader().Time.UTC().String()))
}
_, err := keeper.ck.SendCoins(ctx, TimeLockCoinsAccAddr, from, record.Amount)
if err != nil {
return err
}
keeper.deleteTimeLockRecord(ctx, from, recordId)
return nil
}
func (keeper Keeper) TimeRelock(ctx sdk.Context, from sdk.AccAddress, recordId int64, newRecord TimeLockRecord) sdk.Error {
record, found := keeper.GetTimeLockRecord(ctx, from, recordId)
if !found {
return ErrTimeLockRecordDoesNotExist(DefaultCodespace, from, recordId)
}
if newRecord.Description != "" {
record.Description = newRecord.Description
}
if !newRecord.Amount.IsZero() {
if newRecord.Amount.IsEqual(record.Amount) || newRecord.Amount.IsLT(record.Amount) {
return ErrInvalidLockAmount(DefaultCodespace,
fmt.Sprintf("new locked coins(%s) should be more than original locked coins(%s)",
newRecord.Amount.String(), record.Amount.String()))
}
amountToIncrease := newRecord.Amount.Minus(record.Amount)
_, err := keeper.ck.SendCoins(ctx, from, TimeLockCoinsAccAddr, amountToIncrease)
if err != nil {
return err
}
record.Amount = newRecord.Amount
}
if !newRecord.LockTime.Equal(time.Unix(0, 0)) {
if !newRecord.LockTime.After(record.LockTime) {
return ErrInvalidLockTime(DefaultCodespace,
fmt.Sprintf("new lock time(%s) should after original lock time(%s)",
newRecord.LockTime.UTC().String(), record.LockTime.UTC().String()))
}
if !newRecord.LockTime.After(ctx.BlockHeader().Time.Add(MinLockTime)) {
return ErrInvalidLockTime(DefaultCodespace,
fmt.Sprintf("new lock time(%s) should be %d minute(s) after now(%s)",
newRecord.LockTime.UTC().String(), MinLockTime/time.Minute, ctx.BlockHeader().Time.Add(MinLockTime).UTC().String()))
}
record.LockTime = newRecord.LockTime
}
keeper.setTimeLockRecord(ctx, from, record)
return nil
}