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 asset api support #1062

Open
wants to merge 17 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ GoOnchain.iml
vendor/*
!vendor/github.com/ontio/ontology-crypto/*
Chain/*
Chain
/config/*
nodectl
ontology
Expand Down
11 changes: 6 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ language: go

go_import_path: github.com/ontio/ontology

install:
- go get github.com/Masterminds/glide
- glide install
os:
- linux
- osx

go:
- 1.10.x
- "1.12.x"

script:
- env GO111MODULE=on make all-cross
- env GO111MODULE=on go mod vendor
- bash ./.travis.check-license.sh
- bash ./.travis.gofmt.sh
- make all-cross
- bash ./.travis.gotest.sh
1 change: 1 addition & 0 deletions cmd/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ var AppHelpFlagGroups = []flagGroup{
Flags: []cli.Flag{
utils.ConfigFlag,
utils.LogLevelFlag,
utils.DisableLogFileFlag,
utils.DisableEventLogFlag,
utils.DataDirFlag,
},
Expand Down
4 changes: 4 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ var (
Usage: "Set the log level to `<level>` (0~6). 0:Trace 1:Debug 2:Info 3:Warn 4:Error 5:Fatal 6:MaxLevel",
Value: config.DEFAULT_LOG_LEVEL,
}
DisableLogFileFlag = cli.BoolFlag{
Name: "disable-log-file",
Usage: "Discard log output to file",
}
DisableEventLogFlag = cli.BoolFlag{
Name: "disable-event-log",
Usage: "Discard event log output by smart contract execution",
Expand Down
8 changes: 8 additions & 0 deletions cmd/utils/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package utils
import (
"encoding/hex"
"fmt"
"github.com/ontio/ontology/common"
"reflect"
"strconv"
"strings"
Expand All @@ -34,6 +35,7 @@ const (
PARAM_TYPE_STRING = "string"
PARAM_TYPE_INTEGER = "int"
PARAM_TYPE_BOOLEAN = "bool"
PARAM_TYPE_ADDRESS = "address"
PARAM_LEFT_BRACKET = "["
PARAM_RIGHT_BRACKET = "]"
PARAM_ESC_CHAR = `/`
Expand Down Expand Up @@ -194,6 +196,12 @@ func parseRawParamValue(pType string, pValue string) (interface{}, error) {
default:
return nil, fmt.Errorf("parse boolean param:%s failed", pValue)
}
case PARAM_TYPE_ADDRESS:
address, err := common.AddressFromBase58(pValue)
if err != nil {
return nil, fmt.Errorf("parse address param:%s failed", pValue)
}
return address[:], nil
default:
return nil, fmt.Errorf("unspport param type:%s", pType)
}
Expand Down
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 = 6300000
const OPCODE_HEIGHT_UPDATE_FIRST_POLARIS = 2100000
25 changes: 22 additions & 3 deletions consensus/vbft/block_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/ontio/ontology-crypto/keypair"
"github.com/ontio/ontology/common"
"github.com/ontio/ontology/common/log"
"github.com/ontio/ontology/core/store/overlaydb"
)

type BlockList []*Block
Expand Down Expand Up @@ -88,7 +89,7 @@ func newBlockPool(server *Server, historyLen uint32, store *ChainStore) (*BlockP

// load history blocks from chainstore
for ; blkNum <= store.GetChainedBlockNum(); blkNum++ {
blk, err := store.GetBlock(blkNum)
blk, err := store.getBlock(blkNum)
if err != nil {
return nil, fmt.Errorf("failed to load block %d: %s", blkNum, err)
}
Expand Down Expand Up @@ -660,7 +661,7 @@ func (pool *BlockPool) setBlockSealed(block *Block, forEmpty bool, sigdata bool)
if err := pool.chainStore.AddBlock(c.SealedBlock); err != nil {
return fmt.Errorf("failed to seal block (%d) to chainstore: %s", blkNum, err)
}
stateRoot, err := pool.chainStore.GetExecMerkleRoot(pool.chainStore.GetChainedBlockNum())
stateRoot, err := pool.chainStore.getExecMerkleRoot(pool.chainStore.GetChainedBlockNum())
if err != nil {
log.Errorf("handleBlockSubmit failed:%s", err)
return nil
Expand Down Expand Up @@ -691,7 +692,7 @@ func (pool *BlockPool) getSealedBlock(blockNum uint32) (*Block, common.Uint256)
}

// get from chainstore
blk, err := pool.chainStore.GetBlock(blockNum)
blk, err := pool.chainStore.getBlock(blockNum)
if err != nil {
log.Errorf("getSealedBlock %d err:%v", blockNum, err)
return nil, common.Uint256{}
Expand Down Expand Up @@ -755,3 +756,21 @@ func (pool *BlockPool) onBlockSealed(blockNum uint32) {
delete(pool.candidateBlocks, n)
}
}

func (pool *BlockPool) getExecMerkleRoot(blkNum uint32) (common.Uint256, error) {
pool.lock.RLock()
defer pool.lock.RUnlock()
return pool.chainStore.getExecMerkleRoot(blkNum)
}

func (pool *BlockPool) getExecWriteSet(blkNum uint32) *overlaydb.MemDB {
pool.lock.RLock()
defer pool.lock.RUnlock()
return pool.chainStore.getExecWriteSet(blkNum)
}

func (pool *BlockPool) submitBlock(blkNum uint32) error {
pool.lock.Lock()
defer pool.lock.Unlock()
return pool.chainStore.submitBlock(blkNum)
}
90 changes: 90 additions & 0 deletions consensus/vbft/block_pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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 vbft

import (
"testing"
"time"

"github.com/ontio/ontology/common"
"github.com/ontio/ontology/core/ledger"
"github.com/ontio/ontology/core/types"
)

func buildTestBlockPool(t *testing.T) (*BlockPool, error) {
store := newTestChainStore(t)
return newBlockPool(nil, 64, store)
}

func buildTestBlock(t *testing.T, lastBlock *types.Block, lgr *ledger.Ledger) (*Block, error) {
timestamp := uint32(time.Now().Unix())
if timestamp <= lastBlock.Header.Timestamp {
timestamp = lastBlock.Header.Timestamp + 1
}
txs := []*types.Transaction{}
txHash := []common.Uint256{}
txRoot := common.ComputeMerkleRoot(txHash)
blockRoot := lgr.GetBlockRootWithNewTxRoots(lastBlock.Header.Height, []common.Uint256{lastBlock.Header.TransactionsRoot, txRoot})

blkHeader := &types.Header{
PrevBlockHash: lastBlock.Header.Hash(),
TransactionsRoot: txRoot,
BlockRoot: blockRoot,
Timestamp: timestamp,
Height: lastBlock.Header.Height + 1,
ConsensusData: common.GetNonce(),
ConsensusPayload: nil,
}
blk := &types.Block{
Header: blkHeader,
Transactions: txs,
}
block := &Block{
Block: blk,
}
return block, nil
}
func TestAddBlock(t *testing.T) {
blockpool, err := buildTestBlockPool(t)
if err != nil {
t.Errorf("buildTestBlockPool err:%s", err)
}
lastBlock, _ := blockpool.getSealedBlock(0)
if lastBlock == nil {
t.Errorf("getblock err")
}
t.Logf("block height:%d", blockpool.chainStore.GetChainedBlockNum())
blk, err := buildTestBlock(t, lastBlock.Block, blockpool.chainStore.db)
if err != nil {
t.Errorf("buildTestBlock err:%s", err)
}
err = blockpool.chainStore.AddBlock(blk)
if err != nil {
t.Errorf("AddBlock err:%s", err)
}
merkleRoot, err := blockpool.getExecMerkleRoot(blockpool.chainStore.GetChainedBlockNum())
if err != nil {
t.Errorf("getExecMerkleRoot err:%s", err)
}
t.Logf("block height:%d,merkleRoot:%s", blockpool.chainStore.GetChainedBlockNum(), merkleRoot.ToHexString())
err = blockpool.submitBlock(blockpool.chainStore.GetChainedBlockNum())
if err != nil {
t.Errorf("submitBlock err:%s", err)
}
}
25 changes: 14 additions & 11 deletions consensus/vbft/chain_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func OpenBlockStore(db *ledger.Ledger, serverPid *actor.PID) (*ChainStore, error
return nil, fmt.Errorf("GetStateMerkleRoot blockNum:%d, error :%s", chainstore.chainedBlockNum, err)
}
writeSet := overlaydb.NewMemDB(1, 1)
block, err := chainstore.GetBlock(chainstore.chainedBlockNum)
block, err := chainstore.getBlock(chainstore.chainedBlockNum)
if err != nil {
return nil, err
}
Expand All @@ -71,7 +71,7 @@ func (self *ChainStore) GetChainedBlockNum() uint32 {
return self.chainedBlockNum
}

func (self *ChainStore) GetExecMerkleRoot(blkNum uint32) (common.Uint256, error) {
func (self *ChainStore) getExecMerkleRoot(blkNum uint32) (common.Uint256, error) {
if blk, present := self.pendingBlocks[blkNum]; blk != nil && present {
return blk.execResult.MerkleRoot, nil
}
Expand All @@ -85,7 +85,7 @@ func (self *ChainStore) GetExecMerkleRoot(blkNum uint32) (common.Uint256, error)

}

func (self *ChainStore) GetExecWriteSet(blkNum uint32) *overlaydb.MemDB {
func (self *ChainStore) getExecWriteSet(blkNum uint32) *overlaydb.MemDB {
if blk, present := self.pendingBlocks[blkNum]; blk != nil && present {
return blk.execResult.WriteSet
}
Expand Down Expand Up @@ -123,7 +123,7 @@ func (self *ChainStore) AddBlock(block *Block) error {
panic("nil block header")
}
blkNum := self.GetChainedBlockNum() + 1
err := self.SubmitBlock(blkNum - 1)
err := self.submitBlock(blkNum - 1)
if err != nil {
log.Errorf("chainstore blkNum:%d, SubmitBlock: %s", blkNum-1, err)
}
Expand All @@ -133,15 +133,18 @@ func (self *ChainStore) AddBlock(block *Block) error {
return fmt.Errorf("chainstore AddBlock GetBlockExecResult: %s", err)
}
self.pendingBlocks[blkNum] = &PendingBlock{block: block, execResult: &execResult, hasSubmitted: false}
self.pid.Tell(
&message.BlockConsensusComplete{
Block: block.Block,
})

if self.pid != nil {
self.pid.Tell(
&message.BlockConsensusComplete{
Block: block.Block,
})
}
self.chainedBlockNum = blkNum
return nil
}

func (self *ChainStore) SubmitBlock(blkNum uint32) error {
func (self *ChainStore) submitBlock(blkNum uint32) error {
if blkNum == 0 {
return nil
}
Expand All @@ -150,15 +153,15 @@ func (self *ChainStore) SubmitBlock(blkNum uint32) error {
if err != nil && blkNum > self.GetChainedBlockNum() {
return fmt.Errorf("ledger add submitBlk (%d, %d) failed: %s", blkNum, self.GetChainedBlockNum(), err)
}
submitBlk.hasSubmitted = true
if _, present := self.pendingBlocks[blkNum-1]; present {
delete(self.pendingBlocks, blkNum-1)
}
submitBlk.hasSubmitted = true
}
return nil
}

func (self *ChainStore) GetBlock(blockNum uint32) (*Block, error) {
func (self *ChainStore) getBlock(blockNum uint32) (*Block, error) {
if blk, present := self.pendingBlocks[blockNum]; present {
return blk.block, nil
}
Expand Down
7 changes: 3 additions & 4 deletions consensus/vbft/chain_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
package vbft

import (
"os"
"testing"

"github.com/ontio/ontology-crypto/keypair"
"github.com/ontio/ontology/account"
"github.com/ontio/ontology/common/config"
"github.com/ontio/ontology/common/log"
"github.com/ontio/ontology/core/genesis"
"github.com/ontio/ontology/core/ledger"
"os"
"testing"
)

func newTestChainStore(t *testing.T) *ChainStore {
Expand All @@ -37,7 +36,7 @@ func newTestChainStore(t *testing.T) *ChainStore {
if acct == nil {
t.Fatalf("GetDefaultAccount error: acc is nil")
}

os.RemoveAll(config.DEFAULT_DATA_DIR)
db, err := ledger.NewLedger(config.DEFAULT_DATA_DIR, 0)
if err != nil {
t.Fatalf("NewLedger error %s", err)
Expand Down
10 changes: 5 additions & 5 deletions consensus/vbft/msg_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ func (self *Server) constructBlock(blkNum uint32, prevBlkHash common.Uint256, tx
for _, t := range txs {
txHash = append(txHash, t.Hash())
}
lastBlock, err := self.chainStore.GetBlock(blkNum - 1)
if err != nil {
log.Errorf("constructBlock getlastblock err:%s,blknum:%d", err, blkNum-1)
return nil, err
lastBlock, _ := self.blockPool.getSealedBlock(blkNum - 1)
if lastBlock == nil {
log.Errorf("constructBlock getlastblock failed blknum:%d", blkNum-1)
return nil, fmt.Errorf("constructBlock getlastblock failed blknum:%d", blkNum-1)
}

txRoot := common.ComputeMerkleRoot(txHash)
Expand Down Expand Up @@ -264,7 +264,7 @@ func (self *Server) constructProposalMsg(blkNum uint32, sysTxs, userTxs []*types
if err != nil {
return nil, fmt.Errorf("failed to constuct blk: %s", err)
}
merkleRoot, err := self.chainStore.GetExecMerkleRoot(blkNum - 1)
merkleRoot, err := self.blockPool.getExecMerkleRoot(blkNum - 1)
if err != nil {
return nil, fmt.Errorf("failed to GetExecMerkleRoot: %s,blkNum:%d", err, (blkNum - 1))
}
Expand Down
Loading