func (n *Namespace) NumInputs() int {
	rwSet, err := n.tx.RWSet()
	if err != nil {
		panic(errors.Wrap(err, "filed getting rw set").Error())
	}
	return rwSet.NumReads(n.namespace())
}
NumInputs returns the number of inputs (or reads in the RWSet) contained in this namespace
package state
import (
	"crypto/sha256"
	"encoding/base64"
	"encoding/json"
	"github.com/hyperledger-labs/fabric-smart-client/platform/fabric"
	"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/flogging"
	"github.com/pkg/errors"
	"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/services/endorser"
	"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
)
var logger = flogging.MustGetLogger("fabric-sdk.state")
type EndorseTransaction interface {
}
type MetaHandler interface {
	StoreMeta(ns *Namespace, s interface{}, namespace string, key string, options *addOutputOptions) error
}
type ViewManager interface {
	InitiateView(view view.View) (interface{}, error)
}
type Command struct {
	Name string
	Ids Identities
}
type Header struct {
	Commands []*Command
}
type Namespace struct {
	tx              *endorser.Transaction
	ns              string
	codec           Codec
	metaHandlers    []MetaHandler
	certifiedInputs map[string][]byte
}
func NewNamespace(tx *endorser.Transaction, forceSBE bool) *Namespace {
	return &Namespace{
		tx:    tx,
		codec: &JSONCodec{},
		metaHandlers: []MetaHandler{
			&sbeMetaHandler{forceSBE: forceSBE},
			&contractMetaHandler{},
		},
		certifiedInputs: map[string][]byte{},
	}
}
func NewNamespaceForName(tx *endorser.Transaction, ns string, forceSBE bool) *Namespace {
	return &Namespace{
		tx:    tx,
		ns:    ns,
		codec: &JSONCodec{},
		metaHandlers: []MetaHandler{
			&sbeMetaHandler{forceSBE: forceSBE},
			&contractMetaHandler{},
		},
		certifiedInputs: map[string][]byte{},
	}
}
func (n *Namespace) Present() bool {
	rwSet, err := n.tx.RWSet()
	if err != nil {
		return false
	}
	ns := n.namespace()
	for _, s := range rwSet.Namespaces() {
		if s == ns {
			return true
		}
	}
	return false
}
func (n *Namespace) SetNamespace(ns string) {
	n.tx.SetProposal(ns, "Version-0.0", "_state")
}
func (n *Namespace) AddCommand(command string, ids ...view.Identity) error {
	tx := &Header{}
	params := n.tx.Parameters()
	flag := false
	if len(params) == 0 {
		tx = &Header{
			Commands: []*Command{},
		}
		flag = true
	} else {
		err := json.Unmarshal(params[0], tx)
		if err != nil {
			return errors.Wrap(err, "failed unmarshalling tx entry")
		}
	}
	tx.Commands = append(tx.Commands, &Command{
		Name: command,
		Ids:  ids,
	})
	raw, err := json.Marshal(tx)
	if err != nil {
		return errors.Wrap(err, "failed marshalling tx entry")
	}
	if flag {
		n.tx.AppendParameter(raw)
	} else {
		if err := n.tx.SetParameterAt(0, raw); err != nil {
			return errors.Wrap(err, "failed setting tx entry")
		}
	}
	return nil
}
func (n *Namespace) AddInputByLinearID(id string, state interface{}, opts ...AddInputOption) error {
	rwSet, err := n.tx.RWSet()
	if err != nil {
		return errors.Wrap(err, "filed getting rw set")
	}
	raw, err := rwSet.GetState(n.namespace(), id)
	if err != nil {
		return errors.Wrapf(err, "failed getting state [%s, %s]", n.namespace(), id)
	}
	mapping, err := n.getFieldMapping(n.namespace(), id, true)
	if err != nil {
		return errors.Wrapf(err, "failed getting mapping [%s, %s]", n.namespace(), id)
	}
	if len(mapping) != 0 && len(mapping["_root_"]) != 0 {
		raw = mapping["_root_"]
	}
	logger.Debugf("AddInputByLinearID [%ss,%s] [%s]", n.namespace(), id, base64.StdEncoding.EncodeToString(raw))
	err = n.codec.Unmarshal(raw, state)
	if err != nil {
		return errors.Wrapf(err, "failed unmarshalling state [%s, %s] [%s]", n.namespace(), id, string(raw))
	}
	err = n.unmarshalTags(rwSet, state, mapping)
	if err != nil {
		return errors.Wrapf(err, "failed unmarshalling tags [%s, %s]", n.namespace(), id)
	}
	addInputOptions := &addInputOptions{}
	for _, opt := range opts {
		if err := opt(addInputOptions); err != nil {
			return errors.Wrapf(err, "failed parsing opts [%s, %s] [%s]", n.namespace(), id, string(raw))
		}
	}
	if addInputOptions.certification {
		if err := n.certifyInput(id); err != nil {
			return errors.Wrapf(err, "failed certifying input [%s, %s] [%s]", n.namespace(), id, string(raw))
		}
	}
	if err := n.setFieldMapping(n.namespace(), id, mapping); err != nil {
		return errors.Wrap(err, "failed setting meta mapping")
	}
	return nil
}
func (n *Namespace) AddOutput(st interface{}, opts ...AddOutputOption) error {
	var err error
	rwSet, err := n.tx.RWSet()
	if err != nil {
		return errors.Wrap(err, "filed getting rw set")
	}
	id, err := n.getStateID(st)
	if err != nil {
		return err
	}
	options := &addOutputOptions{}
	for _, opt := range opts {
		if err := opt(options); err != nil {
			return errors.Wrapf(err, "failed parsing opts [%s, %s]", n.namespace(), id)
		}
	}
	st, mapping, err := n.marshalTags(rwSet, st)
	if err != nil {
		return errors.Wrap(err, "failed parsing tags")
	}
	raw, err := n.codec.Marshal(st)
	if err != nil {
		return err
	}
	logger.Debugf("AddOutput [%s,%s][%b] [%s]", n.namespace(), id, options.hashHiding, base64.StdEncoding.EncodeToString(raw))
	switch {
	case options.hashHiding:
		hash := sha256.New()
		hash.Write(raw)
		rawHashed := hash.Sum(nil)
		if len(mapping) == 0 {
			mapping = map[string][]byte{}
		}
		mapping["_root_"] = raw
		err = rwSet.SetState(n.namespace(), id, rawHashed)
		if err != nil {
			return err
		}
	default:
		err = rwSet.SetState(n.namespace(), id, raw)
		if err != nil {
			return err
		}
	}
	if err := n.setFieldMapping(n.namespace(), id, mapping); err != nil {
		return errors.Wrap(err, "failed setting meta mapping")
	}
	if err = n.setMeta(st, n.namespace(), id, options); err != nil {
		return errors.Wrap(err, "failed setting metadata")
	}
	return nil
}
func (n *Namespace) GetOutputAt(index int, state interface{}) error {
	rwSet, err := n.tx.RWSet()
	if err != nil {
		return errors.Wrap(err, "filed getting rw set")
	}
	k, raw, err := rwSet.GetWriteAt(n.namespace(), index)
	if err != nil {
		return errors.Wrapf(err, "failed getting state [%s, %d]", n.namespace(), index)
	}
	mapping, err := n.getFieldMapping(n.namespace(), k, true)
	if err != nil {
		return errors.Wrapf(err, "failed getting mapping [%s, %d] [%s]", n.namespace(), index, string(raw))
	}
	if len(mapping) != 0 && len(mapping["_root_"]) != 0 {
		raw = mapping["_root_"]
	}
	logger.Debugf("GetOutputAt [%s,%d] [%s]", n.namespace(), index, string(raw))
	err = n.codec.Unmarshal(raw, state)
	if err != nil {
		return errors.Wrapf(err, "failed unmarshalling state [%s, %d] [%s]", n.namespace(), index, string(raw))
	}
	err = n.unmarshalTags(rwSet, state, mapping)
	if err != nil {
		return errors.Wrapf(err, "failed unmarshalling tags [%s, %d] [%s]", n.namespace(), index, string(raw))
	}
	return nil
}
func (n *Namespace) GetInputAt(index int, state interface{}) error {
	rwSet, err := n.tx.RWSet()
	if err != nil {
		return errors.Wrap(err, "filed getting rw set")
	}
	flag := true
	k, raw, err := rwSet.GetReadAt(n.namespace(), index)
	if err != nil {
		k, err = rwSet.GetReadKeyAt(n.namespace(), index)
		if err != nil {
			return errors.Wrapf(err, "failed getting state [%s, %d]", n.namespace(), index)
		}
		v, ok := n.certifiedInputs[k]
		if !ok {
			return errors.Wrapf(err, "failed getting state [%s, %d]", n.namespace(), index)
		}
		raw = v
		flag = false
	}
	mapping, err := n.getFieldMapping(n.namespace(), k, flag)
	if err != nil {
		return errors.Wrapf(err, "failed getting mapping [%s, %d] [%s]", n.namespace(), index, string(raw))
	}
	if len(mapping) != 0 && len(mapping["_root_"]) != 0 {
		raw = mapping["_root_"]
	}
	logger.Debugf("GetInputAt [%s,%d] [%s]", n.namespace(), index, string(raw))
	err = n.codec.Unmarshal(raw, state)
	if err != nil {
		return errors.Wrapf(err, "failed unmarshalling state [%s, %d] [%s]", n.namespace(), index, string(raw))
	}
	err = n.unmarshalTags(rwSet, state, mapping)
	if err != nil {
		return errors.Wrapf(err, "failed unmarshalling tags [%s, %d] [%s]", n.namespace(), index, string(raw))
	}
	return nil
}
func (n *Namespace) Delete(state interface{}) error {
	var err error
	rwSet, err := n.tx.RWSet()
	if err != nil {
		return errors.Wrap(err, "filed getting rw set")
	}
	id, err := n.getStateID(state)
	if err != nil {
		return err
	}
	logger.Debugf("Delete [%s,%s]", n.namespace(), id)
	err = rwSet.SetState(n.namespace(), id, nil)
	if err != nil {
		return err
	}
	return nil
}