func (stateDelta *StateDelta) Unmarshal(bytes []byte) error {
	buffer := proto.NewBuffer(bytes)
	size, err := buffer.DecodeVarint()
	if err != nil {
		return fmt.Errorf("Error unmarashaling size: %s", err)
	}
	stateDelta.ChaincodeStateDeltas = make(map[string]*ChaincodeStateDelta, size)
	for i := uint64(0); i < size; i++ {
		chaincodeID, err := buffer.DecodeStringBytes()
		if err != nil {
			return fmt.Errorf("Error unmarshaling chaincodeID : %s", err)
		}
		chaincodeStateDelta := newChaincodeStateDelta(chaincodeID)
		err = chaincodeStateDelta.unmarshal(buffer)
		if err != nil {
			return fmt.Errorf("Error unmarshalling chaincodeStateDelta : %s", err)
		}
		stateDelta.ChaincodeStateDeltas[chaincodeID] = chaincodeStateDelta
	}
	return nil
}
Unmarshal deserializes StateDelta
package statemgmt
import (
	"bytes"
	"fmt"
	"sort"
	"github.com/golang/protobuf/proto"
	"github.com/openblockchain/obc-peer/openchain/util"
)
type StateDelta struct {
	ChaincodeStateDeltas map[string]*ChaincodeStateDelta
	RollBackwards bool
}
func NewStateDelta() *StateDelta {
	return &StateDelta{make(map[string]*ChaincodeStateDelta), false}
}
func (stateDelta *StateDelta) Get(chaincodeID string, key string) *UpdatedValue {
	chaincodeStateDelta, ok := stateDelta.ChaincodeStateDeltas[chaincodeID]
	if ok {
		return chaincodeStateDelta.get(key)
	}
	return nil
}
func (stateDelta *StateDelta) Set(chaincodeID string, key string, value, previousValue []byte) {
	chaincodeStateDelta := stateDelta.getOrCreateChaincodeStateDelta(chaincodeID)
	chaincodeStateDelta.set(key, value, previousValue)
	return
}
func (stateDelta *StateDelta) Delete(chaincodeID string, key string, previousValue []byte) {
	chaincodeStateDelta := stateDelta.getOrCreateChaincodeStateDelta(chaincodeID)
	chaincodeStateDelta.remove(key, previousValue)
	return
}
func (stateDelta *StateDelta) IsUpdatedValueSet(chaincodeID, key string) bool {
	chaincodeStateDelta, ok := stateDelta.ChaincodeStateDeltas[chaincodeID]
	if !ok {
		return false
	}
	if _, ok := chaincodeStateDelta.UpdatedKVs[key]; ok {
		return true
	}
	return false
}
func (stateDelta *StateDelta) ApplyChanges(anotherStateDelta *StateDelta) {
	for chaincodeID, chaincodeStateDelta := range anotherStateDelta.ChaincodeStateDeltas {
		existingChaincodeStateDelta, existingChaincode := stateDelta.ChaincodeStateDeltas[chaincodeID]
		for key, valueHolder := range chaincodeStateDelta.UpdatedKVs {
			var previousValue []byte
			if existingChaincode {
				existingUpdateValue, existingUpdate := existingChaincodeStateDelta.UpdatedKVs[key]
				if existingUpdate {
					previousValue = existingUpdateValue.PreviousValue
				} else {
					previousValue = valueHolder.PreviousValue
				}
			} else {
				previousValue = valueHolder.PreviousValue
			}
			if valueHolder.IsDelete() {
				stateDelta.Delete(chaincodeID, key, previousValue)
			} else {
				stateDelta.Set(chaincodeID, key, valueHolder.Value, previousValue)
			}
		}
	}
}
func (stateDelta *StateDelta) IsEmpty() bool {
	return len(stateDelta.ChaincodeStateDeltas) == 0
}
func (stateDelta *StateDelta) GetUpdatedChaincodeIds(sorted bool) []string {
	updatedChaincodeIds := make([]string, len(stateDelta.ChaincodeStateDeltas))
	i := 0
	for k := range stateDelta.ChaincodeStateDeltas {
		updatedChaincodeIds[i] = k
		i++
	}
	if sorted {
		sort.Strings(updatedChaincodeIds)
	}
	return updatedChaincodeIds
}
func (stateDelta *StateDelta) GetUpdates(chaincodeID string) map[string]*UpdatedValue {
	chaincodeStateDelta := stateDelta.ChaincodeStateDeltas[chaincodeID]
	if chaincodeStateDelta == nil {
		return nil
	}
	return chaincodeStateDelta.UpdatedKVs
}
func (stateDelta *StateDelta) getOrCreateChaincodeStateDelta(chaincodeID string) *ChaincodeStateDelta {
	chaincodeStateDelta, ok := stateDelta.ChaincodeStateDeltas[chaincodeID]
	if !ok {
		chaincodeStateDelta = newChaincodeStateDelta(chaincodeID)
		stateDelta.ChaincodeStateDeltas[chaincodeID] = chaincodeStateDelta
	}
	return chaincodeStateDelta
}
func (stateDelta *StateDelta) ComputeCryptoHash() []byte {
	if stateDelta.IsEmpty() {
		return nil
	}
	var buffer bytes.Buffer
	sortedChaincodeIds := stateDelta.GetUpdatedChaincodeIds(true)
	for _, chaincodeID := range sortedChaincodeIds {
		buffer.WriteString(chaincodeID)
		chaincodeStateDelta := stateDelta.ChaincodeStateDeltas[chaincodeID]
		sortedKeys := chaincodeStateDelta.getSortedKeys()
		for _, key := range sortedKeys {
			buffer.WriteString(key)
			updatedValue := chaincodeStateDelta.get(key)
			if !updatedValue.IsDelete() {
				buffer.Write(updatedValue.Value)
			}
		}
	}
	hashingContent := buffer.Bytes()
	logger.Debug("computing hash on %#v", hashingContent)
	return util.ComputeCryptoHash(hashingContent)
}
type ChaincodeStateDelta struct {
	ChaincodeID string
	UpdatedKVs  map[string]*UpdatedValue
}
func newChaincodeStateDelta(chaincodeID string) *ChaincodeStateDelta {
	return &ChaincodeStateDelta{chaincodeID, make(map[string]*UpdatedValue)}
}
func (chaincodeStateDelta *ChaincodeStateDelta) get(key string) *UpdatedValue {
	return chaincodeStateDelta.UpdatedKVs[key]
}
func (chaincodeStateDelta *ChaincodeStateDelta) set(key string, updatedValue, previousValue []byte) {
	updatedKV, ok := chaincodeStateDelta.UpdatedKVs[key]
	if ok {
		updatedKV.Value = updatedValue
	} else {
		chaincodeStateDelta.UpdatedKVs[key] = &UpdatedValue{updatedValue, previousValue}
	}
}
func (chaincodeStateDelta *ChaincodeStateDelta) remove(key string, previousValue []byte) {
	updatedKV, ok := chaincodeStateDelta.UpdatedKVs[key]
	if ok {
		updatedKV.Value = nil
	} else {
		chaincodeStateDelta.UpdatedKVs[key] = &UpdatedValue{nil, previousValue}
	}
}
func (chaincodeStateDelta *ChaincodeStateDelta) hasChanges() bool {
	return len(chaincodeStateDelta.UpdatedKVs) > 0
}
func (chaincodeStateDelta *ChaincodeStateDelta) getSortedKeys() []string {
	updatedKeys := []string{}
	for k := range chaincodeStateDelta.UpdatedKVs {
		updatedKeys = append(updatedKeys, k)
	}
	sort.Strings(updatedKeys)
	logger.Debug("Sorted keys = %#v", updatedKeys)
	return updatedKeys
}
type UpdatedValue struct {
	Value         []byte
	PreviousValue []byte
}
func (updatedValue *UpdatedValue) IsDelete() bool {
	return updatedValue.Value == nil
}
func (updatedValue *UpdatedValue) GetValue() []byte {
	return updatedValue.Value
}
func (updatedValue *UpdatedValue) GetPreviousValue() []byte {
	return updatedValue.PreviousValue
}
func (stateDelta *StateDelta) Marshal() (b []byte) {
	buffer := proto.NewBuffer([]byte{})
	err := buffer.EncodeVarint(uint64(len(stateDelta.ChaincodeStateDeltas)))
	if err != nil {
		panic(fmt.Errorf("This error should not occure: %s", err))
	}
	for chaincodeID, chaincodeStateDelta := range stateDelta.ChaincodeStateDeltas {
		buffer.EncodeStringBytes(chaincodeID)
		chaincodeStateDelta.marshal(buffer)
	}
	b = buffer.Bytes()
	return
}
func (chaincodeStateDelta *ChaincodeStateDelta) marshal(buffer *proto.Buffer) {
	err := buffer.EncodeVarint(uint64(len(chaincodeStateDelta.UpdatedKVs)))
	if err != nil {
		panic(fmt.Errorf("This error should not occur: %s", err))
	}
	for key, valueHolder := range chaincodeStateDelta.UpdatedKVs {
		err = buffer.EncodeStringBytes(key)
		if err != nil {
			return
		}
		err = buffer.EncodeRawBytes(valueHolder.Value)
		if err != nil {
			panic(fmt.Errorf("This error should not occur: %s", err))
		}
		err = buffer.EncodeRawBytes(valueHolder.PreviousValue)
		if err != nil {
			panic(fmt.Errorf("This error should not occur: %s", err))
		}
	}
	return
}