-
Notifications
You must be signed in to change notification settings - Fork 0
/
dandelion.go
273 lines (230 loc) · 8.19 KB
/
dandelion.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package dandelion
import (
"errors"
"fmt"
"github.com/coschain/contentos-go/app"
"github.com/coschain/contentos-go/app/table"
"github.com/coschain/contentos-go/common/constants"
"github.com/coschain/contentos-go/dandelion/core"
"github.com/coschain/contentos-go/prototype"
"github.com/coschain/contentos-go/vm/contract/abi"
table2 "github.com/coschain/contentos-go/vm/contract/table"
"github.com/sirupsen/logrus"
"testing"
)
type Dandelion struct {
*core.DandelionCore
}
func NewDandelion(logger *logrus.Logger) *Dandelion {
return NewDandelionWithPlugins(logger, false, nil)
}
func NewDandelionWithPlugins(logger *logrus.Logger, enablePlugins bool, sqlPlugins []string) *Dandelion {
d := &Dandelion{
DandelionCore: core.NewDandelionCore(logger, enablePlugins, sqlPlugins),
}
return d
}
type DandelionTestFunc func(*testing.T, *Dandelion)
func NewDandelionTest(f DandelionTestFunc, actors int) func(*testing.T) {
return NewDandelionTestWithPlugins(false, nil, f, actors)
}
func NewDandelionTestWithPlugins(enablePlugins bool, sqlPlugins []string, f DandelionTestFunc, actors int) func(*testing.T) {
return func(t *testing.T) {
d := NewDandelionWithPlugins(nil, enablePlugins, sqlPlugins)
if d == nil {
t.Fatal("dandelion creation failed")
}
err := d.Start()
if err != nil {
t.Fatalf("dandelion start failed: %s", err.Error())
}
defer func() {
_ = d.Stop()
}()
err = d.CreateAndFund("actor", actors, 100000 * constants.COSTokenDecimals, constants.DefaultAccountCreateFee)
if err != nil {
t.Fatalf("dandelion createAndFund failed: %s", err.Error())
}
f(t, d)
}
}
func (d *Dandelion) CreateAndFund(prefix string, n int, coins uint64, fee uint64) error {
if n <= 0 {
return nil
}
var ops []*prototype.Operation
accounts := make(map[string]*prototype.PrivateKeyType)
for i := 0; i < n; i++ {
name := fmt.Sprintf("%s%d", prefix, i)
if d.Account(name).CheckExist() {
return fmt.Errorf("account %s already exists", name)
}
priv, _ := prototype.GenerateNewKey()
pub, _ := priv.PubKey()
accounts[name] = priv
ops = append(ops,
AccountCreate(constants.COSInitMiner, name, pub, fee, ""),
Transfer(constants.COSInitMiner, name, coins, ""))
}
if err := d.SendTrxByAccount(constants.COSInitMiner, ops...); err != nil {
return err
} else if err = d.ProduceBlocks(1); err != nil {
return err
}
for name := range accounts {
if !d.Account(name).CheckExist() {
return fmt.Errorf("createAndFund account %s failed", name)
}
}
for name, priv := range accounts {
d.PutAccount(name, priv)
}
return nil
}
func (d *Dandelion) CreateAndFundUser(name string, coins uint64, fee uint64) error {
if d.Account(name).CheckExist() {
return fmt.Errorf("account %s already exists", name)
}
priv, _ := prototype.GenerateNewKey()
pub, _ := priv.PubKey()
err := d.SendTrxByAccount(constants.COSInitMiner,
AccountCreate(constants.COSInitMiner, name, pub, fee, ""),
Transfer(constants.COSInitMiner, name, coins, ""))
if err != nil {
return err
} else if err = d.ProduceBlocks(1); err != nil {
return err
}
if !d.Account(name).CheckExist() {
return fmt.Errorf("createAndFund account %s failed", name)
}
return nil
}
func (d *Dandelion) Test(f DandelionTestFunc) func(*testing.T) {
return func(t *testing.T) {
f(t, d)
}
}
//
// Table Record Retrieval by Primary keys
//
func (d *Dandelion) GlobalProps() *prototype.DynamicProperties {
return d.TrxPool().GetProps()
}
func (d *Dandelion) GetBlockProducerTopN(n uint32) ([]string, []*prototype.PublicKeyType) {
return d.TrxPool().GetBlockProducerTopN(n)
}
func (d *Dandelion) Account(name string) *DandelionAccount {
return NewDandelionAccount(name, d)
}
func (d *Dandelion) ExtTrx(trxId *prototype.Sha256) *table.SoExtTrxWrap {
return table.NewSoExtTrxWrap(d.Database(), trxId)
}
func (d *Dandelion) ExtReplyCreated(postId uint64) *table.SoExtReplyCreatedWrap {
return table.NewSoExtReplyCreatedWrap(d.Database(), &postId)
}
func (d *Dandelion) ExtUserPost(postId uint64) *table.SoExtUserPostWrap {
return table.NewSoExtUserPostWrap(d.Database(), &postId)
}
func (d *Dandelion) Blocktrxs(block uint64) *table.SoBlocktrxsWrap {
return table.NewSoBlocktrxsWrap(d.Database(), &block)
}
func (d *Dandelion) Vote(voter string, postId uint64) *table.SoVoteWrap {
return table.NewSoVoteWrap(d.Database(), &prototype.VoterId{
Voter: prototype.NewAccountName(voter),
PostId: postId,
})
}
func (d *Dandelion) ExtDailyTrx(date uint32) *table.SoExtDailyTrxWrap {
return table.NewSoExtDailyTrxWrap(d.Database(), prototype.NewTimePointSec(date))
}
func (d *Dandelion) Post(postId uint64) *table.SoPostWrap {
return table.NewSoPostWrap(d.Database(), &postId)
}
func (d *Dandelion) GiftTicket(ticket *prototype.GiftTicketKeyType) *table.SoGiftTicketWrap {
return table.NewSoGiftTicketWrap(d.Database(), ticket)
}
func (d *Dandelion) BlockProducer(owner string) *table.SoBlockProducerWrap {
return table.NewSoBlockProducerWrap(d.Database(), prototype.NewAccountName(owner))
}
func (d *Dandelion) ExtFollowing(account string, following string) *table.SoExtFollowingWrap {
return table.NewSoExtFollowingWrap(d.Database(), &prototype.FollowingRelation{
Account: prototype.NewAccountName(account),
Following: prototype.NewAccountName(following),
})
}
func (d *Dandelion) TransactionObject(trxId *prototype.Sha256) *table.SoTransactionObjectWrap {
return table.NewSoTransactionObjectWrap(d.Database(), trxId)
}
func (d *Dandelion) ReportList(uuid uint64) *table.SoReportListWrap {
return table.NewSoReportListWrap(d.Database(), &uuid)
}
func (d *Dandelion) ExtFollowCount(account string) *table.SoExtFollowCountWrap {
return table.NewSoExtFollowCountWrap(d.Database(), prototype.NewAccountName(account))
}
func (d *Dandelion) Contract(owner string, cname string) *table.SoContractWrap {
return table.NewSoContractWrap(d.Database(), &prototype.ContractId{
Owner: prototype.NewAccountName(owner),
Cname: cname,
})
}
func (d *Dandelion) ExtFollower(account string, follower string) *table.SoExtFollowerWrap {
return table.NewSoExtFollowerWrap(d.Database(), &prototype.FollowerRelation{
Account: prototype.NewAccountName(account),
Follower: prototype.NewAccountName(follower),
})
}
func (d *Dandelion) StakeRecord(from string, to string) *table.SoStakeRecordWrap {
return table.NewSoStakeRecordWrap(d.Database(), &prototype.StakeRecord{
From: prototype.NewAccountName(from),
To: prototype.NewAccountName(to),
})
}
func (d *Dandelion) BlockSummaryObject(id uint32) *table.SoBlockSummaryObjectWrap {
return table.NewSoBlockSummaryObjectWrap(d.Database(), &id)
}
func (d *Dandelion) ExtHourTrx(hour uint32) *table.SoExtHourTrxWrap {
return table.NewSoExtHourTrxWrap(d.Database(), prototype.NewTimePointSec(hour))
}
func (d *Dandelion) ExtPostCreated(postId uint64) *table.SoExtPostCreatedWrap {
return table.NewSoExtPostCreatedWrap(d.Database(), &postId)
}
func (d *Dandelion) BlockProducerVote(voter string, blockProducer string) *table.SoBlockProducerVoteWrap {
return table.NewSoBlockProducerVoteWrap(d.Database(), &prototype.BpBlockProducerId{
BlockProducer: prototype.NewAccountName(blockProducer),
Voter: prototype.NewAccountName(voter),
})
}
func (d *Dandelion) VestDelegation(orderId uint64) *table.SoVestDelegationWrap {
return table.NewSoVestDelegationWrap(d.Database(), &orderId)
}
func (d *Dandelion) CurrentRecordID() uint64 {
return table.NewSoIncIdWrap(d.Database(), &app.SingleId).GetCounter()
}
//
// Contract tables
//
func (d *Dandelion) ContractTables(owner, contract string) *table2.ContractTables {
if abiInterface, err := abi.UnmarshalABI([]byte(d.Contract(owner, contract).GetAbi())); err != nil {
return nil
} else {
return table2.NewContractTables(owner, contract, abiInterface, d.Database())
}
}
func (d *Dandelion) ModifyProps(modifier func(oldProps *prototype.DynamicProperties)) (err error) {
defer func() {
e := recover()
if e != nil && err == nil {
err = errors.New(fmt.Sprint(e))
}
}()
chainId := int32(constants.SingletonId)
dgpWrap := table.NewSoGlobalWrap(d.Database(), &chainId)
props := dgpWrap.GetProps()
modifier(props)
dgpWrap.SetProps(props, "modify global props failed")
return
}
func (d *Dandelion) CalculateUserMaxStamina(name string) uint64 {
return d.TrxPool().CalculateUserMaxStamina(d.Database(), name)
}