Commit 0b8360b0 authored by jiangpeng's avatar jiangpeng Committed by 33cn

replace proto Marshal with types.Encode

parent 6f55ad81
......@@ -118,11 +118,7 @@ func (ca *ContractAccount) updateStorageHash() {
for k, v := range ca.State.GetStorage() {
state.Storage[k] = v
}
ret, err := proto.Marshal(state)
if err != nil {
log15.Error("marshal contract state data error", "error", err)
return
}
ret := types.Encode(state)
ca.State.StorageHash = common.ToHash(ret).Bytes()
}
......@@ -242,43 +238,27 @@ func (ca *ContractAccount) GetExecName() string {
// GetDataKV 合约固定数据,包含合约代码,以及代码哈希
func (ca *ContractAccount) GetDataKV() (kvSet []*types.KeyValue) {
ca.Data.Addr = ca.Addr
datas, err := proto.Marshal(&ca.Data)
if err != nil {
log15.Error("marshal contract data error!", "addr", ca.Addr, "error", err)
return
}
datas := types.Encode(&ca.Data)
kvSet = append(kvSet, &types.KeyValue{Key: ca.GetDataKey(), Value: datas})
return
}
// GetStateKV 获取合约状态数据,包含nonce、是否自杀、存储哈希、存储数据
func (ca *ContractAccount) GetStateKV() (kvSet []*types.KeyValue) {
datas, err := proto.Marshal(&ca.State)
if err != nil {
log15.Error("marshal contract state error!", "addr", ca.Addr, "error", err)
return
}
datas := types.Encode(&ca.State)
kvSet = append(kvSet, &types.KeyValue{Key: ca.GetStateKey(), Value: datas})
return
}
// BuildDataLog 构建变更日志
func (ca *ContractAccount) BuildDataLog() (log *types.ReceiptLog) {
datas, err := proto.Marshal(&ca.Data)
if err != nil {
log15.Error("marshal contract data error!", "addr", ca.Addr, "error", err)
return
}
datas := types.Encode(&ca.Data)
return &types.ReceiptLog{Ty: evmtypes.TyLogContractData, Log: datas}
}
// BuildStateLog 构建变更日志
func (ca *ContractAccount) BuildStateLog() (log *types.ReceiptLog) {
datas, err := proto.Marshal(&ca.State)
if err != nil {
log15.Error("marshal contract state log error!", "addr", ca.Addr, "error", err)
return
}
datas := types.Encode(&ca.State)
return &types.ReceiptLog{Ty: evmtypes.TyLogContractState, Log: datas}
}
......
......@@ -24,7 +24,6 @@ import (
wcom "github.com/33cn/chain33/wallet/common"
privacy "github.com/33cn/plugin/plugin/dapp/privacy/crypto"
privacytypes "github.com/33cn/plugin/plugin/dapp/privacy/types"
"github.com/golang/protobuf/proto"
)
func (policy *privacyPolicy) rescanAllTxAddToUpdateUTXOs() {
......@@ -984,11 +983,7 @@ func (policy *privacyPolicy) buildAndStoreWalletTxDetail(param *buildStoreWallet
txdetail.Amount, _ = txInfo.tx.Amount()
txdetail.Fromaddr = param.addr
txdetailbyte, err := proto.Marshal(&txdetail)
if err != nil {
bizlog.Error("buildAndStoreWalletTxDetail err", "Height", txInfo.blockHeight, "txHash", txInfo.txHashHex)
return
}
txdetailbyte := types.Encode(&txdetail)
txInfo.batch.Set(key, txdetailbyte)
//额外存储可以快速定位到接收隐私的交易
......
......@@ -140,11 +140,7 @@ func (store *privacyStore) setWalletAccountPrivacy(addr string, privacy *privacy
return types.ErrInvalidParam
}
privacybyte, err := proto.Marshal(privacy)
if err != nil {
bizlog.Error("SetWalletAccountPrivacy proto.Marshal err!", "err", err)
return types.ErrMarshal
}
privacybyte := types.Encode(privacy)
newbatch := store.NewBatch(true)
newbatch.Set(calcPrivacyAddrKey(addr), privacybyte)
......@@ -652,11 +648,7 @@ func (store *privacyStore) selectCurrentWalletPrivacyTx(txDetal *types.Transacti
//2.calcUTXOKey4TokenAddr-->calcUTXOKey,创建kv,方便查询现在某个地址下某种token的可用utxo
func (store *privacyStore) setUTXO(utxoInfo *privacytypes.PrivacyDBStore, txHash string, newbatch db.Batch) error {
privacyStorebyte, err := proto.Marshal(utxoInfo)
if err != nil {
bizlog.Error("setUTXO proto.Marshal err!", "err", err)
return types.ErrMarshal
}
privacyStorebyte := types.Encode(utxoInfo)
outIndex := int(utxoInfo.OutIndex)
utxoKey := calcUTXOKey(txHash, outIndex)
bizlog.Debug("setUTXO", "addr", utxoInfo.Owner, "tx with hash", txHash, "amount:", utxoInfo.Amount/store.GetCoinPrecision())
......
......@@ -22,12 +22,12 @@ package mpt
import (
"fmt"
"github.com/33cn/chain33/types"
"sync"
"time"
"github.com/33cn/chain33/common"
dbm "github.com/33cn/chain33/common/db"
proto "github.com/golang/protobuf/proto"
)
// secureKeyPrefix is the database key prefix used to store trie node preimages.
......@@ -98,11 +98,7 @@ type cachedNode struct {
}
func (n *cachedNode) proto() []byte {
blob, err := proto.Marshal(n.node.create())
if err != nil {
panic(err)
}
return blob
return types.Encode(n.node.create())
}
// expandNode traverses the node hierarchy of a collapsed storage node and converts
......
......@@ -21,12 +21,12 @@
package mpt
import (
"github.com/33cn/chain33/types"
"hash"
"sync"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/crypto/sha3"
proto "github.com/golang/protobuf/proto"
)
type hasher struct {
......@@ -179,10 +179,7 @@ func (h *hasher) store(n node, db *Database, force bool) (node, error) {
return n, nil // Nodes smaller than 64 bytes are stored inside their parent
}
nn := n.create()
data, err := proto.Marshal(nn)
if err != nil {
panic("encode error: " + err.Error())
}
data := types.Encode(nn)
// Larger nodes are replaced by their hash and stored in the database.
hash, _ := n.cache()
if hash.HashNode == nil {
......
......@@ -24,9 +24,9 @@ import (
"bytes"
"container/heap"
"errors"
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/common"
proto "github.com/golang/protobuf/proto"
)
// Iterator is a key-value trie iterator that traverses a Trie.
......@@ -192,7 +192,7 @@ func (it *nodeIterator) LeafProof() [][]byte {
node, _, _ := hasher.hashChildren(item.node, nil)
hashed, _ := hasher.store(node, nil, false)
if _, ok := hashed.(hashNode); ok || i == 0 {
enc, _ := proto.Marshal(node.create())
enc := types.Encode(node.create())
proofs = append(proofs, enc)
}
}
......
......@@ -22,6 +22,7 @@ package mpt
import (
"fmt"
"github.com/33cn/chain33/types"
"io"
"strings"
......@@ -75,12 +76,8 @@ type valueNode struct {
// EncodeRLP encodes a full node into the consensus RLP format.
func (n *fullNode) EncodeProto(w io.Writer) error {
node := n.create()
data, err := proto.Marshal(node)
if err != nil {
return err
}
_, err = w.Write(data)
data := types.Encode(n.create())
_, err := w.Write(data)
return err
}
......
......@@ -21,9 +21,9 @@
package mpt
import (
"github.com/33cn/chain33/types"
"testing"
proto "github.com/golang/protobuf/proto"
"github.com/stretchr/testify/assert"
)
......@@ -68,7 +68,6 @@ func TestCanUnload(t *testing.T) {
func TestNodeProto(t *testing.T) {
n := &Node{Value: &Node_Full{Full: &FullNode{}}}
d, err := proto.Marshal(n)
assert.Nil(t, nil, err)
d := types.Encode(n)
assert.Equal(t, 2, len(d))
}
......@@ -23,10 +23,10 @@ package mpt
import (
"bytes"
"fmt"
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/common"
dbm "github.com/33cn/chain33/common/db"
proto "github.com/golang/protobuf/proto"
)
// Prove constructs a merkle proof for key. The result contains all encoded nodes
......@@ -79,7 +79,7 @@ func (t *Trie) Prove(key []byte, fromLevel uint, proofDb dbm.DB) error {
if fromLevel > 0 {
fromLevel--
} else {
enc, _ := proto.Marshal(n.create())
enc := types.Encode(n.create())
if !ok {
hash = createHashNode(common.Sha3(enc))
}
......
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