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

upate tendermint rpc

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