2020-02-17 12:17:02 +00:00
|
|
|
package server
|
2018-03-23 20:36:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-20 17:39:32 +00:00
|
|
|
"encoding/hex"
|
2020-01-14 12:02:38 +00:00
|
|
|
"encoding/json"
|
2018-03-23 20:36:59 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-01-25 11:20:35 +00:00
|
|
|
"strconv"
|
2018-03-23 20:36:59 +00:00
|
|
|
|
2019-11-01 10:23:46 +00:00
|
|
|
"github.com/CityOfZion/neo-go/config"
|
2018-03-23 20:36:59 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core"
|
2020-01-29 16:01:00 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/state"
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
2020-02-21 14:56:28 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
2020-01-13 08:27:22 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/encoding/address"
|
2019-09-16 09:18:13 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
2018-03-23 20:36:59 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/network"
|
2020-01-14 12:02:38 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/rpc/request"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/rpc/response"
|
2020-01-13 15:20:30 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/rpc/response/result"
|
2018-03-23 20:36:59 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
2019-02-08 08:04:38 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-12-30 08:44:52 +00:00
|
|
|
"go.uber.org/zap"
|
2018-03-23 20:36:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Server represents the JSON-RPC 2.0 server.
|
|
|
|
Server struct {
|
|
|
|
*http.Server
|
|
|
|
chain core.Blockchainer
|
2019-11-01 10:23:46 +00:00
|
|
|
config config.RPCConfig
|
2018-03-23 20:36:59 +00:00
|
|
|
coreServer *network.Server
|
2019-12-30 08:44:52 +00:00
|
|
|
log *zap.Logger
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-11-21 13:42:51 +00:00
|
|
|
var invalidBlockHeightError = func(index int, height int) error {
|
|
|
|
return errors.Errorf("Param at index %d should be greater than or equal to 0 and less then or equal to current block height, got: %d", index, height)
|
|
|
|
}
|
2018-03-25 10:13:47 +00:00
|
|
|
|
2020-02-17 12:17:02 +00:00
|
|
|
// New creates a new Server struct.
|
|
|
|
func New(chain core.Blockchainer, conf config.RPCConfig, coreServer *network.Server, log *zap.Logger) Server {
|
2019-11-01 10:23:46 +00:00
|
|
|
httpServer := &http.Server{
|
|
|
|
Addr: conf.Address + ":" + strconv.FormatUint(uint64(conf.Port), 10),
|
|
|
|
}
|
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
return Server{
|
2019-11-01 10:23:46 +00:00
|
|
|
Server: httpServer,
|
2018-03-23 20:36:59 +00:00
|
|
|
chain: chain,
|
2019-11-01 10:23:46 +00:00
|
|
|
config: conf,
|
2018-03-23 20:36:59 +00:00
|
|
|
coreServer: coreServer,
|
2019-12-30 08:44:52 +00:00
|
|
|
log: log,
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start creates a new JSON-RPC server
|
|
|
|
// listening on the configured port.
|
|
|
|
func (s *Server) Start(errChan chan error) {
|
2019-11-01 10:23:46 +00:00
|
|
|
if !s.config.Enabled {
|
2019-12-30 08:44:52 +00:00
|
|
|
s.log.Info("RPC server is not enabled")
|
2019-11-01 10:23:46 +00:00
|
|
|
return
|
|
|
|
}
|
2018-03-23 20:36:59 +00:00
|
|
|
s.Handler = http.HandlerFunc(s.requestHandler)
|
2019-12-30 08:44:52 +00:00
|
|
|
s.log.Info("starting rpc-server", zap.String("endpoint", s.Addr))
|
2018-03-23 20:36:59 +00:00
|
|
|
|
|
|
|
errChan <- s.ListenAndServe()
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Shutdown overrides the http.Server Shutdown
|
2018-03-23 20:36:59 +00:00
|
|
|
// method.
|
|
|
|
func (s *Server) Shutdown() error {
|
2019-12-30 08:44:52 +00:00
|
|
|
s.log.Info("shutting down rpc-server", zap.String("endpoint", s.Addr))
|
2018-03-23 20:36:59 +00:00
|
|
|
return s.Server.Shutdown(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) requestHandler(w http.ResponseWriter, httpRequest *http.Request) {
|
2020-01-14 12:02:38 +00:00
|
|
|
req := request.NewIn()
|
2018-03-23 20:36:59 +00:00
|
|
|
|
|
|
|
if httpRequest.Method != "POST" {
|
2019-12-30 08:44:52 +00:00
|
|
|
s.WriteErrorResponse(
|
|
|
|
req,
|
2018-03-23 20:36:59 +00:00
|
|
|
w,
|
2020-01-14 12:02:38 +00:00
|
|
|
response.NewInvalidParamsError(
|
2018-03-23 20:36:59 +00:00
|
|
|
fmt.Sprintf("Invalid method '%s', please retry with 'POST'", httpRequest.Method), nil,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := req.DecodeData(httpRequest.Body)
|
|
|
|
if err != nil {
|
2020-01-14 12:02:38 +00:00
|
|
|
s.WriteErrorResponse(req, w, response.NewParseError("Problem parsing JSON-RPC request body", err))
|
2018-03-23 20:36:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
reqParams, err := req.Params()
|
|
|
|
if err != nil {
|
2020-01-14 12:02:38 +00:00
|
|
|
s.WriteErrorResponse(req, w, response.NewInvalidParamsError("Problem parsing request parameters", err))
|
2018-03-23 20:36:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.methodHandler(w, req, *reqParams)
|
|
|
|
}
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
func (s *Server) methodHandler(w http.ResponseWriter, req *request.In, reqParams request.Params) {
|
2020-01-13 13:45:36 +00:00
|
|
|
s.log.Debug("processing rpc request",
|
2019-12-30 08:44:52 +00:00
|
|
|
zap.String("method", req.Method),
|
|
|
|
zap.String("params", fmt.Sprintf("%v", reqParams)))
|
2018-03-23 20:36:59 +00:00
|
|
|
|
2019-02-20 16:28:11 +00:00
|
|
|
var (
|
|
|
|
results interface{}
|
|
|
|
resultsErr error
|
|
|
|
)
|
2018-03-23 20:36:59 +00:00
|
|
|
|
2018-03-25 10:13:47 +00:00
|
|
|
Methods:
|
2018-03-23 20:36:59 +00:00
|
|
|
switch req.Method {
|
2020-02-21 14:56:28 +00:00
|
|
|
case "getapplicationlog":
|
|
|
|
getapplicationlogCalled.Inc()
|
|
|
|
results, resultsErr = s.getApplicationLog(reqParams)
|
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
case "getbestblockhash":
|
2019-10-29 17:51:17 +00:00
|
|
|
getbestblockhashCalled.Inc()
|
2019-11-27 09:23:18 +00:00
|
|
|
results = "0x" + s.chain.CurrentBlockHash().StringLE()
|
2018-03-23 20:36:59 +00:00
|
|
|
|
|
|
|
case "getblock":
|
2019-10-29 17:51:17 +00:00
|
|
|
getbestblockCalled.Inc()
|
2018-03-23 20:36:59 +00:00
|
|
|
var hash util.Uint256
|
|
|
|
|
2019-11-21 13:42:51 +00:00
|
|
|
param, ok := reqParams.Value(0)
|
|
|
|
if !ok {
|
2020-01-14 12:02:38 +00:00
|
|
|
resultsErr = response.ErrInvalidParams
|
2019-02-20 16:28:11 +00:00
|
|
|
break Methods
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch param.Type {
|
2020-01-14 12:02:38 +00:00
|
|
|
case request.StringT:
|
2019-11-21 13:42:51 +00:00
|
|
|
var err error
|
|
|
|
hash, err = param.GetUint256()
|
2018-03-23 20:36:59 +00:00
|
|
|
if err != nil {
|
2020-01-14 12:02:38 +00:00
|
|
|
resultsErr = response.ErrInvalidParams
|
2019-02-20 16:28:11 +00:00
|
|
|
break Methods
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
2020-01-14 12:02:38 +00:00
|
|
|
case request.NumberT:
|
2019-11-26 10:13:17 +00:00
|
|
|
num, err := s.blockHeightFromParam(param)
|
|
|
|
if err != nil {
|
2020-01-14 12:02:38 +00:00
|
|
|
resultsErr = response.ErrInvalidParams
|
2018-03-25 10:13:47 +00:00
|
|
|
break Methods
|
|
|
|
}
|
2019-11-26 10:13:17 +00:00
|
|
|
hash = s.chain.GetHeaderHash(num)
|
2019-11-26 10:31:11 +00:00
|
|
|
default:
|
2020-01-14 12:02:38 +00:00
|
|
|
resultsErr = response.ErrInvalidParams
|
2019-02-20 16:28:11 +00:00
|
|
|
break Methods
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 06:15:03 +00:00
|
|
|
block, err := s.chain.GetBlock(hash)
|
2018-03-23 20:36:59 +00:00
|
|
|
if err != nil {
|
2020-01-14 12:02:38 +00:00
|
|
|
resultsErr = response.NewInternalServerError(fmt.Sprintf("Problem locating block with hash: %s", hash), err)
|
2018-03-23 20:36:59 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-12-24 20:01:16 +00:00
|
|
|
if len(reqParams) == 2 && reqParams[1].Value == 1 {
|
2020-01-13 10:21:44 +00:00
|
|
|
results = result.NewBlock(block, s.chain)
|
2019-12-24 20:01:16 +00:00
|
|
|
} else {
|
|
|
|
writer := io.NewBufBinWriter()
|
|
|
|
block.EncodeBinary(writer.BinWriter)
|
|
|
|
results = hex.EncodeToString(writer.Bytes())
|
|
|
|
}
|
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
case "getblockcount":
|
2019-10-29 17:51:17 +00:00
|
|
|
getblockcountCalled.Inc()
|
2019-11-01 17:13:00 +00:00
|
|
|
results = s.chain.BlockHeight() + 1
|
2018-03-23 20:36:59 +00:00
|
|
|
|
|
|
|
case "getblockhash":
|
2019-10-29 17:51:17 +00:00
|
|
|
getblockHashCalled.Inc()
|
2020-01-14 12:02:38 +00:00
|
|
|
param, ok := reqParams.ValueWithType(0, request.NumberT)
|
2019-11-21 13:42:51 +00:00
|
|
|
if !ok {
|
2020-01-14 12:02:38 +00:00
|
|
|
resultsErr = response.ErrInvalidParams
|
2019-02-20 16:28:11 +00:00
|
|
|
break Methods
|
2019-11-26 10:13:17 +00:00
|
|
|
}
|
|
|
|
num, err := s.blockHeightFromParam(param)
|
|
|
|
if err != nil {
|
2020-01-14 12:02:38 +00:00
|
|
|
resultsErr = response.ErrInvalidParams
|
2019-02-20 16:28:11 +00:00
|
|
|
break Methods
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 10:13:17 +00:00
|
|
|
results = s.chain.GetHeaderHash(num)
|
2019-02-20 16:28:11 +00:00
|
|
|
|
2020-02-19 09:44:31 +00:00
|
|
|
case "getblocksysfee":
|
|
|
|
getblocksysfeeCalled.Inc()
|
|
|
|
results, resultsErr = s.getBlockSysFee(reqParams)
|
|
|
|
|
2020-02-26 12:42:04 +00:00
|
|
|
case "getclaimable":
|
|
|
|
getclaimableCalled.Inc()
|
|
|
|
results, resultsErr = s.getClaimable(reqParams)
|
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
case "getconnectioncount":
|
2019-10-29 17:51:17 +00:00
|
|
|
getconnectioncountCalled.Inc()
|
2018-03-23 20:36:59 +00:00
|
|
|
results = s.coreServer.PeerCount()
|
|
|
|
|
|
|
|
case "getversion":
|
2019-10-29 17:51:17 +00:00
|
|
|
getversionCalled.Inc()
|
2018-03-23 20:36:59 +00:00
|
|
|
results = result.Version{
|
2019-11-05 12:22:07 +00:00
|
|
|
Port: s.coreServer.Port,
|
2018-03-23 20:36:59 +00:00
|
|
|
Nonce: s.coreServer.ID(),
|
|
|
|
UserAgent: s.coreServer.UserAgent,
|
|
|
|
}
|
|
|
|
|
|
|
|
case "getpeers":
|
2019-10-29 17:51:17 +00:00
|
|
|
getpeersCalled.Inc()
|
2020-01-10 12:13:29 +00:00
|
|
|
peers := result.NewGetPeers()
|
|
|
|
peers.AddUnconnected(s.coreServer.UnconnectedPeers())
|
|
|
|
peers.AddConnected(s.coreServer.ConnectedPeers())
|
|
|
|
peers.AddBad(s.coreServer.BadPeers())
|
2018-03-23 20:36:59 +00:00
|
|
|
results = peers
|
|
|
|
|
2020-03-02 16:13:44 +00:00
|
|
|
case "getrawmempool":
|
|
|
|
getrawmempoolCalled.Inc()
|
|
|
|
mp := s.chain.GetMemPool()
|
|
|
|
hashList := make([]util.Uint256, 0)
|
|
|
|
for _, item := range mp.GetVerifiedTransactions() {
|
|
|
|
hashList = append(hashList, item.Tx.Hash())
|
|
|
|
}
|
|
|
|
results = hashList
|
|
|
|
|
2020-01-30 08:03:44 +00:00
|
|
|
case "getstorage":
|
|
|
|
getstorageCalled.Inc()
|
|
|
|
results, resultsErr = s.getStorage(reqParams)
|
|
|
|
|
2019-02-13 18:18:47 +00:00
|
|
|
case "validateaddress":
|
2019-10-29 17:51:17 +00:00
|
|
|
validateaddressCalled.Inc()
|
2019-11-21 13:42:51 +00:00
|
|
|
param, ok := reqParams.Value(0)
|
|
|
|
if !ok {
|
2020-01-14 12:02:38 +00:00
|
|
|
resultsErr = response.ErrInvalidParams
|
2019-02-20 16:28:11 +00:00
|
|
|
break Methods
|
2019-02-13 18:18:47 +00:00
|
|
|
}
|
2020-01-13 08:27:22 +00:00
|
|
|
results = validateAddress(param.Value)
|
2019-02-13 18:18:47 +00:00
|
|
|
|
2018-11-26 21:12:33 +00:00
|
|
|
case "getassetstate":
|
2019-10-29 17:51:17 +00:00
|
|
|
getassetstateCalled.Inc()
|
2020-01-14 12:02:38 +00:00
|
|
|
param, ok := reqParams.ValueWithType(0, request.StringT)
|
2019-11-21 13:42:51 +00:00
|
|
|
if !ok {
|
2020-01-14 12:02:38 +00:00
|
|
|
resultsErr = response.ErrInvalidParams
|
2019-02-20 16:28:11 +00:00
|
|
|
break Methods
|
2018-11-26 21:12:33 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 13:42:51 +00:00
|
|
|
paramAssetID, err := param.GetUint256()
|
2019-02-08 08:04:38 +00:00
|
|
|
if err != nil {
|
2020-01-14 12:02:38 +00:00
|
|
|
resultsErr = response.ErrInvalidParams
|
2018-11-26 21:12:33 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
as := s.chain.GetAssetState(paramAssetID)
|
|
|
|
if as != nil {
|
2020-01-13 10:21:44 +00:00
|
|
|
results = result.NewAssetState(as)
|
2018-11-26 21:12:33 +00:00
|
|
|
} else {
|
2020-01-14 12:02:38 +00:00
|
|
|
resultsErr = response.NewRPCError("Unknown asset", "", nil)
|
2018-11-26 21:12:33 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 08:04:38 +00:00
|
|
|
case "getaccountstate":
|
2019-10-29 17:51:17 +00:00
|
|
|
getaccountstateCalled.Inc()
|
2019-11-15 19:04:10 +00:00
|
|
|
results, resultsErr = s.getAccountState(reqParams, false)
|
|
|
|
|
2020-02-15 16:53:08 +00:00
|
|
|
case "getcontractstate":
|
|
|
|
getcontractstateCalled.Inc()
|
|
|
|
results, resultsErr = s.getContractState(reqParams)
|
|
|
|
|
2019-02-20 17:39:32 +00:00
|
|
|
case "getrawtransaction":
|
2019-10-29 17:51:17 +00:00
|
|
|
getrawtransactionCalled.Inc()
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
results, resultsErr = s.getrawtransaction(reqParams)
|
2019-02-20 17:39:32 +00:00
|
|
|
|
2020-02-06 12:02:03 +00:00
|
|
|
case "gettxout":
|
|
|
|
gettxoutCalled.Inc()
|
|
|
|
results, resultsErr = s.getTxOut(reqParams)
|
|
|
|
|
2019-11-15 19:04:10 +00:00
|
|
|
case "getunspents":
|
|
|
|
getunspentsCalled.Inc()
|
|
|
|
results, resultsErr = s.getAccountState(reqParams, true)
|
|
|
|
|
2019-11-28 16:08:31 +00:00
|
|
|
case "invoke":
|
|
|
|
results, resultsErr = s.invoke(reqParams)
|
|
|
|
|
2019-11-26 10:13:17 +00:00
|
|
|
case "invokefunction":
|
|
|
|
results, resultsErr = s.invokeFunction(reqParams)
|
|
|
|
|
2019-10-29 15:31:39 +00:00
|
|
|
case "invokescript":
|
|
|
|
results, resultsErr = s.invokescript(reqParams)
|
|
|
|
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
case "sendrawtransaction":
|
2019-10-29 17:51:17 +00:00
|
|
|
sendrawtransactionCalled.Inc()
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
results, resultsErr = s.sendrawtransaction(reqParams)
|
2019-02-08 08:04:38 +00:00
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
default:
|
2020-01-14 12:02:38 +00:00
|
|
|
resultsErr = response.NewMethodNotFoundError(fmt.Sprintf("Method '%s' not supported", req.Method), nil)
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if resultsErr != nil {
|
2019-12-30 08:44:52 +00:00
|
|
|
s.WriteErrorResponse(req, w, resultsErr)
|
2018-03-25 10:13:47 +00:00
|
|
|
return
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 08:44:52 +00:00
|
|
|
s.WriteResponse(req, w, results)
|
2018-03-25 10:13:47 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 14:56:28 +00:00
|
|
|
// getApplicationLog returns the contract log based on the specified txid.
|
|
|
|
func (s *Server) getApplicationLog(reqParams request.Params) (interface{}, error) {
|
|
|
|
param, ok := reqParams.Value(0)
|
|
|
|
if !ok {
|
|
|
|
return nil, response.ErrInvalidParams
|
|
|
|
}
|
|
|
|
|
|
|
|
txHash, err := param.GetUint256()
|
|
|
|
if err != nil {
|
|
|
|
return nil, response.ErrInvalidParams
|
|
|
|
}
|
|
|
|
|
|
|
|
appExecResult, err := s.chain.GetAppExecResult(txHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, response.NewRPCError("Unknown transaction", "", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, _, err := s.chain.GetTransaction(txHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, response.NewRPCError("Error while getting transaction", "", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
var scriptHash util.Uint160
|
|
|
|
switch t := tx.Data.(type) {
|
|
|
|
case *transaction.InvocationTX:
|
|
|
|
scriptHash = hash.Hash160(t.Script)
|
|
|
|
default:
|
|
|
|
return nil, response.NewRPCError("Invalid transaction type", "", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.NewApplicationLog(appExecResult, scriptHash), nil
|
|
|
|
}
|
|
|
|
|
2020-02-26 12:42:04 +00:00
|
|
|
func (s *Server) getClaimable(ps request.Params) (interface{}, error) {
|
|
|
|
p, ok := ps.ValueWithType(0, request.StringT)
|
|
|
|
if !ok {
|
|
|
|
return nil, response.ErrInvalidParams
|
|
|
|
}
|
|
|
|
u, err := p.GetUint160FromAddress()
|
|
|
|
if err != nil {
|
|
|
|
return nil, response.ErrInvalidParams
|
|
|
|
}
|
|
|
|
|
|
|
|
var unclaimed []state.UnclaimedBalance
|
|
|
|
if acc := s.chain.GetAccountState(u); acc != nil {
|
|
|
|
unclaimed = acc.Unclaimed
|
|
|
|
}
|
|
|
|
|
|
|
|
var sum util.Fixed8
|
|
|
|
claimable := make([]result.Claimable, 0, len(unclaimed))
|
|
|
|
for _, ub := range unclaimed {
|
|
|
|
gen, sys, err := s.chain.CalculateClaimable(ub.Value, ub.Start, ub.End)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Info("error while calculating claim bonus", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
uc := gen.Add(sys)
|
|
|
|
sum += uc
|
|
|
|
|
|
|
|
claimable = append(claimable, result.Claimable{
|
|
|
|
Tx: ub.Tx,
|
|
|
|
N: int(ub.Index),
|
|
|
|
Value: ub.Value,
|
|
|
|
StartHeight: ub.Start,
|
|
|
|
EndHeight: ub.End,
|
|
|
|
Generated: gen,
|
|
|
|
SysFee: sys,
|
|
|
|
Unclaimed: uc,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.ClaimableInfo{
|
|
|
|
Spents: claimable,
|
|
|
|
Address: p.String(),
|
|
|
|
Unclaimed: sum,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
func (s *Server) getStorage(ps request.Params) (interface{}, error) {
|
2020-01-30 08:03:44 +00:00
|
|
|
param, ok := ps.Value(0)
|
|
|
|
if !ok {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
2020-01-30 08:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scriptHash, err := param.GetUint160FromHex()
|
|
|
|
if err != nil {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
2020-01-30 08:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scriptHash = scriptHash.Reverse()
|
|
|
|
|
|
|
|
param, ok = ps.Value(1)
|
|
|
|
if !ok {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
2020-01-30 08:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
key, err := param.GetBytesHex()
|
|
|
|
if err != nil {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
2020-01-30 08:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
item := s.chain.GetStorageItem(scriptHash.Reverse(), key)
|
|
|
|
if item == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return hex.EncodeToString(item.Value), nil
|
|
|
|
}
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
func (s *Server) getrawtransaction(reqParams request.Params) (interface{}, error) {
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
var resultsErr error
|
|
|
|
var results interface{}
|
|
|
|
|
2019-11-21 13:42:51 +00:00
|
|
|
if param0, ok := reqParams.Value(0); !ok {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
2019-11-21 13:42:51 +00:00
|
|
|
} else if txHash, err := param0.GetUint256(); err != nil {
|
2020-01-14 12:02:38 +00:00
|
|
|
resultsErr = response.ErrInvalidParams
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
} else if tx, height, err := s.chain.GetTransaction(txHash); err != nil {
|
|
|
|
err = errors.Wrapf(err, "Invalid transaction hash: %s", txHash)
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.NewRPCError("Unknown transaction", err.Error(), err)
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
} else if len(reqParams) >= 2 {
|
|
|
|
_header := s.chain.GetHeaderHash(int(height))
|
|
|
|
header, err := s.chain.GetHeader(_header)
|
|
|
|
if err != nil {
|
2020-01-14 12:02:38 +00:00
|
|
|
resultsErr = response.NewInvalidParamsError(err.Error(), err)
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 13:42:51 +00:00
|
|
|
param1, _ := reqParams.Value(1)
|
|
|
|
switch v := param1.Value.(type) {
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
|
|
|
|
case int, float64, bool, string:
|
|
|
|
if v == 0 || v == "0" || v == 0.0 || v == false || v == "false" {
|
|
|
|
results = hex.EncodeToString(tx.Bytes())
|
|
|
|
} else {
|
2020-01-13 10:21:44 +00:00
|
|
|
results = result.NewTransactionOutputRaw(tx, header, s.chain)
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
}
|
|
|
|
default:
|
2020-01-13 10:21:44 +00:00
|
|
|
results = result.NewTransactionOutputRaw(tx, header, s.chain)
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
results = hex.EncodeToString(tx.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, resultsErr
|
|
|
|
}
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
func (s *Server) getTxOut(ps request.Params) (interface{}, error) {
|
2020-02-06 12:02:03 +00:00
|
|
|
p, ok := ps.Value(0)
|
|
|
|
if !ok {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
2020-02-06 12:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h, err := p.GetUint256()
|
|
|
|
if err != nil {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
2020-02-06 12:02:03 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
p, ok = ps.ValueWithType(1, request.NumberT)
|
2020-02-06 12:02:03 +00:00
|
|
|
if !ok {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
2020-02-06 12:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
num, err := p.GetInt()
|
|
|
|
if err != nil || num < 0 {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
2020-02-06 12:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tx, _, err := s.chain.GetTransaction(h)
|
|
|
|
if err != nil {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.NewInvalidParamsError(err.Error(), err)
|
2020-02-06 12:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if num >= len(tx.Outputs) {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.NewInvalidParamsError("invalid index", errors.New("too big index"))
|
2020-02-06 12:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out := tx.Outputs[num]
|
2020-01-13 10:21:44 +00:00
|
|
|
return result.NewTxOutput(&out), nil
|
2020-02-06 12:02:03 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 16:53:08 +00:00
|
|
|
// getContractState returns contract state (contract information, according to the contract script hash).
|
2020-01-14 12:02:38 +00:00
|
|
|
func (s *Server) getContractState(reqParams request.Params) (interface{}, error) {
|
2020-02-15 16:53:08 +00:00
|
|
|
var results interface{}
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
param, ok := reqParams.ValueWithType(0, request.StringT)
|
2020-02-15 16:53:08 +00:00
|
|
|
if !ok {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
2020-02-15 16:53:08 +00:00
|
|
|
} else if scriptHash, err := param.GetUint160FromHex(); err != nil {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
2020-02-15 16:53:08 +00:00
|
|
|
} else {
|
|
|
|
cs := s.chain.GetContractState(scriptHash)
|
|
|
|
if cs != nil {
|
2020-01-13 10:21:44 +00:00
|
|
|
results = result.NewContractState(cs)
|
2020-02-15 16:53:08 +00:00
|
|
|
} else {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.NewRPCError("Unknown contract", "", nil)
|
2020-02-15 16:53:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2019-11-15 19:04:10 +00:00
|
|
|
// getAccountState returns account state either in short or full (unspents included) form.
|
2020-01-14 12:02:38 +00:00
|
|
|
func (s *Server) getAccountState(reqParams request.Params, unspents bool) (interface{}, error) {
|
2019-11-15 19:04:10 +00:00
|
|
|
var resultsErr error
|
|
|
|
var results interface{}
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
param, ok := reqParams.ValueWithType(0, request.StringT)
|
2019-11-21 13:42:51 +00:00
|
|
|
if !ok {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
2019-11-26 10:13:17 +00:00
|
|
|
} else if scriptHash, err := param.GetUint160FromAddress(); err != nil {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
2020-01-29 16:01:00 +00:00
|
|
|
} else {
|
|
|
|
as := s.chain.GetAccountState(scriptHash)
|
|
|
|
if as == nil {
|
|
|
|
as = state.NewAccount(scriptHash)
|
|
|
|
}
|
2019-11-15 19:04:10 +00:00
|
|
|
if unspents {
|
2019-11-26 10:13:17 +00:00
|
|
|
str, err := param.GetString()
|
|
|
|
if err != nil {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
2019-11-26 10:13:17 +00:00
|
|
|
}
|
2020-01-13 10:21:44 +00:00
|
|
|
results = result.NewUnspents(as, s.chain, str)
|
2019-11-15 19:04:10 +00:00
|
|
|
} else {
|
2020-01-13 10:21:44 +00:00
|
|
|
results = result.NewAccountState(as)
|
2019-11-15 19:04:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return results, resultsErr
|
|
|
|
}
|
|
|
|
|
2020-02-19 09:44:31 +00:00
|
|
|
// getBlockSysFee returns the system fees of the block, based on the specified index.
|
|
|
|
func (s *Server) getBlockSysFee(reqParams request.Params) (util.Fixed8, error) {
|
|
|
|
param, ok := reqParams.ValueWithType(0, request.NumberT)
|
|
|
|
if !ok {
|
|
|
|
return 0, response.ErrInvalidParams
|
|
|
|
}
|
|
|
|
|
|
|
|
num, err := s.blockHeightFromParam(param)
|
|
|
|
if err != nil {
|
|
|
|
return 0, response.NewRPCError("Invalid height", "", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
headerHash := s.chain.GetHeaderHash(num)
|
|
|
|
block, err := s.chain.GetBlock(headerHash)
|
|
|
|
if err != nil {
|
|
|
|
return 0, response.NewRPCError(err.Error(), "", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
var blockSysFee util.Fixed8
|
|
|
|
for _, tx := range block.Transactions {
|
|
|
|
blockSysFee += s.chain.SystemFee(tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return blockSysFee, nil
|
|
|
|
}
|
|
|
|
|
2019-11-28 16:08:31 +00:00
|
|
|
// invoke implements the `invoke` RPC call.
|
2020-01-14 12:02:38 +00:00
|
|
|
func (s *Server) invoke(reqParams request.Params) (interface{}, error) {
|
|
|
|
scriptHashHex, ok := reqParams.ValueWithType(0, request.StringT)
|
2019-11-28 16:08:31 +00:00
|
|
|
if !ok {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
2019-11-28 16:08:31 +00:00
|
|
|
}
|
|
|
|
scriptHash, err := scriptHashHex.GetUint160FromHex()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-14 12:02:38 +00:00
|
|
|
sliceP, ok := reqParams.ValueWithType(1, request.ArrayT)
|
2019-11-28 16:08:31 +00:00
|
|
|
if !ok {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
2019-11-28 16:08:31 +00:00
|
|
|
}
|
|
|
|
slice, err := sliceP.GetArray()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-17 11:54:53 +00:00
|
|
|
script, err := request.CreateInvocationScript(scriptHash, slice)
|
2019-11-28 16:08:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-28 16:12:23 +00:00
|
|
|
return s.runScriptInVM(script), nil
|
2019-11-28 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 10:13:17 +00:00
|
|
|
// invokescript implements the `invokescript` RPC call.
|
2020-01-14 12:02:38 +00:00
|
|
|
func (s *Server) invokeFunction(reqParams request.Params) (interface{}, error) {
|
|
|
|
scriptHashHex, ok := reqParams.ValueWithType(0, request.StringT)
|
2019-11-26 10:13:17 +00:00
|
|
|
if !ok {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
2019-11-26 10:13:17 +00:00
|
|
|
}
|
|
|
|
scriptHash, err := scriptHashHex.GetUint160FromHex()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-17 11:54:53 +00:00
|
|
|
script, err := request.CreateFunctionInvocationScript(scriptHash, reqParams[1:])
|
2019-11-26 10:13:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-28 16:12:23 +00:00
|
|
|
return s.runScriptInVM(script), nil
|
2019-11-26 10:13:17 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 15:31:39 +00:00
|
|
|
// invokescript implements the `invokescript` RPC call.
|
2020-01-14 12:02:38 +00:00
|
|
|
func (s *Server) invokescript(reqParams request.Params) (interface{}, error) {
|
2019-11-21 14:42:02 +00:00
|
|
|
if len(reqParams) < 1 {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
2019-10-29 15:31:39 +00:00
|
|
|
}
|
2019-11-21 14:42:02 +00:00
|
|
|
|
|
|
|
script, err := reqParams[0].GetBytesHex()
|
2019-10-29 15:31:39 +00:00
|
|
|
if err != nil {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
2019-10-29 15:31:39 +00:00
|
|
|
}
|
2019-11-21 14:42:02 +00:00
|
|
|
|
2019-11-28 16:12:23 +00:00
|
|
|
return s.runScriptInVM(script), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// runScriptInVM runs given script in a new test VM and returns the invocation
|
|
|
|
// result.
|
2020-01-13 09:27:34 +00:00
|
|
|
func (s *Server) runScriptInVM(script []byte) *result.Invoke {
|
2019-10-29 15:31:39 +00:00
|
|
|
vm, _ := s.chain.GetTestVM()
|
2020-01-21 12:42:56 +00:00
|
|
|
vm.SetGasLimit(s.config.MaxGasInvoke)
|
2019-10-29 15:31:39 +00:00
|
|
|
vm.LoadScript(script)
|
|
|
|
_ = vm.Run()
|
2020-01-13 09:27:34 +00:00
|
|
|
result := &result.Invoke{
|
2019-10-29 15:31:39 +00:00
|
|
|
State: vm.State(),
|
2020-01-21 12:37:47 +00:00
|
|
|
GasConsumed: vm.GasConsumed().String(),
|
2019-11-28 16:12:23 +00:00
|
|
|
Script: hex.EncodeToString(script),
|
2019-10-29 15:31:39 +00:00
|
|
|
Stack: vm.Estack(),
|
|
|
|
}
|
2019-11-28 16:12:23 +00:00
|
|
|
return result
|
2019-10-29 15:31:39 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
func (s *Server) sendrawtransaction(reqParams request.Params) (interface{}, error) {
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
var resultsErr error
|
|
|
|
var results interface{}
|
|
|
|
|
2019-11-21 14:42:02 +00:00
|
|
|
if len(reqParams) < 1 {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
2019-11-21 14:42:02 +00:00
|
|
|
} else if byteTx, err := reqParams[0].GetBytesHex(); err != nil {
|
2020-01-14 12:02:38 +00:00
|
|
|
return nil, response.ErrInvalidParams
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
} else {
|
2019-09-16 09:18:13 +00:00
|
|
|
r := io.NewBinReaderFromBuf(byteTx)
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
tx := &transaction.Transaction{}
|
2019-09-16 16:31:49 +00:00
|
|
|
tx.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
err = errors.Wrap(r.Err, "transaction DecodeBinary failed")
|
|
|
|
} else {
|
|
|
|
relayReason := s.coreServer.RelayTxn(tx)
|
|
|
|
switch relayReason {
|
|
|
|
case network.RelaySucceed:
|
|
|
|
results = true
|
|
|
|
case network.RelayAlreadyExists:
|
|
|
|
err = errors.New("block or transaction already exists and cannot be sent repeatedly")
|
|
|
|
case network.RelayOutOfMemory:
|
|
|
|
err = errors.New("the memory pool is full and no more transactions can be sent")
|
|
|
|
case network.RelayUnableToVerify:
|
|
|
|
err = errors.New("the block cannot be validated")
|
|
|
|
case network.RelayInvalid:
|
|
|
|
err = errors.New("block or transaction validation failed")
|
|
|
|
case network.RelayPolicyFail:
|
|
|
|
err = errors.New("one of the Policy filters failed")
|
|
|
|
default:
|
|
|
|
err = errors.New("unknown error")
|
|
|
|
}
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2020-01-14 12:02:38 +00:00
|
|
|
resultsErr = response.NewInternalServerError(err.Error(), err)
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, resultsErr
|
|
|
|
}
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
func (s *Server) blockHeightFromParam(param *request.Param) (int, error) {
|
2019-11-26 10:13:17 +00:00
|
|
|
num, err := param.GetInt()
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if num < 0 || num > int(s.chain.BlockHeight()) {
|
|
|
|
return 0, invalidBlockHeightError(0, num)
|
|
|
|
}
|
|
|
|
return num, nil
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
2020-01-13 08:27:22 +00:00
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
// WriteErrorResponse writes an error response to the ResponseWriter.
|
|
|
|
func (s *Server) WriteErrorResponse(r *request.In, w http.ResponseWriter, err error) {
|
|
|
|
jsonErr, ok := err.(*response.Error)
|
|
|
|
if !ok {
|
|
|
|
jsonErr = response.NewInternalServerError("Internal server error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := response.Raw{
|
|
|
|
HeaderAndError: response.HeaderAndError{
|
|
|
|
Header: response.Header{
|
|
|
|
JSONRPC: r.JSONRPC,
|
|
|
|
ID: r.RawID,
|
|
|
|
},
|
|
|
|
Error: jsonErr,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
logFields := []zap.Field{
|
|
|
|
zap.Error(jsonErr.Cause),
|
|
|
|
zap.String("method", r.Method),
|
|
|
|
}
|
|
|
|
|
|
|
|
params, err := r.Params()
|
|
|
|
if err == nil {
|
|
|
|
logFields = append(logFields, zap.Any("params", params))
|
|
|
|
}
|
|
|
|
|
|
|
|
s.log.Error("Error encountered with rpc request", logFields...)
|
|
|
|
|
|
|
|
w.WriteHeader(jsonErr.HTTPCode)
|
|
|
|
s.writeServerResponse(r, w, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteResponse encodes the response and writes it to the ResponseWriter.
|
|
|
|
func (s *Server) WriteResponse(r *request.In, w http.ResponseWriter, result interface{}) {
|
2020-02-20 18:08:22 +00:00
|
|
|
resJSON, err := json.Marshal(result)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("Error encountered while encoding response",
|
|
|
|
zap.String("err", err.Error()),
|
|
|
|
zap.String("method", r.Method))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-14 12:02:38 +00:00
|
|
|
resp := response.Raw{
|
|
|
|
HeaderAndError: response.HeaderAndError{
|
|
|
|
Header: response.Header{
|
|
|
|
JSONRPC: r.JSONRPC,
|
|
|
|
ID: r.RawID,
|
|
|
|
},
|
|
|
|
},
|
2020-02-20 18:08:22 +00:00
|
|
|
Result: resJSON,
|
2020-01-14 12:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.writeServerResponse(r, w, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) writeServerResponse(r *request.In, w http.ResponseWriter, resp response.Raw) {
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
if s.config.EnableCORSWorkaround {
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With")
|
|
|
|
}
|
|
|
|
|
|
|
|
encoder := json.NewEncoder(w)
|
|
|
|
err := encoder.Encode(resp)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("Error encountered while encoding response",
|
|
|
|
zap.String("err", err.Error()),
|
|
|
|
zap.String("method", r.Method))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 08:27:22 +00:00
|
|
|
// validateAddress verifies that the address is a correct NEO address
|
|
|
|
// see https://docs.neo.org/en-us/node/cli/2.9.4/api/validateaddress.html
|
2020-01-13 08:33:04 +00:00
|
|
|
func validateAddress(addr interface{}) result.ValidateAddress {
|
|
|
|
resp := result.ValidateAddress{Address: addr}
|
2020-01-13 08:27:22 +00:00
|
|
|
if addr, ok := addr.(string); ok {
|
|
|
|
_, err := address.StringToUint160(addr)
|
|
|
|
resp.IsValid = (err == nil)
|
|
|
|
}
|
|
|
|
return resp
|
|
|
|
}
|