Commit e612ac49 authored by caoping's avatar caoping Committed by vipwzw

upate tendermint rpc

parent 25101730
......@@ -96,12 +96,12 @@ createEmptyBlocksInterval=1
validatorNodes=["127.0.0.1:46656", "127.0.0.2:46656"]
[store]
name="kvdb"
name="mavl"
driver="leveldb"
dbPath="datadir/mavltree"
dbCache=128
[store.sub.kvdb]
[store.sub.mavl]
enableMavlPrefix=false
enableMVCC=false
......@@ -129,3 +129,8 @@ enable=false
cryptoPath="authdir/crypto"
# 带证书签名类型,支持"auth_ecdsa", "auth_sm2"
signType="auth_ecdsa"
[exec.sub.manage]
superManager=[
"14KEKbYtKKQm4wMthSK9J4La4nAiidGozt",
]
......@@ -833,19 +833,21 @@ func (cs *ConsensusState) defaultDoPrevote(height int64, round int) {
return
}
// PreExec proposal block
blockCopy := *cs.ProposalBlock.Data
blockNew := cs.client.PreExecBlock(&blockCopy, true)
if blockNew == nil {
tendermintlog.Error("enterPrevote: PreExec ProposalBlock fail")
cs.signAddVote(ttypes.VoteTypePrevote, nil)
return
}
cfg := cs.client.GetQueueClient().GetConfig()
if !bytes.Equal(blockNew.Hash(cfg), cs.ProposalBlock.Data.Hash(cfg)) {
tendermintlog.Error("enterPrevote: PreExec ProposalBlock has change")
cs.signAddVote(ttypes.VoteTypePrevote, nil)
return
if !cs.isProposer() {
// PreExec proposal block
blockCopy := *cs.ProposalBlock.Data
blockNew := cs.client.PreExecBlock(&blockCopy, true)
if blockNew == nil {
tendermintlog.Error("enterPrevote: PreExec ProposalBlock fail")
cs.signAddVote(ttypes.VoteTypePrevote, nil)
return
}
cfg := cs.client.GetQueueClient().GetConfig()
if !bytes.Equal(blockNew.Hash(cfg), cs.ProposalBlock.Data.Hash(cfg)) {
tendermintlog.Error("enterPrevote: PreExec ProposalBlock has change")
cs.signAddVote(ttypes.VoteTypePrevote, nil)
return
}
}
// Prevote cs.ProposalBlock
......
......@@ -25,6 +25,7 @@ import (
"github.com/33cn/chain33/queue"
"github.com/33cn/chain33/rpc"
"github.com/33cn/chain33/store"
mty "github.com/33cn/chain33/system/dapp/manage/types"
"github.com/33cn/chain33/types"
ty "github.com/33cn/plugin/plugin/consensus/tendermint/types"
pty "github.com/33cn/plugin/plugin/dapp/norm/types"
......@@ -71,6 +72,7 @@ func TendermintPerf(t *testing.T) {
err = createConn()
}
time.Sleep(2 * time.Second)
ConfigManager()
for i := 0; i < loopCount; i++ {
NormPut()
time.Sleep(time.Second)
......@@ -210,6 +212,28 @@ func AddNode() {
}
}
func ConfigManager() {
v := &types.ModifyConfig{Key: "tendermint-manager", Op: "add", Value: "14KEKbYtKKQm4wMthSK9J4La4nAiidGozt", Addr: ""}
modify := &mty.ManageAction{
Ty: mty.ManageActionModifyConfig,
Value: &mty.ManageAction_Modify{Modify: v},
}
tx := &types.Transaction{Execer: []byte("manage"), Payload: types.Encode(modify), Fee: fee}
tx.To = address.ExecAddress("manage")
tx.Nonce = r.Int63()
tx.Sign(types.SECP256K1, getprivkey("CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944"))
reply, err := c.SendTransaction(context.Background(), tx)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
if !reply.IsOk {
fmt.Fprintln(os.Stderr, errors.New(string(reply.GetMsg())))
return
}
}
func CheckState(t *testing.T, client *Client) {
state := client.csState.GetState()
assert.NotEmpty(t, state)
......
......@@ -12,8 +12,6 @@ import (
"strconv"
"time"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/address"
"github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/rpc/jsonclient"
rpctypes "github.com/33cn/chain33/rpc/types"
......@@ -75,26 +73,11 @@ func GetNodeInfoCmd() *cobra.Command {
func getNodeInfo(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
var res string
var res []*vt.Validator
ctx := jsonclient.NewRPCCtx(rpcLaddr, "valnode.GetNodeInfo", nil, &res)
ctx.SetResultCb(parseNodeInfo)
ctx.Run()
}
func parseNodeInfo(arg interface{}) (interface{}, error) {
var result vt.ValidatorSet
res := arg.(*string)
data, err := hex.DecodeString(*res)
if err != nil {
return nil, err
}
err = types.Decode(data, &result)
if err != nil {
return nil, err
}
return result.Validators, nil
}
// GetBlockInfoCmd get block info
func GetBlockInfoCmd() *cobra.Command {
cmd := &cobra.Command{
......@@ -149,8 +132,6 @@ func addNodeFlags(cmd *cobra.Command) {
func addNode(cmd *cobra.Command, args []string) {
title, _ := cmd.Flags().GetString("title")
cfg := types.GetCliSysParam(title)
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
pubkey, _ := cmd.Flags().GetString("pubkey")
power, _ := cmd.Flags().GetInt64("power")
......@@ -159,48 +140,16 @@ func addNode(cmd *cobra.Command, args []string) {
fmt.Fprintln(os.Stderr, err)
return
}
privkey, err := getprivkey()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
value := &vt.ValNodeAction_Node{Node: &vt.ValNode{PubKey: pubkeybyte, Power: power}}
action := &vt.ValNodeAction{Value: value, Ty: vt.ValNodeActionUpdate}
tx := &types.Transaction{Execer: []byte(vt.ValNodeX), Payload: types.Encode(action), Fee: 0}
err = tx.SetRealFee(cfg.GetMinTxFeeRate())
tx := &types.Transaction{Payload: types.Encode(action)}
tx, err = types.FormatTx(cfg, vt.ValNodeX, tx)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
random := rand.New(rand.NewSource(time.Now().UnixNano()))
tx.Nonce = random.Int63()
tx.To = address.ExecAddress(vt.ValNodeX)
tx.Sign(types.SECP256K1, privkey)
txHex := types.Encode(tx)
data := hex.EncodeToString(txHex)
params := rpctypes.RawParm{
Data: data,
}
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain33.SendTransaction", params, nil)
ctx.RunWithoutMarshal()
}
func getprivkey() (crypto.PrivKey, error) {
key := "CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944"
cr, err := crypto.New(types.GetSignName("", types.SECP256K1))
if err != nil {
return nil, err
}
bkey, err := common.FromHex(key)
if err != nil {
return nil, err
}
priv, err := cr.PrivKeyFromBytes(bkey)
if err != nil {
return nil, err
}
return priv, nil
fmt.Println(hex.EncodeToString(txHex))
}
//CreateCmd to create keyfiles
......
......@@ -5,12 +5,26 @@
package executor
import (
"errors"
dbm "github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/types"
pty "github.com/33cn/plugin/plugin/dapp/valnode/types"
)
const managerKey = "tendermint-manager"
// Exec_Node method
func (val *ValNode) Exec_Node(node *pty.ValNode, tx *types.Transaction, index int) (*types.Receipt, error) {
if !isValidManager(tx.From(), val.GetStateDB()) {
return nil, errors.New("not valid manager")
}
if len(node.GetPubKey()) == 0 {
return nil, errors.New("validator pubkey is empty")
}
if node.GetPower() < 0 {
return nil, errors.New("validator power must not be negative")
}
receipt := &types.Receipt{Ty: types.ExecOk, KV: nil, Logs: nil}
return receipt, nil
}
......@@ -20,3 +34,38 @@ func (val *ValNode) Exec_BlockInfo(blockInfo *pty.TendermintBlockInfo, tx *types
receipt := &types.Receipt{Ty: types.ExecOk, KV: nil, Logs: nil}
return receipt, nil
}
func getManageKey(key string, db dbm.KV) ([]byte, error) {
manageKey := types.ManageKey(key)
value, err := db.Get([]byte(manageKey))
if err != nil {
return nil, err
}
return value, nil
}
func isValidManager(addr string, db dbm.KV) bool {
value, err := getManageKey(managerKey, db)
if err != nil {
clog.Error("isValidManager nil key", "managerKey", managerKey)
return false
}
if value == nil {
clog.Error("isValidManager nil value")
return false
}
var item types.ConfigItem
err = types.Decode(value, &item)
if err != nil {
clog.Error("isValidManager decode fail", "err", err)
return false
}
for _, op := range item.GetArr().Value {
if op == addr {
return true
}
}
return false
}
......@@ -6,7 +6,6 @@ package executor
import (
"encoding/hex"
"errors"
"github.com/33cn/chain33/types"
pty "github.com/33cn/plugin/plugin/dapp/valnode/types"
......@@ -15,12 +14,6 @@ import (
// ExecLocal_Node method
func (val *ValNode) ExecLocal_Node(node *pty.ValNode, tx *types.Transaction, receipt *types.ReceiptData, index int) (*types.LocalDBSet, error) {
set := &types.LocalDBSet{}
if len(node.GetPubKey()) == 0 {
return nil, errors.New("validator pubkey is empty")
}
if node.GetPower() < 0 {
return nil, errors.New("validator power must not be negative")
}
clog.Info("update validator", "pubkey", hex.EncodeToString(node.GetPubKey()), "power", node.GetPower())
key := CalcValNodeUpdateHeightIndexKey(val.GetHeight(), index)
set.KV = append(set.KV, &types.KeyValue{Key: key, Value: types.Encode(node)})
......
......@@ -6,8 +6,6 @@ package rpc
import (
"context"
"encoding/hex"
"github.com/33cn/chain33/types"
vt "github.com/33cn/plugin/plugin/dapp/valnode/types"
)
......@@ -34,7 +32,7 @@ func (c *Jrpc) IsSync(req *types.ReqNil, result *interface{}) error {
return nil
}
// GetNodeInfo query block info
// GetNodeInfo query node info
func (c *channelClient) GetNodeInfo(ctx context.Context, req *types.ReqNil) (*vt.ValidatorSet, error) {
data, err := c.QueryConsensusFunc("tendermint", "NodeInfo", &types.ReqNil{})
if err != nil {
......@@ -46,12 +44,12 @@ func (c *channelClient) GetNodeInfo(ctx context.Context, req *types.ReqNil) (*vt
return nil, types.ErrDecode
}
// GetNodeInfo query block info
// GetNodeInfo query node info
func (c *Jrpc) GetNodeInfo(req *types.ReqNil, result *interface{}) error {
data, err := c.cli.GetNodeInfo(context.Background(), req)
if err != nil {
return err
}
*result = hex.EncodeToString(types.Encode(data))
*result = data.Validators
return nil
}
......@@ -8,7 +8,6 @@ package rpc
//only load all plugin and system
import (
"encoding/hex"
"testing"
"strings"
......@@ -98,5 +97,5 @@ func TestJrpc_GetNodeInfo(t *testing.T) {
api.On("QueryConsensusFunc", "tendermint", "NodeInfo", req).Return(set, nil)
err := J.GetNodeInfo(req, &result)
assert.Nil(t, err)
assert.EqualValues(t, hex.EncodeToString(types.Encode(set)), result)
assert.EqualValues(t, set.Validators, result)
}
......@@ -4,8 +4,16 @@
package types
//valnode action
// ValNodeX define
const ValNodeX = "valnode"
// valnode action
const (
ValNodeActionUpdate = 1
ValNodeActionBlockInfo = 2
)
// action name
const (
ActionNodeUpdate = "NodeUpdate"
)
// 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 types
// NodeUpdateTx for construction
type NodeUpdateTx struct {
PubKey string `json:"pubKey"`
Power int64 `json:"power"`
}
......@@ -5,11 +5,15 @@
package types
import (
"encoding/hex"
"encoding/json"
"github.com/33cn/chain33/common/address"
log "github.com/33cn/chain33/common/log/log15"
"github.com/33cn/chain33/types"
)
// ValNodeX define
var ValNodeX = "valnode"
var tlog = log.New("module", "exectype."+ValNodeX)
func init() {
types.AllowUserExec = append(types.AllowUserExec, []byte(ValNodeX))
......@@ -60,3 +64,52 @@ func (t *ValNodeType) GetTypeMap() map[string]int32 {
func (t *ValNodeType) GetLogMap() map[int64]*types.LogInfo {
return map[int64]*types.LogInfo{}
}
// CreateTx
func (t *ValNodeType) CreateTx(action string, message json.RawMessage) (*types.Transaction, error) {
tlog.Debug("valnode.CreateTx", "action", action)
cfg := t.GetConfig()
if action == ActionNodeUpdate {
var param NodeUpdateTx
err := json.Unmarshal(message, &param)
if err != nil {
tlog.Error("valnode.CreateTx", "err", err)
return nil, types.ErrInvalidParam
}
return CreateNodeUpdateTx(cfg, &param)
}
return nil, types.ErrNotSupport
}
// CreateNodeUpdateTx
func CreateNodeUpdateTx(cfg *types.Chain33Config, parm *NodeUpdateTx) (*types.Transaction, error) {
if parm == nil {
tlog.Error("CreateNodeUpdateTx", "parm", parm)
return nil, types.ErrInvalidParam
}
pubkeybyte, err := hex.DecodeString(parm.PubKey)
if err != nil {
return nil, err
}
v := &ValNode{
PubKey: pubkeybyte,
Power: parm.Power,
}
update := &ValNodeAction{
Ty: ValNodeActionUpdate,
Value: &ValNodeAction_Node{v},
}
execName := cfg.ExecName(ValNodeX)
tx := &types.Transaction{
Execer: []byte(execName),
Payload: types.Encode(update),
To: address.ExecAddress(execName),
}
tx, err = types.FormatTx(cfg, execName, tx)
if err != nil {
return nil, err
}
return tx, nil
}
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