Skip to content
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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ func GetStateHashCheckHeight(id uint32) uint32 {
return STATE_HASH_CHECK_HEIGHT[id]
}

var OPCODE_UPDATE_CHECK_HEIGHT = map[uint32]uint32{
NETWORK_ID_MAIN_NET: constants.OPCODE_HEIGHT_UPDATE_FIRST_MAINNET, //Network main
NETWORK_ID_POLARIS_NET: constants.OPCODE_HEIGHT_UPDATE_FIRST_POLARIS, //Network polaris
NETWORK_ID_SOLO_NET: 0, //Network solo
}

func GetOpcodeUpdateCheckHeight(id uint32) uint32 {
return OPCODE_UPDATE_CHECK_HEIGHT[id]
}

func GetNetworkName(id uint32) string {
name, ok := NETWORK_NAME[id]
if ok {
Expand Down
4 changes: 4 additions & 0 deletions common/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,7 @@ const (
// ledger state hash check height
const STATE_HASH_HEIGHT_MAINNET = 3000000
const STATE_HASH_HEIGHT_POLARIS = 850000

// neovm opcode update check height
const OPCODE_HEIGHT_UPDATE_FIRST_MAINNET = 6000000
const OPCODE_HEIGHT_UPDATE_FIRST_POLARIS = 2100000
28 changes: 28 additions & 0 deletions consensus/vbft/msg_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,31 @@ func TestBlockFetchRespMsgDeserialize(t *testing.T) {
}
t.Logf("BlockFetchRespMsg Serialize succ: %v\n", respmsg.BlockNumber)
}

func TestBlockSerialization(t *testing.T) {
blk, err := constructBlock()
if err != nil {
t.Errorf("constructBlock failed: %v", err)
return
}

data, err := blk.Serialize()
if err != nil {
t.Fatalf("serialize blk: %s", err)
}

blk2 := &Block{}
if err := blk2.Deserialize(data); err != nil {
t.Fatalf("deserialize blk: %s", err)
}

blk.EmptyBlock = nil
data2, err := blk.Serialize()
if err != nil {
t.Fatalf("serialize blk2: %s", err)
}
blk3 := &Block{}
if err := blk3.Deserialize(data2); err != nil {
t.Fatalf("deserialize blk2: %s", err)
}
}
2 changes: 1 addition & 1 deletion consensus/vbft/node_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func testCalcParticipantPeers(t *testing.T, n, c int) {
for _, p := range pc {
peers[p] = true
}
if len(peers) <= 2*c+1 {
if len(peers) < 2*c+1 {
t.Fatalf("peers(%d, %d, %d, %d, %d, %d): %v, %v, %v", n, c, len(peers), len(pp), len(pe), len(pc), pp, pe, pc)
}
}
33 changes: 20 additions & 13 deletions consensus/vbft/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"io"

"github.com/ontio/ontology/common"
"github.com/ontio/ontology/common/log"
"github.com/ontio/ontology/consensus/vbft/config"
"github.com/ontio/ontology/core/types"
)
Expand Down Expand Up @@ -78,6 +77,7 @@ func (blk *Block) Serialize() ([]byte, error) {
payload := common.NewZeroCopySink(nil)
payload.WriteVarBytes(sink.Bytes())

payload.WriteBool(blk.EmptyBlock != nil)
if blk.EmptyBlock != nil {
sink2 := common.NewZeroCopySink(nil)
blk.EmptyBlock.Serialization(sink2)
Expand Down Expand Up @@ -109,22 +109,29 @@ func (blk *Block) Deserialize(data []byte) error {
}

var emptyBlock *types.Block
if source.Len() > 0 {
hasEmptyBlock, irr, eof := source.NextBool()
if irr {
return fmt.Errorf("read empty-block-bool: %s", common.ErrIrregularData)
}
if eof {
return fmt.Errorf("read empty-block-bool: %s", io.ErrUnexpectedEOF)
}
if hasEmptyBlock {
buf2, _, irregular, eof := source.NextVarBytes()
if irregular == false && eof == false {
block2, err := types.BlockFromRawBytes(buf2)
if err == nil {
emptyBlock = block2
}
if irregular || eof {
return fmt.Errorf("read empty block failed: %v, %v", irregular, eof)
}
block2, err := types.BlockFromRawBytes(buf2)
if err != nil {
return fmt.Errorf("deserialize empty blk failed: %s", err)
}
emptyBlock = block2
}

var merkleRoot common.Uint256
if source.Len() > 0 {
merkleRoot, eof = source.NextHash()
if eof {
log.Errorf("Block Deserialize merkleRoot")
return io.ErrUnexpectedEOF
}
merkleRoot, eof = source.NextHash()
if eof {
return fmt.Errorf("block deserialize merkleRoot: %s", io.ErrUnexpectedEOF)
}
blk.Block = block
blk.EmptyBlock = emptyBlock
Expand Down
68 changes: 68 additions & 0 deletions docs/specifications/restful_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ This document describes the restful api format for the http/https used in the On
| [post_raw_tx](#21-post_raw_tx) | post /api/v1/transaction?preExec=0 | send transaction to ontology network |
| [get_networkid](#22-get_networkid) | GET /api/v1/networkid | return the networkid |
| [get_grantong](#23-get_grantong) | GET /api/v1/grantong/:addr | get grant ong |
| [sendpretransactions](#24-sendpretransactions) | POST /api/v1/pretransactions | Batch execution of pre-executed transactions |

### 1 get_conn_count

Expand Down Expand Up @@ -872,6 +873,73 @@ curl -i http://localhost:20334/api/v1/grantong/AKDFapcoUhewN9Kaj6XhHusurfHzUiZqU
}
```

### 24 sendpretransactions

Batch execution of pre-executed transactions

POST
```
/api/v1/pretransactions
```
#### Request Example:
```
curl -i http://localhost:20334/api/v1/pretransactions
```

#### post parameters

```json
{
"jsonrpc": "2.0",
"Action": "sendpretransactions",
"Data": [
"00d1de5655540000000000000000000000000000000000000000000000000000000000000000000000004d14322df60ebaddf100501817f2171930d79ae81f660962616c616e63654f661400000000000000000000000000000000000000010068164f6e746f6c6f67792e4e61746976652e496e766f6b650000",
"00d1c9657e8b0000000000000000000000000000000000000000000000000000000000000000000000004d1498dee28a01a16f116f8c6e6b437af92a190e2c750962616c616e63654f661400000000000000000000000000000000000000010068164f6e746f6c6f67792e4e61746976652e496e766f6b650000",
"00d11aec11060000000000000000000000000000000000000000000000000000000000000000000000004d149e1969e4c7813787a8214e73d1b1206f3d2ffbcc0962616c616e63654f661400000000000000000000000000000000000000010068164f6e746f6c6f67792e4e61746976652e496e766f6b650000",
"00d1452edb510000000000000000000000000000000000000000000000000000000000000000000000004d14e224ca2cd7482712bdd64b2cc02b10db21494fd90962616c616e63654f661400000000000000000000000000000000000000010068164f6e746f6c6f67792e4e61746976652e496e766f6b650000"
],
"id": 0

}
```

#### Response
```
{
"Action": "sendpretransactions",
"Desc": "SUCCESS",
"Error": 0,
"Result": [
{
"State": 1,
"Gas": 20000,
"Result": "",
"Notify": null
},
{
"State": 1,
"Gas": 20000,
"Result": "",
"Notify": null
},
{
"State": 1,
"Gas": 20000,
"Result": "",
"Notify": null
},
{
"State": 1,
"Gas": 20000,
"Result": "",
"Notify": null
}
],
"Version": "1.0.0"
}
```


## Error Code

| Field | Type | Description |
Expand Down
67 changes: 67 additions & 0 deletions docs/specifications/restful_api_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
| [post_raw_tx](#21-post_raw_tx) | post /api/v1/transaction?preExec=0 | 向ontology网络发送交易 |
| [get_networkid](#22-get_networkid) | GET /api/v1/networkid | 得到network id |
| [get_grantong](#23-get_grantong) | GET /api/v1/grantong/:addr | 得到grant ong |
| [sendpretransactions](#24-sendpretransactions) | POST /api/v1/pretransactions | 预执行交易批量执行 |

### 1 get_conn_count

Expand Down Expand Up @@ -871,6 +872,72 @@ curl -i http://localhost:20334/api/v1/grantong/AKDFapcoUhewN9Kaj6XhHusurfHzUiZqU
}
```

### 24 sendpretransactions

批量执行预执行交易

POST
```
/api/v1/pretransactions
```
#### Request Example:
```
curl -i http://localhost:20334/api/v1/pretransactions
```

#### post parameters

```json
{
"jsonrpc": "2.0",
"Action": "sendpretransactions",
"Data": [
"00d1de5655540000000000000000000000000000000000000000000000000000000000000000000000004d14322df60ebaddf100501817f2171930d79ae81f660962616c616e63654f661400000000000000000000000000000000000000010068164f6e746f6c6f67792e4e61746976652e496e766f6b650000",
"00d1c9657e8b0000000000000000000000000000000000000000000000000000000000000000000000004d1498dee28a01a16f116f8c6e6b437af92a190e2c750962616c616e63654f661400000000000000000000000000000000000000010068164f6e746f6c6f67792e4e61746976652e496e766f6b650000",
"00d11aec11060000000000000000000000000000000000000000000000000000000000000000000000004d149e1969e4c7813787a8214e73d1b1206f3d2ffbcc0962616c616e63654f661400000000000000000000000000000000000000010068164f6e746f6c6f67792e4e61746976652e496e766f6b650000",
"00d1452edb510000000000000000000000000000000000000000000000000000000000000000000000004d14e224ca2cd7482712bdd64b2cc02b10db21494fd90962616c616e63654f661400000000000000000000000000000000000000010068164f6e746f6c6f67792e4e61746976652e496e766f6b650000"
],
"id": 0

}
```

#### Response
```
{
"Action": "sendpretransactions",
"Desc": "SUCCESS",
"Error": 0,
"Result": [
{
"State": 1,
"Gas": 20000,
"Result": "",
"Notify": null
},
{
"State": 1,
"Gas": 20000,
"Result": "",
"Notify": null
},
{
"State": 1,
"Gas": 20000,
"Result": "",
"Notify": null
},
{
"State": 1,
"Gas": 20000,
"Result": "",
"Notify": null
}
],
"Version": "1.0.0"
}
```

## 错误代码

| Field | Type | Description |
Expand Down
68 changes: 68 additions & 0 deletions docs/specifications/rpc_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ There are some description of parameter used in rpc:
| [getblocktxsbyheight](#20-getblocktxsbyheight) | height | return transaction hashes | |
| [getnetworkid](#21-getnetworkid) | | Get the network id | |
| [getgrantong](#22-getgrantong) | | Get grant ong | |
| [sendpretransactions](#23-sendpretransactions) | POST /api/v1/sendpretransactions | Batch execution of pre-executed transactions |

### 1. getbestblockhash

Expand Down Expand Up @@ -1099,6 +1100,73 @@ Response:
}
```

### 23 sendpretransactions

Batch execution of pre-executed transactions

POST
```
/api/v1/sendpretransactions
```
#### Request Example:
```
curl -i http://localhost:20336/api/v1/sendpretransactions
```

#### post parameters

```json
{
"jsonrpc": "2.0",
"method": "sendpretransactions",
"params": [
"00d1de5655540000000000000000000000000000000000000000000000000000000000000000000000004d14322df60ebaddf100501817f2171930d79ae81f660962616c616e63654f661400000000000000000000000000000000000000010068164f6e746f6c6f67792e4e61746976652e496e766f6b650000",
"00d1c9657e8b0000000000000000000000000000000000000000000000000000000000000000000000004d1498dee28a01a16f116f8c6e6b437af92a190e2c750962616c616e63654f661400000000000000000000000000000000000000010068164f6e746f6c6f67792e4e61746976652e496e766f6b650000",
"00d11aec11060000000000000000000000000000000000000000000000000000000000000000000000004d149e1969e4c7813787a8214e73d1b1206f3d2ffbcc0962616c616e63654f661400000000000000000000000000000000000000010068164f6e746f6c6f67792e4e61746976652e496e766f6b650000",
"00d1452edb510000000000000000000000000000000000000000000000000000000000000000000000004d14e224ca2cd7482712bdd64b2cc02b10db21494fd90962616c616e63654f661400000000000000000000000000000000000000010068164f6e746f6c6f67792e4e61746976652e496e766f6b650000"
],
"id": 0

}
```

#### Response
```
{
"Action": "sendpretransactions",
"Desc": "SUCCESS",
"Error": 0,
"Result": [
{
"State": 1,
"Gas": 20000,
"Result": "",
"Notify": null
},
{
"State": 1,
"Gas": 20000,
"Result": "",
"Notify": null
},
{
"State": 1,
"Gas": 20000,
"Result": "",
"Notify": null
},
{
"State": 1,
"Gas": 20000,
"Result": "",
"Notify": null
}
],
"Version": "1.0.0"
}
```


## Error Code

errorcode instruction
Expand Down
Loading