-
Notifications
You must be signed in to change notification settings - Fork 16
/
send.go
138 lines (107 loc) · 3.24 KB
/
send.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
package transfer
import (
"encoding/json"
"fmt"
"github.com/tendermint/tendermint/libs/kv"
"github.com/pkg/errors"
"github.com/Oneledger/protocol/action"
)
var _ action.Msg = &Send{}
type Send struct {
From action.Address `json:"from"`
To action.Address `json:"to"`
Amount action.Amount `json:"amount"`
}
func (s Send) Marshal() ([]byte, error) {
return json.Marshal(s)
}
func (s *Send) Unmarshal(data []byte) error {
return json.Unmarshal(data, s)
}
func (s Send) Signers() []action.Address {
return []action.Address{s.From.Bytes()}
}
func (s Send) Type() action.Type {
return action.SEND
}
func (s Send) Tags() kv.Pairs {
tags := make([]kv.Pair, 0)
tag := kv.Pair{
Key: []byte("tx.type"),
Value: []byte(s.Type().String()),
}
tag2 := kv.Pair{
Key: []byte("tx.owner"),
Value: s.From.Bytes(),
}
tag3 := kv.Pair{
Key: []byte("tx.to"),
Value: s.To.Bytes(),
}
tags = append(tags, tag, tag2, tag3)
return tags
}
var _ action.Tx = sendTx{}
type sendTx struct {
}
func (sendTx) Validate(ctx *action.Context, tx action.SignedTx) (bool, error) {
send := &Send{}
err := send.Unmarshal(tx.Data)
if err != nil {
return false, errors.Wrap(action.ErrWrongTxType, err.Error())
}
//validate basic signature
err = action.ValidateBasic(tx.RawBytes(), send.Signers(), tx.Signatures)
if err != nil {
return false, err
}
err = action.ValidateFee(ctx.FeePool.GetOpt(), tx.Fee)
if err != nil {
return false, err
}
//validate transaction specific field
if !send.Amount.IsValid(ctx.Currencies) {
return false, errors.Wrap(action.ErrInvalidAmount, send.Amount.String())
}
if send.From.Err() != nil || send.To.Err() != nil {
return false, action.ErrInvalidAddress
}
return true, nil
}
func (s sendTx) ProcessCheck(ctx *action.Context, tx action.RawTx) (ok bool, result action.Response) {
ctx.Logger.Detail("Processing Send Transaction for CheckTx", tx)
ok, result = runTx(ctx, tx)
return
}
func (s sendTx) ProcessDeliver(ctx *action.Context, tx action.RawTx) (ok bool, result action.Response) {
ctx.Logger.Detail("Processing Send Transaction for DeliverTx", tx)
ok, result = runTx(ctx, tx)
return
}
func (sendTx) ProcessFee(ctx *action.Context, signedTx action.SignedTx, start action.Gas, size action.Gas, gasUsed action.Gas) (bool, action.Response) {
return action.BasicFeeHandling(ctx, signedTx, start, size, 1)
}
func runTx(ctx *action.Context, tx action.RawTx) (bool, action.Response) {
balances := ctx.Balances
send := &Send{}
err := send.Unmarshal(tx.Data)
if err != nil {
return false, action.Response{Log: err.Error()}
}
if !send.Amount.IsValid(ctx.Currencies) {
log := fmt.Sprint("amount is invalid", send.Amount, ctx.Currencies)
return false, action.Response{Log: log}
}
coin := send.Amount.ToCoin(ctx.Currencies)
err = balances.MinusFromAddress(send.From.Bytes(), coin)
if err != nil {
log := fmt.Sprint("error debiting balance in send transaction ", send.From, "err", err)
return false, action.Response{Log: log}
}
err = balances.AddToAddress(send.To.Bytes(), coin)
if err != nil {
log := fmt.Sprint("error crediting balance in send transaction ", send.From, "err", err)
return false, action.Response{Log: log}
}
return true, action.Response{Events: action.GetEvent(send.Tags(), "send_tx")}
}