Commit 4483d296 authored by vipwzw's avatar vipwzw Committed by 33cn

ticket add price

parent 715c73dd
Title="bityuan"
TestNet=true
FixTime=false
[log]
# 日志级别,支持debug(dbug)/info/warn/error(eror)/crit
loglevel = "debug"
logConsoleLevel = "info"
# 日志文件名,可带目录,所有生成的日志文件都放到此目录下
logFile = "logs/chain33.log"
# 单个日志文件的最大值(单位:兆)
maxFileSize = 300
# 最多保存的历史日志文件个数
maxBackups = 100
# 最多保存的历史日志消息(单位:天)
maxAge = 28
# 日志文件名是否使用本地事件(否则使用UTC时间)
localTime = true
# 历史日志文件是否压缩(压缩格式为gz)
compress = true
# 是否打印调用源文件和行号
callerFile = false
# 是否打印调用方法
callerFunction = false
[blockchain]
defCacheSize=128
maxFetchBlockNum=128
timeoutSeconds=5
batchBlockNum=128
driver="memdb"
dbPath="datadir"
dbCache=64
isStrongConsistency=false
singleMode=true
batchsync=false
isRecordBlockSequence=true
isParaChain=false
enableTxQuickIndex=false
[p2p]
seeds=[]
enable=false
isSeed=false
serverStart=true
innerSeedEnable=true
useGithub=true
innerBounds=300
msgCacheSize=10240
driver="memdb"
dbPath="datadir/addrbook"
dbCache=4
grpcLogFile="grpc33.log"
[rpc]
jrpcBindAddr="localhost:0"
grpcBindAddr="localhost:0"
whitelist=["127.0.0.1"]
jrpcFuncWhitelist=["*"]
grpcFuncWhitelist=["*"]
[mempool]
name="timeline"
poolCacheSize=10240
minTxFee=100000
maxTxNumPerAccount=10000
[consensus]
name="ticket"
minerstart=true
genesisBlockTime=1514533394
genesis="14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
[mver.consensus]
fundKeyAddr = "1BQXS6TxaYYG5mADaWij4AxhZZUTpw95a5"
coinReward = 18
coinDevFund = 12
ticketPrice = 10000
powLimitBits = "0x1f00ffff"
retargetAdjustmentFactor = 4
futureBlockTime = 16
ticketFrozenTime = 5
ticketWithdrawTime = 10
ticketMinerWaitTime = 2
maxTxNumber = 1600
targetTimespan = 2304
targetTimePerBlock = 16
[mver.consensus.ForkChainParamV1]
maxTxNumber = 10000
targetTimespan = 288 #only for test
targetTimePerBlock = 2
[mver.consensus.ForkChainParamV2]
coinReward = 5
coinDevFund = 3
ticketPrice = 3000
maxTxNumber = 1500
targetTimespan = 720
targetTimePerBlock = 1
powLimitBits = "0x1f2fffff"
[consensus.sub.solo]
genesis="14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
genesisBlockTime=1514533394
waitTxMs=10
[consensus.sub.ticket]
genesisBlockTime=1514533394
[[consensus.sub.ticket.genesis]]
minerAddr="12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv"
returnAddr="14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
count=10000
[[consensus.sub.ticket.genesis]]
minerAddr="1PUiGcbsccfxW3zuvHXZBJfznziph5miAo"
returnAddr="1EbDHAXpoiewjPLX9uqoz38HsKqMXayZrF"
count=100
[[consensus.sub.ticket.genesis]]
minerAddr="1EDnnePAZN48aC2hiTDzhkczfF39g1pZZX"
returnAddr="1KcCVZLSQYRUwE5EXTsAoQs9LuJW6xwfQa"
count=100
[store]
name="mavl"
driver="memdb"
dbPath="datadir/mavltree"
dbCache=128
[store.sub.mavl]
enableMavlPrefix=false
enableMVCC=false
[wallet]
minFee=100000
driver="memdb"
dbPath="wallet"
dbCache=16
signType="secp256k1"
[wallet.sub.ticket]
minerdisable=false
minerwhitelist=["*"]
minerWaitTime="1s"
[exec]
isFree=false
minExecFee=100000
enableStat=false
enableMVCC=false
alias=["token1:token","token2:token","token3:token"]
[exec.sub.token]
saveTokenTxList=true
tokenApprs = [
"1Bsg9j6gW83sShoee1fZAt9TkUjcrCgA9S",
"1Q8hGLfoGe63efeWa8fJ4Pnukhkngt6poK",
"1LY8GFia5EiyoTodMLfkB5PHNNpXRqxhyB",
"1GCzJDS6HbgTQ2emade7mEJGGWFfA15pS9",
"1JYB8sxi4He5pZWHCd3Zi2nypQ4JMB6AxN",
"12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv",
]
[exec.sub.relay]
genesis="14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
[exec.sub.cert]
# 是否启用证书验证和签名
enable=false
# 加密文件路径
cryptoPath="authdir/crypto"
# 带证书签名类型,支持"auth_ecdsa", "auth_sm2"
signType="auth_ecdsa"
[exec.sub.manage]
superManager=[
"1Bsg9j6gW83sShoee1fZAt9TkUjcrCgA9S",
"12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv",
"1Q8hGLfoGe63efeWa8fJ4Pnukhkngt6poK"
]
#系统中所有的fork,默认用chain33的测试网络的
#但是我们可以替换
[fork.system]
ForkChainParamV1= 0
ForkCheckTxDup=0
ForkBlockHash= 1
ForkMinerTime= 0
ForkTransferExec= 100000
ForkExecKey= 200000
ForkTxGroup= 200000
ForkResetTx0= 200000
ForkWithdraw= 200000
ForkExecRollback= 450000
ForkCheckBlockTime=-1 #fork 6.2
ForkTxHeight= -1
ForkTxGroupPara= -1
ForkChainParamV2= 1
ForkMultiSignAddress=-1 #fork 6.2
ForkStateDBSet=-1 #fork 6.2
ForkLocalDBAccess=-1 #fork 6.2
ForkBlockCheck=-1 #fork 6.2
[fork.sub.coins]
Enable=0
[fork.sub.ticket]
Enable=0
ForkTicketId = 1600000
\ No newline at end of file
package executor_test
import (
"encoding/hex"
"fmt"
"strings"
"testing"
"github.com/33cn/chain33/account"
"github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/util"
"github.com/33cn/chain33/util/testnode"
"github.com/33cn/plugin/plugin/dapp/ticket/executor"
ty "github.com/33cn/plugin/plugin/dapp/ticket/types"
"github.com/stretchr/testify/assert"
_ "github.com/33cn/chain33/system"
_ "github.com/33cn/plugin/plugin/consensus/init"
_ "github.com/33cn/plugin/plugin/dapp/ticket"
)
var mock33 *testnode.Chain33Mock
func TestMain(m *testing.M) {
mock33 = testnode.New("testdata/chain33.cfg.toml", nil)
mock33.Listen()
m.Run()
mock33.Close()
}
func TestTicketPrice(t *testing.T) {
//test price
ti := &executor.DB{}
assert.Equal(t, ti.GetRealPrice(), 10000*types.Coin)
ti = &executor.DB{}
ti.Price = 10
assert.Equal(t, ti.GetRealPrice(), int64(10))
}
func TestCheckFork(t *testing.T) {
assert.Equal(t, int64(1), types.GetFork("ForkChainParamV2"))
p1 := types.GetP(0)
assert.Equal(t, int64(10000*types.Coin), p1.TicketPrice)
p1 = types.GetP(1)
assert.Equal(t, int64(3000*types.Coin), p1.TicketPrice)
p1 = types.GetP(2)
assert.Equal(t, int64(3000*types.Coin), p1.TicketPrice)
p1 = types.GetP(3)
assert.Equal(t, int64(3000*types.Coin), p1.TicketPrice)
}
func TestTicket(t *testing.T) {
reply, err := mock33.GetAPI().ExecWalletFunc("ticket", "WalletAutoMiner", &ty.MinerFlag{Flag: 1})
assert.Nil(t, err)
assert.Equal(t, true, reply.(*types.Reply).IsOk)
acc := account.NewCoinsAccount()
addr := mock33.GetGenesisAddress()
accounts, err := acc.GetBalance(mock33.GetAPI(), &types.ReqBalance{Execer: "ticket", Addresses: []string{addr}})
assert.Nil(t, err)
assert.Equal(t, accounts[0].Balance, int64(0))
hotaddr := mock33.GetHotAddress()
_, err = acc.GetBalance(mock33.GetAPI(), &types.ReqBalance{Execer: "coins", Addresses: []string{hotaddr}})
assert.Nil(t, err)
//assert.Equal(t, accounts[0].Balance, int64(1000000000000))
//send to address
tx := util.CreateCoinsTx(mock33.GetHotKey(), mock33.GetGenesisAddress(), types.Coin/100)
mock33.SendTx(tx)
mock33.Wait()
//bind miner
tx = createBindMiner(t, hotaddr, addr, mock33.GetGenesisKey())
hash := mock33.SendTx(tx)
detail, err := mock33.WaitTx(hash)
assert.Nil(t, err)
//debug:
//js, _ := json.MarshalIndent(detail, "", " ")
//fmt.Println(string(js))
_, err = mock33.GetAPI().ExecWalletFunc("ticket", "WalletAutoMiner", &ty.MinerFlag{Flag: 0})
assert.Nil(t, err)
status, err := mock33.GetAPI().GetWalletStatus()
assert.Nil(t, err)
assert.Equal(t, false, status.IsAutoMining)
assert.Equal(t, int32(2), detail.Receipt.Ty)
_, err = mock33.GetAPI().ExecWalletFunc("ticket", "WalletAutoMiner", &ty.MinerFlag{Flag: 1})
assert.Nil(t, err)
status, err = mock33.GetAPI().GetWalletStatus()
assert.Nil(t, err)
assert.Equal(t, true, status.IsAutoMining)
for i := mock33.GetLastBlock().Height; i < 100; i++ {
err = mock33.WaitHeight(i)
assert.Nil(t, err)
//查询票是否自动close,并且购买了新的票
req := &types.ReqWalletTransactionList{Count: 1000}
list, err := mock33.GetAPI().WalletTransactionList(req)
assert.Nil(t, err)
hastclose := false
hastopen := false
for _, tx := range list.TxDetails {
if tx.Height < 1 {
continue
}
if tx.ActionName == "tclose" && tx.Receipt.Ty == 2 {
hastclose = true
}
if tx.ActionName == "topen" && tx.Receipt.Ty == 2 {
hastopen = true
fmt.Println(tx)
list := ticketList(t, mock33, &ty.TicketList{Addr: tx.Fromaddr, Status: 1})
for _, ti := range list.GetTickets() {
if strings.Contains(ti.TicketId, hex.EncodeToString(tx.Txhash)) {
assert.Equal(t, 3000*types.Coin, ti.Price)
}
}
}
}
if hastclose && hastopen {
return
}
}
t.Error("wait 100 , open and close not happened")
}
func createBindMiner(t *testing.T, m, r string, priv crypto.PrivKey) *types.Transaction {
ety := types.LoadExecutorType("ticket")
tx, err := ety.Create("Tbind", &ty.TicketBind{MinerAddress: m, ReturnAddress: r})
assert.Nil(t, err)
tx, err = types.FormatTx("ticket", tx)
assert.Nil(t, err)
tx.Sign(types.SECP256K1, priv)
return tx
}
func ticketList(t *testing.T, mock33 *testnode.Chain33Mock, req *ty.TicketList) *ty.ReplyTicketList {
data, err := mock33.GetAPI().Query("ticket", "TicketList", req)
assert.Nil(t, err)
return data.(*ty.ReplyTicketList)
}
......@@ -6,6 +6,7 @@ package executor
//database opeartion for execs ticket
import (
//"bytes"
"encoding/hex"
"errors"
......@@ -33,8 +34,17 @@ type DB struct {
prevstatus int32
}
//GetRealPrice 获取真实的价格
func (ticket *DB) GetRealPrice() int64 {
if ticket.GetPrice() == 0 {
cfg := types.GetP(types.GetFork("ForkChainParamV1"))
return cfg.TicketPrice
}
return ticket.GetPrice()
}
// NewDB new instance
func NewDB(id, minerAddress, returnWallet string, blocktime int64, isGenesis bool) *DB {
func NewDB(id, minerAddress, returnWallet string, blocktime, height, price int64, isGenesis bool) *DB {
t := &DB{}
t.TicketId = id
t.MinerAddress = minerAddress
......@@ -43,6 +53,9 @@ func NewDB(id, minerAddress, returnWallet string, blocktime int64, isGenesis boo
t.Status = 1
t.IsGenesis = isGenesis
t.prevstatus = 0
if types.IsFork(height, "ForkChainParamV2") {
t.Price = price
}
return t
}
......@@ -131,7 +144,7 @@ func (action *Action) GenesisInit(genesis *ty.TicketGenesis) (*types.Receipt, er
cfg := types.GetP(action.height)
for i := 0; i < int(genesis.Count); i++ {
id := prefix + fmt.Sprintf("%010d", i)
t := NewDB(id, genesis.MinerAddress, genesis.ReturnAddress, action.blocktime, true)
t := NewDB(id, genesis.MinerAddress, genesis.ReturnAddress, action.blocktime, action.height, cfg.TicketPrice, true)
//冻结子账户资金
receipt, err := action.coinsAccount.ExecFrozen(genesis.ReturnAddress, action.execaddr, cfg.TicketPrice)
if err != nil {
......@@ -236,8 +249,7 @@ func (action *Action) TicketOpen(topen *ty.TicketOpen) (*types.Receipt, error) {
}
id = id + ":" + fmt.Sprintf("%x:%d", topen.PubHashes[i], topen.RandSeed)
}
t := NewDB(id, topen.MinerAddress, topen.ReturnAddress, action.blocktime, false)
t := NewDB(id, topen.MinerAddress, topen.ReturnAddress, action.blocktime, action.height, cfg.TicketPrice, false)
//冻结子账户资金
receipt, err := action.coinsAccount.ExecFrozen(topen.ReturnAddress, action.execaddr, cfg.TicketPrice)
......@@ -384,7 +396,7 @@ func (action *Action) TicketClose(tclose *ty.TicketClose) (*types.Receipt, error
if t.prevstatus == 1 {
t.MinerValue = 0
}
retValue := cfg.TicketPrice + t.MinerValue
retValue := t.GetRealPrice() + t.MinerValue
receipt1, err := action.coinsAccount.ExecActive(t.ReturnAddress, action.execaddr, retValue)
if err != nil {
tlog.Error("TicketClose.ExecActive user", "addr", t.ReturnAddress, "execaddr", action.execaddr, "value", retValue)
......
......@@ -18,6 +18,8 @@ message Ticket {
string minerAddress = 6;
// return wallet
string returnAddress = 7;
//miner Price
int64 price = 9;
}
// message for execs.ticket
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment