Commit 4dbe1874 authored by yxq's avatar yxq Committed by vipwzw

feat: prune expired kv

parent 822bd87c
...@@ -17,6 +17,7 @@ EventTransfer -> 转移资产 ...@@ -17,6 +17,7 @@ EventTransfer -> 转移资产
//nofee transaction will not pack into block //nofee transaction will not pack into block
import ( import (
"bytes"
"fmt" "fmt"
log "github.com/33cn/chain33/common/log/log15" log "github.com/33cn/chain33/common/log/log15"
...@@ -31,6 +32,7 @@ var driverName = "ticket" ...@@ -31,6 +32,7 @@ var driverName = "ticket"
// Init initial // Init initial
func Init(name string, cfg *types.Chain33Config, sub []byte) { func Init(name string, cfg *types.Chain33Config, sub []byte) {
drivers.Register(cfg, GetName(), newTicket, cfg.GetDappFork(driverName, "Enable")) drivers.Register(cfg, GetName(), newTicket, cfg.GetDappFork(driverName, "Enable"))
drivers.RegisterKVExpiredChecker(ty.TicketX, expiredKVChecker)
InitExecType() InitExecType()
} }
...@@ -193,3 +195,23 @@ func (t *Ticket) CheckTx(tx *types.Transaction, index int) error { ...@@ -193,3 +195,23 @@ func (t *Ticket) CheckTx(tx *types.Transaction, index int) error {
func (t *Ticket) CheckReceiptExecOk() bool { func (t *Ticket) CheckReceiptExecOk() bool {
return true return true
} }
// 自定义接口,用于删除不再需要保存的kv
// 比如 ticket 已经 close 之后就废弃了,可以删除
func expiredKVChecker(key ,value []byte) bool {
// 由于 ticketBindKeyPrefix 包含了 ticketKeyPrefix,所以需要多做一次检查
if bytes.HasPrefix(key, ticketBindKeyPrefix) {
return false
}
if !bytes.HasPrefix(key, ticketKeyPrefix) {
return false
}
var tk ty.Ticket
if err := types.Decode(value, &tk); err != nil {
return false
}
if tk.Status == ty.TicketClosed {
return true
}
return false
}
...@@ -29,6 +29,11 @@ var tlog = log.New("module", "ticket.db") ...@@ -29,6 +29,11 @@ var tlog = log.New("module", "ticket.db")
//var genesisKey = []byte("mavl-acc-genesis") //var genesisKey = []byte("mavl-acc-genesis")
//var addrSeed = []byte("address seed bytes for public key") //var addrSeed = []byte("address seed bytes for public key")
var (
ticketKeyPrefix = []byte("mavl-ticket-")
ticketBindKeyPrefix = []byte("mavl-ticket-tbind-")
)
// DB db // DB db
type DB struct { type DB struct {
ty.Ticket ty.Ticket
...@@ -106,14 +111,14 @@ func (t *DB) Save(db dbm.KV) { ...@@ -106,14 +111,14 @@ func (t *DB) Save(db dbm.KV) {
//Key address to save key //Key address to save key
func Key(id string) (key []byte) { func Key(id string) (key []byte) {
key = append(key, []byte("mavl-ticket-")...) key = append(key, ticketKeyPrefix...)
key = append(key, []byte(id)...) key = append(key, []byte(id)...)
return key return key
} }
// BindKey bind key // BindKey bind key
func BindKey(id string) (key []byte) { func BindKey(id string) (key []byte) {
key = append(key, []byte("mavl-ticket-tbind-")...) key = append(key, ticketBindKeyPrefix...)
key = append(key, []byte(id)...) key = append(key, []byte(id)...)
return key return key
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment