/
request.go
140 lines (116 loc) · 3.63 KB
/
request.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
package tx
import (
"bytes"
"encoding/binary"
"encoding/json"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
"github.com/amolabs/amoabci/amo/code"
"github.com/amolabs/amoabci/amo/store"
"github.com/amolabs/amoabci/amo/types"
)
type RequestParam struct {
Target tmbytes.HexBytes `json:"target"`
Payment types.Currency `json:"payment"`
Recipient crypto.Address `json:"recipient,omitempty"`
Dealer crypto.Address `json:"dealer,omitempty"`
DealerFee types.Currency `json:"dealer_fee,omitempty"`
Extra json.RawMessage `json:"extra,omitempty"`
}
func parseRequestParam(raw []byte) (RequestParam, error) {
var param RequestParam
err := json.Unmarshal(raw, ¶m)
if err != nil {
return param, err
}
return param, nil
}
type TxRequest struct {
TxBase
Param RequestParam `json:"-"`
}
var _ Tx = &TxRequest{}
func (t *TxRequest) Check() (uint32, string) {
txParam, err := parseRequestParam(t.getPayload())
if err != nil {
return code.TxCodeBadParam, err.Error()
}
rpkSize := len(txParam.Recipient)
if rpkSize != 0 && rpkSize != crypto.AddressSize {
return code.TxCodeBadParam, "improper recipient address"
}
return code.TxCodeOK, "ok"
}
func (t *TxRequest) Execute(store *store.Store) (uint32, string, []abci.Event) {
txParam, err := parseRequestParam(t.getPayload())
if err != nil {
return code.TxCodeBadParam, err.Error(), nil
}
if txParam.Payment.LessThan(zero) {
return code.TxCodeInvalidAmount, "invalid amount", nil
}
if txParam.DealerFee.LessThan(zero) {
return code.TxCodeInvalidAmount, "invalid amount", nil
}
parcel := store.GetParcel(txParam.Target, false)
if parcel == nil {
return code.TxCodeParcelNotFound, "parcel not found", nil
}
var (
requestor crypto.Address = t.GetSender()
recipient crypto.Address = t.GetSender()
target tmbytes.HexBytes = txParam.Target
request types.Request = types.Request{
Payment: txParam.Payment,
Dealer: txParam.Dealer,
DealerFee: txParam.DealerFee,
Extra: types.Extra{
Register: parcel.Extra.Register,
Request: txParam.Extra,
},
}
)
rpkSize := len(txParam.Recipient)
if rpkSize != 0 {
if rpkSize != crypto.AddressSize {
return code.TxCodeBadParam, "improper recipient address", nil
}
recipient = txParam.Recipient
request.Agency = t.GetSender()
}
if bytes.Equal(parcel.Owner, recipient) {
return code.TxCodeSelfTransaction, "requesting owned parcel", nil
}
usage := store.GetUsage(recipient, target, false)
if usage != nil {
return code.TxCodeAlreadyGranted, "parcel already granted", nil
}
existingReq := store.GetRequest(recipient, target, false)
if existingReq != nil {
return code.TxCodeAlreadyRequested, "parcel already requested", nil
}
storageID := binary.BigEndian.Uint32(target[:types.StorageIDLen])
storage := store.GetStorage(storageID, false)
if storage == nil || storage.Active == false {
return code.TxCodeNoStorage, "no active storage for this parcel", nil
}
if len(request.Dealer) == 0 {
request.DealerFee.Set(0)
} else if len(request.Dealer) != crypto.AddressSize {
return code.TxCodeBadParam, "invalid dealer address", nil
}
balance := store.GetBalance(requestor, false)
wanted, err := request.Payment.Clone()
if err != nil {
return code.TxCodeInvalidAmount, err.Error(), nil
}
wanted.Add(&request.DealerFee)
if balance.LessThan(wanted) {
return code.TxCodeNotEnoughBalance, "not enough balance", nil
}
store.SetRequest(recipient, target, &request)
balance.Sub(wanted)
store.SetBalance(requestor, balance)
return code.TxCodeOK, "ok", []abci.Event{}
}