-
Notifications
You must be signed in to change notification settings - Fork 16
/
transaction_private.go
149 lines (119 loc) · 2.76 KB
/
transaction_private.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 transaction
// Core
// Transaction
// Copyright © 2017-2018 Eduard Sesigin. All rights reserved. Contacts: <claygod@yandex.ru>
/*
newTransaction - create new Transaction.
*/
func newTransaction(c *Core) *Transaction {
t := &Transaction{
core: c,
up: make([]*request, 0, usualNumTransaction),
reqs: make([]*request, 0, usualNumTransaction),
}
return t
}
/*
exeTransaction - execution of a transaction.
Returned codes:
ErrCodeUnitNotExist // unit not exist
ErrCodeTransactionCatch // account not catch
ErrCodeTransactionCredit // such a unit already exists
Ok
*/
func (t *Transaction) exeTransaction() errorCodes {
// catch (core)
if !t.core.catch() {
log(errMsgCoreNotCatch).context("Method", "exeTransaction").send()
return ErrCodeCoreCatch
}
defer t.core.throw()
// fill
if err := t.fill(); err != Ok {
log(errMsgTransactionNotFill).context("Method", "exeTransaction").send()
return err
}
// catch (accounts)
if err := t.catch(); err != Ok {
log(errMsgTransactionNotCatch).context("Method", "exeTransaction").send()
return err
}
// addition
for num, i := range t.reqs {
if res := i.account.addition(i.amount); res < 0 {
t.rollback(num)
t.throw(len(t.reqs))
log(errMsgAccountCredit).context("Unit", i.id).
context("Account", i.key).context("Amount", i.amount).
context("Method", "exeTransaction").context("Wrong balance", res).send()
return ErrCodeTransactionCredit
}
}
// throw
t.throw(len(t.reqs))
return Ok
}
/*
rollback - rolled back account operations.
*/
func (t *Transaction) rollback(num int) {
for i := 0; i < num; i++ {
t.reqs[i].account.addition(-t.reqs[i].amount)
}
}
/*
fill - getting accounts in the list.
Returned codes:
ErrCodeUnitNotExist // unit not exist
Ok
*/
func (t *Transaction) fill() errorCodes {
for i, r := range t.reqs {
a, err := t.core.getAccount(r.id, r.key)
if err != Ok {
// NOTE: log in method getAccount
return err
}
t.reqs[i].account = a
}
return Ok
}
/*
catch - obtaining permissions from accounts.
Returned codes:
ErrCodeTransactionCatch // account not allowed operation
Ok
*/
func (t *Transaction) catch() errorCodes {
for i, r := range t.reqs {
if !r.account.catch() {
t.throw(i)
log(errMsgAccountNotCatch).context("Unit", r.id).
context("Account", r.key).context("Method", "Transaction.catch").
context("Acc counter", r.account.counter).
context("Acc balance", r.account.balance).send()
return ErrCodeTransactionCatch
}
}
return Ok
}
/*
throw - remove permissions in accounts.
*/
func (t *Transaction) throw(num int) {
for i, r := range t.reqs {
if i >= num {
break
}
r.account.throw()
}
}
/*
request - single operation data
*/
type request struct {
id int64
key string
amount int64
account *account
}