Unverified Commit 13f60945 authored by lyh169's avatar lyh169 Committed by GitHub

Merge branch 'master' into mod_global_config

parents 93bd040a e89dbbfa
......@@ -25,6 +25,7 @@ default: depends build
build: depends
go build $(BUILD_FLAGS) -v -i -o $(APP)
go build $(BUILD_FLAGS) -v -i -o $(CLI) $(SRC_CLI)
go build $(BUILD_FLAGS) -v -i -o build/fork-config github.com/33cn/plugin/cli/fork_config/
@cp chain33.toml $(CHAIN33_PATH)/build/system-test-rpc.sh build/
@cp chain33.para.toml build/ci/paracross/
......
#!/bin/bash
# 在 plugin/plugin_type/plugin_name 找出fork
function subdir_forks() {
plugin_dir=$1
plugin_name=$2
full_dir=$1
forks=$(grep types.RegisterDappFork "${full_dir}" -R | cut -d '(' -f 2 | cut -d ')' -f 1 | sed 's/ //g')
if [ -z "${forks}" ]; then
return
fi
cnt=$(echo "${forks}" | grep "^\"" | wc -l)
if [ $cnt -gt 0 ]; then
name=$(echo $forks | head -n1 | cut -d ',' -f 1 | sed 's/"//g')
echo "[fork.sub.${name}]"
else
echo "[fork.sub.${plugin_name}]";
fi
for fork in "${forks}"
do
echo "${fork}" | awk -F ',' '{ \
if(match($2,"\"")) gsub("\"","",$2); else gsub("X$","",$2); \
print $2 "=" $3}'
#/*print "debug" $1 $2 $3;*/ \
done
echo
}
dir=$(go list -f '{{.Dir}}' github.com/33cn/plugin)/plugin/
plugins=$(find $dir -maxdepth 2 -mindepth 2 -type d | sort)
for plugin in ${plugins}
do
name=$(echo $plugin | sed 's/.*\///g')
subdir_forks $plugin $name
done
package main
import (
"fmt"
"os"
"sort"
"strings"
_ "github.com/33cn/chain33/system"
"github.com/33cn/chain33/types"
_ "github.com/33cn/plugin/plugin"
)
func main() {
forks, err := types.CloneFork("chain33")
if err != nil {
fmt.Printf("clone fork failed: %v", err)
return
}
fmtForks(forks)
}
/*
两个规则:
key 有 ".", Part1.Part2 为 [fork.sub.Part1] Part2=value
key 没有 "." [fork.system] key=value
把相同段的fork打印到一起
[fork.system]
ForkChainParamV1= 0 # ForkBlockCheck=1560000
[fork.sub.ticket]
Enable=0 # manage.ForkManageExec=400000
[fork.sub.store-kvmvccmavl]
ForkKvmvccmavl=2270000 # store-kvmvccmavl.ForkKvmvccmavl=1870000
*/
func fmtForks(forks map[string]int64) {
systemFork := make(map[string]int64)
subFork := make(map[string]map[string]int64)
for k, v := range forks {
if strings.Contains(k, ".") {
str2 := strings.SplitN(k, ".", 2)
if len(str2) != 2 {
fmt.Fprintf(os.Stderr, "can't deal key=%s ", k)
continue
}
_, ok := subFork[str2[0]]
if !ok {
subFork[str2[0]] = make(map[string]int64)
}
subFork[str2[0]][str2[1]] = v
} else {
systemFork[k] = v
}
}
fmt.Println("[fork.system]")
for k, v := range systemFork {
fmt.Printf("%s=%d\n", k, v)
}
fmt.Println("")
plugins := make([]string, 0)
for plugin := range subFork {
plugins = append(plugins, plugin)
}
sort.Strings(plugins)
for _, plugin := range plugins {
fmt.Printf("[fork.sub.%s]\n", plugin)
forks := subFork[plugin]
for k, v := range forks {
fmt.Printf("%s=%d\n", k, v)
}
fmt.Println("")
}
}
......@@ -3,7 +3,7 @@ module github.com/33cn/plugin
go 1.12
require (
github.com/33cn/chain33 v0.0.0-20190925142515-31e357c36c74
github.com/33cn/chain33 v0.0.0-20191011025601-06dbefe7d2e8
github.com/BurntSushi/toml v0.3.1
github.com/NebulousLabs/Sia v1.3.7
github.com/btcsuite/btcd v0.0.0-20181013004428-67e573d211ac
......
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/33cn/chain33 v0.0.0-20190925142515-31e357c36c74 h1:8PC5TDbLIV5haxz3uhiSS2zrgDwkAFQKOSa6KgNTn9c=
github.com/33cn/chain33 v0.0.0-20190925142515-31e357c36c74/go.mod h1:4I8n+Zyf3t0UKM5jjpqJY627Tub62oXkLsdzIv4r6rQ=
github.com/33cn/chain33 v0.0.0-20191011025601-06dbefe7d2e8 h1:YorXd8yAS26S49lY8Mdmn7Z5HhGJ1pat9QfXAEZfkw4=
github.com/33cn/chain33 v0.0.0-20191011025601-06dbefe7d2e8/go.mod h1:4I8n+Zyf3t0UKM5jjpqJY627Tub62oXkLsdzIv4r6rQ=
github.com/AndreasBriese/bbloom v0.0.0-20180913140656-343706a395b7 h1:PqzgE6kAMi81xWQA2QIVxjWkFHptGgC547vchpUbtFo=
github.com/AndreasBriese/bbloom v0.0.0-20180913140656-343706a395b7/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
......
......@@ -39,7 +39,7 @@ const (
ProposalPOLID = byte(0x05)
VoteID = byte(0x06)
HasVoteID = byte(0x07)
VoteSetMaj23ID = byte(0X08)
VoteSetMaj23ID = byte(0x08)
VoteSetBitsID = byte(0x09)
ProposalHeartbeatID = byte(0x0a)
ProposalBlockID = byte(0x0b)
......
package executor
import (
"testing"
"github.com/33cn/chain33/account"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/crypto"
dbm "github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/util"
pkt "github.com/33cn/plugin/plugin/dapp/pokerbull/types"
"github.com/stretchr/testify/assert"
)
type execEnv struct {
blockTime int64
blockHeight int64
difficulty uint64
}
var (
PrivKeyA = "0x6da92a632ab7deb67d38c0f6560bcfed28167998f6496db64c258d5e8393a81b" // 1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4
PrivKeyB = "0x19c069234f9d3e61135fefbeb7791b149cdf6af536f26bebb310d4cd22c3fee4" // 1JRNjdEqp4LJ5fqycUBm9ayCKSeeskgMKR
Nodes = [][]byte{
[]byte("1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4"),
[]byte("1JRNjdEqp4LJ5fqycUBm9ayCKSeeskgMKR"),
}
)
func TestPokerbull(t *testing.T) {
types.SetTitleOnlyForTest("chain33")
total := 1000 * types.Coin
accountA := types.Account{
Balance: total,
Frozen: 0,
Addr: string(Nodes[0]),
}
accountB := types.Account{
Balance: total,
Frozen: 0,
Addr: string(Nodes[1]),
}
execAddr := dapp.ExecAddress(pkt.PokerBullX)
stateDB, _ := dbm.NewGoMemDB("1", "2", 100)
_, _, kvdb := util.CreateTestDB()
accA := account.NewCoinsAccount()
accA.SetDB(stateDB)
accA.SaveExecAccount(execAddr, &accountA)
accB := account.NewCoinsAccount()
accB.SetDB(stateDB)
accB.SaveExecAccount(execAddr, &accountB)
env := execEnv{
10,
types.GetDappFork(pkt.PokerBullX, "Enable"),
1539918074,
}
// start game
p1 := &pkt.PBGameStart{
Value: 5 * types.Coin,
PlayerNum: 2,
}
createTx, err := types.CallCreateTransaction(pkt.PokerBullX, "Start", p1)
if err != nil {
t.Error("RPC_Default_Process", "err", err)
}
createTx.Execer = pkt.ExecerPokerBull
createTx, err = signTx(createTx, PrivKeyA)
if err != nil {
t.Error("RPC_Default_Process sign", "err", err)
}
exec := newPBGame()
exec.SetStateDB(stateDB)
assert.Equal(t, exec.GetCoinsAccount().LoadExecAccount(string(Nodes[0]), execAddr).GetBalance(), total)
exec.SetLocalDB(kvdb)
exec.SetEnv(env.blockHeight, env.blockTime, 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 {
stateDB.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 {
kvdb.Set(kv.Key, kv.Value)
}
gameID := createTx.Hash()
// start game p2
createTx, err = types.CallCreateTransaction(pkt.PokerBullX, "Start", p1)
if err != nil {
t.Error("RPC_Default_Process", "err", err)
}
createTx.Execer = pkt.ExecerPokerBull
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 {
stateDB.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 {
kvdb.Set(kv.Key, kv.Value)
}
// continue game
p2 := &pkt.PBGameContinue{
GameId: common.ToHex(gameID),
}
createTx, err = types.CallCreateTransaction(pkt.PokerBullX, "Continue", p2)
if err != nil {
t.Error("RPC_Default_Process", "err", err)
}
createTx.Execer = pkt.ExecerPokerBull
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 {
stateDB.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 {
kvdb.Set(kv.Key, kv.Value)
}
// quit game
p3 := &pkt.PBGameQuit{
GameId: common.ToHex(gameID),
}
createTx, err = types.CallCreateTransaction(pkt.PokerBullX, "Quit", p3)
if err != nil {
t.Error("RPC_Default_Process", "err", err)
}
createTx.Execer = pkt.ExecerPokerBull
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 {
stateDB.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 {
kvdb.Set(kv.Key, kv.Value)
}
// query
res, err := exec.Query(pkt.FuncNameQueryGameByID, types.Encode(&pkt.QueryPBGameInfo{GameId: common.ToHex(gameID)}))
assert.Nil(t, err)
assert.NotNil(t, res)
res, err = exec.Query(pkt.FuncNameQueryGameByAddr, types.Encode(&pkt.QueryPBGameInfo{Addr: string(Nodes[0])}))
assert.Nil(t, err)
assert.NotNil(t, res)
res, err = exec.Query(pkt.FuncNameQueryGameByStatus, types.Encode(&pkt.QueryPBGameInfo{Status: pkt.PBGameActionQuit}))
assert.Nil(t, err)
assert.NotNil(t, res)
res, err = exec.Query(pkt.FuncNameQueryGameByRound, types.Encode(&pkt.QueryPBGameByRound{GameId: common.ToHex(gameID), Round: int32(1)}))
assert.Nil(t, err)
assert.NotNil(t, res)
var gameIDsS []string
gameIDsS = append(gameIDsS, common.ToHex(gameID))
res, err = exec.Query(pkt.FuncNameQueryGameListByIDs, types.Encode(&pkt.QueryPBGameInfos{GameIds: gameIDsS}))
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.PokerBullX, 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
}
......@@ -41,7 +41,9 @@ func TestJRPCChannel(t *testing.T) {
{fn: testStartRawTxCmd},
{fn: testContinueRawTxCmd},
{fn: testQuitRawTxCmd},
{fn: testPlayRawTxCmd},
}
for _, testCase := range testCases {
err := testCase.fn(t, jrpcClient)
assert.Nil(t, err)
......@@ -93,6 +95,17 @@ func testContinueRawTxCmd(t *testing.T, jrpc *jsonclient.JSONClient) error {
return jrpc.Call("Chain33.CreateTransaction", params, &res)
}
func testPlayRawTxCmd(t *testing.T, jrpc *jsonclient.JSONClient) error {
payload := &pty.PBGamePlay{GameId: "123", Round: 1, Value: 5, Address: []string{"a", "b"}}
params := &rpctypes.CreateTxIn{
Execer: types.ExecName(pty.PokerBullX),
ActionName: pty.CreatePlayTx,
Payload: types.MustPBToJSON(payload),
}
var res string
return jrpc.Call("Chain33.CreateTransaction", params, &res)
}
func testQuitRawTxCmd(t *testing.T, jrpc *jsonclient.JSONClient) error {
payload := &pty.PBGameQuit{GameId: "123"}
params := &rpctypes.CreateTxIn{
......
......@@ -10,7 +10,7 @@ import (
rpctypes "github.com/33cn/chain33/rpc/types"
"github.com/33cn/chain33/types"
ty "github.com/33cn/plugin/plugin/dapp/ticket/types"
context "golang.org/x/net/context"
"golang.org/x/net/context"
)
func bindMiner(cfg *types.Chain33Config, param *ty.ReqBindMiner) (*ty.ReplyBindMiner, error) {
......@@ -28,15 +28,7 @@ func bindMiner(cfg *types.Chain33Config, param *ty.ReqBindMiner) (*ty.ReplyBindM
// CreateBindMiner 创建绑定挖矿
func (g *channelClient) CreateBindMiner(ctx context.Context, in *ty.ReqBindMiner) (*ty.ReplyBindMiner, error) {
header, err := g.GetLastHeader()
if err != nil {
return nil, err
}
cfg := g.GetConfig()
if in.Amount%ty.GetTicketMinerParam(cfg, header.Height).TicketPrice != 0 || in.Amount < 0 {
return nil, types.ErrAmount
}
err = address.CheckAddress(in.BindAddr)
err := address.CheckAddress(in.BindAddr)
if err != nil {
return nil, err
}
......@@ -44,8 +36,17 @@ func (g *channelClient) CreateBindMiner(ctx context.Context, in *ty.ReqBindMiner
if err != nil {
return nil, err
}
cfg := g.GetConfig()
if in.CheckBalance {
header, err := g.GetLastHeader()
if err != nil {
return nil, err
}
if in.Amount%ty.GetTicketMinerParam(cfg, header.Height).TicketPrice != 0 || in.Amount < 0 {
return nil, types.ErrAmount
}
getBalance := &types.ReqBalance{Addresses: []string{in.OriginAddr}, Execer: "coins", AssetSymbol: "bty", AssetExec: "coins"}
balances, err := g.GetCoinsAccountDB().GetBalance(g, getBalance)
if err != nil {
......
......@@ -26,6 +26,26 @@ import (
var cfgstring = `
Title="test"
[mempool]
poolCacheSize=102400
minTxFee=100000
maxTxNumPerAccount=100
[exec]
isFree=false
minExecFee=100000
enableStat=false
enableMVCC=false
[wallet]
minFee=100000
driver="leveldb"
dbPath="wallet"
dbCache=16
signType="secp256k1"
minerdisable=false
minerwhitelist=["*"]
[mver.consensus]
fundKeyAddr = "1BQXS6TxaYYG5mADaWij4AxhZZUTpw95a5"
powLimitBits = "0x1f00ffff"
......@@ -43,6 +63,46 @@ ticketWithdrawTime = 10
ticketMinerWaitTime = 2
targetTimespan = 2304
targetTimePerBlock = 16
[mver.consensus.ticket.ForkChainParamV1]
ticketPrice = 3000
[mver.consensus.ticket.ForkChainParamV2]
ticketPrice = 6000
[fork.system]
ForkChainParamV1= 10
ForkChainParamV2= 20
ForkStateDBSet=-1
ForkCheckTxDup=0
ForkBlockHash= 1
ForkMinerTime= 10
ForkTransferExec= 100000
ForkExecKey= 200000
ForkTxGroup= 200000
ForkResetTx0= 200000
ForkWithdraw= 200000
ForkExecRollback= 450000
ForkTxHeight= -1
ForkTxGroupPara= -1
ForkCheckBlockTime=1200000
ForkMultiSignAddress=1298600
ForkBlockCheck=1
ForkLocalDBAccess=0
ForkBase58AddressCheck=1800000
ForkEnableParaRegExec=0
ForkCacheDriver=0
ForkTicketFundAddrV1=-1
[fork.sub.coins]
Enable=0
[fork.sub.manage]
Enable=0
ForkManageExec=100000
[fork.sub.store-kvmvccmavl]
ForkKvmvccmavl=1
`
func newGrpc(api client.QueueProtocolAPI) *channelClient {
......@@ -59,17 +119,18 @@ func TestChannelClient_BindMiner(t *testing.T) {
api := new(mocks.QueueProtocolAPI)
client := newGrpc(api)
client.Init("ticket", nil, nil, nil)
head := &types.Header{StateHash: []byte("sdfadasds")}
api.On("GetLastHeader").Return(head, nil)
head := &types.Header{Height: 2, StateHash: []byte("sdfadasds")}
api.On("GetLastHeader").Return(head, nil).Times(4)
var acc = &types.Account{Addr: "1Jn2qu84Z1SUUosWjySggBS9pKWdAP3tZt", Balance: 100000 * types.Coin}
accv := types.Encode(acc)
storevalue := &types.StoreReplyValue{}
storevalue.Values = append(storevalue.Values, accv)
api.On("StoreGet", mock.Anything).Return(storevalue, nil)
api.On("StoreGet", mock.Anything).Return(storevalue, nil).Twice()
types.SetTitleOnlyForTest("test")
types.InitCfgString(cfgstring)
cfg, _ := types.InitCfgString(cfgstring)
types.Init("test", cfg)
//var addrs = make([]string, 1)
//addrs = append(addrs, "1Jn2qu84Z1SUUosWjySggBS9pKWdAP3tZt")
......@@ -81,6 +142,16 @@ func TestChannelClient_BindMiner(t *testing.T) {
}
_, err := client.CreateBindMiner(context.Background(), in)
assert.Nil(t, err)
in.Amount = 200000 * types.Coin
_, err = client.CreateBindMiner(context.Background(), in)
assert.Equal(t, types.ErrNoBalance, err)
head.Height = 20 //ForkChainParamV2
api.On("GetLastHeader").Return(head, nil).Times(2)
_, err = client.CreateBindMiner(context.Background(), in)
assert.Equal(t, types.ErrAmount, err)
}
func testGetTicketCountOK(t *testing.T) {
......
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