Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
plugin
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
link33
plugin
Commits
fd74b16e
Commit
fd74b16e
authored
Nov 14, 2019
by
pengjun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#627 add issuance unit test
parent
9051d557
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
539 additions
and
6 deletions
+539
-6
collateralizedb.go
plugin/dapp/collateralize/executor/collateralizedb.go
+4
-3
issuance_test.go
plugin/dapp/issuance/executor/issuance_test.go
+531
-0
issuancedb.go
plugin/dapp/issuance/executor/issuancedb.go
+4
-3
No files found.
plugin/dapp/collateralize/executor/collateralizedb.go
View file @
fd74b16e
...
...
@@ -607,6 +607,7 @@ func (action *Action) CollateralizeBorrow(borrow *pty.CollateralizeBorrow) (*typ
coll
.
Status
=
pty
.
CollateralizeStatusCreated
coll
.
Balance
-=
borrow
.
Value
coll
.
CollBalance
+=
btyFrozen
coll
.
LatestExpireTime
=
getLatestExpireTime
(
&
coll
.
Collateralize
)
coll
.
Save
(
action
.
db
)
kv
=
append
(
kv
,
coll
.
GetKVSet
()
...
)
...
...
@@ -871,7 +872,6 @@ func (action *Action) systemLiquidation(coll *pty.Collateralize, price float32)
var
logs
[]
*
types
.
ReceiptLog
var
kv
[]
*
types
.
KeyValue
collDB
:=
&
CollateralizeDB
{
*
coll
}
for
index
,
borrowRecord
:=
range
coll
.
BorrowRecords
{
if
borrowRecord
.
LiquidationPrice
*
PriceWarningRate
<
price
{
if
borrowRecord
.
Status
==
pty
.
CollateralizeUserStatusWarning
{
...
...
@@ -922,6 +922,7 @@ func (action *Action) systemLiquidation(coll *pty.Collateralize, price float32)
// 保存
coll
.
LatestLiquidationPrice
=
getLatestLiquidationPrice
(
coll
)
coll
.
LatestExpireTime
=
getLatestExpireTime
(
coll
)
collDB
:=
&
CollateralizeDB
{
*
coll
}
collDB
.
Save
(
action
.
db
)
kv
=
append
(
kv
,
collDB
.
GetKVSet
()
...
)
...
...
@@ -934,7 +935,6 @@ func (action *Action) expireLiquidation(coll *pty.Collateralize) (*types.Receipt
var
logs
[]
*
types
.
ReceiptLog
var
kv
[]
*
types
.
KeyValue
collDB
:=
&
CollateralizeDB
{
*
coll
}
for
index
,
borrowRecord
:=
range
coll
.
BorrowRecords
{
if
borrowRecord
.
ExpireTime
-
ExpireWarningTime
>
action
.
blocktime
{
continue
...
...
@@ -981,6 +981,7 @@ func (action *Action) expireLiquidation(coll *pty.Collateralize) (*types.Receipt
// 保存
coll
.
LatestLiquidationPrice
=
getLatestLiquidationPrice
(
coll
)
coll
.
LatestExpireTime
=
getLatestExpireTime
(
coll
)
collDB
:=
&
CollateralizeDB
{
*
coll
}
collDB
.
Save
(
action
.
db
)
kv
=
append
(
kv
,
collDB
.
GetKVSet
()
...
)
...
...
@@ -1101,7 +1102,7 @@ func (action *Action) CollateralizeClose(close *pty.CollateralizeClose) (*types.
}
// 解冻ccny
receipt
,
err
=
action
.
tokenAccount
.
ExecActive
(
action
.
fromaddr
,
action
.
execaddr
,
collateralize
.
Total
Balance
*
Coin
)
receipt
,
err
=
action
.
tokenAccount
.
ExecActive
(
action
.
fromaddr
,
action
.
execaddr
,
collateralize
.
Balance
*
Coin
)
if
err
!=
nil
{
clog
.
Error
(
"IssuanceClose.ExecActive"
,
"addr"
,
action
.
fromaddr
,
"execaddr"
,
action
.
execaddr
,
"amount"
,
collateralize
.
TotalBalance
)
return
nil
,
err
...
...
plugin/dapp/issuance/executor/issuance_test.go
0 → 100644
View file @
fd74b16e
package
executor
import
(
"github.com/33cn/chain33/client"
"testing"
"time"
"github.com/33cn/chain33/account"
apimock
"github.com/33cn/chain33/client/mocks"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/crypto"
dbm
"github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/system/dapp"
pty
"github.com/33cn/chain33/system/dapp/manage/types"
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/util"
pkt
"github.com/33cn/plugin/plugin/dapp/issuance/types"
tokenE
"github.com/33cn/plugin/plugin/dapp/token/executor"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type
execEnv
struct
{
blockTime
int64
blockHeight
int64
difficulty
uint64
kvdb
dbm
.
KVDB
api
client
.
QueueProtocolAPI
db
dbm
.
KV
execAddr
string
cfg
*
types
.
Chain33Config
}
var
(
PrivKeyA
=
"0x6da92a632ab7deb67d38c0f6560bcfed28167998f6496db64c258d5e8393a81b"
// 1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4
PrivKeyB
=
"0x19c069234f9d3e61135fefbeb7791b149cdf6af536f26bebb310d4cd22c3fee4"
// 1JRNjdEqp4LJ5fqycUBm9ayCKSeeskgMKR
PrivKeyC
=
"0xc2b31057b8692a56c7dd18199df71c1d21b781c0b6858c52997c9dbf778e8550"
// 12evczYyX9ZKPYvwSEvRkRyTjpSrJuLudg
Nodes
=
[][]
byte
{
[]
byte
(
"1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4"
),
[]
byte
(
"1JRNjdEqp4LJ5fqycUBm9ayCKSeeskgMKR"
),
[]
byte
(
"12evczYyX9ZKPYvwSEvRkRyTjpSrJuLudg"
),
}
total
=
10000
*
types
.
Coin
totalToken
=
100000
*
types
.
Coin
)
func
manageKeySet
(
key
string
,
value
string
,
db
dbm
.
KV
)
{
var
item
types
.
ConfigItem
item
.
Key
=
key
item
.
Addr
=
value
item
.
Ty
=
pty
.
ConfigItemArrayConfig
emptyValue
:=
&
types
.
ArrayConfig
{
Value
:
make
([]
string
,
0
)}
arr
:=
types
.
ConfigItem_Arr
{
Arr
:
emptyValue
}
item
.
Value
=
&
arr
item
.
GetArr
()
.
Value
=
append
(
item
.
GetArr
()
.
Value
,
value
)
manageKey
:=
types
.
ManageKey
(
key
)
valueSave
:=
types
.
Encode
(
&
item
)
db
.
Set
([]
byte
(
manageKey
),
valueSave
)
}
func
initEnv
()
*
execEnv
{
cfg
:=
types
.
NewChain33Config
(
types
.
GetDefaultCfgstring
())
cfg
.
SetTitleOnlyForTest
(
"chain33"
)
Init
(
pkt
.
IssuanceX
,
cfg
,
nil
)
_
,
_
,
kvdb
:=
util
.
CreateTestDB
()
accountA
:=
types
.
Account
{
Balance
:
total
,
Frozen
:
0
,
Addr
:
string
(
Nodes
[
0
]),
}
accountAToken
:=
types
.
Account
{
Balance
:
totalToken
,
Frozen
:
0
,
Addr
:
string
(
Nodes
[
0
]),
}
accountB
:=
types
.
Account
{
Balance
:
total
,
Frozen
:
0
,
Addr
:
string
(
Nodes
[
1
]),
}
accountC
:=
types
.
Account
{
Balance
:
total
,
Frozen
:
0
,
Addr
:
string
(
Nodes
[
2
]),
}
api
:=
new
(
apimock
.
QueueProtocolAPI
)
api
.
On
(
"GetConfig"
,
mock
.
Anything
)
.
Return
(
cfg
,
nil
)
execAddr
:=
dapp
.
ExecAddress
(
pkt
.
IssuanceX
)
stateDB
,
_
:=
dbm
.
NewGoMemDB
(
"1"
,
"2"
,
100
)
accA
:=
account
.
NewCoinsAccount
(
cfg
)
accA
.
SetDB
(
stateDB
)
accA
.
SaveExecAccount
(
execAddr
,
&
accountA
)
manageKeySet
(
"issuance-manage"
,
accountA
.
Addr
,
stateDB
)
tokenAccA
,
_
:=
account
.
NewAccountDB
(
cfg
,
tokenE
.
GetName
(),
pkt
.
CCNYTokenName
,
stateDB
)
tokenAccA
.
SaveExecAccount
(
execAddr
,
&
accountAToken
)
accB
:=
account
.
NewCoinsAccount
(
cfg
)
accB
.
SetDB
(
stateDB
)
accB
.
SaveExecAccount
(
execAddr
,
&
accountB
)
manageKeySet
(
"issuance-price-feed"
,
accountB
.
Addr
,
stateDB
)
accC
:=
account
.
NewCoinsAccount
(
cfg
)
accC
.
SetDB
(
stateDB
)
accC
.
SaveExecAccount
(
execAddr
,
&
accountC
)
manageKeySet
(
"issuance-guarantor"
,
accountC
.
Addr
,
stateDB
)
return
&
execEnv
{
blockTime
:
time
.
Now
()
.
Unix
(),
blockHeight
:
cfg
.
GetDappFork
(
pkt
.
IssuanceX
,
"Enable"
),
difficulty
:
1539918074
,
kvdb
:
kvdb
,
api
:
api
,
db
:
stateDB
,
execAddr
:
execAddr
,
cfg
:
cfg
,
}
}
func
TestIssuance
(
t
*
testing
.
T
)
{
env
:=
initEnv
()
// issuance create
p1
:=
&
pkt
.
IssuanceCreateTx
{
TotalBalance
:
1000
,
DebtCeiling
:
100
,
LiquidationRatio
:
0.25
,
Period
:
5
,
}
createTx
,
err
:=
pkt
.
CreateRawIssuanceCreateTx
(
env
.
cfg
,
p1
)
if
err
!=
nil
{
t
.
Error
(
"RPC_Default_Process"
,
"err"
,
err
)
}
createTx
.
Execer
=
[]
byte
(
pkt
.
IssuanceX
)
createTx
,
err
=
signTx
(
createTx
,
PrivKeyA
)
if
err
!=
nil
{
t
.
Error
(
"RPC_Default_Process sign"
,
"err"
,
err
)
}
exec
:=
newIssuance
()
exec
.
SetAPI
(
env
.
api
)
exec
.
SetStateDB
(
env
.
db
)
assert
.
Equal
(
t
,
exec
.
GetCoinsAccount
()
.
LoadExecAccount
(
string
(
Nodes
[
0
]),
env
.
execAddr
)
.
GetBalance
(),
total
)
exec
.
SetLocalDB
(
env
.
kvdb
)
exec
.
SetEnv
(
env
.
blockHeight
,
env
.
blockTime
,
env
.
difficulty
)
receipt
,
err
:=
exec
.
Exec
(
createTx
,
int
(
1
))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
receipt
)
for
_
,
kv
:=
range
receipt
.
KV
{
env
.
db
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
receiptData
:=
&
types
.
ReceiptData
{
Ty
:
receipt
.
Ty
,
Logs
:
receipt
.
Logs
}
set
,
err
:=
exec
.
ExecLocal
(
createTx
,
receiptData
,
int
(
1
))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
set
)
for
_
,
kv
:=
range
set
.
KV
{
env
.
kvdb
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
issuanceID
:=
createTx
.
Hash
()
// query issuance by id
res
,
err
:=
exec
.
Query
(
"IssuanceInfoByID"
,
types
.
Encode
(
&
pkt
.
ReqIssuanceInfo
{
IssuanceId
:
common
.
ToHex
(
issuanceID
),}))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
res
)
// query issuance by status
res
,
err
=
exec
.
Query
(
"IssuanceByStatus"
,
types
.
Encode
(
&
pkt
.
ReqIssuanceByStatus
{
Status
:
1
}))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
res
)
// query issuances by ids
var
issuanceIDsS
[]
string
issuanceIDsS
=
append
(
issuanceIDsS
,
common
.
ToHex
(
issuanceID
))
res
,
err
=
exec
.
Query
(
"IssuanceInfoByIDs"
,
types
.
Encode
(
&
pkt
.
ReqIssuanceInfos
{
IssuanceIds
:
issuanceIDsS
}))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
res
)
// issuance price
p2
:=
&
pkt
.
IssuanceFeedTx
{}
p2
.
Price
=
append
(
p2
.
Price
,
1
)
p2
.
Volume
=
append
(
p2
.
Volume
,
100
)
createTx
,
err
=
pkt
.
CreateRawIssuanceFeedTx
(
env
.
cfg
,
p2
)
if
err
!=
nil
{
t
.
Error
(
"RPC_Default_Process"
,
"err"
,
err
)
}
createTx
.
Execer
=
[]
byte
(
pkt
.
IssuanceX
)
createTx
,
err
=
signTx
(
createTx
,
PrivKeyB
)
if
err
!=
nil
{
t
.
Error
(
"RPC_Default_Process sign"
,
"err"
,
err
)
}
exec
.
SetEnv
(
env
.
blockHeight
+
1
,
env
.
blockTime
+
1
,
env
.
difficulty
)
receipt
,
err
=
exec
.
Exec
(
createTx
,
int
(
1
))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
receipt
)
t
.
Log
(
receipt
)
for
_
,
kv
:=
range
receipt
.
KV
{
env
.
db
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
receiptData
=
&
types
.
ReceiptData
{
Ty
:
receipt
.
Ty
,
Logs
:
receipt
.
Logs
}
set
,
err
=
exec
.
ExecLocal
(
createTx
,
receiptData
,
int
(
1
))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
set
)
for
_
,
kv
:=
range
set
.
KV
{
env
.
kvdb
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
// query issuance by id
res
,
err
=
exec
.
Query
(
"IssuancePrice"
,
nil
)
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
res
)
// issuance manage
p3
:=
&
pkt
.
IssuanceManageTx
{}
p3
.
Addr
=
append
(
p3
.
Addr
,
string
(
Nodes
[
1
]))
createTx
,
err
=
pkt
.
CreateRawIssuanceManageTx
(
env
.
cfg
,
p3
)
if
err
!=
nil
{
t
.
Error
(
"RPC_Default_Process"
,
"err"
,
err
)
}
createTx
.
Execer
=
[]
byte
(
pkt
.
IssuanceX
)
createTx
,
err
=
signTx
(
createTx
,
PrivKeyA
)
if
err
!=
nil
{
t
.
Error
(
"RPC_Default_Process sign"
,
"err"
,
err
)
}
exec
.
SetEnv
(
env
.
blockHeight
+
1
,
env
.
blockTime
+
1
,
env
.
difficulty
)
receipt
,
err
=
exec
.
Exec
(
createTx
,
int
(
1
))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
receipt
)
t
.
Log
(
receipt
)
for
_
,
kv
:=
range
receipt
.
KV
{
env
.
db
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
receiptData
=
&
types
.
ReceiptData
{
Ty
:
receipt
.
Ty
,
Logs
:
receipt
.
Logs
}
set
,
err
=
exec
.
ExecLocal
(
createTx
,
receiptData
,
int
(
1
))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
set
)
for
_
,
kv
:=
range
set
.
KV
{
env
.
kvdb
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
// issuance debt
p4
:=
&
pkt
.
IssuanceDebtTx
{
IssuanceID
:
common
.
ToHex
(
issuanceID
),
Value
:
100
,
}
createTx
,
err
=
pkt
.
CreateRawIssuanceDebtTx
(
env
.
cfg
,
p4
)
if
err
!=
nil
{
t
.
Error
(
"RPC_Default_Process"
,
"err"
,
err
)
}
createTx
.
Execer
=
[]
byte
(
pkt
.
IssuanceX
)
createTx
,
err
=
signTx
(
createTx
,
PrivKeyB
)
if
err
!=
nil
{
t
.
Error
(
"RPC_Default_Process sign"
,
"err"
,
err
)
}
exec
.
SetEnv
(
env
.
blockHeight
+
1
,
env
.
blockTime
+
1
,
env
.
difficulty
)
receipt
,
err
=
exec
.
Exec
(
createTx
,
int
(
1
))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
receipt
)
t
.
Log
(
receipt
)
for
_
,
kv
:=
range
receipt
.
KV
{
env
.
db
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
receiptData
=
&
types
.
ReceiptData
{
Ty
:
receipt
.
Ty
,
Logs
:
receipt
.
Logs
}
set
,
err
=
exec
.
ExecLocal
(
createTx
,
receiptData
,
int
(
1
))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
set
)
for
_
,
kv
:=
range
set
.
KV
{
env
.
kvdb
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
debtID
:=
createTx
.
Hash
()
// query issuance by id
res
,
err
=
exec
.
Query
(
"IssuanceRecordByID"
,
types
.
Encode
(
&
pkt
.
ReqIssuanceDebtInfo
{
IssuanceId
:
common
.
ToHex
(
issuanceID
),
DebtId
:
common
.
ToHex
(
debtID
)}))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
res
)
// query issuance by status
res
,
err
=
exec
.
Query
(
"IssuanceRecordsByStatus"
,
types
.
Encode
(
&
pkt
.
ReqIssuanceRecordsByStatus
{
IssuanceId
:
common
.
ToHex
(
issuanceID
),
Status
:
1
}))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
res
)
// query issuance by addr
res
,
err
=
exec
.
Query
(
"IssuanceRecordsByAddr"
,
types
.
Encode
(
&
pkt
.
ReqIssuanceRecordsByAddr
{
IssuanceId
:
common
.
ToHex
(
issuanceID
),
Addr
:
string
(
Nodes
[
1
]),
Status
:
1
}))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
res
)
// issuance repay
p5
:=
&
pkt
.
IssuanceRepayTx
{
IssuanceID
:
common
.
ToHex
(
issuanceID
),
DebtID
:
common
.
ToHex
(
debtID
),
}
createTx
,
err
=
pkt
.
CreateRawIssuanceRepayTx
(
env
.
cfg
,
p5
)
if
err
!=
nil
{
t
.
Error
(
"RPC_Default_Process"
,
"err"
,
err
)
}
createTx
.
Execer
=
[]
byte
(
pkt
.
IssuanceX
)
createTx
,
err
=
signTx
(
createTx
,
PrivKeyB
)
if
err
!=
nil
{
t
.
Error
(
"RPC_Default_Process sign"
,
"err"
,
err
)
}
exec
.
SetEnv
(
env
.
blockHeight
+
1
,
env
.
blockTime
+
1
,
env
.
difficulty
)
receipt
,
err
=
exec
.
Exec
(
createTx
,
int
(
1
))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
receipt
)
t
.
Log
(
receipt
)
for
_
,
kv
:=
range
receipt
.
KV
{
env
.
db
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
receiptData
=
&
types
.
ReceiptData
{
Ty
:
receipt
.
Ty
,
Logs
:
receipt
.
Logs
}
set
,
err
=
exec
.
ExecLocal
(
createTx
,
receiptData
,
int
(
1
))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
set
)
for
_
,
kv
:=
range
set
.
KV
{
env
.
kvdb
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
// query issuance by status
res
,
err
=
exec
.
Query
(
"IssuanceRecordsByStatus"
,
types
.
Encode
(
&
pkt
.
ReqIssuanceRecordsByStatus
{
IssuanceId
:
common
.
ToHex
(
issuanceID
),
Status
:
6
}))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
res
)
// query issuance by addr
res
,
err
=
exec
.
Query
(
"IssuanceRecordsByAddr"
,
types
.
Encode
(
&
pkt
.
ReqIssuanceRecordsByAddr
{
IssuanceId
:
common
.
ToHex
(
issuanceID
),
Addr
:
string
(
Nodes
[
1
]),
Status
:
6
}))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
res
)
// issuance liquidate
p6
:=
&
pkt
.
IssuanceDebtTx
{
IssuanceID
:
common
.
ToHex
(
issuanceID
),
Value
:
100
,
}
createTx
,
err
=
pkt
.
CreateRawIssuanceDebtTx
(
env
.
cfg
,
p6
)
if
err
!=
nil
{
t
.
Error
(
"RPC_Default_Process"
,
"err"
,
err
)
}
createTx
.
Execer
=
[]
byte
(
pkt
.
IssuanceX
)
createTx
,
err
=
signTx
(
createTx
,
PrivKeyB
)
if
err
!=
nil
{
t
.
Error
(
"RPC_Default_Process sign"
,
"err"
,
err
)
}
exec
.
SetEnv
(
env
.
blockHeight
+
1
,
env
.
blockTime
+
1
,
env
.
difficulty
)
receipt
,
err
=
exec
.
Exec
(
createTx
,
int
(
1
))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
receipt
)
t
.
Log
(
receipt
)
for
_
,
kv
:=
range
receipt
.
KV
{
env
.
db
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
receiptData
=
&
types
.
ReceiptData
{
Ty
:
receipt
.
Ty
,
Logs
:
receipt
.
Logs
}
set
,
err
=
exec
.
ExecLocal
(
createTx
,
receiptData
,
int
(
1
))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
set
)
for
_
,
kv
:=
range
set
.
KV
{
env
.
kvdb
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
p7
:=
&
pkt
.
IssuanceFeedTx
{}
p7
.
Price
=
append
(
p7
.
Price
,
0.25
)
p7
.
Volume
=
append
(
p7
.
Volume
,
100
)
createTx
,
err
=
pkt
.
CreateRawIssuanceFeedTx
(
env
.
cfg
,
p7
)
if
err
!=
nil
{
t
.
Error
(
"RPC_Default_Process"
,
"err"
,
err
)
}
createTx
.
Execer
=
[]
byte
(
pkt
.
IssuanceX
)
createTx
,
err
=
signTx
(
createTx
,
PrivKeyB
)
if
err
!=
nil
{
t
.
Error
(
"RPC_Default_Process sign"
,
"err"
,
err
)
}
exec
.
SetEnv
(
env
.
blockHeight
+
1
,
env
.
blockTime
+
1
,
env
.
difficulty
)
receipt
,
err
=
exec
.
Exec
(
createTx
,
int
(
1
))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
receipt
)
t
.
Log
(
receipt
)
for
_
,
kv
:=
range
receipt
.
KV
{
env
.
db
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
receiptData
=
&
types
.
ReceiptData
{
Ty
:
receipt
.
Ty
,
Logs
:
receipt
.
Logs
}
set
,
err
=
exec
.
ExecLocal
(
createTx
,
receiptData
,
int
(
1
))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
set
)
for
_
,
kv
:=
range
set
.
KV
{
env
.
kvdb
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
// query issuance by status
res
,
err
=
exec
.
Query
(
"IssuanceRecordsByStatus"
,
types
.
Encode
(
&
pkt
.
ReqIssuanceRecordsByStatus
{
IssuanceId
:
common
.
ToHex
(
issuanceID
),
Status
:
3
}))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
res
)
// expire liquidate
p8
:=
&
pkt
.
IssuanceDebtTx
{
IssuanceID
:
common
.
ToHex
(
issuanceID
),
Value
:
100
,
}
createTx
,
err
=
pkt
.
CreateRawIssuanceDebtTx
(
env
.
cfg
,
p8
)
if
err
!=
nil
{
t
.
Error
(
"RPC_Default_Process"
,
"err"
,
err
)
}
createTx
.
Execer
=
[]
byte
(
pkt
.
IssuanceX
)
createTx
,
err
=
signTx
(
createTx
,
PrivKeyB
)
if
err
!=
nil
{
t
.
Error
(
"RPC_Default_Process sign"
,
"err"
,
err
)
}
exec
.
SetEnv
(
env
.
blockHeight
+
1
,
env
.
blockTime
+
1
,
env
.
difficulty
)
receipt
,
err
=
exec
.
Exec
(
createTx
,
int
(
1
))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
receipt
)
t
.
Log
(
receipt
)
for
_
,
kv
:=
range
receipt
.
KV
{
env
.
db
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
receiptData
=
&
types
.
ReceiptData
{
Ty
:
receipt
.
Ty
,
Logs
:
receipt
.
Logs
}
set
,
err
=
exec
.
ExecLocal
(
createTx
,
receiptData
,
int
(
1
))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
set
)
for
_
,
kv
:=
range
set
.
KV
{
env
.
kvdb
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
p9
:=
&
pkt
.
IssuanceFeedTx
{}
p9
.
Price
=
append
(
p9
.
Price
,
1
)
p9
.
Volume
=
append
(
p9
.
Volume
,
100
)
createTx
,
err
=
pkt
.
CreateRawIssuanceFeedTx
(
env
.
cfg
,
p9
)
if
err
!=
nil
{
t
.
Error
(
"RPC_Default_Process"
,
"err"
,
err
)
}
createTx
.
Execer
=
[]
byte
(
pkt
.
IssuanceX
)
createTx
,
err
=
signTx
(
createTx
,
PrivKeyB
)
if
err
!=
nil
{
t
.
Error
(
"RPC_Default_Process sign"
,
"err"
,
err
)
}
exec
.
SetEnv
(
env
.
blockHeight
+
1
,
env
.
blockTime
+
6
,
env
.
difficulty
)
receipt
,
err
=
exec
.
Exec
(
createTx
,
int
(
1
))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
receipt
)
t
.
Log
(
receipt
)
for
_
,
kv
:=
range
receipt
.
KV
{
env
.
db
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
receiptData
=
&
types
.
ReceiptData
{
Ty
:
receipt
.
Ty
,
Logs
:
receipt
.
Logs
}
set
,
err
=
exec
.
ExecLocal
(
createTx
,
receiptData
,
int
(
1
))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
set
)
for
_
,
kv
:=
range
set
.
KV
{
env
.
kvdb
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
// query issuance by status
res
,
err
=
exec
.
Query
(
"IssuanceRecordsByStatus"
,
types
.
Encode
(
&
pkt
.
ReqIssuanceRecordsByStatus
{
IssuanceId
:
common
.
ToHex
(
issuanceID
),
Status
:
5
}))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
res
)
// issuance close
p10
:=
&
pkt
.
IssuanceCloseTx
{
IssuanceID
:
common
.
ToHex
(
issuanceID
),
}
createTx
,
err
=
pkt
.
CreateRawIssuanceCloseTx
(
env
.
cfg
,
p10
)
if
err
!=
nil
{
t
.
Error
(
"RPC_Default_Process"
,
"err"
,
err
)
}
createTx
.
Execer
=
[]
byte
(
pkt
.
IssuanceX
)
createTx
,
err
=
signTx
(
createTx
,
PrivKeyA
)
if
err
!=
nil
{
t
.
Error
(
"RPC_Default_Process sign"
,
"err"
,
err
)
}
exec
.
SetEnv
(
env
.
blockHeight
+
2
,
env
.
blockTime
+
2
,
env
.
difficulty
)
receipt
,
err
=
exec
.
Exec
(
createTx
,
int
(
1
))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
receipt
)
for
_
,
kv
:=
range
receipt
.
KV
{
env
.
db
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
receiptData
=
&
types
.
ReceiptData
{
Ty
:
receipt
.
Ty
,
Logs
:
receipt
.
Logs
}
set
,
err
=
exec
.
ExecLocal
(
createTx
,
receiptData
,
int
(
1
))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
set
)
for
_
,
kv
:=
range
set
.
KV
{
env
.
kvdb
.
Set
(
kv
.
Key
,
kv
.
Value
)
}
// query issuance by status
res
,
err
=
exec
.
Query
(
"IssuanceByStatus"
,
types
.
Encode
(
&
pkt
.
ReqIssuanceByStatus
{
Status
:
2
}))
assert
.
Nil
(
t
,
err
)
assert
.
NotNil
(
t
,
res
)
}
func
signTx
(
tx
*
types
.
Transaction
,
hexPrivKey
string
)
(
*
types
.
Transaction
,
error
)
{
signType
:=
types
.
SECP256K1
c
,
err
:=
crypto
.
New
(
types
.
GetSignName
(
pkt
.
IssuanceX
,
signType
))
if
err
!=
nil
{
return
tx
,
err
}
bytes
,
err
:=
common
.
FromHex
(
hexPrivKey
[
:
])
if
err
!=
nil
{
return
tx
,
err
}
privKey
,
err
:=
c
.
PrivKeyFromBytes
(
bytes
)
if
err
!=
nil
{
return
tx
,
err
}
tx
.
Sign
(
int32
(
signType
),
privKey
)
return
tx
,
nil
}
//func TestSlice(t *testing.T) {
// var a []int
// a = append(a, 1)
// fmt.Println(a)
//
// a = append(a[:0], a[1:]...)
// fmt.Println(a)
//}
\ No newline at end of file
plugin/dapp/issuance/executor/issuancedb.go
View file @
fd74b16e
...
...
@@ -614,6 +614,7 @@ func (action *Action) IssuanceDebt(debt *pty.IssuanceDebt) (*types.Receipt, erro
issu
.
CollateralValue
+=
btyFrozen
issu
.
DebtValue
+=
debt
.
Value
issu
.
Balance
-=
debt
.
Value
issu
.
LatestExpireTime
=
getLatestExpireTime
(
&
issu
.
Issuance
)
issu
.
Save
(
action
.
db
)
kv
=
append
(
kv
,
issu
.
GetKVSet
()
...
)
...
...
@@ -723,7 +724,6 @@ func (action *Action) systemLiquidation(issu *pty.Issuance, price float32) (*typ
var
logs
[]
*
types
.
ReceiptLog
var
kv
[]
*
types
.
KeyValue
collDB
:=
&
IssuanceDB
{
*
issu
}
for
index
,
debtRecord
:=
range
issu
.
DebtRecords
{
if
debtRecord
.
LiquidationPrice
*
PriceWarningRate
<
price
{
if
debtRecord
.
Status
==
pty
.
IssuanceUserStatusWarning
{
...
...
@@ -773,6 +773,7 @@ func (action *Action) systemLiquidation(issu *pty.Issuance, price float32) (*typ
// 保存
issu
.
LatestLiquidationPrice
=
getLatestLiquidationPrice
(
issu
)
issu
.
LatestExpireTime
=
getLatestExpireTime
(
issu
)
collDB
:=
&
IssuanceDB
{
*
issu
}
collDB
.
Save
(
action
.
db
)
kv
=
append
(
kv
,
collDB
.
GetKVSet
()
...
)
...
...
@@ -785,7 +786,6 @@ func (action *Action) expireLiquidation(issu *pty.Issuance) (*types.Receipt, err
var
logs
[]
*
types
.
ReceiptLog
var
kv
[]
*
types
.
KeyValue
collDB
:=
&
IssuanceDB
{
*
issu
}
for
index
,
debtRecord
:=
range
issu
.
DebtRecords
{
if
debtRecord
.
ExpireTime
-
ExpireWarningTime
>
action
.
blocktime
{
continue
...
...
@@ -831,6 +831,7 @@ func (action *Action) expireLiquidation(issu *pty.Issuance) (*types.Receipt, err
// 保存
issu
.
LatestLiquidationPrice
=
getLatestLiquidationPrice
(
issu
)
issu
.
LatestExpireTime
=
getLatestExpireTime
(
issu
)
collDB
:=
&
IssuanceDB
{
*
issu
}
collDB
.
Save
(
action
.
db
)
kv
=
append
(
kv
,
collDB
.
GetKVSet
()
...
)
...
...
@@ -954,7 +955,7 @@ func (action *Action) IssuanceClose(close *pty.IssuanceClose) (*types.Receipt, e
}
// 解冻ccny
receipt
,
err
=
action
.
tokenAccount
.
ExecActive
(
action
.
fromaddr
,
action
.
execaddr
,
issuance
.
Total
Balance
*
Coin
)
receipt
,
err
=
action
.
tokenAccount
.
ExecActive
(
action
.
fromaddr
,
action
.
execaddr
,
issuance
.
Balance
*
Coin
)
if
err
!=
nil
{
clog
.
Error
(
"IssuanceClose.ExecActive"
,
"addr"
,
action
.
fromaddr
,
"execaddr"
,
action
.
execaddr
,
"amount"
,
issuance
.
TotalBalance
)
return
nil
,
err
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment