Unverified Commit 3f3dd047 authored by vipwzw's avatar vipwzw Committed by GitHub

Merge pull request #322 from linj-disanbo/js-creator

Js creator
parents 32187648 9e69cb9f
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package autotest
import (
"github.com/33cn/chain33/cmd/autotest/types"
)
// JsCreateCase token createcase command
type JsCreateCase struct {
types.BaseCase
}
// JsCreatePack defines create package command
type JsCreatePack struct {
types.BaseCasePack
}
// SendCommand defines send command function of tokenprecreatecase
func (testCase *JsCreateCase) SendCommand(packID string) (types.PackFunc, error) {
return types.DefaultSend(testCase, &JsCreatePack{}, packID)
}
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package autotest
import (
"reflect"
"github.com/33cn/chain33/cmd/autotest/types"
)
type jsAutoTest struct {
SimpleCaseArr []types.SimpleCase `toml:"SimpleCase,omitempty"`
JSCreateCaseArr []JsCreateCase `toml:"jsCreateCase,omitempty"`
}
func init() {
types.RegisterAutoTest(jsAutoTest{})
}
func (config jsAutoTest) GetName() string {
return "js"
}
func (config jsAutoTest) GetTestConfigType() reflect.Type {
return reflect.TypeOf(config)
}
# 1Q8hGLfoGe63efeWa8fJ4Pnukhkngt6poK manage addr
# 0xc34b5d9d44ac7b754806f761d3d4d2c4fe5214f6b074c19f069c4f5c2a29c8cc
[[jsCreateCase]]
id = "create1"
command = "send jsvm create -c ../../../plugin/dapp/js/executor/game.js -n hello -k 14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
fail = true
[[jsCreateCase]]
id = "create2"
command = "send jsvm create -c ../../../plugin/dapp/js/executor/game.js -n hello -k 14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
dep = ["create1", "configJS"]
[[SimpleCase]]
id = "configJS"
dep = ["create1"]
command = "send config config_tx -c js-creator -o add -v 14KEKbYtKKQm4wMthSK9J4La4nAiidGozt -k 0xc34b5d9d44ac7b754806f761d3d4d2c4fe5214f6b074c19f069c4f5c2a29c8cc"
\ No newline at end of file
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package executor
import (
dbm "github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/types"
ptypes "github.com/33cn/plugin/plugin/dapp/js/types"
)
func getManageKey(key string, db dbm.KV) ([]byte, error) {
manageKey := types.ManageKey(key)
return db.Get([]byte(manageKey))
}
func checkPriv(addr, key string, db dbm.KV) error {
value, err := getManageKey(key, db)
if err != nil {
return err
}
if value == nil {
return ptypes.ErrJsCreator
}
var item types.ConfigItem
err = types.Decode(value, &item)
if err != nil {
return err
}
for _, op := range item.GetArr().Value {
if op == addr {
return nil
}
}
return ptypes.ErrJsCreator
}
......@@ -8,13 +8,18 @@ import (
)
func (c *js) Exec_Create(payload *jsproto.Create, tx *types.Transaction, index int) (*types.Receipt, error) {
err := checkPriv(tx.From(), ptypes.JsCreator, c.GetStateDB())
if err != nil {
return nil, err
}
execer := types.ExecName("user." + ptypes.JsX + "." + payload.Name)
if string(tx.Execer) != ptypes.JsX {
return nil, types.ErrExecNameNotMatch
}
c.prefix = calcStatePrefix([]byte(execer))
kvc := dapp.NewKVCreator(c.GetStateDB(), c.prefix, nil)
_, err := kvc.GetNoPrefix(calcCodeKey(payload.Name))
_, err = kvc.GetNoPrefix(calcCodeKey(payload.Name))
if err != nil && err != types.ErrNotFound {
return nil, err
}
......
......@@ -37,6 +37,17 @@ func initExec(ldb db.DB, kvdb db.KVDB, code string, t assert.TestingT) *js {
e.SetLocalDB(kvdb)
e.SetStateDB(kvdb)
c, tx := createCodeTx("test", code)
// set config key
item := &types.ConfigItem{
Key: "mavl-manage-js-creator",
Addr: tx.From(),
Value: &types.ConfigItem_Arr{
Arr: &types.ArrayConfig{Value: []string{tx.From()}},
},
}
kvdb.Set([]byte(item.Key), types.Encode(item))
receipt, err := e.Exec_Create(c, tx, 0)
assert.Nil(t, err)
util.SaveKVList(ldb, receipt.KV)
......
......@@ -30,6 +30,8 @@ func TestJsVM(t *testing.T) {
mocker := testnode.NewWithConfig(cfg, sub, nil)
defer mocker.Close()
mocker.Listen()
configCreator(mocker, t)
//开始部署合约, 测试阶段任何人都可以部署合约
//后期需要加上权限控制
//1. 部署合约
......@@ -94,6 +96,9 @@ func TestJsGame(t *testing.T) {
mocker.Listen()
err := mocker.SendHot()
assert.Nil(t, err)
// 需要配置
configCreator(mocker, t)
//开始部署合约, 测试阶段任何人都可以部署合约
//后期需要加上权限控制
//1. 部署合约
......@@ -288,3 +293,26 @@ func TestJsGame(t *testing.T) {
assert.Nil(t, err)
t.Log(queryresult.Data)
}
func configCreator(mocker *testnode.Chain33Mock, t *testing.T) {
// 需要配置
addr := address.PubKeyToAddress(mocker.GetHotKey().PubKey().Bytes()).String()
creator := &types.ModifyConfig{
Key: "js-creator",
Op: "add",
Value: addr,
Addr: addr,
}
cfgReq := &rpctypes.CreateTxIn{
Execer: "manage",
ActionName: "Modify",
Payload: types.MustPBToJSON(creator),
}
var cfgtxhex string
err := mocker.GetJSONC().Call("Chain33.CreateTransaction", cfgReq, &cfgtxhex)
assert.Nil(t, err)
hash1, err := mocker.SendAndSign(mocker.GetHotKey(), cfgtxhex)
assert.Nil(t, err)
_, err = mocker.WaitTx(hash1)
assert.Nil(t, err)
}
......@@ -5,6 +5,9 @@ import (
"github.com/33cn/plugin/plugin/dapp/js/cmd"
"github.com/33cn/plugin/plugin/dapp/js/executor"
ptypes "github.com/33cn/plugin/plugin/dapp/js/types"
// init auto test
_ "github.com/33cn/plugin/plugin/dapp/js/autotest"
)
func init() {
......
......@@ -19,6 +19,9 @@ const (
TyLogJs = 10000
)
// JsCreator 配置项 创建js合约的管理员
const JsCreator = "js-creator"
var (
typeMap = map[string]int32{
"Create": jsActionCreate,
......@@ -47,6 +50,8 @@ var (
ErrSymbolName = errors.New("chain33.js: ErrSymbolName")
ErrExecerName = errors.New("chain33.js: ErrExecerName")
ErrDBType = errors.New("chain33.js: ErrDBType")
// ErrJsCreator
ErrJsCreator = errors.New("ErrJsCreator")
)
func init() {
......
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