Commit 34a91a59 authored by vipwzw's avatar vipwzw

update chain33

parent ebd5ec20
......@@ -943,7 +943,7 @@ func (bs *BlockStore) saveBlockSequence(storeBatch dbm.Batch, hash []byte, heigh
storeBatch.Set(calcSequenceToHashKey(newSequence), BlockSequenceByte)
//parachain hash->seq 只记录add block时的hash和seq对应关系
if Type == AddBlock && isParaChain {
if Type == AddBlock {
Sequencebytes := types.Encode(&types.Int64{Data: newSequence})
storeBatch.Set(calcHashToSequenceKey(hash), Sequencebytes)
}
......@@ -991,7 +991,7 @@ func (bs *BlockStore) GetSequenceByHash(hash []byte) (int64, error) {
if err != dbm.ErrNotFoundInDb {
storeLog.Error("GetSequenceByHash", "error", err)
}
return -1, types.ErrHeightNotExist
return -1, types.ErrHashNotExist
}
err = types.Decode(seqbytes, &seq)
......
......@@ -634,6 +634,7 @@ func testGetSeqByHash(t *testing.T, blockchain *blockchain.BlockChain) {
reqBlock.IsDetail = true
hashes := make([][]byte, 1)
Sequences, err := blockchain.GetBlockSequences(&reqBlock)
if err == nil && Sequences != nil {
for index, sequence := range Sequences.Items {
hashes[index] = sequence.Hash
......@@ -641,8 +642,8 @@ func testGetSeqByHash(t *testing.T, blockchain *blockchain.BlockChain) {
}
seq, _ := blockchain.ProcGetSeqByHash(hashes[0])
if seq != -1 {
t.Error("testGetSeqByHash only para chain GetSeqByHash ")
if seq == -1 {
t.Error(" GetSeqByHash err")
}
chainlog.Info("testGetSeqByHash end --------------------")
......
......@@ -455,11 +455,13 @@ func (chain *BlockChain) addParaChainBlockDetail(msg queue.Message) {
//parachian 通过blockhash获取对应的seq,只记录了addblock时的seq
func (chain *BlockChain) getSeqByHash(msg queue.Message) {
var sequence types.Int64
blockhash := (msg.Data).(*types.ReqHash)
sequence.Data, _ = chain.ProcGetSeqByHash(blockhash.Hash)
msg.Reply(chain.client.NewMessage("rpc", types.EventGetSeqByHash, &sequence))
seq, err := chain.ProcGetSeqByHash(blockhash.Hash)
if err != nil {
chainlog.Error("getSeqByHash", "err", err.Error())
msg.Reply(chain.client.NewMessage("rpc", types.EventReply, err))
}
msg.Reply(chain.client.NewMessage("rpc", types.EventGetSeqByHash, &types.Int64{Data: seq}))
}
//获取指定前缀key的数量
......
......@@ -25,7 +25,7 @@ func TestReindex(t *testing.T) {
chain := mock33.GetBlockChain()
db := chain.GetDB()
kvs := getAllKeys(db)
assert.Equal(t, len(kvs), 19)
assert.Equal(t, len(kvs), 20)
defer mock33.Close()
txs := util.GenCoinsTxs(mock33.GetGenesisKey(), 10)
for i := 0; i < len(txs); i++ {
......
......@@ -101,6 +101,9 @@ func (m *mockBlockChain) SetQueueClient(q queue.Queue) {
} else {
msg.ReplyErr("Do not support", types.ErrInvalidParam)
}
case types.EventGetSeqByHash:
msg.Reply(client.NewMessage(blockchainKey, types.EventReplyQuery, &types.Int64{Data: 1}))
case types.EventIsSync:
msg.Reply(client.NewMessage(blockchainKey, types.EventReplyIsSync, &types.IsCaughtUp{}))
case types.EventIsNtpClockSync:
......
......@@ -499,6 +499,29 @@ func (_m *QueueProtocolAPI) GetSeqCallBackLastNum(param *types.ReqString) (*type
return r0, r1
}
// GetSequenceByHash provides a mock function with given fields: param
func (_m *QueueProtocolAPI) GetSequenceByHash(param *types.ReqHash) (*types.Int64, error) {
ret := _m.Called(param)
var r0 *types.Int64
if rf, ok := ret.Get(0).(func(*types.ReqHash) *types.Int64); ok {
r0 = rf(param)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*types.Int64)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(*types.ReqHash) error); ok {
r1 = rf(param)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// GetTransactionByAddr provides a mock function with given fields: param
func (_m *QueueProtocolAPI) GetTransactionByAddr(param *types.ReqAddr) (*types.ReplyTxInfos, error) {
ret := _m.Called(param)
......
......@@ -898,6 +898,25 @@ func (q *QueueProtocol) GetLastBlockSequence() (*types.Int64, error) {
return nil, types.ErrTypeAsset
}
// GetSequenceByHash 通过hash获取对应的执行序列号
func (q *QueueProtocol) GetSequenceByHash(param *types.ReqHash) (*types.Int64, error) {
if param == nil {
err := types.ErrInvalidParam
log.Error("GetSequenceByHash", "Error", err)
return nil, err
}
msg, err := q.query(blockchainKey, types.EventGetSeqByHash, param)
if err != nil {
log.Error("GetSequenceByHash", "Error", err.Error())
return nil, err
}
if reply, ok := msg.GetData().(*types.Int64); ok {
return reply, nil
}
return nil, types.ErrTypeAsset
}
// WalletCreateTx create transaction
func (q *QueueProtocol) WalletCreateTx(param *types.ReqCreateTransaction) (*types.Transaction, error) {
msg, err := q.query(walletKey, types.EventWalletCreateTx, param)
......
......@@ -806,6 +806,7 @@ func TestGRPC(t *testing.T) {
testGetBlockOverviewGRPC(t, &grpcMock)
testGetAddrOverviewGRPC(t, &grpcMock)
testGetBlockHashGRPC(t, &grpcMock)
testGetSequenceByHashGRPC(t, &grpcMock)
testGenSeedGRPC(t, &grpcMock)
testGetSeedGRPC(t, &grpcMock)
testSaveSeedGRPC(t, &grpcMock)
......@@ -1131,3 +1132,11 @@ func testSendTxGRPC(t *testing.T, rpc *mockGRPCSystem) {
t.Error("Call SendTransaction Failed.", err)
}
}
func testGetSequenceByHashGRPC(t *testing.T, rpc *mockGRPCSystem) {
var res types.Int64
err := rpc.newRpcCtx("GetSequenceByHash", &types.ReqHash{}, &res)
if err != nil {
t.Error("Call GetSequenceByHash Failed.", err)
}
}
......@@ -111,6 +111,8 @@ type QueueProtocolAPI interface {
GetBlockSequences(param *types.ReqBlocks) (*types.BlockSequences, error)
//types.EventGetBlockByHashes:
GetBlockByHashes(param *types.ReqHashes) (*types.BlockDetails, error)
//types.EventGetSequenceByHash:
GetSequenceByHash(param *types.ReqHash) (*types.Int64, error)
// --------------- blockchain interfaces end
......
......@@ -315,6 +315,12 @@ func (c *GrpcCtx) Run() (err error) {
*c.Res.(*types.NodeNetInfo) = *reply
}
errRet = err
case "GetSequenceByHash":
reply, err := rpc.GetSequenceByHash(context.Background(), c.Params.(*types.ReqHash))
if err == nil {
*c.Res.(*types.Int64) = *reply
}
errRet = err
default:
errRet = errors.New(fmt.Sprintf("Unsupport method %v", c.Method))
}
......
......@@ -345,6 +345,11 @@ func (g *Grpc) GetBlockByHashes(ctx context.Context, in *pb.ReqHashes) (*pb.Bloc
return g.cli.GetBlockByHashes(in)
}
// GetSequenceByHash get block sequece by hash
func (g *Grpc) GetSequenceByHash(ctx context.Context, in *pb.ReqHash) (*pb.Int64, error) {
return g.cli.GetSequenceByHash(in)
}
// SignRawTx signature rawtransaction
func (g *Grpc) SignRawTx(ctx context.Context, in *pb.ReqSignRawTx) (*pb.ReplySignRawTx, error) {
return g.cli.SignRawTx(in)
......
......@@ -5,10 +5,10 @@
package rpc_test
import (
"fmt"
"testing"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/address"
"github.com/33cn/chain33/rpc/jsonclient"
rpctypes "github.com/33cn/chain33/rpc/types"
"github.com/33cn/chain33/types"
......@@ -19,16 +19,18 @@ import (
_ "github.com/33cn/chain33/system"
)
func getRPCClient(t *testing.T, mocker *testnode.Chain33Mock) *jsonclient.JSONClient {
jrpcClient := mocker.GetJSONC()
assert.NotNil(t, jrpcClient)
return jrpcClient
}
func TestErrLog(t *testing.T) {
// 启动RPCmocker
mocker := testnode.New("--free--", nil)
defer mocker.Close()
mocker.Listen()
rpcCfg := mocker.GetCfg().RPC
jrpcClient, err := jsonclient.NewJSONClient(fmt.Sprintf("http://%s/", rpcCfg.JrpcBindAddr))
assert.NoError(t, err)
assert.NotNil(t, jrpcClient)
jrpcClient := getRPCClient(t, mocker)
gen := mocker.GetGenesisKey()
//发送交易到区块链
addr1, key1 := util.Genaddress()
......@@ -55,3 +57,43 @@ func TestErrLog(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, string(testResult.Receipt.Logs[0].Log), `"ErrNoBalance"`)
}
func getTx(t *testing.T, hex string) *types.Transaction {
data, err := common.FromHex(hex)
assert.Nil(t, err)
var tx types.Transaction
err = types.Decode(data, &tx)
assert.Nil(t, err)
return &tx
}
func TestSendToExec(t *testing.T) {
mocker := testnode.New("--free--", nil)
defer mocker.Close()
mocker.Listen()
jrpcClient := getRPCClient(t, mocker)
//1. 调用createrawtransaction 创建交易
req := &rpctypes.CreateTx{
To: address.ExecAddress("user.f3d"),
Amount: 10,
Fee: 1,
Note: "12312",
IsWithdraw: false,
IsToken: false,
TokenSymbol: "",
ExecName: "user.f3d",
}
var res string
err := jrpcClient.Call("Chain33.CreateRawTransaction", req, &res)
assert.Nil(t, err)
gen := mocker.GetGenesisKey()
tx := getTx(t, res)
tx.Sign(types.SECP256K1, gen)
reply, err := mocker.GetAPI().SendTx(tx)
assert.Nil(t, err)
_, err = mocker.WaitTx(reply.GetMsg())
assert.Nil(t, err)
block := mocker.GetLastBlock()
balance := mocker.GetExecAccount(block.StateHash, "user.f3d", mocker.GetGenesisAddress()).Balance
assert.Equal(t, int64(10), balance)
}
......@@ -64,18 +64,19 @@ type Mempool struct {
// Consensus 配置
type Consensus struct {
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
GenesisBlockTime int64 `protobuf:"varint,2,opt,name=genesisBlockTime" json:"genesisBlockTime,omitempty"`
Minerstart bool `protobuf:"varint,3,opt,name=minerstart" json:"minerstart,omitempty"`
Genesis string `protobuf:"bytes,4,opt,name=genesis" json:"genesis,omitempty"`
HotkeyAddr string `protobuf:"bytes,5,opt,name=hotkeyAddr" json:"hotkeyAddr,omitempty"`
ForceMining bool `protobuf:"varint,6,opt,name=forceMining" json:"forceMining,omitempty"`
WriteBlockSeconds int64 `protobuf:"varint,20,opt,name=writeBlockSeconds" json:"writeBlockSeconds,omitempty"`
ParaRemoteGrpcClient string `protobuf:"bytes,22,opt,name=paraRemoteGrpcClient" json:"paraRemoteGrpcClient,omitempty"`
StartHeight int64 `protobuf:"varint,23,opt,name=startHeight" json:"startHeight,omitempty"`
EmptyBlockInterval int64 `protobuf:"varint,24,opt,name=emptyBlockInterval" json:"emptyBlockInterval,omitempty"`
AuthAccount string `protobuf:"bytes,25,opt,name=authAccount" json:"authAccount,omitempty"`
WaitBlocks4CommitMsg int32 `protobuf:"varint,26,opt,name=waitBlocks4CommitMsg" json:"waitBlocks4CommitMsg,omitempty"`
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
GenesisBlockTime int64 `protobuf:"varint,2,opt,name=genesisBlockTime" json:"genesisBlockTime,omitempty"`
Minerstart bool `protobuf:"varint,3,opt,name=minerstart" json:"minerstart,omitempty"`
Genesis string `protobuf:"bytes,4,opt,name=genesis" json:"genesis,omitempty"`
HotkeyAddr string `protobuf:"bytes,5,opt,name=hotkeyAddr" json:"hotkeyAddr,omitempty"`
ForceMining bool `protobuf:"varint,6,opt,name=forceMining" json:"forceMining,omitempty"`
WriteBlockSeconds int64 `protobuf:"varint,20,opt,name=writeBlockSeconds" json:"writeBlockSeconds,omitempty"`
ParaRemoteGrpcClient string `protobuf:"bytes,22,opt,name=paraRemoteGrpcClient" json:"paraRemoteGrpcClient,omitempty"`
StartHeight int64 `protobuf:"varint,23,opt,name=startHeight" json:"startHeight,omitempty"`
EmptyBlockInterval int64 `protobuf:"varint,24,opt,name=emptyBlockInterval" json:"emptyBlockInterval,omitempty"`
AuthAccount string `protobuf:"bytes,25,opt,name=authAccount" json:"authAccount,omitempty"`
WaitBlocks4CommitMsg int32 `protobuf:"varint,26,opt,name=waitBlocks4CommitMsg" json:"waitBlocks4CommitMsg,omitempty"`
SearchHashMatchedBlockDepth int32 `protobuf:"varint,27,opt,name=searchHashMatchedBlockDepth" json:"searchHashMatchedBlockDepth,omitempty"`
}
// Wallet 配置
......
......@@ -762,6 +762,36 @@ func (_m *Chain33Client) GetSeed(ctx context.Context, in *types.GetSeedByPw, opt
return r0, r1
}
// GetSequenceByHash provides a mock function with given fields: ctx, in, opts
func (_m *Chain33Client) GetSequenceByHash(ctx context.Context, in *types.ReqHash, opts ...grpc.CallOption) (*types.Int64, error) {
_va := make([]interface{}, len(opts))
for _i := range opts {
_va[_i] = opts[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, in)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
var r0 *types.Int64
if rf, ok := ret.Get(0).(func(context.Context, *types.ReqHash, ...grpc.CallOption) *types.Int64); ok {
r0 = rf(ctx, in, opts...)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*types.Int64)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, *types.ReqHash, ...grpc.CallOption) error); ok {
r1 = rf(ctx, in, opts...)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// GetTransactionByAddr provides a mock function with given fields: ctx, in, opts
func (_m *Chain33Client) GetTransactionByAddr(ctx context.Context, in *types.ReqAddr, opts ...grpc.CallOption) (*types.ReplyTxInfos, error) {
_va := make([]interface{}, len(opts))
......
......@@ -125,6 +125,9 @@ service chain33 {
//获取指定区间的block加载序列号信息
rpc GetBlockSequences(ReqBlocks) returns (BlockSequences) {}
//get add block's sequence by hash
rpc GetSequenceByHash(ReqHash) returns (Int64) {}
//通过block hash 获取对应的blocks信息
rpc GetBlockByHashes(ReqHashes) returns (BlockDetails) {}
//关闭chain33
......
......@@ -329,6 +329,14 @@ func (mock *Chain33Mock) GetAccount(stateHash []byte, addr string) *types.Accoun
return acc.LoadAccount(addr)
}
//GetExecAccount :get execer account info
func (mock *Chain33Mock) GetExecAccount(stateHash []byte, execer, addr string) *types.Account {
statedb := executor.NewStateDB(mock.client, stateHash, nil, nil)
acc := account.NewCoinsAccount()
acc.SetDB(statedb)
return acc.LoadExecAccount(addr, address.ExecAddress(execer))
}
//GetBlock :
func (mock *Chain33Mock) GetBlock(height int64) *types.Block {
blocks, err := mock.api.GetBlocks(&types.ReqBlocks{Start: height, End: height})
......
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