2018-03-23 20:36:59 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-20 17:39:32 +00:00
|
|
|
"encoding/hex"
|
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
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core"
|
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"
|
2019-02-08 08:04:38 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto"
|
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"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/rpc/result"
|
2018-03-30 06:15:03 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/rpc/wrappers"
|
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"
|
2018-03-23 20:36:59 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Server represents the JSON-RPC 2.0 server.
|
|
|
|
Server struct {
|
|
|
|
*http.Server
|
|
|
|
chain core.Blockchainer
|
|
|
|
coreServer *network.Server
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-03-25 10:13:47 +00:00
|
|
|
var (
|
|
|
|
invalidBlockHeightError = func(index int, height int) error {
|
2019-02-08 08:04:38 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
// NewServer creates a new Server struct.
|
|
|
|
func NewServer(chain core.Blockchainer, port uint16, coreServer *network.Server) Server {
|
|
|
|
return Server{
|
|
|
|
Server: &http.Server{
|
2019-01-25 11:20:35 +00:00
|
|
|
Addr: ":" + strconv.FormatUint(uint64(port), 10),
|
2018-03-23 20:36:59 +00:00
|
|
|
},
|
|
|
|
chain: chain,
|
|
|
|
coreServer: coreServer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start creates a new JSON-RPC server
|
|
|
|
// listening on the configured port.
|
|
|
|
func (s *Server) Start(errChan chan error) {
|
|
|
|
s.Handler = http.HandlerFunc(s.requestHandler)
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"endpoint": s.Addr,
|
|
|
|
}).Info("starting rpc-server")
|
|
|
|
|
|
|
|
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 {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"endpoint": s.Addr,
|
|
|
|
}).Info("shutting down rpc-server")
|
|
|
|
return s.Server.Shutdown(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) requestHandler(w http.ResponseWriter, httpRequest *http.Request) {
|
|
|
|
req := NewRequest()
|
|
|
|
|
|
|
|
if httpRequest.Method != "POST" {
|
|
|
|
req.WriteErrorResponse(
|
|
|
|
w,
|
|
|
|
NewInvalidParamsError(
|
|
|
|
fmt.Sprintf("Invalid method '%s', please retry with 'POST'", httpRequest.Method), nil,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := req.DecodeData(httpRequest.Body)
|
|
|
|
if err != nil {
|
|
|
|
req.WriteErrorResponse(w, NewParseError("Problem parsing JSON-RPC request body", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
reqParams, err := req.Params()
|
|
|
|
if err != nil {
|
|
|
|
req.WriteErrorResponse(w, NewInvalidParamsError("Problem parsing request parameters", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.methodHandler(w, req, *reqParams)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) methodHandler(w http.ResponseWriter, req *Request, reqParams Params) {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"method": req.Method,
|
|
|
|
"params": fmt.Sprintf("%v", reqParams),
|
|
|
|
}).Info("processing rpc request")
|
|
|
|
|
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 {
|
|
|
|
case "getbestblockhash":
|
2019-10-29 17:51:17 +00:00
|
|
|
getbestblockhashCalled.Inc()
|
2019-08-22 17:33:58 +00:00
|
|
|
results = s.chain.CurrentBlockHash().ReverseString()
|
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-02-20 16:28:11 +00:00
|
|
|
param, err := reqParams.Value(0)
|
|
|
|
if err != nil {
|
|
|
|
resultsErr = err
|
|
|
|
break Methods
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch param.Type {
|
|
|
|
case "string":
|
2019-08-22 17:33:58 +00:00
|
|
|
hash, err = util.Uint256DecodeReverseString(param.StringVal)
|
2018-03-23 20:36:59 +00:00
|
|
|
if err != nil {
|
2019-02-20 16:28:11 +00:00
|
|
|
resultsErr = errInvalidParams
|
|
|
|
break Methods
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
case "number":
|
2018-03-25 10:13:47 +00:00
|
|
|
if !s.validBlockHeight(param) {
|
2019-02-20 16:28:11 +00:00
|
|
|
resultsErr = errInvalidParams
|
2018-03-25 10:13:47 +00:00
|
|
|
break Methods
|
|
|
|
}
|
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
hash = s.chain.GetHeaderHash(param.IntVal)
|
|
|
|
case "default":
|
2019-02-20 16:28:11 +00:00
|
|
|
resultsErr = errInvalidParams
|
|
|
|
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 {
|
|
|
|
resultsErr = NewInternalServerError(fmt.Sprintf("Problem locating block with hash: %s", hash), err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2018-03-30 06:15:03 +00:00
|
|
|
results = wrappers.NewBlock(block, s.chain)
|
2018-03-23 20:36:59 +00:00
|
|
|
case "getblockcount":
|
2019-10-29 17:51:17 +00:00
|
|
|
getblockcountCalled.Inc()
|
2018-03-23 20:36:59 +00:00
|
|
|
results = s.chain.BlockHeight()
|
|
|
|
|
|
|
|
case "getblockhash":
|
2019-10-29 17:51:17 +00:00
|
|
|
getblockHashCalled.Inc()
|
2019-02-20 16:28:11 +00:00
|
|
|
param, err := reqParams.ValueWithType(0, "number")
|
|
|
|
if err != nil {
|
|
|
|
resultsErr = err
|
|
|
|
break Methods
|
|
|
|
} else if !s.validBlockHeight(param) {
|
|
|
|
resultsErr = invalidBlockHeightError(0, param.IntVal)
|
|
|
|
break Methods
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 16:28:11 +00:00
|
|
|
results = s.chain.GetHeaderHash(param.IntVal)
|
|
|
|
|
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{
|
|
|
|
Port: s.coreServer.ListenTCP,
|
|
|
|
Nonce: s.coreServer.ID(),
|
|
|
|
UserAgent: s.coreServer.UserAgent,
|
|
|
|
}
|
|
|
|
|
|
|
|
case "getpeers":
|
2019-10-29 17:51:17 +00:00
|
|
|
getpeersCalled.Inc()
|
2018-03-23 20:36:59 +00:00
|
|
|
peers := result.NewPeers()
|
|
|
|
for _, addr := range s.coreServer.UnconnectedPeers() {
|
|
|
|
peers.AddPeer("unconnected", addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range s.coreServer.BadPeers() {
|
|
|
|
peers.AddPeer("bad", addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
for addr := range s.coreServer.Peers() {
|
2019-09-09 14:54:38 +00:00
|
|
|
peers.AddPeer("connected", addr.NetAddr().String())
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
results = peers
|
|
|
|
|
2019-02-13 18:18:47 +00:00
|
|
|
case "validateaddress":
|
2019-10-29 17:51:17 +00:00
|
|
|
validateaddressCalled.Inc()
|
2019-02-13 18:18:47 +00:00
|
|
|
param, err := reqParams.Value(0)
|
|
|
|
if err != nil {
|
|
|
|
resultsErr = err
|
2019-02-20 16:28:11 +00:00
|
|
|
break Methods
|
2019-02-13 18:18:47 +00:00
|
|
|
}
|
|
|
|
results = wrappers.ValidateAddress(param.RawValue)
|
|
|
|
|
2018-11-26 21:12:33 +00:00
|
|
|
case "getassetstate":
|
2019-10-29 17:51:17 +00:00
|
|
|
getassetstateCalled.Inc()
|
2019-02-20 16:28:11 +00:00
|
|
|
param, err := reqParams.ValueWithType(0, "string")
|
|
|
|
if err != nil {
|
|
|
|
resultsErr = err
|
|
|
|
break Methods
|
2018-11-26 21:12:33 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 17:33:58 +00:00
|
|
|
paramAssetID, err := util.Uint256DecodeReverseString(param.StringVal)
|
2019-02-08 08:04:38 +00:00
|
|
|
if err != nil {
|
2019-02-20 16:28:11 +00:00
|
|
|
resultsErr = errInvalidParams
|
2018-11-26 21:12:33 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
as := s.chain.GetAssetState(paramAssetID)
|
|
|
|
if as != nil {
|
|
|
|
results = wrappers.NewAssetState(as)
|
|
|
|
} else {
|
|
|
|
results = "Invalid assetid"
|
|
|
|
}
|
|
|
|
|
2019-02-08 08:04:38 +00:00
|
|
|
case "getaccountstate":
|
2019-10-29 17:51:17 +00:00
|
|
|
getaccountstateCalled.Inc()
|
2019-02-13 18:01:52 +00:00
|
|
|
param, err := reqParams.ValueWithType(0, "string")
|
|
|
|
if err != nil {
|
|
|
|
resultsErr = err
|
2019-02-08 08:04:38 +00:00
|
|
|
} else if scriptHash, err := crypto.Uint160DecodeAddress(param.StringVal); err != nil {
|
2019-02-20 16:28:11 +00:00
|
|
|
resultsErr = errInvalidParams
|
2019-02-08 08:04:38 +00:00
|
|
|
} else if as := s.chain.GetAccountState(scriptHash); as != nil {
|
|
|
|
results = wrappers.NewAccountState(as)
|
|
|
|
} else {
|
|
|
|
results = "Invalid public account address"
|
|
|
|
}
|
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
|
|
|
|
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:
|
|
|
|
resultsErr = NewMethodNotFoundError(fmt.Sprintf("Method '%s' not supported", req.Method), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resultsErr != nil {
|
|
|
|
req.WriteErrorResponse(w, resultsErr)
|
2018-03-25 10:13:47 +00:00
|
|
|
return
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-25 10:13:47 +00:00
|
|
|
req.WriteResponse(w, results)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (s *Server) getrawtransaction(reqParams Params) (interface{}, error) {
|
|
|
|
var resultsErr error
|
|
|
|
var results interface{}
|
|
|
|
|
|
|
|
param0, err := reqParams.ValueWithType(0, "string")
|
|
|
|
if err != nil {
|
|
|
|
resultsErr = err
|
2019-08-22 17:33:58 +00:00
|
|
|
} else if txHash, err := util.Uint256DecodeReverseString(param0.StringVal); err != nil {
|
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
|
|
|
resultsErr = errInvalidParams
|
|
|
|
} else if tx, height, err := s.chain.GetTransaction(txHash); err != nil {
|
|
|
|
err = errors.Wrapf(err, "Invalid transaction hash: %s", txHash)
|
|
|
|
resultsErr = NewInvalidParamsError(err.Error(), err)
|
|
|
|
} else if len(reqParams) >= 2 {
|
|
|
|
_header := s.chain.GetHeaderHash(int(height))
|
|
|
|
header, err := s.chain.GetHeader(_header)
|
|
|
|
if err != nil {
|
|
|
|
resultsErr = NewInvalidParamsError(err.Error(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
param1, _ := reqParams.ValueAt(1)
|
|
|
|
switch v := param1.RawValue.(type) {
|
|
|
|
|
|
|
|
case int, float64, bool, string:
|
|
|
|
if v == 0 || v == "0" || v == 0.0 || v == false || v == "false" {
|
|
|
|
results = hex.EncodeToString(tx.Bytes())
|
|
|
|
} else {
|
|
|
|
results = wrappers.NewTransactionOutputRaw(tx, header, s.chain)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
results = wrappers.NewTransactionOutputRaw(tx, header, s.chain)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
results = hex.EncodeToString(tx.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, resultsErr
|
|
|
|
}
|
|
|
|
|
2019-10-29 15:31:39 +00:00
|
|
|
// invokescript implements the `invokescript` RPC call.
|
|
|
|
func (s *Server) invokescript(reqParams Params) (interface{}, error) {
|
|
|
|
hexScript, err := reqParams.ValueWithType(0, "string")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
script, err := hex.DecodeString(hexScript.StringVal)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
vm, _ := s.chain.GetTestVM()
|
|
|
|
vm.LoadScript(script)
|
|
|
|
_ = vm.Run()
|
|
|
|
result := &wrappers.InvokeResult{
|
|
|
|
State: vm.State(),
|
|
|
|
GasConsumed: "0.1",
|
|
|
|
Script: hexScript.StringVal,
|
|
|
|
Stack: vm.Estack(),
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (s *Server) sendrawtransaction(reqParams Params) (interface{}, error) {
|
|
|
|
var resultsErr error
|
|
|
|
var results interface{}
|
|
|
|
|
|
|
|
param, err := reqParams.ValueWithType(0, "string")
|
|
|
|
if err != nil {
|
|
|
|
resultsErr = err
|
|
|
|
} else if byteTx, err := hex.DecodeString(param.StringVal); err != nil {
|
|
|
|
resultsErr = errInvalidParams
|
|
|
|
} 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 {
|
|
|
|
resultsErr = NewInternalServerError(err.Error(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, resultsErr
|
|
|
|
}
|
|
|
|
|
2018-03-25 10:13:47 +00:00
|
|
|
func (s Server) validBlockHeight(param *Param) bool {
|
|
|
|
return param.IntVal >= 0 && param.IntVal <= int(s.chain.BlockHeight())
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|