/
lock.go
77 lines (63 loc) · 1.6 KB
/
lock.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
package tx
import (
"bytes"
"encoding/json"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
"github.com/amolabs/amoabci/amo/code"
"github.com/amolabs/amoabci/amo/store"
"github.com/amolabs/amoabci/amo/types"
)
type LockParam struct {
UDC uint32 `json:"udc"`
Holder crypto.Address `json:"holder"`
Amount types.Currency `json:"amount"`
}
func parseLockParam(raw []byte) (LockParam, error) {
var param LockParam
err := json.Unmarshal(raw, ¶m)
if err != nil {
return param, err
}
return param, nil
}
type TxLock struct {
TxBase
Param LockParam `json:"-"`
}
var _ Tx = &TxLock{}
func (t *TxLock) Check() (uint32, string) {
param := t.Param
if len(param.Holder) != crypto.AddressSize {
return code.TxCodeBadParam, "wrong size of operator address"
}
return code.TxCodeOK, "ok"
}
func (t *TxLock) Execute(s *store.Store) (uint32, string, []abci.Event) {
param := t.Param
sender := t.GetSender()
if !param.Amount.GreaterThan(zero) {
return code.TxCodeInvalidAmount, "invalid amount", nil
}
udc := s.GetUDC(param.UDC, false)
if udc == nil {
return code.TxCodeUDCNotFound, "UDC not found", nil
}
if bytes.Equal(sender, udc.Owner) == false {
match := false
for _, op := range udc.Operators {
if bytes.Equal(sender, op) {
match = true
break
}
}
if match == false {
return code.TxCodePermissionDenied, "permission denied", nil
}
}
err := s.SetUDCLock(param.UDC, param.Holder, ¶m.Amount)
if err != nil {
return code.TxCodeUnknown, "error setting internal db", nil
}
return code.TxCodeOK, "ok", nil
}