-
Notifications
You must be signed in to change notification settings - Fork 12
/
shared.go
149 lines (121 loc) · 3.55 KB
/
shared.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
141
142
143
144
145
146
147
148
149
package executors
import (
"github.com/gallactic/gallactic/core/account"
"github.com/gallactic/gallactic/core/account/permission"
"github.com/gallactic/gallactic/core/state"
"github.com/gallactic/gallactic/core/validator"
"github.com/gallactic/gallactic/crypto"
e "github.com/gallactic/gallactic/errors"
"github.com/gallactic/gallactic/txs/tx"
)
func getInputAccount(ch *state.Cache, in tx.TxInput, req account.Permissions) (*account.Account, error) {
acc, err := ch.GetAccount(in.Address)
if err != nil {
return nil, err
}
// Check sequences
if acc.Sequence()+1 != uint64(in.Sequence) {
return nil, e.Errorf(e.ErrInvalidSequence, "%v has set sequence to %v. It should be %v", in.Address, in.Sequence, acc.Sequence()+1)
}
// Check amount
if acc.Balance() < uint64(in.Amount) {
return nil, e.Error(e.ErrInsufficientFunds)
}
if !ch.HasPermissions(acc, req) {
return nil, e.Errorf(e.ErrPermissionDenied, "%v has %v but needs %v permission", in.Address, acc.Permissions(), permission.Send)
}
return acc, nil
}
func getOutputAccount(ch *state.Cache, out tx.TxOutput) (*account.Account, error) {
if !ch.HasAccount(out.Address) {
return nil, nil
}
return ch.GetAccount(out.Address)
}
func getInputValidator(ch *state.Cache, in tx.TxInput) (*validator.Validator, error) {
val, err := ch.GetValidator(in.Address)
if err != nil {
return nil, err
}
// Check sequences
if val.Sequence()+1 != uint64(in.Sequence) {
return nil, e.Errorf(e.ErrInvalidSequence, "%v has set sequence to %v. It should be %v", in.Address, in.Sequence, val.Sequence()+1)
}
// Check amount
if val.Stake() < uint64(in.Amount) {
return nil, e.Error(e.ErrInsufficientFunds)
}
return val, nil
}
func getOutputValidator(ch *state.Cache, out tx.TxOutput) (*validator.Validator, error) {
if !ch.HasValidator(out.Address) {
return nil, nil
}
return ch.GetValidator(out.Address)
}
func getInputAccounts(ch *state.Cache, ins []tx.TxInput, req account.Permissions, accs map[crypto.Address]*account.Account) error {
for _, in := range ins {
acc, err := getInputAccount(ch, in, req)
if err != nil {
return err
}
accs[in.Address] = acc
}
return nil
}
func getOutputAccounts(ch *state.Cache, outs []tx.TxOutput, accs map[crypto.Address]*account.Account) error {
for _, out := range outs {
acc, err := getOutputAccount(ch, out)
if err != nil {
return err
}
accs[out.Address] = acc
}
return nil
}
func adjustInputAccounts(accs map[crypto.Address]*account.Account, ins []tx.TxInput) error {
for _, in := range ins {
acc := accs[in.Address]
if acc == nil {
return e.Error(e.ErrInvalidAddress)
}
if err := adjustInputAccount(acc, in); err != nil {
return err
}
}
return nil
}
func adjustOutputAccounts(accs map[crypto.Address]*account.Account, outs []tx.TxOutput) error {
for _, out := range outs {
acc := accs[out.Address]
if acc == nil {
return e.Error(e.ErrInvalidAddress)
}
if err := adjustOutputAccount(acc, out); err != nil {
return err
}
}
return nil
}
func adjustInputAccount(acc *account.Account, in tx.TxInput) error {
err := acc.SubtractFromBalance(in.Amount)
if err != nil {
return err
}
acc.IncSequence()
return nil
}
func adjustOutputAccount(acc *account.Account, out tx.TxOutput) error {
return acc.AddToBalance(out.Amount)
}
func adjustInputValidator(val *validator.Validator, in tx.TxInput) error {
err := val.SubtractFromStake(in.Amount)
if err != nil {
return err
}
val.IncSequence()
return nil
}
func adjustOutputValidator(val *validator.Validator, out tx.TxOutput) error {
return val.AddToStake(out.Amount)
}