github.com/deroproject/derosuite@v2.1.6-1.0.20200307070847-0f2e589c7a2b+incompatible/blockchain/rpcserver/sendrawtransaction.go (about) 1 // Copyright 2017-2018 DERO Project. All rights reserved. 2 // Use of this source code in any form is governed by RESEARCH license. 3 // license can be found in the LICENSE file. 4 // GPG: 0F39 E425 8C65 3947 702A 8234 08B2 0360 A03A 9DE8 5 // 6 // 7 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 8 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 9 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 10 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 11 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 12 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 13 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 14 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 15 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 16 17 package rpcserver 18 19 import "fmt" 20 import "net/http" 21 import "encoding/hex" 22 import "encoding/json" 23 24 import "github.com/romana/rlog" 25 26 import "github.com/deroproject/derosuite/structures" 27 import "github.com/deroproject/derosuite/transaction" 28 29 // we definitely need to clear up the MESS that has been created by the MONERO project 30 // half of their APIs are json rpc and half are http 31 // for compatibility reasons, we are implementing theirs ( however we are also providin a json rpc implementation) 32 // we should DISCARD the http 33 34 // NOTE: we have currently not implemented the decode as json parameter 35 // it is however on the pending list 36 37 //type SendRawTransaction_Handler struct{} 38 39 func SendRawTransaction_Handler(rw http.ResponseWriter, req *http.Request) { 40 decoder := json.NewDecoder(req.Body) 41 var p structures.SendRawTransaction_Params 42 var result structures.SendRawTransaction_Result 43 var tx transaction.Transaction 44 45 defer func() { // provide result back whatever it is 46 encoder := json.NewEncoder(rw) 47 encoder.Encode(result) 48 }() 49 err := decoder.Decode(&p) 50 if err != nil { 51 logger.Warnf("err while decoding incoming sendrawtransaaction json err: %s", err) 52 return 53 } 54 defer req.Body.Close() 55 56 rlog.Debugf("Incoming TX from RPC Server") 57 58 //lets decode the tx from hex 59 tx_bytes, err := hex.DecodeString(p.Tx_as_hex) 60 61 if err != nil { 62 result.Status = "TX could be hex decoded" 63 return 64 } 65 if len(tx_bytes) < 100 { 66 result.Status = "TX insufficient length" 67 return 68 } 69 // lets add tx to pool, if we can do it, so can every one else 70 err = tx.DeserializeHeader(tx_bytes) 71 if err != nil { 72 rlog.Debugf("Incoming TX from RPC Server could NOT be deserialized") 73 result.Status = err.Error() 74 return 75 } 76 77 rlog.Infof("Incoming TXID %s from RPC Server", tx.GetHash()) 78 // lets try to add it to pool 79 success := chain.Add_TX_To_Pool(&tx) 80 81 if success { 82 result.Status = "OK" 83 rlog.Infof("Incoming TXID %s from RPC Server successfully accepted by MEMPOOL", tx.GetHash()) 84 } else { 85 86 result.Status = fmt.Sprintf("Transaction %s rejected by daemon, check daemon msgs", tx.GetHash()) 87 rlog.Warnf("Incoming TXID %s from RPC Server rejected by MEMPOOL", tx.GetHash()) 88 } 89 90 }