Commit eaaeeb54 authored by vipwzw's avatar vipwzw Committed by 33cn

update chain33

parent 60e4efbc
package init package init
import (
_ "github.com/33cn/plugin/plugin/consensus/para" //auto gen
_ "github.com/33cn/plugin/plugin/consensus/pbft" //auto gen
_ "github.com/33cn/plugin/plugin/consensus/raft" //auto gen
_ "github.com/33cn/plugin/plugin/consensus/tendermint" //auto gen
_ "github.com/33cn/plugin/plugin/consensus/ticket" //auto gen
)
package init package init
import (
_ "github.com/33cn/plugin/plugin/crypto/ecdsa" //auto gen
_ "github.com/33cn/plugin/plugin/crypto/sm2" //auto gen
)
package init package init
import (
_ "github.com/33cn/plugin/plugin/dapp/blackwhite" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/cert" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/evm" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/game" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/hashlock" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/lottery" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/multisig" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/norm" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/paracross" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/pokerbull" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/privacy" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/relay" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/retrieve" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/ticket" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/token" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/trade" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/unfreeze" //auto gen
_ "github.com/33cn/plugin/plugin/dapp/valnode" //auto gen
)
package init package init
import (
_ "github.com/33cn/plugin/plugin/store/kvdb" //auto gen
_ "github.com/33cn/plugin/plugin/store/kvmvcc" //auto gen
_ "github.com/33cn/plugin/plugin/store/mpt" //auto gen
)
...@@ -6,9 +6,9 @@ dist: xenial ...@@ -6,9 +6,9 @@ dist: xenial
notifications: notifications:
email: false email: false
jobs: matrix:
include: include:
- stage: check_fmt - name: check_fmt
sudo: require sudo: require
go: go:
- "1.9" - "1.9"
...@@ -23,13 +23,13 @@ jobs: ...@@ -23,13 +23,13 @@ jobs:
- make checkgofmt && make fmt_go - make checkgofmt && make fmt_go
- make linter - make linter
- stage: unit-test - name: unit-test
go: "1.9.x" go: "1.9.x"
install: skip install: skip
script: script:
- make test - make test
- stage: coverage - name: coverage
if: branch = master if: branch = master
go: go:
- "1.9.x" - "1.9.x"
...@@ -41,7 +41,7 @@ jobs: ...@@ -41,7 +41,7 @@ jobs:
after_success: after_success:
- bash <(curl -s https://codecov.io/bash) - bash <(curl -s https://codecov.io/bash)
- stage: deploy - name: deploy
sudo: required sudo: required
services: services:
- docker - docker
......
...@@ -8,7 +8,6 @@ package db ...@@ -8,7 +8,6 @@ package db
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
...@@ -16,7 +15,7 @@ import ( ...@@ -16,7 +15,7 @@ import (
) )
//ErrNotFoundInDb error //ErrNotFoundInDb error
var ErrNotFoundInDb = errors.New("ErrNotFoundInDb") var ErrNotFoundInDb = types.ErrNotFound
//Lister 列表接口 //Lister 列表接口
type Lister interface { type Lister interface {
......
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"fmt" "fmt"
"github.com/33cn/chain33/types"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
...@@ -234,3 +235,26 @@ func testDBIteratorDel(t *testing.T, db DB) { ...@@ -234,3 +235,26 @@ func testDBIteratorDel(t *testing.T, db DB) {
batch.Write() batch.Write()
} }
} }
func testLevelDBBatch(t *testing.T, db DB) {
batch := db.NewBatch(false)
batch.Set([]byte("hello"), []byte("world"))
err := batch.Write()
assert.Nil(t, err)
v, err := db.Get([]byte("hello"))
assert.Nil(t, err)
assert.Equal(t, v, []byte("world"))
//set and del
batch.Set([]byte("hello1"), []byte("world"))
batch.Set([]byte("hello2"), []byte("world"))
batch.Set([]byte("hello3"), []byte("world"))
batch.Set([]byte("hello4"), []byte("world"))
batch.Set([]byte("hello5"), []byte("world"))
batch.Delete([]byte("hello1"))
err = batch.Write()
assert.Nil(t, err)
v, err = db.Get([]byte("hello1"))
assert.Equal(t, err, types.ErrNotFound)
assert.Nil(t, v)
}
...@@ -43,6 +43,18 @@ func TestGoLevelDBIteratorDel(t *testing.T) { ...@@ -43,6 +43,18 @@ func TestGoLevelDBIteratorDel(t *testing.T) {
testDBIteratorDel(t, leveldb) testDBIteratorDel(t, leveldb)
} }
func TestLevelDBBatch(t *testing.T) {
dir, err := ioutil.TempDir("", "goleveldb")
require.NoError(t, err)
t.Log(dir)
leveldb, err := NewGoLevelDB("goleveldb", dir, 128)
require.NoError(t, err)
defer leveldb.Close()
testLevelDBBatch(t, leveldb)
}
// leveldb边界测试 // leveldb边界测试
func TestGoLevelDBBoundary(t *testing.T) { func TestGoLevelDBBoundary(t *testing.T) {
dir, err := ioutil.TempDir("", "goleveldb") dir, err := ioutil.TempDir("", "goleveldb")
......
...@@ -55,7 +55,6 @@ func (db *ListHelper) List(prefix, key []byte, count, direction int32) (values [ ...@@ -55,7 +55,6 @@ func (db *ListHelper) List(prefix, key []byte, count, direction int32) (values [
return db.IteratorScanFromFirst(prefix, count) return db.IteratorScanFromFirst(prefix, count)
} }
return db.IteratorScanFromLast(prefix, count) return db.IteratorScanFromLast(prefix, count)
} }
if count == 1 && direction == ListSeek { if count == 1 && direction == ListSeek {
it := db.db.Iterator(prefix, nil, true) it := db.db.Iterator(prefix, nil, true)
......
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package table
import (
"math"
"github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/types"
)
//Count 计数器
type Count struct {
prefix string
name string
kvdb db.KV
num int64
keydata []byte
}
//NewCount 创建一个计数器
func NewCount(prefix string, name string, kvdb db.KV) *Count {
keydata := []byte(prefix + "#" + name)
return &Count{
prefix: prefix,
name: name,
kvdb: kvdb,
keydata: keydata,
num: math.MinInt64,
}
}
func (c *Count) getKey() []byte {
return c.keydata
}
//Save 保存kv
func (c *Count) Save() (kvs []*types.KeyValue, err error) {
if c.num == math.MinInt64 {
return nil, nil
}
var i types.Int64
i.Data = c.num
item := &types.KeyValue{Key: c.getKey(), Value: types.Encode(&i)}
kvs = append(kvs, item)
return
}
//Get count
func (c *Count) Get() (int64, error) {
if c.num == math.MinInt64 {
data, err := c.kvdb.Get(c.getKey())
if err == types.ErrNotFound {
c.num = 0
} else if err != nil {
return 0, err
}
var num types.Int64
err = types.Decode(data, &num)
if err != nil {
return 0, err
}
c.num = num.Data
}
return c.num, nil
}
//Inc 增加1
func (c *Count) Inc() (num int64, err error) {
c.num, err = c.Get()
if err != nil {
return 0, err
}
c.num++
return c.num, nil
}
//Dec 减少1
func (c *Count) Dec() (num int64, err error) {
c.num, err = c.Get()
if err != nil {
return 0, err
}
c.num--
return c.num, nil
}
//Set 这个操作要谨慎使用
func (c *Count) Set(i int64) {
c.num = i
}
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package table
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCount(t *testing.T) {
dir, leveldb, kvdb := getdb()
defer dbclose(dir, leveldb)
count := NewCount("prefix", "name#hello", kvdb)
count.Inc()
count.Dec()
count.Inc()
i, err := count.Get()
assert.Nil(t, err)
assert.Equal(t, i, int64(1))
kvs, err := count.Save()
assert.Nil(t, err)
setKV(leveldb, kvs)
count = NewCount("prefix", "name#hello", kvdb)
i, err = count.Get()
assert.Nil(t, err)
assert.Equal(t, i, int64(1))
count.Set(2)
i, err = count.Get()
assert.Nil(t, err)
assert.Equal(t, i, int64(2))
}
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package table
import "errors"
//table 中的错误处理
var (
ErrEmptyPrimaryKey = errors.New("ErrEmptyPrimaryKey")
ErrPrimaryKey = errors.New("ErrPrimaryKey")
ErrIndexKey = errors.New("ErrIndexKey")
ErrTooManyIndex = errors.New("ErrTooManyIndex")
ErrTablePrefixOrTableName = errors.New("ErrTablePrefixOrTableName")
ErrDupPrimaryKey = errors.New("ErrDupPrimaryKey")
)
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package table
import (
"github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/types"
)
//Query 列表查询结构
type Query struct {
table *Table
kvdb db.KVDB
}
//ListIndex 根据索引查询列表
//index 用哪个index
//prefix 必须要符合的前缀, 可以为空
//primaryKey 开始查询的位置(不包含数据本身)
//count 最多取的数量
//direction 方向
func (query *Query) ListIndex(indexName string, prefix []byte, primaryKey []byte, count, direction int32) (rows []*Row, err error) {
if indexName == "" {
return query.ListPrimary(prefix, primaryKey, count, direction)
}
p := query.table.indexPrefix(indexName)
var k []byte
if len(primaryKey) > 0 {
row, err := query.table.GetData(primaryKey)
if err != nil {
return nil, err
}
key, err := query.table.index(row, indexName)
if err != nil {
return nil, err
}
//如果存在prefix
if prefix != nil {
p2 := commonPrefix(prefix, key)
if len(p2) != len(prefix) {
return nil, types.ErrNotFound
}
p = append(p, p2...)
}
k = query.table.getIndexKey(indexName, key, row.Primary)
} else {
//这个情况下 k == nil
p = append(p, prefix...)
}
values, err := query.kvdb.List(p, k, count, direction)
if err != nil {
return nil, err
}
for _, value := range values {
row, err := query.table.GetData(value)
if err != nil {
return nil, err
}
rows = append(rows, row)
}
return rows, nil
}
//ListPrimary list primary data
func (query *Query) ListPrimary(prefix []byte, primaryKey []byte, count, direction int32) (rows []*Row, err error) {
p := query.table.primaryPrefix()
var k []byte
if primaryKey != nil {
if prefix != nil {
p2 := commonPrefix(prefix, primaryKey)
if len(p2) != len(prefix) {
return nil, types.ErrNotFound
}
p = append(p, p2...)
}
k = append(p, primaryKey...)
} else {
p = append(p, prefix...)
}
values, err := query.kvdb.List(p, k, count, direction)
if err != nil {
return nil, err
}
for _, value := range values {
row, err := query.table.getRow(value)
if err != nil {
return nil, err
}
rows = append(rows, row)
}
return rows, nil
}
func commonPrefix(key1, key2 []byte) []byte {
l1 := len(key1)
l2 := len(key2)
l := min(l1, l2)
for i := 0; i < l; i++ {
if key1[i] != key2[i] {
return key1[:i]
}
}
return key1[0:l]
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
This diff is collapsed.
This diff is collapsed.
...@@ -105,7 +105,7 @@ func CreateRawTx(cmd *cobra.Command, to string, amount float64, note string, isW ...@@ -105,7 +105,7 @@ func CreateRawTx(cmd *cobra.Command, to string, amount float64, note string, isW
transfer.Ty = cty.CoinsActionTransfer transfer.Ty = cty.CoinsActionTransfer
} }
} else { } else {
v := &cty.CoinsAction_Withdraw{Withdraw: &types.AssetsWithdraw{Amount: amountInt64, Note: []byte(note), ExecName: execName}} v := &cty.CoinsAction_Withdraw{Withdraw: &types.AssetsWithdraw{Amount: amountInt64, Note: []byte(note), ExecName: execName, To: to}}
transfer.Value = v transfer.Value = v
transfer.Ty = cty.CoinsActionWithdraw transfer.Ty = cty.CoinsActionWithdraw
} }
......
...@@ -97,7 +97,7 @@ func (mem *Mempool) eventProcess() { ...@@ -97,7 +97,7 @@ func (mem *Mempool) eventProcess() {
func (mem *Mempool) eventTx(msg queue.Message) { func (mem *Mempool) eventTx(msg queue.Message) {
if !mem.getSync() { if !mem.getSync() {
msg.Reply(mem.client.NewMessage("", types.EventReply, &types.Reply{Msg: []byte(types.ErrNotSync.Error())})) msg.Reply(mem.client.NewMessage("", types.EventReply, &types.Reply{Msg: []byte(types.ErrNotSync.Error())}))
mlog.Error("wrong tx", "err", types.ErrNotSync.Error()) mlog.Debug("wrong tx", "err", types.ErrNotSync.Error())
} else { } else {
checkedMsg := mem.checkTxs(msg) checkedMsg := mem.checkTxs(msg)
select { select {
......
...@@ -111,7 +111,9 @@ func CreateTxWithExecer(priv crypto.PrivKey, execer string) *types.Transaction { ...@@ -111,7 +111,9 @@ func CreateTxWithExecer(priv crypto.PrivKey, execer string) *types.Transaction {
tx := &types.Transaction{Execer: []byte(execer), Payload: []byte("none")} tx := &types.Transaction{Execer: []byte(execer), Payload: []byte("none")}
tx.To = address.ExecAddress(execer) tx.To = address.ExecAddress(execer)
tx, _ = types.FormatTx(execer, tx) tx, _ = types.FormatTx(execer, tx)
if priv != nil {
tx.Sign(types.SECP256K1, priv) tx.Sign(types.SECP256K1, priv)
}
return tx return tx
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment