-
Notifications
You must be signed in to change notification settings - Fork 293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add multi pretransaction support #1026
base: master
Are you sure you want to change the base?
Changes from 7 commits
d3b0e46
cada27a
6475ec6
6939a6e
7b621c7
6ff4ddf
1ca8709
c1ac107
8cccaea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -287,6 +287,45 @@ func SendRawTransaction(cmd map[string]interface{}) map[string]interface{} { | |
return resp | ||
} | ||
|
||
// multi pre tx, [tx1, tx2,tx3] | ||
func MultiPreTransaction(params map[string]interface{}) map[string]interface{} { | ||
if len(params) < 1 { | ||
return ResponsePack(berr.INVALID_PARAMS) | ||
} | ||
paras, ok := params["Data"].([]interface{}) | ||
if !ok || len(paras) < 1 { | ||
return ResponsePack(berr.INVALID_PARAMS) | ||
} | ||
res := make([]interface{}, 0) | ||
for _, param := range paras { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. set a limit on len(paras) |
||
txStr, ok := param.(string) | ||
if !ok { | ||
return ResponsePack(berr.INVALID_PARAMS) | ||
} | ||
raw, err := common.HexToBytes(txStr) | ||
if err != nil { | ||
return ResponsePack(berr.INVALID_PARAMS) | ||
} | ||
txn, err := types.TransactionFromRawBytes(raw) | ||
if err != nil { | ||
return ResponsePack(berr.INVALID_TRANSACTION) | ||
} | ||
hash := txn.Hash() | ||
log.Debugf("SendRawTransaction recv %s", hash.ToHexString()) | ||
if txn.TxType == types.Invoke || txn.TxType == types.Deploy { | ||
result, err := bactor.PreExecuteContract(txn) | ||
if err != nil { | ||
log.Infof("PreExec: ", err) | ||
return ResponsePack(berr.SMARTCODE_ERROR) | ||
} | ||
res = append(res, result) | ||
} | ||
} | ||
resp := ResponsePack(berr.SUCCESS) | ||
resp["result"] = res | ||
return resp | ||
} | ||
|
||
//get smartcontract event by height | ||
func GetSmartCodeEventTxsByHeight(cmd map[string]interface{}) map[string]interface{} { | ||
resp := ResponsePack(berr.SUCCESS) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -303,6 +303,39 @@ func SendRawTransaction(params []interface{}) map[string]interface{} { | |
return responseSuccess(hash.ToHexString()) | ||
} | ||
|
||
// multi pre tx, [tx1, tx2,tx3] | ||
func MultiPreTransaction(params []interface{}) map[string]interface{} { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not reuse the above function? |
||
if len(params) < 1 { | ||
return responsePack(berr.INVALID_PARAMS, nil) | ||
} | ||
res := make([]interface{}, 0) | ||
for _, param := range params { | ||
txStr, ok := param.(string) | ||
if !ok { | ||
return responsePack(berr.INVALID_PARAMS, "") | ||
} | ||
raw, err := common.HexToBytes(txStr) | ||
if err != nil { | ||
return responsePack(berr.INVALID_PARAMS, err.Error()) | ||
} | ||
txn, err := types.TransactionFromRawBytes(raw) | ||
if err != nil { | ||
return responsePack(berr.INVALID_TRANSACTION, "") | ||
} | ||
hash := txn.Hash() | ||
log.Debugf("SendRawTransaction recv %s", hash.ToHexString()) | ||
if txn.TxType == types.Invoke || txn.TxType == types.Deploy { | ||
result, err := bactor.PreExecuteContract(txn) | ||
if err != nil { | ||
log.Infof("PreExec: ", err) | ||
return responsePack(berr.SMARTCODE_ERROR, err.Error()) | ||
} | ||
res = append(res, result) | ||
} | ||
} | ||
return responseSuccess(res) | ||
} | ||
|
||
//get node version | ||
func GetNodeVersion(params []interface{}) map[string]interface{} { | ||
return responseSuccess(config.Version) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,7 +76,8 @@ const ( | |
GET_VERSION = "/api/v1/version" | ||
GET_NETWORKID = "/api/v1/networkid" | ||
|
||
POST_RAW_TX = "/api/v1/transaction" | ||
POST_RAW_TX = "/api/v1/transaction" | ||
POST_MULTI_RAW_TX = "/api/v1/multitransaction" | ||
) | ||
|
||
//init restful server | ||
|
@@ -158,7 +159,8 @@ func (this *restServer) registryMethod() { | |
} | ||
|
||
postMethodMap := map[string]Action{ | ||
POST_RAW_TX: {name: "sendrawtransaction", handler: rest.SendRawTransaction}, | ||
POST_RAW_TX: {name: "sendrawtransaction", handler: rest.SendRawTransaction}, | ||
POST_MULTI_RAW_TX: {name: "multipretransaction", handler: rest.MultiPreTransaction}, | ||
} | ||
this.postMap = postMethodMap | ||
this.getMap = getMethodMap | ||
|
@@ -220,6 +222,8 @@ func (this *restServer) getParams(r *http.Request, url string, req map[string]in | |
req["Hash"], req["Raw"] = getParam(r, "hash"), r.FormValue("raw") | ||
case POST_RAW_TX: | ||
req["PreExec"] = r.FormValue("preExec") | ||
case POST_MULTI_RAW_TX: | ||
req["PreExec"] = r.FormValue("preExec") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MULTI_RAW_TX only supports preExec ? |
||
case GET_STORAGE: | ||
req["Hash"], req["Key"] = getParam(r, "hash"), getParam(r, "key") | ||
case GET_SMTCOCE_EVT_TXS: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (C) 2018 The ontology Authors | ||
* This file is part of The ontology library. | ||
* | ||
* The ontology is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The ontology is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with The ontology. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package test | ||
|
||
import ( | ||
"github.com/ontio/ontology/smartcontract" | ||
"github.com/ontio/ontology/vm/neovm" | ||
"github.com/ontio/ontology/vm/neovm/errors" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestHeight(t *testing.T) { | ||
byteCode0 := []byte{ | ||
byte(neovm.NEWMAP), | ||
byte(neovm.PUSH0), | ||
byte(neovm.HASKEY), | ||
} | ||
|
||
byteCode1 := []byte{ | ||
byte(neovm.NEWMAP), | ||
byte(neovm.KEYS), | ||
} | ||
|
||
byteCode2 := []byte{ | ||
byte(neovm.NEWMAP), | ||
byte(neovm.VALUES), | ||
} | ||
|
||
bytecode := [...][]byte{byteCode0, byteCode1, byteCode2} | ||
|
||
for i := 0; i < 3; i++ { | ||
config := &smartcontract.Config{ | ||
Time: 10, | ||
Height: 10, | ||
//Tx: &types.Transaction{}, | ||
} | ||
sc := smartcontract.SmartContract{ | ||
Config: config, | ||
Gas: 100, | ||
CacheDB: nil, | ||
} | ||
engine, err := sc.NewExecuteEngine(bytecode[i]) | ||
|
||
_, err = engine.Invoke() | ||
|
||
assert.EqualError(t, err, "[NeoVmService] vm execution error!: "+errors.ERR_NOT_SUPPORT_OPCODE.Error()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename as "PreexecuteMultiTransactions" ?