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

fix test

parent 7957fb75
......@@ -443,7 +443,7 @@ func (client *client) GetBlockOnMainBySeq(seq int64) (*types.BlockSeq, error) {
hash := blockSeq.Detail.Block.HashByForkHeight(mainBlockHashForkHeight)
if !bytes.Equal(blockSeq.Seq.Hash, hash) {
plog.Error("para compare ForkBlockHash fail", "forkHeight", mainBlockHashForkHeight,
"seqHash", common.Bytes2Hex(blockSeq.Seq.Hash), "calcHash", common.Bytes2Hex(hash))
"seqHash", hex.EncodeToString(blockSeq.Seq.Hash), "calcHash", hex.EncodeToString(hash))
return nil, types.ErrBlockHashNoMatch
}
......@@ -484,8 +484,8 @@ func (client *client) RequestTx(currSeq int64, preMainBlockHash []byte) ([]*type
return txs, blockSeq, nil
}
//not consistent case be processed at below
plog.Error("RequestTx", "preMainHash", common.Bytes2Hex(preMainBlockHash), "currSeq preMainHash", common.Bytes2Hex(blockSeq.Detail.Block.ParentHash),
"currSeq mainHash", common.Bytes2Hex(blockSeq.Seq.Hash), "curr seq", currSeq, "ty", blockSeq.Seq.Type, "currSeq Mainheight", blockSeq.Detail.Block.Height)
plog.Error("RequestTx", "preMainHash", hex.EncodeToString(preMainBlockHash), "currSeq preMainHash", hex.EncodeToString(blockSeq.Detail.Block.ParentHash),
"currSeq mainHash", hex.EncodeToString(blockSeq.Seq.Hash), "curr seq", currSeq, "ty", blockSeq.Seq.Type, "currSeq Mainheight", blockSeq.Detail.Block.Height)
return nil, nil, paracross.ErrParaCurHashNotMatch
}
//lastSeq < CurrSeq case:
......@@ -537,19 +537,19 @@ func (client *client) switchHashMatchedBlock(currSeq int64) (int64, []byte, erro
return -2, nil, err
}
plog.Info("switchHashMatchedBlock", "lastParaBlock height", miner.Height, "mainHeight",
miner.MainBlockHeight, "mainHash", common.Bytes2Hex(miner.MainBlockHash))
miner.MainBlockHeight, "mainHash", hex.EncodeToString(miner.MainBlockHash))
mainSeq, err := client.GetSeqByHashOnMainChain(miner.MainBlockHash)
if err != nil {
depth--
if depth == 0 {
plog.Error("switchHashMatchedBlock depth overflow", "last info:mainHeight", miner.MainBlockHeight,
"mainHash", common.Bytes2Hex(miner.MainBlockHash), "search startHeight", lastBlock.Height, "curHeight", miner.Height,
"mainHash", hex.EncodeToString(miner.MainBlockHash), "search startHeight", lastBlock.Height, "curHeight", miner.Height,
"search depth", searchHashMatchDepth)
panic("search HashMatchedBlock overflow, re-setting search depth and restart to try")
}
if height == 1 {
plog.Error("switchHashMatchedBlock search to height=1 not found", "lastBlockHeight", lastBlock.Height,
"height1 mainHash", common.Bytes2Hex(miner.MainBlockHash))
"height1 mainHash", hex.EncodeToString(miner.MainBlockHash))
err = client.removeBlocks(0)
if err != nil {
return currSeq, nil, nil
......@@ -567,7 +567,7 @@ func (client *client) switchHashMatchedBlock(currSeq int64) (int64, []byte, erro
}
plog.Info("switchHashMatchedBlock succ", "currHeight", height, "initHeight", lastBlock.Height,
"new currSeq", mainSeq+1, "new preMainBlockHash", common.Bytes2Hex(miner.MainBlockHash))
"new currSeq", mainSeq+1, "new preMainBlockHash", hex.EncodeToString(miner.MainBlockHash))
return mainSeq + 1, miner.MainBlockHash, nil
}
return -2, nil, paracross.ErrParaCurHashNotMatch
......
......@@ -106,7 +106,7 @@ func ExecAddress(execName string) Address {
func BytesToAddress(b []byte) Address {
a := new(address.Address)
a.Version = 0
a.Hash160 = copyBytes(LeftPadBytes(b, 20))
a.SetBytes(copyBytes(LeftPadBytes(b, 20)))
return Address{addr: a}
}
......@@ -127,12 +127,14 @@ func StringToAddress(s string) *Address {
return &Address{addr: addr}
}
func copyBytes(data []byte) (out [20]byte) {
func copyBytes(data []byte) (out []byte) {
out = make([]byte, 20)
copy(out[:], data)
return
}
func bigBytes(b *big.Int) (out [20]byte) {
func bigBytes(b *big.Int) (out []byte) {
out = make([]byte, 20)
copy(out[:], b.Bytes())
return
}
......@@ -141,7 +143,7 @@ func bigBytes(b *big.Int) (out [20]byte) {
func BigToAddress(b *big.Int) Address {
a := new(address.Address)
a.Version = 0
a.Hash160 = bigBytes(b)
a.SetBytes(bigBytes(b))
return Address{addr: a}
}
......
......@@ -5,8 +5,9 @@
package common
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAddressBig(t *testing.T) {
......@@ -21,6 +22,5 @@ func TestAddressBig(t *testing.T) {
func TestAddressBytes(t *testing.T) {
addr := BytesToAddress([]byte{1})
fmt.Println(addr.String())
assert.Equal(t, addr.String(), "11111111111111111111BZbvjr")
}
......@@ -44,7 +44,7 @@ func (h *Hash) SetBytes(b []byte) {
// BigToHash 大数字转换为哈希
func BigToHash(b *big.Int) Hash {
return Hash(common.BigToHash(b))
return Hash(common.BytesToHash(b.Bytes()))
}
// BytesToHash 将[]byte直接当做哈希处理
......
......@@ -6,10 +6,10 @@ package executor
import (
"bytes"
"encoding/hex"
"github.com/33cn/chain33/account"
"github.com/33cn/chain33/client"
"github.com/33cn/chain33/common"
dbm "github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/types"
......@@ -230,8 +230,8 @@ func (a *action) Commit(commit *pt.ParacrossCommitAction) (*types.Receipt, error
if titleStatus.Height+1 == commit.Status.Height && commit.Status.Height > 0 {
if !bytes.Equal(titleStatus.BlockHash, commit.Status.PreBlockHash) {
clog.Error("paracross.Commit", "check PreBlockHash", common.Bytes2Hex(titleStatus.BlockHash),
"commit tx", common.Bytes2Hex(commit.Status.PreBlockHash), "commitheit", commit.Status.Height,
clog.Error("paracross.Commit", "check PreBlockHash", hex.EncodeToString(titleStatus.BlockHash),
"commit tx", hex.EncodeToString(commit.Status.PreBlockHash), "commitheit", commit.Status.Height,
"from", a.fromaddr)
return nil, pt.ErrParaBlockHashNoMatch
}
......@@ -248,8 +248,8 @@ func (a *action) Commit(commit *pt.ParacrossCommitAction) (*types.Receipt, error
return nil, err
}
if !bytes.Equal(blockHash.Hash, commit.Status.MainBlockHash) && commit.Status.Height > 0 {
clog.Error("paracross.Commit blockHash not match", "db", common.Bytes2Hex(blockHash.Hash),
"commit tx", common.Bytes2Hex(commit.Status.MainBlockHash), "commitHeight", commit.Status.Height,
clog.Error("paracross.Commit blockHash not match", "db", hex.EncodeToString(blockHash.Hash),
"commit tx", hex.EncodeToString(commit.Status.MainBlockHash), "commitHeight", commit.Status.Height,
"from", a.fromaddr)
return nil, types.ErrBlockHashNoMatch
}
......@@ -342,7 +342,7 @@ func (a *action) execCrossTx(tx *types.TransactionDetail, commit *pt.ParacrossCo
if err != nil {
clog.Crit("paracross.Commit Decode Tx failed", "para title", commit.Status.Title,
"para height", commit.Status.Height, "para tx index", i, "error", err, "txHash",
common.Bytes2Hex(commit.Status.CrossTxHashs[i]))
hex.EncodeToString(commit.Status.CrossTxHashs[i]))
return nil, err
}
......@@ -351,13 +351,13 @@ func (a *action) execCrossTx(tx *types.TransactionDetail, commit *pt.ParacrossCo
if err != nil {
clog.Crit("paracross.Commit Decode Tx failed", "para title", commit.Status.Title,
"para height", commit.Status.Height, "para tx index", i, "error", err, "txHash",
common.Bytes2Hex(commit.Status.CrossTxHashs[i]))
hex.EncodeToString(commit.Status.CrossTxHashs[i]))
return nil, errors.Cause(err)
}
clog.Info("paracross.Commit WithdrawCoins", "para title", commit.Status.Title,
"para height", commit.Status.Height, "para tx index", i, "error", err, "txHash",
common.Bytes2Hex(commit.Status.CrossTxHashs[i]))
hex.EncodeToString(commit.Status.CrossTxHashs[i]))
return receiptWithdraw, nil
} //else if tx.ActionName == pt.ParacrossActionAssetTransferStr {
return nil, nil
......@@ -368,20 +368,20 @@ func (a *action) execCrossTxs(commit *pt.ParacrossCommitAction) (*types.Receipt,
var receipt types.Receipt
for i := 0; i < len(commit.Status.CrossTxHashs); i++ {
clog.Info("paracross.Commit commitDone", "do cross number", i, "hash",
common.Bytes2Hex(commit.Status.CrossTxHashs[i]),
hex.EncodeToString(commit.Status.CrossTxHashs[i]),
"res", util.BitMapBit(commit.Status.CrossTxResult, uint32(i)))
if util.BitMapBit(commit.Status.CrossTxResult, uint32(i)) {
tx, err := GetTx(a.api, commit.Status.CrossTxHashs[i])
if err != nil {
clog.Crit("paracross.Commit Load Tx failed", "para title", commit.Status.Title,
"para height", commit.Status.Height, "para tx index", i, "error", err, "txHash",
common.Bytes2Hex(commit.Status.CrossTxHashs[i]))
hex.EncodeToString(commit.Status.CrossTxHashs[i]))
return nil, err
}
if tx == nil {
clog.Error("paracross.Commit Load Tx failed", "para title", commit.Status.Title,
"para height", commit.Status.Height, "para tx index", i, "error", err, "txHash",
common.Bytes2Hex(commit.Status.CrossTxHashs[i]))
hex.EncodeToString(commit.Status.CrossTxHashs[i]))
return nil, types.ErrHashNotExist
}
receiptCross, err := a.execCrossTx(tx, commit, i)
......@@ -395,7 +395,7 @@ func (a *action) execCrossTxs(commit *pt.ParacrossCommitAction) (*types.Receipt,
receipt.Logs = append(receipt.Logs, receiptCross.Logs...)
} else {
clog.Error("paracross.Commit commitDone", "do cross number", i, "hash",
common.Bytes2Hex(commit.Status.CrossTxHashs[i]),
hex.EncodeToString(commit.Status.CrossTxHashs[i]),
"para res", util.BitMapBit(commit.Status.CrossTxResult, uint32(i)))
}
}
......@@ -424,7 +424,7 @@ func (a *action) AssetWithdraw(withdraw *types.AssetsWithdraw) (*types.Receipt,
return nil, nil
}
clog.Debug("paracross.AssetWithdraw isPara", "execer", string(a.tx.Execer),
"txHash", common.Bytes2Hex(a.tx.Hash()))
"txHash", hex.EncodeToString(a.tx.Hash()))
receipt, err := a.assetWithdraw(withdraw, a.tx)
if err != nil {
clog.Error("AssetWithdraw failed", "err", err)
......@@ -469,7 +469,7 @@ func (a *Paracross) CrossLimits(tx *types.Transaction, index int) bool {
txs, err := a.GetTxGroup(index)
if err != nil {
clog.Error("crossLimits", "get tx group failed", err, "hash", common.Bytes2Hex(tx.Hash()))
clog.Error("crossLimits", "get tx group failed", err, "hash", hex.EncodeToString(tx.Hash()))
return false
}
......
......@@ -5,8 +5,9 @@
package executor
import (
"encoding/hex"
"github.com/33cn/chain33/account"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/address"
"github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/types"
......@@ -28,7 +29,7 @@ func (a *action) assetTransfer(transfer *types.AssetsTransfer) (*types.Receipt,
}
toAddr := address.ExecAddress(string(a.tx.Execer))
clog.Debug("paracross.AssetTransfer not isPara", "execer", string(a.tx.Execer),
"txHash", common.Bytes2Hex(a.tx.Hash()))
"txHash", hex.EncodeToString(a.tx.Hash()))
return accDB.ExecTransfer(a.fromaddr, toAddr, execAddr, transfer.Amount)
}
......@@ -46,7 +47,7 @@ func (a *action) assetTransfer(transfer *types.AssetsTransfer) (*types.Receipt,
return nil, errors.Wrap(err, "assetTransferCoins call NewParaAccount failed")
}
clog.Debug("paracross.AssetTransfer isPara", "execer", string(a.tx.Execer),
"txHash", common.Bytes2Hex(a.tx.Hash()))
"txHash", hex.EncodeToString(a.tx.Hash()))
return assetDepositBalance(paraAcc, transfer.To, transfer.Amount)
}
......@@ -78,7 +79,7 @@ func (a *action) assetWithdraw(withdraw *types.AssetsWithdraw, withdrawTx *types
return nil, errors.Wrap(err, "assetWithdrawCoins call NewParaAccount failed")
}
clog.Debug("paracross.assetWithdrawCoins isPara", "execer", string(a.tx.Execer),
"txHash", common.Bytes2Hex(a.tx.Hash()))
"txHash", hex.EncodeToString(a.tx.Hash()))
return assetWithdrawBalance(paraAcc, a.fromaddr, withdraw.Amount)
}
......
......@@ -5,8 +5,9 @@
package executor
import (
"encoding/hex"
"github.com/33cn/chain33/client"
"github.com/33cn/chain33/common"
dbm "github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/types"
pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
......@@ -58,7 +59,7 @@ func GetBlock(api client.QueueProtocolAPI, blockHash []byte) (*types.BlockDetail
blockDetails, err := api.GetBlockByHashes(&types.ReqHashes{Hashes: [][]byte{blockHash}})
if err != nil {
clog.Error("paracross.Commit getBlockHeader", "db", err,
"commit tx hash", common.Bytes2Hex(blockHash))
"commit tx hash", hex.EncodeToString(blockHash))
return nil, err
}
if len(blockDetails.Items) != 1 {
......@@ -66,7 +67,7 @@ func GetBlock(api client.QueueProtocolAPI, blockHash []byte) (*types.BlockDetail
return nil, pt.ErrParaBlockHashNoMatch
}
if blockDetails.Items[0] == nil {
clog.Error("paracross.Commit getBlockHeader", "commit tx hash net found", common.Bytes2Hex(blockHash))
clog.Error("paracross.Commit getBlockHeader", "commit tx hash net found", hex.EncodeToString(blockHash))
return nil, pt.ErrParaBlockHashNoMatch
}
return blockDetails.Items[0], nil
......@@ -94,7 +95,7 @@ func GetTx(api client.QueueProtocolAPI, txHash []byte) (*types.TransactionDetail
txs, err := api.GetTransactionByHash(&types.ReqHashes{Hashes: [][]byte{txHash}})
if err != nil {
clog.Error("paracross.Commit GetTx", "db", err,
"commit tx hash", common.Bytes2Hex(txHash))
"commit tx hash", hex.EncodeToString(txHash))
return nil, err
}
if len(txs.Txs) != 1 {
......@@ -102,7 +103,7 @@ func GetTx(api client.QueueProtocolAPI, txHash []byte) (*types.TransactionDetail
return nil, pt.ErrParaBlockHashNoMatch
}
if txs.Txs == nil {
clog.Error("paracross.Commit GetTx", "commit tx hash net found", common.Bytes2Hex(txHash))
clog.Error("paracross.Commit GetTx", "commit tx hash net found", hex.EncodeToString(txHash))
return nil, pt.ErrParaBlockHashNoMatch
}
return txs.Txs[0], nil
......
......@@ -5,7 +5,8 @@
package executor
import (
"github.com/33cn/chain33/common"
"encoding/hex"
"github.com/33cn/chain33/types"
pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
"github.com/pkg/errors"
......@@ -16,7 +17,7 @@ func (e *Paracross) Exec_Commit(payload *pt.ParacrossCommitAction, tx *types.Tra
a := newAction(e, tx)
receipt, err := a.Commit(payload)
if err != nil {
clog.Error("Paracross commit failed", "error", err, "hash", common.Bytes2Hex(tx.Hash()))
clog.Error("Paracross commit failed", "error", err, "hash", hex.EncodeToString(tx.Hash()))
return nil, errors.Cause(err)
}
return receipt, nil
......@@ -27,13 +28,13 @@ func (e *Paracross) Exec_AssetTransfer(payload *types.AssetsTransfer, tx *types.
clog.Debug("Paracross.Exec", "transfer", "")
_, err := e.checkTxGroup(tx, index)
if err != nil {
clog.Error("ParacrossActionAssetTransfer", "get tx group failed", err, "hash", common.Bytes2Hex(tx.Hash()))
clog.Error("ParacrossActionAssetTransfer", "get tx group failed", err, "hash", hex.EncodeToString(tx.Hash()))
return nil, err
}
a := newAction(e, tx)
receipt, err := a.AssetTransfer(payload)
if err != nil {
clog.Error("Paracross AssetTransfer failed", "error", err, "hash", common.Bytes2Hex(tx.Hash()))
clog.Error("Paracross AssetTransfer failed", "error", err, "hash", hex.EncodeToString(tx.Hash()))
return nil, errors.Cause(err)
}
return receipt, nil
......@@ -44,13 +45,13 @@ func (e *Paracross) Exec_AssetWithdraw(payload *types.AssetsWithdraw, tx *types.
clog.Debug("Paracross.Exec", "withdraw", "")
_, err := e.checkTxGroup(tx, index)
if err != nil {
clog.Error("ParacrossActionAssetWithdraw", "get tx group failed", err, "hash", common.Bytes2Hex(tx.Hash()))
clog.Error("ParacrossActionAssetWithdraw", "get tx group failed", err, "hash", hex.EncodeToString(tx.Hash()))
return nil, err
}
a := newAction(e, tx)
receipt, err := a.AssetWithdraw(payload)
if err != nil {
clog.Error("ParacrossActionAssetWithdraw failed", "error", err, "hash", common.Bytes2Hex(tx.Hash()))
clog.Error("ParacrossActionAssetWithdraw failed", "error", err, "hash", hex.EncodeToString(tx.Hash()))
return nil, errors.Cause(err)
}
return receipt, nil
......
......@@ -6,8 +6,8 @@ package executor
import (
"bytes"
"encoding/hex"
"github.com/33cn/chain33/common"
log "github.com/33cn/chain33/common/log/log15"
drivers "github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/types"
......@@ -59,7 +59,7 @@ func (c *Paracross) checkTxGroup(tx *types.Transaction, index int) ([]*types.Tra
if tx.GroupCount >= 2 {
txs, err := c.GetTxGroup(index)
if err != nil {
clog.Error("ParacrossActionAssetTransfer", "get tx group failed", err, "hash", common.Bytes2Hex(tx.Hash()))
clog.Error("ParacrossActionAssetTransfer", "get tx group failed", err, "hash", hex.EncodeToString(tx.Hash()))
return nil, err
}
return txs, nil
......@@ -117,7 +117,7 @@ func (c *Paracross) saveLocalParaTxs(tx *types.Transaction, isDel bool) (*types.
if err != nil {
clog.Crit("paracross.Commit Load Tx failed", "para title", commit.Status.Title,
"para height", commit.Status.Height, "para tx index", i, "error", err, "txHash",
common.Bytes2Hex(commit.Status.CrossTxHashs[i]))
hex.EncodeToString(commit.Status.CrossTxHashs[i]))
return nil, err
}
......@@ -126,7 +126,7 @@ func (c *Paracross) saveLocalParaTxs(tx *types.Transaction, isDel bool) (*types.
if err != nil {
clog.Crit("paracross.Commit Decode Tx failed", "para title", commit.Status.Title,
"para height", commit.Status.Height, "para tx index", i, "error", err, "txHash",
common.Bytes2Hex(commit.Status.CrossTxHashs[i]))
hex.EncodeToString(commit.Status.CrossTxHashs[i]))
return nil, err
}
if payload.Ty == pt.ParacrossActionAssetTransfer {
......@@ -161,7 +161,7 @@ func getCommitHeight(payload []byte) (int64, error) {
}
func (c *Paracross) initLocalAssetTransfer(tx *types.Transaction, success, isDel bool) (*types.KeyValue, error) {
clog.Debug("para execLocal", "tx hash", common.Bytes2Hex(tx.Hash()), "action name", log.Lazy{Fn: tx.ActionName})
clog.Debug("para execLocal", "tx hash", hex.EncodeToString(tx.Hash()), "action name", log.Lazy{Fn: tx.ActionName})
key := calcLocalAssetKey(tx.Hash())
if isDel {
c.GetLocalDB().Set(key, nil)
......@@ -202,7 +202,7 @@ func (c *Paracross) initLocalAssetTransfer(tx *types.Transaction, success, isDel
err = c.GetLocalDB().Set(key, types.Encode(&asset))
if err != nil {
clog.Error("para execLocal", "set", common.Bytes2Hex(tx.Hash()), "failed", err)
clog.Error("para execLocal", "set", hex.EncodeToString(tx.Hash()), "failed", err)
}
return &types.KeyValue{Key: key, Value: types.Encode(&asset)}, nil
}
......@@ -262,7 +262,7 @@ func (c *Paracross) initLocalAssetWithdraw(txCommit, tx *types.Transaction, isWi
}
func (c *Paracross) updateLocalAssetTransfer(txCommit, tx *types.Transaction, success, isDel bool) (*types.KeyValue, error) {
clog.Debug("para execLocal", "tx hash", common.Bytes2Hex(tx.Hash()))
clog.Debug("para execLocal", "tx hash", hex.EncodeToString(tx.Hash()))
key := calcLocalAssetKey(tx.Hash())
var asset pt.ParacrossAsset
......
......@@ -5,6 +5,7 @@
package commands
import (
"encoding/hex"
"fmt"
"strconv"
"time"
......@@ -425,7 +426,7 @@ func parseShowUTXOs4SpecifiedAmountRes(arg interface{}) (interface{}, error) {
result := &PrivacyAccountResult{
Txhash: common.ToHex(item.Txhash),
OutIndex: item.Outindex,
OnetimePubKey: common.Bytes2Hex(item.Onetimepubkey),
OnetimePubKey: hex.EncodeToString(item.Onetimepubkey),
}
ret = append(ret, result)
}
......
......@@ -6,19 +6,17 @@ package privacy
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"io"
"unsafe"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/common/crypto/sha3"
"github.com/33cn/chain33/common/ed25519/edwards25519"
log "github.com/33cn/chain33/common/log/log15"
sccrypto "github.com/NebulousLabs/Sia/crypto"
//"github.com/33cn/chain33/types"
"fmt"
)
const (
......@@ -72,7 +70,7 @@ func NewPrivacy() *Privacy {
// NewPrivacyWithPrivKey create privacy from private key
func NewPrivacyWithPrivKey(privKey *[KeyLen32]byte) (privacy *Privacy, err error) {
privacylog.Info("NewPrivacyWithPrivKey", "input prikey", common.Bytes2Hex(privKey[:]))
privacylog.Info("NewPrivacyWithPrivKey", "input prikey", hex.EncodeToString(privKey[:]))
hash := sccrypto.HashAll(*privKey)
privacy = &Privacy{}
......@@ -84,8 +82,8 @@ func NewPrivacyWithPrivKey(privKey *[KeyLen32]byte) (privacy *Privacy, err error
if err = generateKeyPairWithPrivKey((*[KeyLen32]byte)(unsafe.Pointer(&hashViewPriv[0])), &privacy.ViewPrivKey, &privacy.ViewPubkey); err != nil {
return nil, err
}
privacylog.Info("NewPrivacyWithPrivKey", "the new privacy created with viewpub", common.Bytes2Hex(privacy.ViewPubkey[:]))
privacylog.Info("NewPrivacyWithPrivKey", "the new privacy created with spendpub", common.Bytes2Hex(privacy.SpendPubkey[:]))
privacylog.Info("NewPrivacyWithPrivKey", "the new privacy created with viewpub", hex.EncodeToString(privacy.ViewPubkey[:]))
privacylog.Info("NewPrivacyWithPrivKey", "the new privacy created with spendpub", hex.EncodeToString(privacy.SpendPubkey[:]))
return privacy, nil
}
......
......@@ -5,6 +5,8 @@
package executor
import (
"encoding/hex"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/address"
"github.com/33cn/chain33/types"
......@@ -16,7 +18,7 @@ func (p *privacy) Exec_Public2Privacy(payload *ty.Public2Privacy, tx *types.Tran
if payload.Tokenname != types.BTY {
return nil, types.ErrNotSupport
}
txhashstr := common.Bytes2Hex(tx.Hash())
txhashstr := hex.EncodeToString(tx.Hash())
coinsAccount := p.GetCoinsAccount()
from := tx.From()
receipt, err := coinsAccount.ExecWithdraw(address.ExecAddress(string(tx.Execer)), from, payload.Amount)
......@@ -56,7 +58,7 @@ func (p *privacy) Exec_Privacy2Privacy(payload *ty.Privacy2Privacy, tx *types.Tr
if payload.Tokenname != types.BTY {
return nil, types.ErrNotSupport
}
txhashstr := common.Bytes2Hex(tx.Hash())
txhashstr := hex.EncodeToString(tx.Hash())
receipt := &types.Receipt{KV: make([]*types.KeyValue, 0)}
privacyInput := payload.Input
for _, keyInput := range privacyInput.Keyinput {
......@@ -98,7 +100,7 @@ func (p *privacy) Exec_Privacy2Public(payload *ty.Privacy2Public, tx *types.Tran
if payload.Tokenname != types.BTY {
return nil, types.ErrNotSupport
}
txhashstr := common.Bytes2Hex(tx.Hash())
txhashstr := hex.EncodeToString(tx.Hash())
coinsAccount := p.GetCoinsAccount()
receipt, err := coinsAccount.ExecDeposit(tx.To, address.ExecAddress(string(tx.Execer)), payload.Amount)
if err != nil {
......
......@@ -5,13 +5,15 @@
package executor
import (
"encoding/hex"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/types"
ty "github.com/33cn/plugin/plugin/dapp/privacy/types"
)
func (p *privacy) execDelLocal(tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
txhashstr := common.Bytes2Hex(tx.Hash())
txhashstr := hex.EncodeToString(tx.Hash())
dbSet := &types.LocalDBSet{}
localDB := p.GetLocalDB()
for i, item := range receiptData.Logs {
......
......@@ -5,6 +5,8 @@
package executor
import (
"encoding/hex"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/types"
ty "github.com/33cn/plugin/plugin/dapp/privacy/types"
......@@ -12,7 +14,7 @@ import (
func (p *privacy) execLocal(receiptData *types.ReceiptData, tx *types.Transaction, index int) (*types.LocalDBSet, error) {
dbSet := &types.LocalDBSet{}
txhashstr := common.Bytes2Hex(tx.Hash())
txhashstr := hex.EncodeToString(tx.Hash())
localDB := p.GetLocalDB()
for _, item := range receiptData.Logs {
if item.Ty != ty.TyLogPrivacyOutput {
......
......@@ -21,6 +21,7 @@ privacy执行器支持隐私交易的执行,
import (
"bytes"
"encoding/hex"
"math/rand"
"sort"
"time"
......@@ -198,7 +199,7 @@ func (p *privacy) ShowUTXOs4SpecifiedAmount(reqtoken *pty.ReqPrivacyToken) (type
// CheckTx check transaction
func (p *privacy) CheckTx(tx *types.Transaction, index int) error {
txhashstr := common.Bytes2Hex(tx.Hash())
txhashstr := hex.EncodeToString(tx.Hash())
var action pty.PrivacyAction
err := types.Decode(tx.Payload, &action)
if err != nil {
......
......@@ -6,6 +6,7 @@ package wallet
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"sort"
......@@ -599,7 +600,6 @@ func (policy *privacyPolicy) createPublic2PrivacyTx(req *types.ReqCreateTransact
Ty: privacytypes.ActionPublic2Privacy,
Value: &privacytypes.PrivacyAction_Public2Privacy{Public2Privacy: value},
}
tx := &types.Transaction{
Execer: []byte(privacytypes.PrivacyX),
Payload: types.Encode(action),
......@@ -678,7 +678,7 @@ func (policy *privacyPolicy) createPrivacy2PrivacyTx(req *types.ReqCreateTransac
To: address.ExecAddress(privacytypes.PrivacyX),
}
// 创建交易成功,将已经使用掉的UTXO冻结
policy.saveFTXOInfo(tx, req.GetTokenname(), req.GetFrom(), common.Bytes2Hex(tx.Hash()), selectedUtxo)
policy.saveFTXOInfo(tx, req.GetTokenname(), req.GetFrom(), hex.EncodeToString(tx.Hash()), selectedUtxo)
tx.Signature = &types.Signature{
Signature: types.Encode(&privacytypes.PrivacySignatureParam{
ActionType: action.Ty,
......@@ -747,7 +747,7 @@ func (policy *privacyPolicy) createPrivacy2PublicTx(req *types.ReqCreateTransact
To: req.GetTo(),
}
// 创建交易成功,将已经使用掉的UTXO冻结
policy.saveFTXOInfo(tx, req.GetTokenname(), req.GetFrom(), common.Bytes2Hex(tx.Hash()), selectedUtxo)
policy.saveFTXOInfo(tx, req.GetTokenname(), req.GetFrom(), hex.EncodeToString(tx.Hash()), selectedUtxo)
tx.Signature = &types.Signature{
Signature: types.Encode(&privacytypes.PrivacySignatureParam{
ActionType: action.Ty,
......@@ -1137,7 +1137,7 @@ func (policy *privacyPolicy) transPri2PriV2(privacykeyParirs *privacy.Privacy, r
bizlog.Error("transPub2Pri", "SendTx ", err)
return nil, err
}
policy.saveFTXOInfo(tx, reqPri2Pri.Tokenname, reqPri2Pri.Sender, common.Bytes2Hex(tx.Hash()), selectedUtxo)
policy.saveFTXOInfo(tx, reqPri2Pri.Tokenname, reqPri2Pri.Sender, hex.EncodeToString(tx.Hash()), selectedUtxo)
return reply, nil
}
......@@ -1263,7 +1263,7 @@ func (policy *privacyPolicy) transPri2PubV2(privacykeyParirs *privacy.Privacy, r
bizlog.Error("transPri2PubV2", "SendTx error", err)
return nil, err
}
txhashstr := common.Bytes2Hex(tx.Hash())
txhashstr := hex.EncodeToString(tx.Hash())
policy.saveFTXOInfo(tx, reqPri2Pub.Tokenname, reqPri2Pub.Sender, txhashstr, selectedUtxo)
bizlog.Info("transPri2PubV2", "txhash", txhashstr)
return reply, nil
......@@ -1352,7 +1352,7 @@ func (policy *privacyPolicy) checkWalletStoreData() {
func (policy *privacyPolicy) addDelPrivacyTxsFromBlock(tx *types.Transaction, index int32, block *types.BlockDetail, newbatch db.Batch, addDelType int32) {
txhash := tx.Hash()
txhashstr := common.Bytes2Hex(txhash)
txhashstr := hex.EncodeToString(txhash)
_, err := tx.Amount()
if err != nil {
bizlog.Error("addDelPrivacyTxsFromBlock", "txhash", txhashstr, "addDelType", addDelType, "index", index, "tx.Amount() error", err)
......
......@@ -6,11 +6,11 @@ package wallet
import (
"bytes"
"encoding/hex"
"sync"
"time"
"unsafe"
//_ "github.com/33cn/plugin/plugin"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/address"
"github.com/33cn/chain33/common/crypto"
......@@ -131,7 +131,7 @@ func (mock *PrivacyMock) CreateUTXOs(sender string, pubkeypair string, amount in
}
txhash := tx.Hash()
txhashstr := common.Bytes2Hex(txhash)
txhashstr := hex.EncodeToString(txhash)
var privateAction ty.PrivacyAction
if err := types.Decode(tx.GetPayload(), &privateAction); err != nil {
return
......
......@@ -6,9 +6,9 @@ package wallet
import (
"bytes"
"encoding/hex"
"encoding/json"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/types"
wcom "github.com/33cn/chain33/wallet/common"
......@@ -343,7 +343,7 @@ func (store *privacyStore) moveUTXO2FTXO(tx *types.Transaction, token, sender, t
FTXOsInOneTx := &privacytypes.FTXOsSTXOsInOneTx{}
newbatch := store.NewBatch(true)
for _, txOutputInfo := range selectedUtxos {
key := calcUTXOKey4TokenAddr(token, sender, common.Bytes2Hex(txOutputInfo.utxoGlobalIndex.Txhash), int(txOutputInfo.utxoGlobalIndex.Outindex))
key := calcUTXOKey4TokenAddr(token, sender, hex.EncodeToString(txOutputInfo.utxoGlobalIndex.Txhash), int(txOutputInfo.utxoGlobalIndex.Outindex))
newbatch.Delete(key)
utxo := &privacytypes.UTXO{
Amount: txOutputInfo.amount,
......@@ -470,7 +470,7 @@ func (store *privacyStore) updateScanInputUTXOs(utxoGlobalIndexs []*privacytypes
var token string
var txhash string
for _, utxoGlobal := range utxoGlobalIndexs {
accPrivacy, err := store.isUTXOExist(common.Bytes2Hex(utxoGlobal.Txhash), int(utxoGlobal.Outindex))
accPrivacy, err := store.isUTXOExist(hex.EncodeToString(utxoGlobal.Txhash), int(utxoGlobal.Outindex))
if err == nil && accPrivacy != nil {
utxo := &privacytypes.UTXO{
Amount: accPrivacy.Amount,
......@@ -482,9 +482,9 @@ func (store *privacyStore) updateScanInputUTXOs(utxoGlobalIndexs []*privacytypes
utxos = append(utxos, utxo)
owner = accPrivacy.Owner
token = accPrivacy.Tokenname
txhash = common.Bytes2Hex(accPrivacy.Txhash)
txhash = hex.EncodeToString(accPrivacy.Txhash)
}
key := calcScanPrivacyInputUTXOKey(common.Bytes2Hex(utxoGlobal.Txhash), int(utxoGlobal.Outindex))
key := calcScanPrivacyInputUTXOKey(hex.EncodeToString(utxoGlobal.Txhash), int(utxoGlobal.Outindex))
newbatch.Delete(key)
}
if len(utxos) > 0 {
......@@ -509,7 +509,7 @@ func (store *privacyStore) moveUTXO2STXO(owner, token, txhash string, utxos []*p
Txhash := utxo.UtxoBasic.UtxoGlobalIndex.Txhash
Outindex := utxo.UtxoBasic.UtxoGlobalIndex.Outindex
//删除存在的UTXO索引
key := calcUTXOKey4TokenAddr(token, owner, common.Bytes2Hex(Txhash), int(Outindex))
key := calcUTXOKey4TokenAddr(token, owner, hex.EncodeToString(Txhash), int(Outindex))
newbatch.Delete(key)
}
......@@ -554,7 +554,7 @@ func (store *privacyStore) selectCurrentWalletPrivacyTx(txDetal *types.Transacti
height := txDetal.Height
txhashInbytes := tx.Hash()
txhash := common.Bytes2Hex(txhashInbytes)
txhash := hex.EncodeToString(txhashInbytes)
var privateAction privacytypes.PrivacyAction
if err := types.Decode(tx.GetPayload(), &privateAction); err != nil {
bizlog.Error("selectCurrentWalletPrivacyTx failed to decode payload")
......@@ -587,8 +587,8 @@ func (store *privacyStore) selectCurrentWalletPrivacyTx(txDetal *types.Transacti
for _, info := range privacyInfo {
bizlog.Debug("SelectCurrentWalletPrivacyTx", "individual privacyInfo's addr", *info.Addr)
privacykeyParirs := info.PrivacyKeyPair
bizlog.Debug("SelectCurrentWalletPrivacyTx", "individual ViewPubkey", common.Bytes2Hex(privacykeyParirs.ViewPubkey.Bytes()),
"individual SpendPubkey", common.Bytes2Hex(privacykeyParirs.SpendPubkey.Bytes()))
bizlog.Debug("SelectCurrentWalletPrivacyTx", "individual ViewPubkey", hex.EncodeToString(privacykeyParirs.ViewPubkey.Bytes()),
"individual SpendPubkey", hex.EncodeToString(privacykeyParirs.SpendPubkey.Bytes()))
var utxos []*privacytypes.UTXO
for indexoutput, output := range privacyOutput.Keyoutput {
......@@ -610,7 +610,7 @@ func (store *privacyStore) selectCurrentWalletPrivacyTx(txDetal *types.Transacti
if types.ExecOk == txExecRes {
// 先判断该UTXO的hash是否存在,不存在则写入
accPrivacy, err := store.isUTXOExist(common.Bytes2Hex(txhashInbytes), indexoutput)
accPrivacy, err := store.isUTXOExist(hex.EncodeToString(txhashInbytes), indexoutput)
if err == nil && accPrivacy != nil {
continue
}
......@@ -696,7 +696,7 @@ func (store *privacyStore) setUTXO(addr, txhash *string, outindex int, dbStore *
func (store *privacyStore) storeScanPrivacyInputUTXO(utxoGlobalIndexs []*privacytypes.UTXOGlobalIndex, newbatch db.Batch) {
for _, utxoGlobalIndex := range utxoGlobalIndexs {
key1 := calcScanPrivacyInputUTXOKey(common.Bytes2Hex(utxoGlobalIndex.Txhash), int(utxoGlobalIndex.Outindex))
key1 := calcScanPrivacyInputUTXOKey(hex.EncodeToString(utxoGlobalIndex.Txhash), int(utxoGlobalIndex.Outindex))
utxoIndex := &privacytypes.UTXOGlobalIndex{
Txhash: utxoGlobalIndex.Txhash,
Outindex: utxoGlobalIndex.Outindex,
......@@ -734,7 +734,7 @@ func (store *privacyStore) listSpendUTXOs(token, addr string) (*privacytypes.UTX
}
for _, ftxo := range ftxosInOneTx.Utxos {
utxohash := common.Bytes2Hex(ftxo.UtxoBasic.UtxoGlobalIndex.Txhash)
utxohash := hex.EncodeToString(ftxo.UtxoBasic.UtxoGlobalIndex.Txhash)
value1, err := store.Get(calcUTXOKey(utxohash, int(ftxo.UtxoBasic.UtxoGlobalIndex.Outindex)))
if err != nil {
continue
......@@ -879,7 +879,7 @@ func (store *privacyStore) moveFTXO2UTXO(key1 []byte, newbatch db.Batch) {
return
}
for _, ftxo := range ftxosInOneTx.Utxos {
utxohash := common.Bytes2Hex(ftxo.UtxoBasic.UtxoGlobalIndex.Txhash)
utxohash := hex.EncodeToString(ftxo.UtxoBasic.UtxoGlobalIndex.Txhash)
outindex := int(ftxo.UtxoBasic.UtxoGlobalIndex.Outindex)
key := calcUTXOKey4TokenAddr(ftxosInOneTx.Tokenname, ftxosInOneTx.Sender, utxohash, outindex)
value := calcUTXOKey(utxohash, int(ftxo.UtxoBasic.UtxoGlobalIndex.Outindex))
......
......@@ -5,6 +5,7 @@
package wallet
import (
"encoding/hex"
"unsafe"
"github.com/33cn/chain33/common"
......@@ -28,7 +29,7 @@ func checkAmountValid(amount int64) bool {
func makeViewSpendPubKeyPairToString(viewPubKey, spendPubKey []byte) string {
pair := viewPubKey
pair = append(pair, spendPubKey...)
return common.Bytes2Hex(pair)
return hex.EncodeToString(pair)
}
//将amount切分为1,2,5的组合,这样在进行amount混淆的时候就能够方便获取相同额度的utxo
......
......@@ -6,21 +6,19 @@ package executor
import (
"bytes"
"math/big"
"strings"
"time"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/golang/protobuf/proto"
"math/big"
"github.com/33cn/chain33/common"
dbm "github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/common/difficulty"
"github.com/33cn/chain33/common/merkle"
"github.com/33cn/chain33/types"
ty "github.com/33cn/plugin/plugin/dapp/relay/types"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/golang/protobuf/proto"
)
type btcStore struct {
......@@ -284,8 +282,8 @@ func getRawTxHash(rawtx string) ([]byte, error) {
if err != nil {
return nil, err
}
h := common.DoubleHashH(data)
return h.Bytes(), nil
h := common.Sha2Sum(data)
return h, nil
}
func getSiblingHash(sibling string) ([][]byte, error) {
......@@ -308,10 +306,17 @@ func btcHashStrRevers(str string) ([]byte, error) {
if err != nil {
return nil, err
}
merkle := common.BytesToHash(data).Revers().Bytes()
merkle := reverse(data)
return merkle, nil
}
func reverse(h []byte) []byte {
for i := 0; i < common.Sha256Len/2; i++ {
h[i], h[common.Sha256Len-1-i] = h[common.Sha256Len-1-i], h[i]
}
return h
}
func (b *btcStore) getHeadHeightList(req *ty.ReqRelayBtcHeaderHeightList) (types.Message, error) {
prefix := []byte(relayBTCHeaderHeightList)
key := calcBtcHeaderKeyHeightList(req.ReqHeight)
......
......@@ -5,6 +5,7 @@
package executor
import (
"encoding/hex"
"errors"
"fmt"
"strconv"
......@@ -247,7 +248,7 @@ type tradeAction struct {
}
func newTradeAction(t *trade, tx *types.Transaction) *tradeAction {
hash := common.Bytes2Hex(tx.Hash())
hash := hex.EncodeToString(tx.Hash())
fromaddr := tx.From()
return &tradeAction{t.GetCoinsAccount(), t.GetStateDB(), hash, fromaddr,
t.GetBlockTime(), t.GetHeight(), dapp.ExecAddress(string(tx.Execer))}
......
......@@ -5,9 +5,9 @@
package executor
import (
"encoding/hex"
"fmt"
"github.com/33cn/chain33/common"
pty "github.com/33cn/plugin/plugin/dapp/unfreeze/types"
)
......@@ -18,7 +18,7 @@ var (
)
func unfreezeID(txHash []byte) []byte {
return []byte(fmt.Sprintf("%s%s", id, common.Bytes2Hex(txHash)))
return []byte(fmt.Sprintf("%s%s", id, hex.EncodeToString(txHash)))
}
func initKey(init string) []byte {
......
......@@ -81,7 +81,7 @@ func (t *Trie) Prove(key []byte, fromLevel uint, proofDb dbm.DB) error {
} else {
enc, _ := proto.Marshal(n.create())
if !ok {
hash = createHashNode(common.ShaKeccak256(enc))
hash = createHashNode(common.Sha3(enc))
}
proofDb.Set(hash.GetHash(), enc)
}
......
......@@ -51,7 +51,7 @@ func makeProvers(trie *Trie) []func(key []byte) *dbm.GoMemDB {
proof, _ := dbm.NewGoMemDB("gomemdb", "", 128)
if it := NewIterator(trie.NodeIterator(key)); it.Next() && bytes.Equal(key, it.Key) {
for _, p := range it.Prove() {
proof.Set(common.ShaKeccak256(p), p)
proof.Set(common.Sha3(p), p)
}
}
return proof
......@@ -130,7 +130,7 @@ func TestBadProof(t *testing.T) {
proof.Delete(key)
mutateByte(val)
proof.Set(common.ShaKeccak256(val), val)
proof.Set(common.Sha3(val), val)
if _, _, err := VerifyProof(root, kv.k, proof); err == nil {
t.Fatalf("prover %d: expected proof to fail for key %x", i, kv.k)
......
......@@ -17,7 +17,6 @@
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package mpt
import (
......@@ -101,7 +100,7 @@ func TestSecureGetKey(t *testing.T) {
key := []byte("foo")
value := []byte("bar")
seckey := common.ShaKeccak256(key)
seckey := common.Sha3(key)
if !bytes.Equal(trie.Get(key), value) {
t.Errorf("Get did not return bar")
......
......@@ -507,7 +507,7 @@ func (t *TrieEx) Get(key []byte) []byte {
// TryGet returns the value for key stored in the trie.
func (t *TrieEx) TryGet(key []byte) ([]byte, error) {
if enableSecure {
key = common.ShaKeccak256(key)
key = common.Sha3(key)
}
return t.Trie.TryGet(key)
}
......@@ -522,7 +522,7 @@ func (t *TrieEx) Update(key, value []byte) {
// TryUpdate set key with value in the trie
func (t *TrieEx) TryUpdate(key, value []byte) error {
if enableSecure {
key = common.ShaKeccak256(key)
key = common.Sha3(key)
}
return t.Trie.TryUpdate(key, value)
}
......@@ -537,7 +537,7 @@ func (t *TrieEx) Delete(key []byte) {
// TryDelete removes any existing value for key from the trie.
func (t *TrieEx) TryDelete(key []byte) error {
if enableSecure {
key = common.ShaKeccak256(key)
key = common.Sha3(key)
}
return t.Trie.TryDelete(key)
}
......@@ -600,7 +600,7 @@ func GetKVPair(db dbm.DB, storeGet *types.StoreGet) ([][]byte, error) {
// GetKVPairProof 获取指定k:v pair的proof证明
func GetKVPairProof(db dbm.DB, roothash []byte, key []byte) []byte {
if enableSecure {
key = common.ShaKeccak256(key)
key = common.Sha3(key)
}
value, _, err := VerifyProof(common.BytesToHash(roothash), key, db)
if nil != err {
......@@ -642,7 +642,7 @@ func DelKVPair(db dbm.DB, storeDel *types.StoreGet) ([]byte, [][]byte, error) {
// VerifyKVPairProof 验证KVPair 的证明
func VerifyKVPairProof(db dbm.DB, roothash []byte, keyvalue types.KeyValue, proof []byte) bool {
if enableSecure {
keyvalue.Key = common.ShaKeccak256(keyvalue.Key)
keyvalue.Key = common.Sha3(keyvalue.Key)
}
_, _, err := VerifyProof(common.BytesToHash(roothash), keyvalue.Key, db)
return nil == err
......
......@@ -612,7 +612,7 @@ func BenchmarkHash(b *testing.B) {
// Insert the accounts into the trie and hash it
trie := newEmpty()
for i := 0; i < len(addresses); i++ {
trie.Update(common.ShaKeccak256(addresses[i][:]), accounts[i])
trie.Update(common.Sha3(addresses[i][:]), accounts[i])
}
b.ResetTimer()
b.ReportAllocs()
......
......@@ -81,7 +81,7 @@ func (t *Trie) Prove(key []byte, fromLevel uint, proofDb dbm.DB) error {
} else {
enc, _ := rlp.EncodeToBytes(n)
if !ok {
hash = common.ShaKeccak256(enc)
hash = common.Sha3(enc)
}
proofDb.Set(hash, enc)
}
......
......@@ -53,7 +53,7 @@ func makeProvers(trie *Trie) []func(key []byte) *dbm.GoMemDB {
proof, _ := dbm.NewGoMemDB("gomemdb", "", 128)
if it := NewIterator(trie.NodeIterator(key)); it.Next() && bytes.Equal(key, it.Key) {
for _, p := range it.Prove() {
proof.Set(common.ShaKeccak256(p), p)
proof.Set(common.Sha3(p), p)
}
}
return proof
......@@ -132,7 +132,7 @@ func TestBadProof(t *testing.T) {
proof.Delete(key)
mutateByte(val)
proof.Set(common.ShaKeccak256(val), val)
proof.Set(common.Sha3(val), val)
if _, _, err := VerifyProof(root, kv.k, proof); err == nil {
t.Fatalf("prover %d: expected proof to fail for key %x", i, kv.k)
......
......@@ -101,7 +101,7 @@ func TestSecureGetKey(t *testing.T) {
key := []byte("foo")
value := []byte("bar")
seckey := common.ShaKeccak256(key)
seckey := common.Sha3(key)
if !bytes.Equal(trie.Get(key), value) {
t.Errorf("Get did not return bar")
......
......@@ -507,7 +507,7 @@ func (t *TrieEx) Get(key []byte) []byte {
// TryGet returns the value for key stored in the trie.
func (t *TrieEx) TryGet(key []byte) ([]byte, error) {
if enableSecure {
key = common.ShaKeccak256(key)
key = common.Sha3(key)
}
return t.Trie.TryGet(key)
}
......@@ -522,7 +522,7 @@ func (t *TrieEx) Update(key, value []byte) {
// TryUpdate set key with value in the trie
func (t *TrieEx) TryUpdate(key, value []byte) error {
if enableSecure {
key = common.ShaKeccak256(key)
key = common.Sha3(key)
}
return t.Trie.TryUpdate(key, value)
}
......@@ -537,7 +537,7 @@ func (t *TrieEx) Delete(key []byte) {
// TryDelete removes any existing value for key from the trie.
func (t *TrieEx) TryDelete(key []byte) error {
if enableSecure {
key = common.ShaKeccak256(key)
key = common.Sha3(key)
}
return t.Trie.TryDelete(key)
}
......@@ -600,7 +600,7 @@ func GetKVPair(db dbm.DB, storeGet *types.StoreGet) ([][]byte, error) {
// GetKVPairProof 获取指定k:v pair的proof证明
func GetKVPairProof(db dbm.DB, roothash []byte, key []byte) []byte {
if enableSecure {
key = common.ShaKeccak256(key)
key = common.Sha3(key)
}
value, _, err := VerifyProof(common.BytesToHash(roothash), key, db)
if nil != err {
......@@ -642,7 +642,7 @@ func DelKVPair(db dbm.DB, storeDel *types.StoreGet) ([]byte, [][]byte, error) {
// VerifyKVPairProof 验证KVPair 的证明
func VerifyKVPairProof(db dbm.DB, roothash []byte, keyvalue types.KeyValue, proof []byte) bool {
if enableSecure {
keyvalue.Key = common.ShaKeccak256(keyvalue.Key)
keyvalue.Key = common.Sha3(keyvalue.Key)
}
_, _, err := VerifyProof(common.BytesToHash(roothash), keyvalue.Key, db)
return nil == err
......
......@@ -600,14 +600,14 @@ func BenchmarkHash(b *testing.B) {
nonce = uint64(random.Int63())
balance = new(big.Int).Rand(random, new(big.Int).Exp(Big2, Big256, nil))
root = emptyRoot
code = common.ShaKeccak256(nil)
code = common.Sha3(nil)
)
accounts[i], _ = rlp.EncodeToBytes([]interface{}{nonce, balance, root, code})
}
// Insert the accounts into the trie and hash it
trie := newEmpty()
for i := 0; i < len(addresses); i++ {
trie.Update(common.ShaKeccak256(addresses[i][:]), accounts[i])
trie.Update(common.Sha3(addresses[i][:]), accounts[i])
}
b.ResetTimer()
b.ReportAllocs()
......
......@@ -107,7 +107,7 @@ func HashToAddress(version byte, in []byte) *Address {
a.Pubkey = make([]byte, len(in))
copy(a.Pubkey[:], in[:])
a.Version = version
a.Hash160 = common.Rimp160(in)
a.SetBytes(common.Rimp160(in))
return a
}
......@@ -191,12 +191,17 @@ func NewAddrFromString(hs string) (a *Address, e error) {
//Address 地址
type Address struct {
Version byte
Hash160 []byte // For a stealth address: it's HASH160
Hash160 [20]byte // For a stealth address: it's HASH160
Checksum []byte // Unused for a stealth address
Pubkey []byte // Unused for a stealth address
Enc58str string
}
//SetBytes 设置地址的bytes
func (a *Address) SetBytes(b []byte) {
copy(a.Hash160[:], b)
}
func (a *Address) String() string {
if a.Enc58str == "" {
var ad [25]byte
......
......@@ -15,6 +15,36 @@ import (
//Sha256Len sha256 bytes len
const Sha256Len = 32
//Hash type
type Hash [Sha256Len]byte
//BytesToHash []byte -> hash
func BytesToHash(b []byte) Hash {
var h Hash
h.SetBytes(b)
return h
}
//HexToHash hex -> hash
func HexToHash(s string) Hash {
b, err := FromHex(s)
if err != nil {
panic(err)
}
return BytesToHash(b)
}
//Bytes Get the []byte representation of the underlying hash
func (h Hash) Bytes() []byte { return h[:] }
//SetBytes Sets the hash to the value of b. If b is larger than len(h), 'b' will be cropped (from the left).
func (h *Hash) SetBytes(b []byte) {
if len(b) > len(h) {
b = b[len(b)-Sha256Len:]
}
copy(h[Sha256Len-len(b):], b)
}
//ToHex []byte -> hex
func ToHex(b []byte) string {
hex := hex.EncodeToString(b)
......
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