func getNeighbor(s Serverer, params map[string]interface{}) map[string]interface{} {
	return respPacking(errcode.SUCCESS, s.GetNetNode().GetNeighborInfo())
}
getNeighbor gets neighbors of this node
params: {}
return: {"resultOrData":<result>|<error data>, "error":<errcode>}
package common
import (
	"bytes"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"net"
	"strings"
	"github.com/nknorg/nkn/v2/api/common/errcode"
	"github.com/nknorg/nkn/v2/block"
	"github.com/nknorg/nkn/v2/chain"
	"github.com/nknorg/nkn/v2/common"
	"github.com/nknorg/nkn/v2/config"
	"github.com/nknorg/nkn/v2/crypto"
	"github.com/nknorg/nkn/v2/node"
	"github.com/nknorg/nkn/v2/transaction"
	"github.com/nknorg/nkn/v2/util/address"
	"github.com/nknorg/nkn/v2/util/log"
)
const (
	BIT_JSONRPC   byte = 1
	BIT_WEBSOCKET byte = 2
)
type Handler func(Serverer, map[string]interface{}) map[string]interface{}
type APIHandler struct {
	Handler    Handler
	AccessCtrl byte
}
func (ah *APIHandler) IsAccessableByJsonrpc() bool {
	if ah.AccessCtrl&BIT_JSONRPC != BIT_JSONRPC {
		return false
	}
	return true
}
func (ah *APIHandler) IsAccessableByWebsocket() bool {
	if ah.AccessCtrl&BIT_WEBSOCKET != BIT_WEBSOCKET {
		return false
	}
	return true
}
func getLatestBlockHash(s Serverer, params map[string]interface{}) map[string]interface{} {
	height := chain.DefaultLedger.Store.GetHeight()
	hash, err := chain.DefaultLedger.Store.GetBlockHash(height)
	if err != nil {
		return respPacking(errcode.INTERNAL_ERROR, err.Error())
	}
	ret := map[string]interface{}{
		"height": height,
		"hash":   hash.ToHexString(),
	}
	return respPacking(errcode.SUCCESS, ret)
}
func getBlock(s Serverer, params map[string]interface{}) map[string]interface{} {
	if len(params) < 1 {
		return respPacking(errcode.INVALID_PARAMS, "length of params is less than 1")
	}
	var hash common.Uint256
	if index, ok := params["height"].(float64); ok {
		var err error
		height := uint32(index)
		if hash, err = chain.DefaultLedger.Store.GetBlockHash(height); err != nil {
			return respPacking(errcode.UNKNOWN_HASH, err.Error())
		}
	} else if str, ok := params["hash"].(string); ok {
		hex, err := hex.DecodeString(str)
		if err != nil {
			return respPacking(errcode.INVALID_PARAMS, err.Error())
		}
		if err := hash.Deserialize(bytes.NewReader(hex)); err != nil {
			return respPacking(errcode.UNKNOWN_HASH, err.Error())
		}
	} else {
		return respPacking(errcode.INVALID_PARAMS, "parameter should be height or hash")
	}
	block, err := chain.DefaultLedger.Store.GetBlock(hash)
	if err != nil {
		return respPacking(errcode.UNKNOWN_BLOCK, err.Error())
	}
	var b interface{}
	info, err := block.GetInfo()
	if err != nil {
		return respPacking(errcode.INTERNAL_ERROR, err.Error())
	}
	json.Unmarshal(info, &b)
	return respPacking(errcode.SUCCESS, b)
}
func getHeader(s Serverer, params map[string]interface{}) map[string]interface{} {
	if len(params) < 1 {
		return respPacking(errcode.INVALID_PARAMS, "length of params is less than 1")
	}
	var hash common.Uint256
	if index, ok := params["height"].(float64); ok {
		var err error
		height := uint32(index)
		if hash, err = chain.DefaultLedger.Store.GetBlockHash(height); err != nil {
			return respPacking(errcode.UNKNOWN_HASH, err.Error())
		}
	} else if str, ok := params["hash"].(string); ok {
		hex, err := hex.DecodeString(str)
		if err != nil {
			return respPacking(errcode.INVALID_PARAMS, err.Error())
		}
		if err := hash.Deserialize(bytes.NewReader(hex)); err != nil {
			return respPacking(errcode.UNKNOWN_HASH, err.Error())
		}
	} else {
		return respPacking(errcode.INVALID_PARAMS, "parameter should be height or hash")
	}
	header, err := chain.DefaultLedger.Store.GetHeader(hash)
	if err != nil {
		return respPacking(errcode.UNKNOWN_BLOCK, err.Error())
	}
	var b interface{}
	info, err := header.GetInfo()
	if err != nil {
		return respPacking(errcode.INTERNAL_ERROR, err.Error())
	}
	json.Unmarshal(info, &b)
	return respPacking(errcode.SUCCESS, b)
}
func getBlockCount(s Serverer, params map[string]interface{}) map[string]interface{} {
	return respPacking(errcode.SUCCESS, chain.DefaultLedger.Store.GetHeight()+1)
}
func getChordRingInfo(s Serverer, params map[string]interface{}) map[string]interface{} {
	return respPacking(errcode.SUCCESS, s.GetNetNode().GetChordInfo())
}
func getLatestBlockHeight(s Serverer, params map[string]interface{}) map[string]interface{} {
	return respPacking(errcode.SUCCESS, chain.DefaultLedger.Store.GetHeight())
}
func GetBlockTransactions(block *block.Block) interface{} {
	trans := make([]string, len(block.Transactions))
	for i := 0; i < len(block.Transactions); i++ {
		h := block.Transactions[i].Hash()
		trans[i] = hex.EncodeToString(h.ToArray())
	}
	hash := block.Hash()
	type BlockTransactions struct {
		Hash         string
		Height       uint32
		Transactions []string
	}
	b := BlockTransactions{
		Hash:         hex.EncodeToString(hash.ToArray()),
		Height:       block.Header.UnsignedHeader.Height,
		Transactions: trans,
	}
	return b
}
func getBlockTxsByHeight(s Serverer, params map[string]interface{}) map[string]interface{} {
	if len(params) < 1 {
		return respPacking(errcode.INVALID_PARAMS, "length of params is less than 1")
	}
	var err error
	if _, ok := params["height"].(float64); !ok {
		return respPacking(errcode.INVALID_PARAMS, "height should be float64")
	}
	index := uint32(params["height"].(float64))
	hash, err := chain.DefaultLedger.Store.GetBlockHash(index)
	if err != nil {
		return respPacking(errcode.UNKNOWN_HASH, err.Error())
	}
	block, err := chain.DefaultLedger.Store.GetBlock(hash)
	if err != nil {
		return respPacking(errcode.UNKNOWN_BLOCK, err.Error())
	}
	txs := GetBlockTransactions(block)
	return respPacking(errcode.SUCCESS, txs)
}
func getConnectionCount(s Serverer, params map[string]interface{}) map[string]interface{} {
	return respPacking(errcode.SUCCESS, s.GetNetNode().GetConnectionCnt())
}
func getRawMemPool(s Serverer, params map[string]interface{}) map[string]interface{} {
	if len(params) < 1 {
		return respPacking(errcode.INVALID_PARAMS, "length of params is less than 1")
	}
	action, ok := params["action"].(string)
	if !ok {
		return respPacking(errcode.INVALID_PARAMS, "action should be a string")
	}
	txpool := s.GetNetNode().GetTxnPool()
	switch action {
	case "addresslist":
		programHashes := txpool.GetAddressList()
		addresses := []interface{}{}
		for programHash, count := range programHashes {
			addr, err := programHash.ToAddress()
			if err != nil {
				return respPacking(errcode.INTERNAL_ERROR, err.Error())
			}
			info := map[string]interface{}{
				"address": addr,
				"txcount": count,
			}
			addresses = append(addresses, info)
		}
		return respPacking(errcode.SUCCESS, addresses)
	case "txnlist":
		addr, ok := params["address"].(string)
		if !ok {
			return respPacking(errcode.INVALID_PARAMS, "address should be a string")
		}
		programHash, err := common.ToScriptHash(addr)
		if err != nil {
			return respPacking(errcode.INVALID_PARAMS, err.Error())
		}
		txs := []interface{}{}
		for _, txn := range txpool.GetAllTransactionsBySender(programHash) {
			info, err := txn.GetInfo()
			if err != nil {
				return respPacking(errcode.INTERNAL_ERROR, err.Error())
			}
			var x interface{}
			err = json.Unmarshal(info, &x)
			if err != nil {
				return respPacking(errcode.INTERNAL_ERROR, err.Error())
			}
			txs = append(txs, x)
		}
		return respPacking(errcode.SUCCESS, txs)
	default:
		return respPacking(errcode.INVALID_PARAMS, "action should be addresslist or txnlist")
	}
}
func getTransaction(s Serverer, params map[string]interface{}) map[string]interface{} {
	if len(params) < 1 {
		return respPacking(errcode.INVALID_PARAMS, "length of params is less than 1")
	}
	str, ok := params["hash"].(string)
	if !ok {
		return respPacking(errcode.INVALID_PARAMS, "hash should be a string")
	}
	hex, err := hex.DecodeString(str)
	if err != nil {
		return respPacking(errcode.INVALID_PARAMS, err.Error())
	}
	var hash common.Uint256
	err = hash.Deserialize(bytes.NewReader(hex))
	if err != nil {
		return respPacking(errcode.INVALID_PARAMS, err.Error())
	}
	tx, err := chain.DefaultLedger.Store.GetTransaction(hash)
	if err != nil {
		return respPacking(errcode.UNKNOWN_TRANSACTION, err.Error())
	}
	tx.Hash()
	var tran interface{}
	info, err := tx.GetInfo()
	if err != nil {
		return respPacking(errcode.INTERNAL_ERROR, err.Error())
	}
	json.Unmarshal(info, &tran)
	return respPacking(errcode.SUCCESS, tran)
}
func sendRawTransaction(s Serverer, params map[string]interface{}) map[string]interface{} {
	if len(params) < 1 {
		return respPacking(errcode.INVALID_PARAMS, "length of params is less than 1")
	}
	var hash common.Uint256
	if str, ok := params["tx"].(string); ok {
		hex, err := hex.DecodeString(str)
		if err != nil {
			return respPacking(errcode.INVALID_PARAMS, err.Error())
		}
		var txn transaction.Transaction
		if err := txn.Unmarshal(hex); err != nil {
			return respPacking(errcode.INVALID_TRANSACTION, err.Error())
		}
		hash = txn.Hash()
		if errCode, err := VerifyAndSendTx(s.GetNetNode(), &txn); errCode != errcode.ErrNoError {
			return respPacking(errCode, err.Error())
		}
	} else {
		return respPacking(errcode.INVALID_PARAMS, "tx should be a hex string")
	}
	return respPacking(errcode.SUCCESS, hex.EncodeToString(hash.ToArray()))
}