Commit 6c0df23b authored by wjx@disanbo.com's avatar wjx@disanbo.com Committed by vipwzw

fix hashlock retrieve lint warning

parent 7e93b4c3
......@@ -15,6 +15,7 @@ import (
"github.com/spf13/cobra"
)
// HashlockCmd cmds
func HashlockCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "hashlock",
......@@ -31,7 +32,7 @@ func HashlockCmd() *cobra.Command {
return cmd
}
// 锁定
// HashlockLockCmd construct lock tx
func HashlockLockCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "lock",
......@@ -96,7 +97,7 @@ func hashlockLockCmd(cmd *cobra.Command, args []string) {
ctx.RunWithoutMarshal()
}
// 解锁
// HashlockUnlockCmd construct unlock tx
func HashlockUnlockCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "unlock",
......@@ -139,7 +140,7 @@ func hashlockUnlockCmd(cmd *cobra.Command, args []string) {
ctx.RunWithoutMarshal()
}
// 发送
// HashlockSendCmd construct send tx
func HashlockSendCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "send",
......
......@@ -11,6 +11,7 @@ import (
pty "github.com/33cn/plugin/plugin/dapp/hashlock/types"
)
// Exec_Hlock Action
func (h *Hashlock) Exec_Hlock(hlock *pty.HashlockLock, tx *types.Transaction, index int) (*types.Receipt, error) {
clog.Debug("hashlocklock action")
if hlock.Amount <= 0 {
......@@ -38,6 +39,7 @@ func (h *Hashlock) Exec_Hlock(hlock *pty.HashlockLock, tx *types.Transaction, in
return actiondb.Hashlocklock(hlock)
}
// Exec_Hsend Action
func (h *Hashlock) Exec_Hsend(transfer *pty.HashlockSend, tx *types.Transaction, index int) (*types.Receipt, error) {
//unlock 有两个条件: 1. 时间已经过期 2. 密码是对的,返回原来的账户
clog.Debug("hashlockunlock action")
......@@ -45,6 +47,7 @@ func (h *Hashlock) Exec_Hsend(transfer *pty.HashlockSend, tx *types.Transaction,
return actiondb.Hashlocksend(transfer)
}
// Exec_Hunlock Action
func (h *Hashlock) Exec_Hunlock(transfer *pty.HashlockUnlock, tx *types.Transaction, index int) (*types.Receipt, error) {
//send 有两个条件:1. 时间没有过期 2. 密码是对的,币转移到 ToAddress
clog.Debug("hashlocksend action")
......
......@@ -10,8 +10,9 @@ import (
pty "github.com/33cn/plugin/plugin/dapp/hashlock/types"
)
// ExecDelLocal_Hlock Action
func (h *Hashlock) ExecDelLocal_Hlock(hlock *pty.HashlockLock, tx *types.Transaction, receipt *types.ReceiptData, index int) (*types.LocalDBSet, error) {
info := pty.Hashlockquery{hlock.Time, hashlockLocked, hlock.Amount, h.GetBlockTime(), 0}
info := pty.Hashlockquery{Time: hlock.Time, Status: hashlockLocked, Amount: hlock.Amount, CreateTime: h.GetBlockTime(), CurrentTime: 0}
kv, err := UpdateHashReciver(h.GetLocalDB(), hlock.Hash, info)
if err != nil {
return nil, err
......@@ -19,8 +20,9 @@ func (h *Hashlock) ExecDelLocal_Hlock(hlock *pty.HashlockLock, tx *types.Transac
return &types.LocalDBSet{KV: []*types.KeyValue{kv}}, nil
}
// ExecDelLocal_Hsend Action
func (h *Hashlock) ExecDelLocal_Hsend(hsend *pty.HashlockSend, tx *types.Transaction, receipt *types.ReceiptData, index int) (*types.LocalDBSet, error) {
info := pty.Hashlockquery{0, hashlockSent, 0, 0, 0}
info := pty.Hashlockquery{Time: 0, Status: hashlockSent, Amount: 0, CreateTime: 0, CurrentTime: 0}
kv, err := UpdateHashReciver(h.GetLocalDB(), common.Sha256(hsend.Secret), info)
if err != nil {
return nil, err
......@@ -28,8 +30,9 @@ func (h *Hashlock) ExecDelLocal_Hsend(hsend *pty.HashlockSend, tx *types.Transac
return &types.LocalDBSet{KV: []*types.KeyValue{kv}}, nil
}
// ExecDelLocal_Hunlock Action
func (h *Hashlock) ExecDelLocal_Hunlock(hunlock *pty.HashlockUnlock, tx *types.Transaction, receipt *types.ReceiptData, index int) (*types.LocalDBSet, error) {
info := pty.Hashlockquery{0, hashlockUnlocked, 0, 0, 0}
info := pty.Hashlockquery{Time: 0, Status: hashlockUnlocked, Amount: 0, CreateTime: 0, CurrentTime: 0}
kv, err := UpdateHashReciver(h.GetLocalDB(), common.Sha256(hunlock.Secret), info)
if err != nil {
return nil, err
......
......@@ -10,8 +10,9 @@ import (
pty "github.com/33cn/plugin/plugin/dapp/hashlock/types"
)
// ExecLocal_Hlock Action
func (h *Hashlock) ExecLocal_Hlock(hlock *pty.HashlockLock, tx *types.Transaction, receipt *types.ReceiptData, index int) (*types.LocalDBSet, error) {
info := pty.Hashlockquery{hlock.Time, hashlockLocked, hlock.Amount, h.GetBlockTime(), 0}
info := pty.Hashlockquery{Time: hlock.Time, Status: hashlockLocked, Amount: hlock.Amount, CreateTime: h.GetBlockTime(), CurrentTime: 0}
clog.Error("ExecLocal", "info", info)
kv, err := UpdateHashReciver(h.GetLocalDB(), hlock.Hash, info)
if err != nil {
......@@ -20,8 +21,9 @@ func (h *Hashlock) ExecLocal_Hlock(hlock *pty.HashlockLock, tx *types.Transactio
return &types.LocalDBSet{KV: []*types.KeyValue{kv}}, nil
}
// ExecLocal_Hsend Action
func (h *Hashlock) ExecLocal_Hsend(hsend *pty.HashlockSend, tx *types.Transaction, receipt *types.ReceiptData, index int) (*types.LocalDBSet, error) {
info := pty.Hashlockquery{0, hashlockSent, 0, 0, 0}
info := pty.Hashlockquery{Time: 0, Status: hashlockSent, Amount: 0, CreateTime: 0, CurrentTime: 0}
clog.Error("ExecLocal", "info", info)
kv, err := UpdateHashReciver(h.GetLocalDB(), common.Sha256(hsend.Secret), info)
if err != nil {
......@@ -30,8 +32,9 @@ func (h *Hashlock) ExecLocal_Hsend(hsend *pty.HashlockSend, tx *types.Transactio
return &types.LocalDBSet{KV: []*types.KeyValue{kv}}, nil
}
// ExecLocal_Hunlock Action
func (h *Hashlock) ExecLocal_Hunlock(hunlock *pty.HashlockUnlock, tx *types.Transaction, receipt *types.ReceiptData, index int) (*types.LocalDBSet, error) {
info := pty.Hashlockquery{0, hashlockUnlocked, 0, 0, 0}
info := pty.Hashlockquery{Time: 0, Status: hashlockUnlocked, Amount: 0, CreateTime: 0, CurrentTime: 0}
clog.Error("ExecLocal", "info", info)
kv, err := UpdateHashReciver(h.GetLocalDB(), common.Sha256(hunlock.Secret), info)
if err != nil {
......
......@@ -21,14 +21,17 @@ func init() {
ety.InitFuncList(types.ListMethod(&Hashlock{}))
}
// Init hashlock
func Init(name string, sub []byte) {
drivers.Register(GetName(), newHashlock, types.GetDappFork(driverName, "Enable"))
}
// GetName for hashlock
func GetName() string {
return newHashlock().GetName()
}
// Hashlock driver
type Hashlock struct {
drivers.DriverBase
}
......@@ -40,10 +43,12 @@ func newHashlock() drivers.Driver {
return h
}
// GetDriverName driverName
func (h *Hashlock) GetDriverName() string {
return driverName
}
// CheckTx nil
func (h *Hashlock) CheckTx(tx *types.Transaction, index int) error {
return nil
}
......@@ -387,7 +387,7 @@ func estHashsend(t *testing.T) {
}
func lock(secret []byte) error {
vlock := &hlt.HashlockAction_Hlock{&hlt.HashlockLock{Amount: lockAmount, Time: int64(locktime), Hash: common.Sha256(secret), ToAddress: addr[accountindexB], ReturnAddress: addr[accountindexA]}}
vlock := &hlt.HashlockAction_Hlock{Hlock: &hlt.HashlockLock{Amount: lockAmount, Time: int64(locktime), Hash: common.Sha256(secret), ToAddress: addr[accountindexB], ReturnAddress: addr[accountindexA]}}
//fmt.Println(vlock)
transfer := &hlt.HashlockAction{Value: vlock, Ty: hlt.HashlockActionLock}
tx := &types.Transaction{Execer: []byte("hashlock"), Payload: types.Encode(transfer), Fee: fee, To: addr[accountindexB]}
......@@ -406,7 +406,7 @@ func lock(secret []byte) error {
}
func unlock(secret []byte) error {
vunlock := &hlt.HashlockAction_Hunlock{&hlt.HashlockUnlock{Secret: secret}}
vunlock := &hlt.HashlockAction_Hunlock{Hunlock: &hlt.HashlockUnlock{Secret: secret}}
transfer := &hlt.HashlockAction{Value: vunlock, Ty: hlt.HashlockActionUnlock}
tx := &types.Transaction{Execer: []byte("hashlock"), Payload: types.Encode(transfer), Fee: fee, To: addr[accountindexB]}
tx.Nonce = r.Int63()
......@@ -424,7 +424,7 @@ func unlock(secret []byte) error {
func send(secret []byte) error {
vsend := &hlt.HashlockAction_Hsend{&hlt.HashlockSend{Secret: secret}}
vsend := &hlt.HashlockAction_Hsend{Hsend: &hlt.HashlockSend{Secret: secret}}
transfer := &hlt.HashlockAction{Value: vsend, Ty: hlt.HashlockActionSend}
tx := &types.Transaction{Execer: []byte("hashlock"), Payload: types.Encode(transfer), Fee: fee, To: addr[accountindexB]}
tx.Nonce = r.Int63()
......@@ -463,9 +463,10 @@ func showOrCheckAcc(c types.Chain33Client, addr string, sorc int, balance int64)
}
if sorc != onlyshow {
return false
} else {
return true
}
return true
}
func showAccount(c types.Chain33Client, addr string) {
......@@ -520,7 +521,7 @@ func sendtoaddress(c types.Chain33Client, priv crypto.PrivKey, to string, amount
//defer conn.Close()
//fmt.Println("sign key privkey: ", common.ToHex(priv.Bytes()))
if amount > 0 {
v := &cty.CoinsAction_Transfer{&types.AssetsTransfer{Amount: amount}}
v := &cty.CoinsAction_Transfer{Transfer: &types.AssetsTransfer{Amount: amount}}
transfer := &cty.CoinsAction{Value: v, Ty: cty.CoinsActionTransfer}
tx := &types.Transaction{Execer: []byte("coins"), Payload: types.Encode(transfer), Fee: fee, To: to}
tx.Nonce = r.Int63()
......@@ -536,8 +537,9 @@ func sendtoaddress(c types.Chain33Client, priv crypto.PrivKey, to string, amount
return errors.New(string(reply.GetMsg()))
}
return nil
} else {
v := &cty.CoinsAction_Withdraw{&types.AssetsWithdraw{Amount: -amount}}
}
v := &cty.CoinsAction_Withdraw{Withdraw: &types.AssetsWithdraw{Amount: -amount}}
withdraw := &cty.CoinsAction{Value: v, Ty: cty.CoinsActionWithdraw}
tx := &types.Transaction{Execer: []byte("coins"), Payload: types.Encode(withdraw), Fee: fee, To: to}
tx.Nonce = r.Int63()
......@@ -553,7 +555,7 @@ func sendtoaddress(c types.Chain33Client, priv crypto.PrivKey, to string, amount
return errors.New(string(reply.GetMsg()))
}
return nil
}
}
func getAccounts() (*types.WalletAccounts, error) {
......
......@@ -103,7 +103,7 @@ func ConstructLockTx() *types.Transaction {
var locktime int64 = 70
var fee int64 = 1e6
vlock := &pty.HashlockAction_Hlock{&pty.HashlockLock{Amount: lockAmount, Time: locktime, Hash: common.Sha256(secret), ToAddress: toAddr, ReturnAddress: returnAddr}}
vlock := &pty.HashlockAction_Hlock{Hlock: &pty.HashlockLock{Amount: lockAmount, Time: locktime, Hash: common.Sha256(secret), ToAddress: toAddr, ReturnAddress: returnAddr}}
transfer := &pty.HashlockAction{Value: vlock, Ty: pty.HashlockActionLock}
tx := &types.Transaction{Execer: []byte("hashlock"), Payload: types.Encode(transfer), Fee: fee, To: toAddr}
tx.Nonce = r.Int63()
......@@ -116,7 +116,7 @@ func ConstructUnlockTx() *types.Transaction {
var fee int64 = 1e6
vunlock := &pty.HashlockAction_Hunlock{&pty.HashlockUnlock{Secret: secret}}
vunlock := &pty.HashlockAction_Hunlock{Hunlock: &pty.HashlockUnlock{Secret: secret}}
transfer := &pty.HashlockAction{Value: vunlock, Ty: pty.HashlockActionUnlock}
tx := &types.Transaction{Execer: []byte("hashlock"), Payload: types.Encode(transfer), Fee: fee, To: toAddr}
tx.Nonce = r.Int63()
......@@ -128,7 +128,7 @@ func ConstructSendTx() *types.Transaction {
var fee int64 = 1e6
vsend := &pty.HashlockAction_Hsend{&pty.HashlockSend{Secret: secret}}
vsend := &pty.HashlockAction_Hsend{Hsend: &pty.HashlockSend{Secret: secret}}
transfer := &pty.HashlockAction{Value: vsend, Ty: pty.HashlockActionSend}
tx := &types.Transaction{Execer: []byte("hashlock"), Payload: types.Encode(transfer), Fee: fee, To: toAddr}
tx.Nonce = r.Int63()
......@@ -174,11 +174,11 @@ func (e *TestDB) Set(key []byte, value []byte) error {
return nil
}
func (db *TestDB) BatchGet(keys [][]byte) (values [][]byte, err error) {
func (e *TestDB) BatchGet(keys [][]byte) (values [][]byte, err error) {
return nil, types.ErrNotFound
}
//从数据库中查询数据列表,set 中的cache 更新不会影响这个list
func (l *TestDB) List(prefix, key []byte, count, direction int32) ([][]byte, error) {
func (e *TestDB) List(prefix, key []byte, count, direction int32) ([][]byte, error) {
return nil, types.ErrNotFound
}
......@@ -25,10 +25,12 @@ const (
hashlockSent = 3
)
// DB struct
type DB struct {
pty.Hashlock
}
// NewDB instance
func NewDB(id []byte, returnWallet string, toAddress string, blocktime int64, amount int64, time int64) *DB {
h := &DB{}
h.HashlockId = id
......@@ -41,13 +43,15 @@ func NewDB(id []byte, returnWallet string, toAddress string, blocktime int64, am
return h
}
// GetKVSet for hashlock
func (h *DB) GetKVSet() (kvset []*types.KeyValue) {
value := types.Encode(&h.Hashlock)
kvset = append(kvset, &types.KeyValue{Key(h.HashlockId), value})
kvset = append(kvset, &types.KeyValue{Key: Key(h.HashlockId), Value: value})
return kvset
}
// Save KV
func (h *DB) Save(db dbm.KV) {
set := h.GetKVSet()
for i := 0; i < len(set); i++ {
......@@ -55,12 +59,14 @@ func (h *DB) Save(db dbm.KV) {
}
}
// Key for hashlock
func Key(id []byte) (key []byte) {
key = append(key, []byte("mavl-hashlock-")...)
key = append(key, id...)
return key
}
// Action def
type Action struct {
coinsAccount *account.DB
db dbm.KV
......@@ -71,12 +77,14 @@ type Action struct {
execaddr string
}
// NewAction gen action instance
func NewAction(h *Hashlock, tx *types.Transaction, execaddr string) *Action {
hash := tx.Hash()
fromaddr := tx.From()
return &Action{h.GetCoinsAccount(), h.GetStateDB(), hash, fromaddr, h.GetBlockTime(), h.GetHeight(), execaddr}
}
// Hashlocklock Action
func (action *Action) Hashlocklock(hlock *pty.HashlockLock) (*types.Receipt, error) {
var logs []*types.ReceiptLog
......@@ -105,10 +113,11 @@ func (action *Action) Hashlocklock(hlock *pty.HashlockLock) (*types.Receipt, err
//logs = append(logs, h.GetReceiptLog())
kv = append(kv, h.GetKVSet()...)
receipt = &types.Receipt{types.ExecOk, kv, logs}
receipt = &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: logs}
return receipt, nil
}
// Hashlockunlock Action
func (action *Action) Hashlockunlock(unlock *pty.HashlockUnlock) (*types.Receipt, error) {
var logs []*types.ReceiptLog
......@@ -150,10 +159,11 @@ func (action *Action) Hashlockunlock(unlock *pty.HashlockUnlock) (*types.Receipt
//logs = append(logs, t.GetReceiptLog())
kv = append(kv, h.GetKVSet()...)
receipt = &types.Receipt{types.ExecOk, kv, logs}
receipt = &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: logs}
return receipt, nil
}
// Hashlocksend Action
func (action *Action) Hashlocksend(send *pty.HashlockSend) (*types.Receipt, error) {
var logs []*types.ReceiptLog
......@@ -194,7 +204,7 @@ func (action *Action) Hashlocksend(send *pty.HashlockSend) (*types.Receipt, erro
//logs = append(logs, t.GetReceiptLog())
kv = append(kv, h.GetKVSet()...)
receipt = &types.Receipt{types.ExecOk, kv, logs}
receipt = &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: logs}
return receipt, nil
}
......@@ -212,19 +222,20 @@ func readHashlock(db dbm.KV, id []byte) (*pty.Hashlock, error) {
return &hashlock, nil
}
// NewHashlockquery gen query instance
func NewHashlockquery() *pty.Hashlockquery {
q := pty.Hashlockquery{}
return &q
}
func calcHashlockIdKey(id []byte) []byte {
func calcHashlockIDKey(id []byte) []byte {
return append([]byte("LODB-hashlock-"), id...)
}
//将Information转换成byte类型,使输出为kv模式
// GeHashReciverKV gen KV
func GeHashReciverKV(hashlockID []byte, information *pty.Hashlockquery) *types.KeyValue {
clog.Error("GeHashReciverKV action")
infor := pty.Hashlockquery{information.Time, information.Status, information.Amount, information.CreateTime, information.CurrentTime}
infor := pty.Hashlockquery{Time: information.Time, Status: information.Status, Amount: information.Amount, CreateTime: information.CreateTime, CurrentTime: information.CurrentTime}
clog.Error("GeHashReciverKV action", "Status", information.Status)
reciver, err := json.Marshal(infor)
if err == nil {
......@@ -233,12 +244,12 @@ func GeHashReciverKV(hashlockID []byte, information *pty.Hashlockquery) *types.K
fmt.Println(err)
}
clog.Error("GeHashReciverKV action", "reciver", reciver)
kv := &types.KeyValue{calcHashlockIdKey(hashlockID), reciver}
kv := &types.KeyValue{Key: calcHashlockIDKey(hashlockID), Value: reciver}
clog.Error("GeHashReciverKV action", "kv", kv)
return kv
}
//从db里面根据key获取value,期间需要进行解码
// GetHashReciver get hashlock
func GetHashReciver(db dbm.KVDB, hashlockID []byte) (*pty.Hashlockquery, error) {
//reciver := types.Int64{}
clog.Error("GetHashReciver action", "hashlockID", hashlockID)
......@@ -264,14 +275,14 @@ func GetHashReciver(db dbm.KVDB, hashlockID []byte) (*pty.Hashlockquery, error)
return reciver, nil
}
//将hashlockId和information都以key和value形式存入db
// SetHashReciver save hashlock
func SetHashReciver(db dbm.KVDB, hashlockID []byte, information *pty.Hashlockquery) error {
clog.Error("SetHashReciver action")
kv := GeHashReciverKV(hashlockID, information)
return db.Set(kv.Key, kv.Value)
}
//根据状态值对db中存入的数据进行更改
// UpdateHashReciver update status for hashlock
func UpdateHashReciver(cachedb dbm.KVDB, hashlockID []byte, information pty.Hashlockquery) (*types.KeyValue, error) {
clog.Error("UpdateHashReciver", "hashlockId", hashlockID)
recv, err := GetHashReciver(cachedb, hashlockID)
......@@ -315,7 +326,7 @@ func UpdateHashReciver(cachedb dbm.KVDB, hashlockID []byte, information pty.Hash
return GeHashReciverKV(hashlockID, recv), nil
}
//根据hashlockid获取相关信息
// GetTxsByHashlockID get hashlock record
func (n *Hashlock) GetTxsByHashlockID(hashlockID []byte, differTime int64) (types.Message, error) {
clog.Error("GetTxsByHashlockId action")
db := n.GetLocalDB()
......
......@@ -6,6 +6,7 @@ package executor
import "github.com/33cn/chain33/types"
// Query_GetHashlocKById get hashlock instance
func (h *Hashlock) Query_GetHashlocKById(in []byte) (types.Message, error) {
differTime := types.Now().UnixNano()/1e9 - h.GetBlockTime()
clog.Error("Query action")
......
......@@ -6,6 +6,7 @@ package types
import "errors"
// hashlock errors
var (
ErrHashlockAmount = errors.New("ErrHashlockAmount")
ErrHashlockHash = errors.New("ErrHashlockHash")
......
......@@ -23,24 +23,27 @@ func init() {
types.RegisterDappFork(HashlockX, "Enable", 0)
}
// HashlockType def
type HashlockType struct {
types.ExecTypeBase
}
// NewType method
func NewType() *HashlockType {
c := &HashlockType{}
c.SetChild(c)
return c
}
// GetPayload method
func (hashlock *HashlockType) GetPayload() types.Message {
return &HashlockAction{}
}
// TODO 暂时不修改实现, 先完成结构的重构
// CreateTx method
func (hashlock *HashlockType) CreateTx(action string, message json.RawMessage) (*types.Transaction, error) {
hlog.Debug("hashlock.CreateTx", "action", action)
var tx *types.Transaction
if action == "HashlockLock" {
var param HashlockLockTx
err := json.Unmarshal(message, &param)
......@@ -68,9 +71,9 @@ func (hashlock *HashlockType) CreateTx(action string, message json.RawMessage) (
} else {
return nil, types.ErrNotSupport
}
return tx, nil
}
// GetTypeMap method
func (hashlock *HashlockType) GetTypeMap() map[string]int32 {
return map[string]int32{
"Hlock": HashlockActionLock,
......@@ -79,42 +82,12 @@ func (hashlock *HashlockType) GetTypeMap() map[string]int32 {
}
}
func (at *HashlockType) GetLogMap() map[int64]*types.LogInfo {
// GetLogMap method
func (hashlock *HashlockType) GetLogMap() map[int64]*types.LogInfo {
return map[int64]*types.LogInfo{}
}
type CoinsDepositLog struct {
}
func (l CoinsDepositLog) Name() string {
return "LogDeposit"
}
func (l CoinsDepositLog) Decode(msg []byte) (interface{}, error) {
var logTmp types.ReceiptAccountTransfer
err := types.Decode(msg, &logTmp)
if err != nil {
return nil, err
}
return logTmp, err
}
type CoinsGetTxsByAddr struct {
}
func (t *CoinsGetTxsByAddr) JsonToProto(message json.RawMessage) ([]byte, error) {
var req types.ReqAddr
err := json.Unmarshal(message, &req)
if err != nil {
return nil, err
}
return types.Encode(&req), nil
}
func (t *CoinsGetTxsByAddr) ProtoToJson(reply *types.Message) (interface{}, error) {
return reply, nil
}
// CreateRawHashlockLockTx method
func CreateRawHashlockLockTx(parm *HashlockLockTx) (*types.Transaction, error) {
if parm == nil {
hlog.Error("CreateRawHashlockLockTx", "parm", parm)
......@@ -145,6 +118,7 @@ func CreateRawHashlockLockTx(parm *HashlockLockTx) (*types.Transaction, error) {
return tx, nil
}
// CreateRawHashlockUnlockTx method
func CreateRawHashlockUnlockTx(parm *HashlockUnlockTx) (*types.Transaction, error) {
if parm == nil {
hlog.Error("CreateRawHashlockUnlockTx", "parm", parm)
......@@ -173,6 +147,7 @@ func CreateRawHashlockUnlockTx(parm *HashlockUnlockTx) (*types.Transaction, erro
return tx, nil
}
// CreateRawHashlockSendTx method
func CreateRawHashlockSendTx(parm *HashlockSendTx) (*types.Transaction, error) {
if parm == nil {
hlog.Error("CreateRawHashlockSendTx", "parm", parm)
......
......@@ -4,6 +4,7 @@
package types
// HashlockLockTx for construction
type HashlockLockTx struct {
Secret string `json:"secret"`
Amount int64 `json:"amount"`
......@@ -13,11 +14,13 @@ type HashlockLockTx struct {
Fee int64 `json:"fee"`
}
// HashlockUnlockTx for construction
type HashlockUnlockTx struct {
Secret string `json:"secret"`
Fee int64 `json:"fee"`
}
// HashlockSendTx for construction
type HashlockSendTx struct {
Secret string `json:"secret"`
Fee int64 `json:"fee"`
......
......@@ -11,4 +11,5 @@ const (
HashlockActionUnlock = 3
)
// HashlockX name
var HashlockX = "hashlock"
......@@ -14,12 +14,14 @@ import (
"github.com/spf13/cobra"
)
// RetrieveResult response
type RetrieveResult struct {
DelayPeriod int64 `json:"delayPeriod"`
//RemainTime int64 `json:"remainTime"`
Status string `json:"status"`
}
// RetrieveCmd cmds
func RetrieveCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "retrieve",
......@@ -38,7 +40,7 @@ func RetrieveCmd() *cobra.Command {
return cmd
}
// 备份
// BackupCmd construct backup tx
func BackupCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "backup",
......@@ -83,7 +85,7 @@ func backupCmd(cmd *cobra.Command, args []string) {
ctx.RunWithoutMarshal()
}
// 准备
// PrepareCmd construct prepare tx
func PrepareCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "prepare",
......@@ -120,7 +122,7 @@ func prepareCmd(cmd *cobra.Command, args []string) {
ctx.RunWithoutMarshal()
}
// 执行
// PerformCmd construct perform tx
func PerformCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "perform",
......@@ -147,7 +149,7 @@ func performCmd(cmd *cobra.Command, args []string) {
ctx.RunWithoutMarshal()
}
// 取消
// CancelCmd construct cancel tx
func CancelCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "cancel",
......@@ -174,7 +176,7 @@ func cancelCmd(cmd *cobra.Command, args []string) {
ctx.RunWithoutMarshal()
}
// 查询
// RetrieveQueryCmd cmds
func RetrieveQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "query",
......
......@@ -9,6 +9,7 @@ import (
rt "github.com/33cn/plugin/plugin/dapp/retrieve/types"
)
// Exec_Backup Action
func (c *Retrieve) Exec_Backup(backup *rt.BackupRetrieve, tx *types.Transaction, index int) (*types.Receipt, error) {
actiondb := NewRetrieveAcction(c, tx)
if backup.DelayPeriod < minPeriod {
......@@ -18,18 +19,21 @@ func (c *Retrieve) Exec_Backup(backup *rt.BackupRetrieve, tx *types.Transaction,
return actiondb.RetrieveBackup(backup)
}
// Exec_Perform Action
func (c *Retrieve) Exec_Perform(perf *rt.PerformRetrieve, tx *types.Transaction, index int) (*types.Receipt, error) {
actiondb := NewRetrieveAcction(c, tx)
rlog.Debug("PerformRetrieve action")
return actiondb.RetrievePerform(perf)
}
// Exec_Prepare Action
func (c *Retrieve) Exec_Prepare(pre *rt.PrepareRetrieve, tx *types.Transaction, index int) (*types.Receipt, error) {
actiondb := NewRetrieveAcction(c, tx)
rlog.Debug("PreRetrieve action")
return actiondb.RetrievePrepare(pre)
}
// Exec_Cancel Action
func (c *Retrieve) Exec_Cancel(cancel *rt.CancelRetrieve, tx *types.Transaction, index int) (*types.Receipt, error) {
actiondb := NewRetrieveAcction(c, tx)
rlog.Debug("PreRetrieve action")
......
......@@ -10,10 +10,11 @@ import (
rt "github.com/33cn/plugin/plugin/dapp/retrieve/types"
)
// DelRetrieveInfo local
func DelRetrieveInfo(info *rt.RetrieveQuery, Status int64, db dbm.KVDB) (*types.KeyValue, error) {
switch Status {
case retrieveBackup:
kv := &types.KeyValue{calcRetrieveKey(info.BackupAddress, info.DefaultAddress), nil}
kv := &types.KeyValue{Key: calcRetrieveKey(info.BackupAddress, info.DefaultAddress), Value: nil}
db.Set(kv.Key, kv.Value)
return kv, nil
case retrievePrepare:
......@@ -25,7 +26,7 @@ func DelRetrieveInfo(info *rt.RetrieveQuery, Status int64, db dbm.KVDB) (*types.
info.Status = retrieveBackup
info.PrepareTime = 0
value := types.Encode(info)
kv := &types.KeyValue{calcRetrieveKey(info.BackupAddress, info.DefaultAddress), value}
kv := &types.KeyValue{Key: calcRetrieveKey(info.BackupAddress, info.DefaultAddress), Value: value}
db.Set(kv.Key, kv.Value)
return kv, nil
case retrievePerform:
......@@ -39,7 +40,7 @@ func DelRetrieveInfo(info *rt.RetrieveQuery, Status int64, db dbm.KVDB) (*types.
info.Status = retrievePrepare
info.PrepareTime = oldInfo.PrepareTime
value := types.Encode(info)
kv := &types.KeyValue{calcRetrieveKey(info.BackupAddress, info.DefaultAddress), value}
kv := &types.KeyValue{Key: calcRetrieveKey(info.BackupAddress, info.DefaultAddress), Value: value}
db.Set(kv.Key, kv.Value)
return kv, nil
default:
......@@ -58,10 +59,11 @@ func (c *Retrieve) execDelLocal(tx *types.Transaction, receipt *types.ReceiptDat
return set, nil
}
// ExecDelLocal_Backup Action
func (c *Retrieve) ExecDelLocal_Backup(backup *rt.BackupRetrieve, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
set, err := c.execDelLocal(tx, receiptData, index)
set, _ := c.execDelLocal(tx, receiptData, index)
info := rt.RetrieveQuery{backup.BackupAddress, backup.DefaultAddress, backup.DelayPeriod, zeroPrepareTime, zeroRemainTime, retrieveBackup}
info := rt.RetrieveQuery{BackupAddress: backup.BackupAddress, DefaultAddress: backup.DefaultAddress, DelayPeriod: backup.DelayPeriod, PrepareTime: zeroPrepareTime, RemainTime: zeroRemainTime, Status: retrieveBackup}
kv, err := DelRetrieveInfo(&info, retrieveBackup, c.GetLocalDB())
if err != nil {
return set, nil
......@@ -74,10 +76,11 @@ func (c *Retrieve) ExecDelLocal_Backup(backup *rt.BackupRetrieve, tx *types.Tran
return set, nil
}
// ExecDelLocal_Prepare Action
func (c *Retrieve) ExecDelLocal_Prepare(pre *rt.PrepareRetrieve, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
set, err := c.execDelLocal(tx, receiptData, index)
set, _ := c.execDelLocal(tx, receiptData, index)
info := rt.RetrieveQuery{pre.BackupAddress, pre.DefaultAddress, zeroDelay, c.GetBlockTime(), zeroRemainTime, retrievePrepare}
info := rt.RetrieveQuery{BackupAddress: pre.BackupAddress, DefaultAddress: pre.DefaultAddress, DelayPeriod: zeroDelay, PrepareTime: c.GetBlockTime(), RemainTime: zeroRemainTime, Status: retrievePrepare}
kv, err := DelRetrieveInfo(&info, retrievePrepare, c.GetLocalDB())
if err != nil {
return set, nil
......@@ -90,10 +93,11 @@ func (c *Retrieve) ExecDelLocal_Prepare(pre *rt.PrepareRetrieve, tx *types.Trans
return set, nil
}
// ExecDelLocal_Perform Action
func (c *Retrieve) ExecDelLocal_Perform(perf *rt.PerformRetrieve, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
set, err := c.execDelLocal(tx, receiptData, index)
set, _ := c.execDelLocal(tx, receiptData, index)
info := rt.RetrieveQuery{perf.BackupAddress, perf.DefaultAddress, zeroDelay, zeroPrepareTime, zeroRemainTime, retrievePerform}
info := rt.RetrieveQuery{BackupAddress: perf.BackupAddress, DefaultAddress: perf.DefaultAddress, DelayPeriod: zeroDelay, PrepareTime: zeroPrepareTime, RemainTime: zeroRemainTime, Status: retrievePerform}
kv, err := DelRetrieveInfo(&info, retrievePerform, c.GetLocalDB())
if err != nil {
return set, nil
......@@ -106,10 +110,11 @@ func (c *Retrieve) ExecDelLocal_Perform(perf *rt.PerformRetrieve, tx *types.Tran
return set, nil
}
// ExecDelLocal_Cancel Action
func (c *Retrieve) ExecDelLocal_Cancel(cancel *rt.CancelRetrieve, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
set, err := c.execDelLocal(tx, receiptData, index)
set, _ := c.execDelLocal(tx, receiptData, index)
info := rt.RetrieveQuery{cancel.BackupAddress, cancel.DefaultAddress, zeroDelay, zeroPrepareTime, zeroRemainTime, retrieveCancel}
info := rt.RetrieveQuery{BackupAddress: cancel.BackupAddress, DefaultAddress: cancel.DefaultAddress, DelayPeriod: zeroDelay, PrepareTime: zeroPrepareTime, RemainTime: zeroRemainTime, Status: retrieveCancel}
kv, err := DelRetrieveInfo(&info, retrieveCancel, c.GetLocalDB())
if err != nil {
return set, nil
......
......@@ -10,6 +10,7 @@ import (
rt "github.com/33cn/plugin/plugin/dapp/retrieve/types"
)
// SaveRetrieveInfo local
func SaveRetrieveInfo(info *rt.RetrieveQuery, Status int64, db dbm.KVDB) (*types.KeyValue, error) {
rlog.Debug("Retrieve SaveRetrieveInfo", "backupaddr", info.BackupAddress, "defaddr", info.DefaultAddress)
switch Status {
......@@ -19,7 +20,7 @@ func SaveRetrieveInfo(info *rt.RetrieveQuery, Status int64, db dbm.KVDB) (*types
return nil, err
}
value := types.Encode(info)
kv := &types.KeyValue{calcRetrieveKey(info.BackupAddress, info.DefaultAddress), value}
kv := &types.KeyValue{Key: calcRetrieveKey(info.BackupAddress, info.DefaultAddress), Value: value}
db.Set(kv.Key, kv.Value)
return kv, nil
case retrievePrepare:
......@@ -29,7 +30,7 @@ func SaveRetrieveInfo(info *rt.RetrieveQuery, Status int64, db dbm.KVDB) (*types
}
info.DelayPeriod = oldInfo.DelayPeriod
value := types.Encode(info)
kv := &types.KeyValue{calcRetrieveKey(info.BackupAddress, info.DefaultAddress), value}
kv := &types.KeyValue{Key: calcRetrieveKey(info.BackupAddress, info.DefaultAddress), Value: value}
db.Set(kv.Key, kv.Value)
return kv, nil
case retrievePerform:
......@@ -42,7 +43,7 @@ func SaveRetrieveInfo(info *rt.RetrieveQuery, Status int64, db dbm.KVDB) (*types
info.DelayPeriod = oldInfo.DelayPeriod
info.PrepareTime = oldInfo.PrepareTime
value := types.Encode(info)
kv := &types.KeyValue{calcRetrieveKey(info.BackupAddress, info.DefaultAddress), value}
kv := &types.KeyValue{Key: calcRetrieveKey(info.BackupAddress, info.DefaultAddress), Value: value}
db.Set(kv.Key, kv.Value)
return kv, nil
default:
......@@ -59,10 +60,11 @@ func (c *Retrieve) execLocal(receipt types.ExecTypeGet) (*types.LocalDBSet, erro
return dbSet, nil
}
// ExecLocal_Backup Action
func (c *Retrieve) ExecLocal_Backup(backup *rt.BackupRetrieve, tx *types.Transaction, receiptData types.ExecTypeGet, index int) (*types.LocalDBSet, error) {
set, err := c.execLocal(receiptData)
set, _ := c.execLocal(receiptData)
info := rt.RetrieveQuery{backup.BackupAddress, backup.DefaultAddress, backup.DelayPeriod, zeroPrepareTime, zeroRemainTime, retrieveBackup}
info := rt.RetrieveQuery{BackupAddress: backup.BackupAddress, DefaultAddress: backup.DefaultAddress, DelayPeriod: backup.DelayPeriod, PrepareTime: zeroPrepareTime, RemainTime: zeroRemainTime, Status: retrieveBackup}
kv, err := SaveRetrieveInfo(&info, retrieveBackup, c.GetLocalDB())
if err != nil {
return set, nil
......@@ -75,10 +77,11 @@ func (c *Retrieve) ExecLocal_Backup(backup *rt.BackupRetrieve, tx *types.Transac
return set, nil
}
// ExecLocal_Prepare Action
func (c *Retrieve) ExecLocal_Prepare(pre *rt.PrepareRetrieve, tx *types.Transaction, receiptData types.ExecTypeGet, index int) (*types.LocalDBSet, error) {
set, err := c.execLocal(receiptData)
set, _ := c.execLocal(receiptData)
info := rt.RetrieveQuery{pre.BackupAddress, pre.DefaultAddress, zeroDelay, zeroPrepareTime, zeroRemainTime, retrievePrepare}
info := rt.RetrieveQuery{BackupAddress: pre.BackupAddress, DefaultAddress: pre.DefaultAddress, DelayPeriod: zeroDelay, PrepareTime: zeroPrepareTime, RemainTime: zeroRemainTime, Status: retrievePrepare}
kv, err := SaveRetrieveInfo(&info, retrievePrepare, c.GetLocalDB())
if err != nil {
return set, nil
......@@ -91,10 +94,11 @@ func (c *Retrieve) ExecLocal_Prepare(pre *rt.PrepareRetrieve, tx *types.Transact
return set, nil
}
// ExecLocal_Perf Action
func (c *Retrieve) ExecLocal_Perf(perf *rt.PerformRetrieve, tx *types.Transaction, receiptData types.ExecTypeGet, index int) (*types.LocalDBSet, error) {
set, err := c.execLocal(receiptData)
set, _ := c.execLocal(receiptData)
info := rt.RetrieveQuery{perf.BackupAddress, perf.DefaultAddress, zeroDelay, zeroPrepareTime, zeroRemainTime, retrievePerform}
info := rt.RetrieveQuery{BackupAddress: perf.BackupAddress, DefaultAddress: perf.DefaultAddress, DelayPeriod: zeroDelay, PrepareTime: zeroPrepareTime, RemainTime: zeroRemainTime, Status: retrievePerform}
kv, err := SaveRetrieveInfo(&info, retrievePerform, c.GetLocalDB())
if err != nil {
return set, nil
......@@ -107,10 +111,11 @@ func (c *Retrieve) ExecLocal_Perf(perf *rt.PerformRetrieve, tx *types.Transactio
return set, nil
}
// ExecLocal_Cancel Action
func (c *Retrieve) ExecLocal_Cancel(cancel *rt.CancelRetrieve, tx *types.Transaction, receiptData types.ExecTypeGet, index int) (*types.LocalDBSet, error) {
set, err := c.execLocal(receiptData)
set, _ := c.execLocal(receiptData)
info := rt.RetrieveQuery{cancel.BackupAddress, cancel.DefaultAddress, zeroDelay, zeroPrepareTime, zeroRemainTime, retrieveCancel}
info := rt.RetrieveQuery{BackupAddress: cancel.BackupAddress, DefaultAddress: cancel.DefaultAddress, DelayPeriod: zeroDelay, PrepareTime: zeroPrepareTime, RemainTime: zeroRemainTime, Status: retrieveCancel}
kv, err := SaveRetrieveInfo(&info, retrieveCancel, c.GetLocalDB())
if err != nil {
return set, nil
......
......@@ -9,6 +9,7 @@ import (
rt "github.com/33cn/plugin/plugin/dapp/retrieve/types"
)
// Query_GetRetrieveInfo get retrieve state
func (r *Retrieve) Query_GetRetrieveInfo(in *rt.ReqRetrieveInfo) (types.Message, error) {
rlog.Debug("Retrieve Query", "backupaddr", in.BackupAddress, "defaddr", in.DefaultAddress)
info, err := getRetrieveInfo(r.GetLocalDB(), in.BackupAddress, in.DefaultAddress)
......
......@@ -32,15 +32,17 @@ func init() {
ety.InitFuncList(types.ListMethod(&Retrieve{}))
}
//const maxTimeWeight = 2
//Init retrieve
func Init(name string, sub []byte) {
drivers.Register(GetName(), newRetrieve, types.GetDappFork(driverName, "Enable"))
}
// GetName method
func GetName() string {
return newRetrieve().GetName()
}
// Retrieve def
type Retrieve struct {
drivers.DriverBase
}
......@@ -52,10 +54,12 @@ func newRetrieve() drivers.Driver {
return r
}
// GetDriverName method
func (r *Retrieve) GetDriverName() string {
return driverName
}
// CheckTx nil
func (r *Retrieve) CheckTx(tx *types.Transaction, index int) error {
return nil
}
......
......@@ -447,7 +447,7 @@ func estRetrievePerformB(t *testing.T) {
}
func backup(backupaddrindex int, defaultaddrindex int, privkeyindex int, delayperiod int64) ([]byte, error) {
vbackup := &rt.RetrieveAction_Backup{&rt.BackupRetrieve{BackupAddress: addr[backupaddrindex], DefaultAddress: addr[defaultaddrindex], DelayPeriod: delayperiod}}
vbackup := &rt.RetrieveAction_Backup{Backup: &rt.BackupRetrieve{BackupAddress: addr[backupaddrindex], DefaultAddress: addr[defaultaddrindex], DelayPeriod: delayperiod}}
//fmt.Println(vlock)
transfer := &rt.RetrieveAction{Value: vbackup, Ty: rt.RetrieveBackup}
tx := &types.Transaction{Execer: []byte("retrieve"), Payload: types.Encode(transfer), Fee: fee, To: addr[backupaddrindex]}
......@@ -466,7 +466,7 @@ func backup(backupaddrindex int, defaultaddrindex int, privkeyindex int, delaype
}
func prepare(backupaddrindex int, defaultaddrindex int, privkeyindex int) ([]byte, error) {
vprepare := &rt.RetrieveAction_Prepare{&rt.PrepareRetrieve{BackupAddress: addr[backupaddrindex], DefaultAddress: addr[defaultaddrindex]}}
vprepare := &rt.RetrieveAction_Prepare{Prepare: &rt.PrepareRetrieve{BackupAddress: addr[backupaddrindex], DefaultAddress: addr[defaultaddrindex]}}
transfer := &rt.RetrieveAction{Value: vprepare, Ty: rt.RetrievePreapre}
tx := &types.Transaction{Execer: []byte("retrieve"), Payload: types.Encode(transfer), Fee: fee, To: addr[backupaddrindex]}
tx.Nonce = r.Int63()
......@@ -483,7 +483,7 @@ func prepare(backupaddrindex int, defaultaddrindex int, privkeyindex int) ([]byt
}
func perform(backupaddrindex int, defaultaddrindex int, privkeyindex int) ([]byte, error) {
vperform := &rt.RetrieveAction_Perform{&rt.PerformRetrieve{BackupAddress: addr[backupaddrindex], DefaultAddress: addr[defaultaddrindex]}}
vperform := &rt.RetrieveAction_Perform{Perform: &rt.PerformRetrieve{BackupAddress: addr[backupaddrindex], DefaultAddress: addr[defaultaddrindex]}}
transfer := &rt.RetrieveAction{Value: vperform, Ty: rt.RetrievePerform}
tx := &types.Transaction{Execer: []byte("retrieve"), Payload: types.Encode(transfer), Fee: fee, To: addr[backupaddrindex]}
tx.Nonce = r.Int63()
......@@ -500,7 +500,7 @@ func perform(backupaddrindex int, defaultaddrindex int, privkeyindex int) ([]byt
}
func cancel(backupaddrindex int, defaultaddrindex int, privkeyindex int) ([]byte, error) {
vcancel := &rt.RetrieveAction_Cancel{&rt.CancelRetrieve{BackupAddress: addr[backupaddrindex], DefaultAddress: addr[defaultaddrindex]}}
vcancel := &rt.RetrieveAction_Cancel{Cancel: &rt.CancelRetrieve{BackupAddress: addr[backupaddrindex], DefaultAddress: addr[defaultaddrindex]}}
transfer := &rt.RetrieveAction{Value: vcancel, Ty: rt.RetrieveCancel}
tx := &types.Transaction{Execer: []byte("retrieve"), Payload: types.Encode(transfer), Fee: fee, To: addr[backupaddrindex]}
tx.Nonce = r.Int63()
......@@ -576,9 +576,8 @@ func showOrCheckAcc(c types.Chain33Client, addr string, sorc int, balance int64)
}
if sorc != onlyshow {
return false
} else {
return true
}
return true
}
func genaddress() (string, crypto.PrivKey) {
......@@ -614,7 +613,7 @@ func sendtoaddress(c types.Chain33Client, priv crypto.PrivKey, to string, amount
//defer conn.Close()
//fmt.Println("sign key privkey: ", common.ToHex(priv.Bytes()))
if amount > 0 {
v := &cty.CoinsAction_Transfer{&types.AssetsTransfer{Amount: amount}}
v := &cty.CoinsAction_Transfer{Transfer: &types.AssetsTransfer{Amount: amount}}
transfer := &cty.CoinsAction{Value: v, Ty: cty.CoinsActionTransfer}
tx := &types.Transaction{Execer: []byte("coins"), Payload: types.Encode(transfer), Fee: fee, To: to}
tx.Nonce = r.Int63()
......@@ -630,8 +629,8 @@ func sendtoaddress(c types.Chain33Client, priv crypto.PrivKey, to string, amount
return nil, errors.New(string(reply.GetMsg()))
}
return tx.Hash(), nil
} else {
v := &cty.CoinsAction_Withdraw{&types.AssetsWithdraw{Amount: -amount}}
}
v := &cty.CoinsAction_Withdraw{Withdraw: &types.AssetsWithdraw{Amount: -amount}}
withdraw := &cty.CoinsAction{Value: v, Ty: cty.CoinsActionWithdraw}
tx := &types.Transaction{Execer: []byte("coins"), Payload: types.Encode(withdraw), Fee: fee, To: to}
tx.Nonce = r.Int63()
......@@ -647,7 +646,7 @@ func sendtoaddress(c types.Chain33Client, priv crypto.PrivKey, to string, amount
return nil, errors.New(string(reply.GetMsg()))
}
return tx.Hash(), nil
}
}
func getAccounts() (*types.WalletAccounts, error) {
......
......@@ -86,7 +86,7 @@ func ConstructBackupTx() *types.Transaction {
var delayPeriod int64 = 70
var fee int64 = 1e6
vbackup := &rt.RetrieveAction_Backup{&rt.BackupRetrieve{BackupAddress: backupAddr, DefaultAddress: defaultAddr, DelayPeriod: delayPeriod}}
vbackup := &rt.RetrieveAction_Backup{Backup: &rt.BackupRetrieve{BackupAddress: backupAddr, DefaultAddress: defaultAddr, DelayPeriod: delayPeriod}}
//fmt.Println(vlock)
transfer := &rt.RetrieveAction{Value: vbackup, Ty: rt.RetrieveBackup}
tx := &types.Transaction{Execer: []byte("retrieve"), Payload: types.Encode(transfer), Fee: fee, To: backupAddr}
......@@ -97,7 +97,7 @@ func ConstructBackupTx() *types.Transaction {
func ConstructPrepareTx() *types.Transaction {
var fee int64 = 1e6
vprepare := &rt.RetrieveAction_Prepare{&rt.PrepareRetrieve{BackupAddress: backupAddr, DefaultAddress: defaultAddr}}
vprepare := &rt.RetrieveAction_Prepare{Prepare: &rt.PrepareRetrieve{BackupAddress: backupAddr, DefaultAddress: defaultAddr}}
transfer := &rt.RetrieveAction{Value: vprepare, Ty: rt.RetrievePreapre}
tx := &types.Transaction{Execer: []byte("retrieve"), Payload: types.Encode(transfer), Fee: fee, To: backupAddr}
tx.Nonce = r.Int63()
......@@ -109,7 +109,7 @@ func ConstructPrepareTx() *types.Transaction {
func ConstructPerformTx() *types.Transaction {
var fee int64 = 1e6
vperform := &rt.RetrieveAction_Perform{&rt.PerformRetrieve{BackupAddress: backupAddr, DefaultAddress: defaultAddr}}
vperform := &rt.RetrieveAction_Perform{Perform: &rt.PerformRetrieve{BackupAddress: backupAddr, DefaultAddress: defaultAddr}}
transfer := &rt.RetrieveAction{Value: vperform, Ty: rt.RetrievePerform}
tx := &types.Transaction{Execer: []byte("retrieve"), Payload: types.Encode(transfer), Fee: fee, To: backupAddr}
tx.Nonce = r.Int63()
......@@ -156,11 +156,11 @@ func (e *TestDB) Set(key []byte, value []byte) error {
return nil
}
func (db *TestDB) BatchGet(keys [][]byte) (values [][]byte, err error) {
func (e *TestDB) BatchGet(keys [][]byte) (values [][]byte, err error) {
return nil, types.ErrNotFound
}
//从数据库中查询数据列表,set 中的cache 更新不会影响这个list
func (l *TestDB) List(prefix, key []byte, count, direction int32) ([][]byte, error) {
func (e *TestDB) List(prefix, key []byte, count, direction int32) ([][]byte, error) {
return nil, types.ErrNotFound
}
......@@ -24,12 +24,15 @@ const (
retrieveCancel = 4
)
// MaxRelation when backup
const MaxRelation = 10
// DB def
type DB struct {
rt.Retrieve
}
// NewDB instance
func NewDB(backupaddress string) *DB {
r := &DB{}
r.BackupAddress = backupaddress
......@@ -37,22 +40,25 @@ func NewDB(backupaddress string) *DB {
return r
}
// RelateDB on retrieve action
func (r *DB) RelateDB(defaultAddress string, createTime int64, delayPeriod int64) bool {
if len(r.RetPara) >= MaxRelation {
return false
}
rlog.Debug("RetrieveBackup", "RelateDB", defaultAddress)
para := &rt.RetrievePara{defaultAddress, retrieveBackup, createTime, 0, delayPeriod}
para := &rt.RetrievePara{DefaultAddress: defaultAddress, Status: retrieveBackup, CreateTime: createTime, PrepareTime: 0, DelayPeriod: delayPeriod}
r.RetPara = append(r.RetPara, para)
return true
}
// UnRelateDB on retrieve action
func (r *DB) UnRelateDB(index int) bool {
r.RetPara = append(r.RetPara[:index], r.RetPara[index+1:]...)
return true
}
// CheckRelation on retrieve action
func (r *DB) CheckRelation(defaultAddress string) (int, bool) {
for i := 0; i < len(r.RetPara); i++ {
if r.RetPara[i].DefaultAddress == defaultAddress {
......@@ -62,12 +68,14 @@ func (r *DB) CheckRelation(defaultAddress string) (int, bool) {
return MaxRelation, false
}
// GetKVSet for retrieve
func (r *DB) GetKVSet() (kvset []*types.KeyValue) {
value := types.Encode(&r.Retrieve)
kvset = append(kvset, &types.KeyValue{Key(r.BackupAddress), value})
kvset = append(kvset, &types.KeyValue{Key: Key(r.BackupAddress), Value: value})
return kvset
}
// Save KV
func (r *DB) Save(db dbm.KV) {
set := r.GetKVSet()
for i := 0; i < len(set); i++ {
......@@ -75,12 +83,14 @@ func (r *DB) Save(db dbm.KV) {
}
}
// Key for retrieve
func Key(address string) (key []byte) {
key = append(key, []byte("mavl-retrieve-")...)
key = append(key, address...)
return key
}
// Action def
type Action struct {
coinsAccount *account.DB
db dbm.KV
......@@ -91,6 +101,7 @@ type Action struct {
execaddr string
}
// NewRetrieveAcction gen instance
func NewRetrieveAcction(r *Retrieve, tx *types.Transaction) *Action {
hash := tx.Hash()
fromaddr := tx.From()
......@@ -98,7 +109,7 @@ func NewRetrieveAcction(r *Retrieve, tx *types.Transaction) *Action {
r.GetBlockTime(), r.GetHeight(), dapp.ExecAddress(string(tx.Execer))}
}
//wait for valuable comment
// RetrieveBackup Action
func (action *Action) RetrieveBackup(backupRet *rt.BackupRetrieve) (*types.Receipt, error) {
var logs []*types.ReceiptLog
var kv []*types.KeyValue
......@@ -149,10 +160,11 @@ func (action *Action) RetrieveBackup(backupRet *rt.BackupRetrieve) (*types.Recei
r.Save(action.db)
kv = append(kv, r.GetKVSet()...)
receipt = &types.Receipt{types.ExecOk, kv, logs}
receipt = &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: logs}
return receipt, nil
}
// RetrievePrepare Action
func (action *Action) RetrievePrepare(preRet *rt.PrepareRetrieve) (*types.Receipt, error) {
var logs []*types.ReceiptLog
var kv []*types.KeyValue
......@@ -187,10 +199,11 @@ func (action *Action) RetrievePrepare(preRet *rt.PrepareRetrieve) (*types.Receip
r.Save(action.db)
kv = append(kv, r.GetKVSet()...)
receipt = &types.Receipt{types.ExecOk, kv, logs}
receipt = &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: logs}
return receipt, nil
}
// RetrievePerform Action
func (action *Action) RetrievePerform(perfRet *rt.PerformRetrieve) (*types.Receipt, error) {
var logs []*types.ReceiptLog
var kv []*types.KeyValue
......@@ -246,10 +259,11 @@ func (action *Action) RetrievePerform(perfRet *rt.PerformRetrieve) (*types.Recei
kv = append(kv, receipt.KV...)
kv = append(kv, r.GetKVSet()...)
receipt = &types.Receipt{types.ExecOk, kv, logs}
receipt = &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: logs}
return receipt, nil
}
// RetrieveCancel Action
func (action *Action) RetrieveCancel(cancel *rt.CancelRetrieve) (*types.Receipt, error) {
var logs []*types.ReceiptLog
var kv []*types.KeyValue
......@@ -285,7 +299,7 @@ func (action *Action) RetrieveCancel(cancel *rt.CancelRetrieve) (*types.Receipt,
r.Save(action.db)
kv = append(kv, r.GetKVSet()...)
receipt = &types.Receipt{types.ExecOk, kv, logs}
receipt = &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: logs}
return receipt, nil
}
......
......@@ -11,6 +11,7 @@ import (
"github.com/33cn/plugin/plugin/dapp/retrieve/types"
)
// CreateRawRetrieveBackupTx construct backup tx
func (c *Jrpc) CreateRawRetrieveBackupTx(in *RetrieveBackupTx, result *interface{}) error {
head := &types.BackupRetrieve{
BackupAddress: in.BackupAddr,
......@@ -27,6 +28,7 @@ func (c *Jrpc) CreateRawRetrieveBackupTx(in *RetrieveBackupTx, result *interface
return nil
}
// CreateRawRetrievePrepareTx construct prepare tx
func (c *Jrpc) CreateRawRetrievePrepareTx(in *RetrievePrepareTx, result *interface{}) error {
head := &types.PrepareRetrieve{
BackupAddress: in.BackupAddr,
......@@ -42,6 +44,7 @@ func (c *Jrpc) CreateRawRetrievePrepareTx(in *RetrievePrepareTx, result *interfa
return nil
}
// CreateRawRetrievePerformTx construct perform tx
func (c *Jrpc) CreateRawRetrievePerformTx(in *RetrievePerformTx, result *interface{}) error {
head := &types.PerformRetrieve{
BackupAddress: in.BackupAddr,
......@@ -56,6 +59,7 @@ func (c *Jrpc) CreateRawRetrievePerformTx(in *RetrievePerformTx, result *interfa
return nil
}
// CreateRawRetrieveCancelTx construct cancel tx
func (c *Jrpc) CreateRawRetrieveCancelTx(in *RetrieveCancelTx, result *interface{}) error {
head := &types.CancelRetrieve{
BackupAddress: in.BackupAddr,
......
......@@ -14,7 +14,7 @@ import (
func (c *channelClient) Backup(ctx context.Context, v *rt.BackupRetrieve) (*types.UnsignTx, error) {
backup := &rt.RetrieveAction{
Ty: rt.RetrieveActionBackup,
Value: &rt.RetrieveAction_Backup{v},
Value: &rt.RetrieveAction_Backup{Backup: v},
}
tx, err := types.CreateFormatTx(string(rt.ExecerRetrieve), types.Encode(backup))
if err != nil {
......@@ -27,7 +27,7 @@ func (c *channelClient) Backup(ctx context.Context, v *rt.BackupRetrieve) (*type
func (c *channelClient) Prepare(ctx context.Context, v *rt.PrepareRetrieve) (*types.UnsignTx, error) {
prepare := &rt.RetrieveAction{
Ty: rt.RetrieveActionPrepare,
Value: &rt.RetrieveAction_Prepare{v},
Value: &rt.RetrieveAction_Prepare{Prepare: v},
}
tx, err := types.CreateFormatTx(string(rt.ExecerRetrieve), types.Encode(prepare))
if err != nil {
......@@ -41,7 +41,7 @@ func (c *channelClient) Prepare(ctx context.Context, v *rt.PrepareRetrieve) (*ty
func (c *channelClient) Perform(ctx context.Context, v *rt.PerformRetrieve) (*types.UnsignTx, error) {
perform := &rt.RetrieveAction{
Ty: rt.RetrieveActionPerform,
Value: &rt.RetrieveAction_Perform{v},
Value: &rt.RetrieveAction_Perform{Perform: v},
}
tx, err := types.CreateFormatTx(string(rt.ExecerRetrieve), types.Encode(perform))
if err != nil {
......@@ -54,7 +54,7 @@ func (c *channelClient) Perform(ctx context.Context, v *rt.PerformRetrieve) (*ty
func (c *channelClient) Cancel(ctx context.Context, v *rt.CancelRetrieve) (*types.UnsignTx, error) {
cancel := &rt.RetrieveAction{
Ty: rt.RetrieveActionCancel,
Value: &rt.RetrieveAction_Cancel{v},
Value: &rt.RetrieveAction_Cancel{Cancel: v},
}
tx, err := types.CreateFormatTx(string(rt.ExecerRetrieve), types.Encode(cancel))
if err != nil {
......
......@@ -4,6 +4,7 @@
package rpc
// RetrieveBackupTx construction
type RetrieveBackupTx struct {
BackupAddr string `json:"backupAddr"`
DefaultAddr string `json:"defaultAddr"`
......@@ -11,18 +12,21 @@ type RetrieveBackupTx struct {
Fee int64 `json:"fee"`
}
// RetrievePrepareTx construction
type RetrievePrepareTx struct {
BackupAddr string `json:"backupAddr"`
DefaultAddr string `json:"defaultAddr"`
Fee int64 `json:"fee"`
}
// RetrievePerformTx construction
type RetrievePerformTx struct {
BackupAddr string `json:"backupAddr"`
DefaultAddr string `json:"defaultAddr"`
Fee int64 `json:"fee"`
}
// RetrieveCancelTx construction
type RetrieveCancelTx struct {
BackupAddr string `json:"backupAddr"`
DefaultAddr string `json:"defaultAddr"`
......
......@@ -8,10 +8,12 @@ import (
"github.com/33cn/chain33/rpc/types"
)
// Jrpc def
type Jrpc struct {
cli *channelClient
}
// Grpc def
type Grpc struct {
*channelClient
}
......@@ -20,6 +22,7 @@ type channelClient struct {
types.ChannelClient
}
// Init retrieve rpc
func Init(name string, s types.RPCServer) {
cli := &channelClient{}
grpc := &Grpc{channelClient: cli}
......
......@@ -22,6 +22,7 @@ const (
RetrieveActionCancel = 4
)
// retrieve names
var (
JRPCName = "Retrieve"
RetrieveX = "retrieve"
......
......@@ -6,6 +6,7 @@ package types
import "errors"
// retrieve errors
var (
ErrRetrieveRepeatAddress = errors.New("ErrRetrieveRepeatAddress")
ErrRetrieveDefaultAddress = errors.New("ErrRetrieveDefaultAddress")
......
......@@ -18,17 +18,19 @@ func init() {
types.RegisterDappFork(RetrieveX, "ForkRetrive", 180000)
}
// RetrieveType def
type RetrieveType struct {
types.ExecTypeBase
}
// NewType for retrieve
func NewType() *RetrieveType {
c := &RetrieveType{}
c.SetChild(c)
return c
}
//GetRealToAddr,避免老的,没有To字段的交易分叉
// GetRealToAddr 避免老的,没有To字段的交易分叉
func (r RetrieveType) GetRealToAddr(tx *types.Transaction) string {
if len(tx.To) == 0 {
return address.ExecAddress(string(tx.Execer))
......@@ -36,22 +38,27 @@ func (r RetrieveType) GetRealToAddr(tx *types.Transaction) string {
return tx.To
}
func (at *RetrieveType) GetPayload() types.Message {
// GetPayload method
func (r *RetrieveType) GetPayload() types.Message {
return &RetrieveAction{}
}
func (at *RetrieveType) GetName() string {
// GetName method
func (r *RetrieveType) GetName() string {
return RetrieveX
}
func (at *RetrieveType) GetLogMap() map[int64]*types.LogInfo {
// GetLogMap method
func (r *RetrieveType) GetLogMap() map[int64]*types.LogInfo {
return nil
}
func (at *RetrieveType) GetTypeMap() map[string]int32 {
// GetTypeMap method
func (r *RetrieveType) GetTypeMap() map[string]int32 {
return actionName
}
// ActionName method
func (r RetrieveType) ActionName(tx *types.Transaction) string {
var action RetrieveAction
err := types.Decode(tx.Payload, &action)
......@@ -70,42 +77,12 @@ func (r RetrieveType) ActionName(tx *types.Transaction) string {
return "unknown"
}
// Amount method
func (r RetrieveType) Amount(tx *types.Transaction) (int64, error) {
return 0, nil
}
func (retrieve RetrieveType) CreateTx(action string, message json.RawMessage) (*types.Transaction, error) {
// CreateTx method
func (r RetrieveType) CreateTx(action string, message json.RawMessage) (*types.Transaction, error) {
return nil, nil
}
type CoinsDepositLog struct {
}
func (l CoinsDepositLog) Name() string {
return "LogDeposit"
}
func (l CoinsDepositLog) Decode(msg []byte) (interface{}, error) {
var logTmp types.ReceiptAccountTransfer
err := types.Decode(msg, &logTmp)
if err != nil {
return nil, err
}
return logTmp, err
}
type RetrieveGetInfo struct {
}
func (t *RetrieveGetInfo) JsonToProto(message json.RawMessage) ([]byte, error) {
var req ReqRetrieveInfo
err := json.Unmarshal(message, &req)
if err != nil {
return nil, err
}
return types.Encode(&req), nil
}
func (t *RetrieveGetInfo) ProtoToJson(reply *types.Message) (interface{}, error) {
return reply, nil
}
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