Skip to content

Commit

Permalink
Merge pull request #226 from QOSGroup/develop
Browse files Browse the repository at this point in the history
merge develop to master
  • Loading branch information
TokenxyWZY authored Dec 3, 2019
2 parents af3670b + 357b44a commit e96a43d
Show file tree
Hide file tree
Showing 87 changed files with 2,831 additions and 648 deletions.
19 changes: 10 additions & 9 deletions account/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@ v0.1
### 账户接口
```
type Account interface {
GetAddress() types.Address
SetAddress(addr types.Address) error
GetPubicKey() crypto.PubKey
GetAddress() types.AccAddress
SetAddress(addr types.AccAddress) error
GetPublicKey() crypto.PubKey
SetPublicKey(pubKey crypto.PubKey) error
GetNonce() uint64
SetNonce(nonce uint64) error
GetNonce() int64
SetNonce(nonce int64) error
}
```
## 数据结构
### BaseAccount
```
type BaseAccount struct{
AccountAddress common.Address `json:"account_address"` // account address
Publickey crypto.PubKey `json:"public_key"` // public key
Nonce uint64 `json:"nonce"` // identifies tx_status of an account
type BaseAccount struct {
AccountAddress types.AccAddress `json:"account_address"` // account address
Publickey crypto.PubKey `json:"public_key"` // public key
Nonce int64 `json:"nonce"` // identifies tx_status of an account
}
```
## 方法规定
- 获得结构原型
Expand Down
71 changes: 61 additions & 10 deletions account/account.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,53 @@
package account

import (
"encoding/json"

"github.com/QOSGroup/qbase/types"
"github.com/tendermint/tendermint/crypto"
)

type Account interface {
GetAddress() types.Address
SetAddress(addr types.Address) error
GetPubicKey() crypto.PubKey
GetAddress() types.AccAddress
SetAddress(addr types.AccAddress) error
GetPublicKey() crypto.PubKey
SetPublicKey(pubKey crypto.PubKey) error
GetNonce() int64
SetNonce(nonce int64) error
}

type BaseAccount struct {
AccountAddress types.Address `json:"account_address"` // account address
Publickey crypto.PubKey `json:"public_key"` // public key
Nonce int64 `json:"nonce"` // identifies tx_status of an account
AccountAddress types.AccAddress `json:"account_address"` // account address
Publickey crypto.PubKey `json:"public_key"` // public key
Nonce int64 `json:"nonce"` // identifies tx_status of an account
}

type jsonifyBaseAccount struct {
AccountAddress string `json:"account_address"`
Publickey string `json:"public_key"`
Nonce int64 `json:"nonce"`
}

func ProtoBaseAccount() Account {
return &BaseAccount{}
}

// getter for account address
func (accnt *BaseAccount) GetAddress() types.Address {
func (accnt *BaseAccount) GetAddress() types.AccAddress {
return accnt.AccountAddress
}

// setter for account address
func (accnt *BaseAccount) SetAddress(addr types.Address) error {
if len(addr) == 0 {
func (accnt *BaseAccount) SetAddress(addr types.AccAddress) error {
if addr.Empty() {
return types.ErrInvalidAddress(types.CodeToDefaultMsg(types.CodeInvalidAddress))
}
accnt.AccountAddress = addr
return nil
}

// getter for public key
func (accnt *BaseAccount) GetPubicKey() crypto.PubKey {
func (accnt *BaseAccount) GetPublicKey() crypto.PubKey {
return accnt.Publickey
}

Expand All @@ -59,3 +67,46 @@ func (accnt *BaseAccount) SetNonce(nonce int64) error {
accnt.Nonce = nonce
return nil
}

func (accnt *BaseAccount) MarshalJSON() ([]byte, error) {

var pk string
if accnt.Publickey != nil {
pk = types.MustAccPubKeyString(accnt.Publickey)
}

acc := jsonifyBaseAccount{
AccountAddress: accnt.AccountAddress.String(),
Publickey: pk,
Nonce: accnt.Nonce,
}

return json.Marshal(acc)
}

func (accnt *BaseAccount) UnmarshalJSON(data []byte) error {
var acc jsonifyBaseAccount
err := json.Unmarshal(data, &acc)
if err != nil {
return err
}

addr, err := types.AccAddressFromBech32(acc.AccountAddress)
if err != nil {
return err
}

var pk crypto.PubKey
if len(acc.Publickey) != 0 {
pk, err = types.GetAccPubKeyBech32(acc.Publickey)
if err != nil {
return err
}
}

accnt.AccountAddress = addr
accnt.Publickey = pk
accnt.Nonce = acc.Nonce

return nil
}
58 changes: 53 additions & 5 deletions account/account_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package account

import (
"encoding/json"
"fmt"
"testing"

Expand All @@ -9,13 +10,13 @@ import (
go_amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/encoding/amino"
cryptoAmino "github.com/tendermint/tendermint/crypto/encoding/amino"
)

func keyPubAddr() (crypto.PrivKey, crypto.PubKey, types.Address) {
func keyPubAddr() (crypto.PrivKey, crypto.PubKey, types.AccAddress) {
key := ed25519.GenPrivKey()
pub := key.PubKey()
addr := types.Address(pub.Address())
addr := types.AccAddress(pub.Address())
return key, pub, addr
}

Expand All @@ -30,6 +31,7 @@ func MakeCdc() *go_amino.Codec {
func TestAccountMarshal(t *testing.T) {

cdc := MakeCdc()
types.RegisterCodec(cdc)

_, pub, addr := keyPubAddr()
baseAccount := BaseAccount{addr, nil, 0}
Expand All @@ -47,13 +49,59 @@ func TestAccountMarshal(t *testing.T) {
err = cdc.UnmarshalBinaryLengthPrefixed(add_binary, &another_add)
require.Nil(t, err)
require.Equal(t, baseAccount, another_add)

// error on bad bytes
another_add = BaseAccount{}
another_json = []byte{}
err = cdc.UnmarshalBinaryLengthPrefixed(add_binary[:len(add_binary)/2], &another_json)
require.NotNil(t, err)

//test json marshal
var a BaseAccount
data, e := json.Marshal(a)
require.Nil(t, e)

e = json.Unmarshal(data, &a)
require.Nil(t, e)

a1 := &BaseAccount{
AccountAddress: addr,
}

data, e = json.Marshal(a1)
require.Nil(t, e)

e = json.Unmarshal(data, &a)
require.Nil(t, e)
require.Equal(t, a1.GetAddress(), a.GetAddress())

a1 = &BaseAccount{
AccountAddress: addr,
Publickey: pub,
}

data, e = json.Marshal(a1)
require.Nil(t, e)

e = json.Unmarshal(data, &a)
require.Nil(t, e)
require.Equal(t, a1.GetAddress(), a.GetAddress())
require.Equal(t, a1.GetPublicKey(), a.GetPublicKey())

a1 = &BaseAccount{
AccountAddress: addr,
Publickey: pub,
Nonce: int64(1001),
}

data, e = json.Marshal(a1)
require.Nil(t, e)

e = json.Unmarshal(data, &a)
require.Nil(t, e)
require.Equal(t, a1.GetAddress(), a.GetAddress())
require.Equal(t, a1.GetPublicKey(), a.GetPublicKey())
require.Equal(t, a1.GetNonce(), a.GetNonce())

}

func TestBaseAccount_GetAddress(t *testing.T) {
Expand All @@ -65,7 +113,7 @@ func TestBaseAccount_GetAddress(t *testing.T) {

aa := appAccount{
BaseAccount: BaseAccount{
AccountAddress: make([]byte, 10),
AccountAddress: types.AccAddress{},
Publickey: nil,
Nonce: 10,
},
Expand Down
14 changes: 7 additions & 7 deletions account/accountmapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (mapper *AccountMapper) Copy() mapper.IMapper {
}

// 用指定地址生成账户返回
func (mapper *AccountMapper) NewAccountWithAddress(add types.Address) Account {
func (mapper *AccountMapper) NewAccountWithAddress(add types.AccAddress) Account {
acc := mapper.proto()
err := acc.SetAddress(add)
if err != nil {
Expand All @@ -52,12 +52,12 @@ func (mapper *AccountMapper) NewAccountWithAddress(add types.Address) Account {
}

// 将地址转换成存储通用的key
func AddressStoreKey(addr types.Address) []byte {
func AddressStoreKey(addr types.AccAddress) []byte {
return append([]byte(accountStoreKey), addr.Bytes()...)
}

// 从存储中获得账户
func (mapper *AccountMapper) GetAccount(addr types.Address) (acc Account) {
func (mapper *AccountMapper) GetAccount(addr types.AccAddress) (acc Account) {
mapper.Get(AddressStoreKey(addr), &acc)
return acc
}
Expand Down Expand Up @@ -85,16 +85,16 @@ func (mapper *AccountMapper) IterateAccounts(process func(Account) (stop bool))
}

// 获取地址代表账户的公钥
func (mapper *AccountMapper) GetPubKey(addr types.Address) (crypto.PubKey, types.Error) {
func (mapper *AccountMapper) GetPubKey(addr types.AccAddress) (crypto.PubKey, types.Error) {
acc := mapper.GetAccount(addr)
if acc == nil {
return nil, types.ErrUnknownAddress(addr.String())
}
return acc.GetPubicKey(), nil
return acc.GetPublicKey(), nil
}

// 获取地址代表账户的nonce
func (mapper *AccountMapper) GetNonce(addr types.Address) (int64, types.Error) {
func (mapper *AccountMapper) GetNonce(addr types.AccAddress) (int64, types.Error) {
acc := mapper.GetAccount(addr)
if acc == nil {
return 0, types.ErrUnknownAddress(addr.String())
Expand All @@ -103,7 +103,7 @@ func (mapper *AccountMapper) GetNonce(addr types.Address) (int64, types.Error) {
}

// 为特定账户设置新的nonce值
func (mapper *AccountMapper) SetNonce(addr types.Address, nonce int64) types.Error {
func (mapper *AccountMapper) SetNonce(addr types.AccAddress, nonce int64) types.Error {
acc := mapper.GetAccount(addr)
if acc == nil {
return types.ErrUnknownAddress(addr.String())
Expand Down
6 changes: 3 additions & 3 deletions account/accountmapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/stretchr/testify/require"

abci "github.com/tendermint/tendermint/abci/types"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
)

func defaultContext(key store.StoreKey, mapperMap map[string]mapper.IMapper) context.Context {
Expand Down Expand Up @@ -45,7 +45,7 @@ func TestAccountMapperGetSet(t *testing.T) {

for i := 0; i < 100; i++ {
pubkey := ed25519.GenPrivKey().PubKey()
addr := types.Address(pubkey.Address())
addr := types.AccAddress(pubkey.Address())

// 没有存过该addr,取出来应为nil
acc := mapper.GetAccount(addr)
Expand All @@ -54,7 +54,7 @@ func TestAccountMapperGetSet(t *testing.T) {
acc = mapper.NewAccountWithAddress(addr)
require.NotNil(t, acc)
require.Equal(t, addr, acc.GetAddress())
require.EqualValues(t, nil, acc.GetPubicKey())
require.EqualValues(t, nil, acc.GetPublicKey())
require.EqualValues(t, 0, acc.GetNonce())

// 新的account尚未存储,依然取出nil
Expand Down
Loading

0 comments on commit e96a43d

Please sign in to comment.