func (s *SoBlockProducerWrap) removeBlockProducer() error {
	if s.dba == nil {
		return errors.New("database is nil")
	}
	s.initWatcherFlag()
	var oldVal *SoBlockProducer
	if s.watcherFlag.AnyWatcher {
		oldVal = s.getBlockProducer()
	}
	if res := s.delAllSortKeys(true, nil); !res {
		return errors.New("delAllSortKeys failed")
	}
	if res := s.delAllUniKeys(true, nil); !res {
		return errors.New("delAllUniKeys failed")
	}
	key, err := s.encodeMainKey()
	if err != nil {
		return fmt.Errorf("encodeMainKey failed: %s", err.Error())
	}
	err = s.dba.Delete(key)
	if err == nil {
		s.mKeyBuf = nil
		s.mKeyFlag = -1
		if s.watcherFlag.AnyWatcher && oldVal != nil {
			ReportTableRecordDelete(s.dba.ServiceId(), s.dba.BranchId(), s.mainKey, oldVal)
		}
		return nil
	} else {
		return fmt.Errorf("database.Delete failed: %s", err.Error())
	}
}
//////////// SECTION LKeys delete/insert //////////////
package table
import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"reflect"
	"sync"
	"github.com/coschain/contentos-go/common/encoding/kope"
	"github.com/coschain/contentos-go/iservices"
	prototype "github.com/coschain/contentos-go/prototype"
	proto "github.com/golang/protobuf/proto"
)
var (
	BlockProducerOwnerTable    uint32 = 2440644301
	BlockProducerBpVestTable   uint32 = 3083635068
	BlockProducerOwnerUniTable uint32 = 404338461
	BlockProducerOwnerRow uint32 = 259692740
)
type SoBlockProducerWrap struct {
	dba         iservices.IDatabaseRW
	mainKey     *prototype.AccountName
	watcherFlag *BlockProducerWatcherFlag
	mKeyFlag    int     
	mKeyBuf     []byte  
	mBuf        []byte  
	mdFuncMap   map[string]interface{}
}
func NewSoBlockProducerWrap(dba iservices.IDatabaseRW, key *prototype.AccountName) *SoBlockProducerWrap {
	if dba == nil || key == nil {
		return nil
	}
	result := &SoBlockProducerWrap{dba, key, nil, -1, nil, nil, nil}
	result.initWatcherFlag()
	return result
}
func (s *SoBlockProducerWrap) CheckExist() bool {
	if s.dba == nil {
		return false
	}
	if s.mKeyFlag != -1 {
		if s.mKeyFlag == 0 {
			return false
		}
		return true
	}
	keyBuf, err := s.encodeMainKey()
	if err != nil {
		return false
	}
	res, err := s.dba.Has(keyBuf)
	if err != nil {
		return false
	}
	if res == false {
		s.mKeyFlag = 0
	} else {
		s.mKeyFlag = 1
	}
	return res
}
func (s *SoBlockProducerWrap) MustExist(errMsgs ...interface{}) *SoBlockProducerWrap {
	if !s.CheckExist() {
		panic(bindErrorInfo(fmt.Sprintf("SoBlockProducerWrap.MustExist: %v not found", s.mainKey), errMsgs...))
	}
	return s
}
func (s *SoBlockProducerWrap) MustNotExist(errMsgs ...interface{}) *SoBlockProducerWrap {
	if s.CheckExist() {
		panic(bindErrorInfo(fmt.Sprintf("SoBlockProducerWrap.MustNotExist: %v already exists", s.mainKey), errMsgs...))
	}
	return s
}
func (s *SoBlockProducerWrap) initWatcherFlag() {
	if s.watcherFlag == nil {
		s.watcherFlag = new(BlockProducerWatcherFlag)
		*(s.watcherFlag) = BlockProducerWatcherFlagOfDb(s.dba.ServiceId())
	}
}
func (s *SoBlockProducerWrap) create(f func(tInfo *SoBlockProducer)) error {
	if s.dba == nil {
		return errors.New("the db is nil")
	}
	if s.mainKey == nil {
		return errors.New("the main key is nil")
	}
	val := &SoBlockProducer{}
	f(val)
	if val.Owner == nil {
		val.Owner = s.mainKey
	}
	if s.CheckExist() {
		return errors.New("the main key is already exist")
	}
	keyBuf, err := s.encodeMainKey()
	if err != nil {
		return err
	}
	buf, err := proto.Marshal(val)
	if err != nil {
		return err
	}
	err = s.dba.Put(keyBuf, buf)
	if err != nil {
		return err
	}
	if err = s.insertAllSortKeys(val); err != nil {
		s.delAllSortKeys(false, val)
		s.dba.Delete(keyBuf)
		return err
	}
	if sucNames, err := s.insertAllUniKeys(val); err != nil {
		s.delAllSortKeys(false, val)
		s.delUniKeysWithNames(sucNames, val)
		s.dba.Delete(keyBuf)
		return err
	}
	s.mKeyFlag = 1
	s.initWatcherFlag()
	if s.watcherFlag.AnyWatcher {
		ReportTableRecordInsert(s.dba.ServiceId(), s.dba.BranchId(), s.mainKey, val)
	}
	return nil
}
func (s *SoBlockProducerWrap) Create(f func(tInfo *SoBlockProducer), errArgs ...interface{}) *SoBlockProducerWrap {
	err := s.create(f)
	if err != nil {
		panic(bindErrorInfo(fmt.Errorf("SoBlockProducerWrap.Create failed: %s", err.Error()), errArgs...))
	}
	return s
}
func (s *SoBlockProducerWrap) getMainKeyBuf() ([]byte, error) {
	if s.mainKey == nil {
		return nil, errors.New("the main key is nil")
	}
	if s.mBuf == nil {
		var err error = nil
		s.mBuf, err = kope.Encode(s.mainKey)
		if err != nil {
			return nil, err
		}
	}
	return s.mBuf, nil
}
func (s *SoBlockProducerWrap) modify(f func(tInfo *SoBlockProducer)) error {
	if !s.CheckExist() {
		return errors.New("the SoBlockProducer table does not exist. Please create a table first")
	}
	oriTable := s.getBlockProducer()
	if oriTable == nil {
		return errors.New("fail to get origin table SoBlockProducer")
	}
	curTable := s.getBlockProducer()
	if curTable == nil {
		return errors.New("fail to create current table SoBlockProducer")
	}
	f(curTable)
	if !reflect.DeepEqual(curTable.Owner, oriTable.Owner) {
		return errors.New("primary key does not support modification")
	}
	s.initWatcherFlag()
	modifiedFields, hasWatcher, err := s.getModifiedFields(oriTable, curTable)
	if err != nil {
		return err
	}
	if modifiedFields == nil || len(modifiedFields) < 1 {
		return nil
	}
	err = s.checkSortAndUniFieldValidity(curTable, modifiedFields)
	if err != nil {
		return err
	}
	err = s.handleFieldMd(FieldMdHandleTypeCheck, curTable, modifiedFields)
	if err != nil {
		return err
	}
	err = s.handleFieldMd(FieldMdHandleTypeDel, oriTable, modifiedFields)
	if err != nil {
		return err
	}
	err = s.updateBlockProducer(curTable)
	if err != nil {
		return err
	}
	err = s.handleFieldMd(FieldMdHandleTypeInsert, curTable, modifiedFields)
	if err != nil {
		return err
	}
	if hasWatcher {
		ReportTableRecordUpdate(s.dba.ServiceId(), s.dba.BranchId(), s.mainKey, oriTable, curTable, modifiedFields)
	}
	return nil
}
func (s *SoBlockProducerWrap) Modify(f func(tInfo *SoBlockProducer), errArgs ...interface{}) *SoBlockProducerWrap {
	err := s.modify(f)
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoBlockProducerWrap.Modify failed: %s", err.Error()), errArgs...))
	}
	return s
}
func (s *SoBlockProducerWrap) SetAccountCreateFee(p *prototype.Coin, errArgs ...interface{}) *SoBlockProducerWrap {
	err := s.modify(func(r *SoBlockProducer) {
		r.AccountCreateFee = p
	})
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoBlockProducerWrap.SetAccountCreateFee( %v ) failed: %s", p, err.Error()), errArgs...))
	}
	return s
}
func (s *SoBlockProducerWrap) SetBpVest(p *prototype.BpVestId, errArgs ...interface{}) *SoBlockProducerWrap {
	err := s.modify(func(r *SoBlockProducer) {
		r.BpVest = p
	})
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoBlockProducerWrap.SetBpVest( %v ) failed: %s", p, err.Error()), errArgs...))
	}
	return s
}
func (s *SoBlockProducerWrap) SetCreatedTime(p *prototype.TimePointSec, errArgs ...interface{}) *SoBlockProducerWrap {
	err := s.modify(func(r *SoBlockProducer) {
		r.CreatedTime = p
	})
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoBlockProducerWrap.SetCreatedTime( %v ) failed: %s", p, err.Error()), errArgs...))
	}
	return s
}
func (s *SoBlockProducerWrap) SetEpochDuration(p uint64, errArgs ...interface{}) *SoBlockProducerWrap {
	err := s.modify(func(r *SoBlockProducer) {
		r.EpochDuration = p
	})
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoBlockProducerWrap.SetEpochDuration( %v ) failed: %s", p, err.Error()), errArgs...))
	}
	return s
}
func (s *SoBlockProducerWrap) SetGenBlockCount(p uint64, errArgs ...interface{}) *SoBlockProducerWrap {
	err := s.modify(func(r *SoBlockProducer) {
		r.GenBlockCount = p
	})
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoBlockProducerWrap.SetGenBlockCount( %v ) failed: %s", p, err.Error()), errArgs...))
	}
	return s
}
func (s *SoBlockProducerWrap) SetPerTicketPrice(p *prototype.Coin, errArgs ...interface{}) *SoBlockProducerWrap {
	err := s.modify(func(r *SoBlockProducer) {
		r.PerTicketPrice = p
	})
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoBlockProducerWrap.SetPerTicketPrice( %v ) failed: %s", p, err.Error()), errArgs...))
	}
	return s
}
func (s *SoBlockProducerWrap) SetPerTicketWeight(p uint64, errArgs ...interface{}) *SoBlockProducerWrap {
	err := s.modify(func(r *SoBlockProducer) {
		r.PerTicketWeight = p
	})
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoBlockProducerWrap.SetPerTicketWeight( %v ) failed: %s", p, err.Error()), errArgs...))
	}
	return s
}
func (s *SoBlockProducerWrap) SetProposedStaminaFree(p uint64, errArgs ...interface{}) *SoBlockProducerWrap {
	err := s.modify(func(r *SoBlockProducer) {
		r.ProposedStaminaFree = p
	})
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoBlockProducerWrap.SetProposedStaminaFree( %v ) failed: %s", p, err.Error()), errArgs...))
	}
	return s
}
func (s *SoBlockProducerWrap) SetSigningKey(p *prototype.PublicKeyType, errArgs ...interface{}) *SoBlockProducerWrap {
	err := s.modify(func(r *SoBlockProducer) {
		r.SigningKey = p
	})
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoBlockProducerWrap.SetSigningKey( %v ) failed: %s", p, err.Error()), errArgs...))
	}
	return s
}
func (s *SoBlockProducerWrap) SetTopNAcquireFreeToken(p uint32, errArgs ...interface{}) *SoBlockProducerWrap {
	err := s.modify(func(r *SoBlockProducer) {
		r.TopNAcquireFreeToken = p
	})
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoBlockProducerWrap.SetTopNAcquireFreeToken( %v ) failed: %s", p, err.Error()), errArgs...))
	}
	return s
}
func (s *SoBlockProducerWrap) SetTpsExpected(p uint64, errArgs ...interface{}) *SoBlockProducerWrap {
	err := s.modify(func(r *SoBlockProducer) {
		r.TpsExpected = p
	})
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoBlockProducerWrap.SetTpsExpected( %v ) failed: %s", p, err.Error()), errArgs...))
	}
	return s
}
func (s *SoBlockProducerWrap) SetUrl(p string, errArgs ...interface{}) *SoBlockProducerWrap {
	err := s.modify(func(r *SoBlockProducer) {
		r.Url = p
	})
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoBlockProducerWrap.SetUrl( %v ) failed: %s", p, err.Error()), errArgs...))
	}
	return s
}
func (s *SoBlockProducerWrap) SetVoterCount(p uint64, errArgs ...interface{}) *SoBlockProducerWrap {
	err := s.modify(func(r *SoBlockProducer) {
		r.VoterCount = p
	})
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoBlockProducerWrap.SetVoterCount( %v ) failed: %s", p, err.Error()), errArgs...))
	}
	return s
}
func (s *SoBlockProducerWrap) checkSortAndUniFieldValidity(curTable *SoBlockProducer, fields map[string]bool) error {
	if curTable != nil && fields != nil && len(fields) > 0 {
		if fields["BpVest"] && curTable.BpVest == nil {
			return errors.New("sort field BpVest can't be modified to nil")
		}
	}
	return nil
}
func (s *SoBlockProducerWrap) getModifiedFields(oriTable *SoBlockProducer, curTable *SoBlockProducer) (map[string]bool, bool, error) {
	if oriTable == nil {
		return nil, false, errors.New("table info is nil, can't get modified fields")
	}
	hasWatcher := false
	fields := make(map[string]bool)
	if !reflect.DeepEqual(oriTable.AccountCreateFee, curTable.AccountCreateFee) {
		fields["AccountCreateFee"] = true
		hasWatcher = hasWatcher || s.watcherFlag.HasAccountCreateFeeWatcher
	}
	if !reflect.DeepEqual(oriTable.BpVest, curTable.BpVest) {
		fields["BpVest"] = true
		hasWatcher = hasWatcher || s.watcherFlag.HasBpVestWatcher
	}
	if !reflect.DeepEqual(oriTable.CreatedTime, curTable.CreatedTime) {
		fields["CreatedTime"] = true
		hasWatcher = hasWatcher || s.watcherFlag.HasCreatedTimeWatcher
	}
	if !reflect.DeepEqual(oriTable.EpochDuration, curTable.EpochDuration) {
		fields["EpochDuration"] = true
		hasWatcher = hasWatcher || s.watcherFlag.HasEpochDurationWatcher
	}
	if !reflect.DeepEqual(oriTable.GenBlockCount, curTable.GenBlockCount) {
		fields["GenBlockCount"] = true
		hasWatcher = hasWatcher || s.watcherFlag.HasGenBlockCountWatcher
	}
	if !reflect.DeepEqual(oriTable.PerTicketPrice, curTable.PerTicketPrice) {
		fields["PerTicketPrice"] = true
		hasWatcher = hasWatcher || s.watcherFlag.HasPerTicketPriceWatcher
	}
	if !reflect.DeepEqual(oriTable.PerTicketWeight, curTable.PerTicketWeight) {
		fields["PerTicketWeight"] = true
		hasWatcher = hasWatcher || s.watcherFlag.HasPerTicketWeightWatcher
	}
	if !reflect.DeepEqual(oriTable.ProposedStaminaFree, curTable.ProposedStaminaFree) {
		fields["ProposedStaminaFree"] = true
		hasWatcher = hasWatcher || s.watcherFlag.HasProposedStaminaFreeWatcher
	}
	if !reflect.DeepEqual(oriTable.SigningKey, curTable.SigningKey) {
		fields["SigningKey"] = true
		hasWatcher = hasWatcher || s.watcherFlag.HasSigningKeyWatcher
	}
	if !reflect.DeepEqual(oriTable.TopNAcquireFreeToken, curTable.TopNAcquireFreeToken) {
		fields["TopNAcquireFreeToken"] = true
		hasWatcher = hasWatcher || s.watcherFlag.HasTopNAcquireFreeTokenWatcher
	}
	if !reflect.DeepEqual(oriTable.TpsExpected, curTable.TpsExpected) {
		fields["TpsExpected"] = true
		hasWatcher = hasWatcher || s.watcherFlag.HasTpsExpectedWatcher
	}
	if !reflect.DeepEqual(oriTable.Url, curTable.Url) {
		fields["Url"] = true
		hasWatcher = hasWatcher || s.watcherFlag.HasUrlWatcher
	}
	if !reflect.DeepEqual(oriTable.VoterCount, curTable.VoterCount) {
		fields["VoterCount"] = true
		hasWatcher = hasWatcher || s.watcherFlag.HasVoterCountWatcher
	}
	hasWatcher = hasWatcher || s.watcherFlag.WholeWatcher
	return fields, hasWatcher, nil
}
func (s *SoBlockProducerWrap) handleFieldMd(t FieldMdHandleType, so *SoBlockProducer, fields map[string]bool) error {
	if so == nil {
		return errors.New("fail to modify empty table")
	}
	if fields == nil || len(fields) < 1 {
		return nil
	}
	errStr := ""
	if fields["AccountCreateFee"] {
		res := true
		if t == FieldMdHandleTypeCheck {
			res = s.mdFieldAccountCreateFee(so.AccountCreateFee, true, false, false, so)
			errStr = fmt.Sprintf("fail to modify exist value of %v", "AccountCreateFee")
		} else if t == FieldMdHandleTypeDel {
			res = s.mdFieldAccountCreateFee(so.AccountCreateFee, false, true, false, so)
			errStr = fmt.Sprintf("fail to delete  sort or unique field  %v", "AccountCreateFee")
		} else if t == FieldMdHandleTypeInsert {
			res = s.mdFieldAccountCreateFee(so.AccountCreateFee, false, false, true, so)
			errStr = fmt.Sprintf("fail to insert  sort or unique field  %v", "AccountCreateFee")
		}
		if !res {
			return errors.New(errStr)
		}
	}
	if fields["BpVest"] {
		res := true
		if t == FieldMdHandleTypeCheck {
			res = s.mdFieldBpVest(so.BpVest, true, false, false, so)
			errStr = fmt.Sprintf("fail to modify exist value of %v", "BpVest")
		} else if t == FieldMdHandleTypeDel {
			res = s.mdFieldBpVest(so.BpVest, false, true, false, so)
			errStr = fmt.Sprintf("fail to delete  sort or unique field  %v", "BpVest")
		} else if t == FieldMdHandleTypeInsert {
			res = s.mdFieldBpVest(so.BpVest, false, false, true, so)
			errStr = fmt.Sprintf("fail to insert  sort or unique field  %v", "BpVest")
		}
		if !res {
			return errors.New(errStr)
		}
	}
	if fields["CreatedTime"] {
		res := true
		if t == FieldMdHandleTypeCheck {
			res = s.mdFieldCreatedTime(so.CreatedTime, true, false, false, so)
			errStr = fmt.Sprintf("fail to modify exist value of %v", "CreatedTime")
		} else if t == FieldMdHandleTypeDel {
			res = s.mdFieldCreatedTime(so.CreatedTime, false, true, false, so)
			errStr = fmt.Sprintf("fail to delete  sort or unique field  %v", "CreatedTime")
		} else if t == FieldMdHandleTypeInsert {
			res = s.mdFieldCreatedTime(so.CreatedTime, false, false, true, so)
			errStr = fmt.Sprintf("fail to insert  sort or unique field  %v", "CreatedTime")
		}
		if !res {
			return errors.New(errStr)
		}
	}
	if fields["EpochDuration"] {
		res := true
		if t == FieldMdHandleTypeCheck {
			res = s.mdFieldEpochDuration(so.EpochDuration, true, false, false, so)
			errStr = fmt.Sprintf("fail to modify exist value of %v", "EpochDuration")
		} else if t == FieldMdHandleTypeDel {
			res = s.mdFieldEpochDuration(so.EpochDuration, false, true, false, so)
			errStr = fmt.Sprintf("fail to delete  sort or unique field  %v", "EpochDuration")
		} else if t == FieldMdHandleTypeInsert {
			res = s.mdFieldEpochDuration(so.EpochDuration, false, false, true, so)
			errStr = fmt.Sprintf("fail to insert  sort or unique field  %v", "EpochDuration")
		}
		if !res {
			return errors.New(errStr)
		}
	}
	if fields["GenBlockCount"] {
		res := true
		if t == FieldMdHandleTypeCheck {
			res = s.mdFieldGenBlockCount(so.GenBlockCount, true, false, false, so)
			errStr = fmt.Sprintf("fail to modify exist value of %v", "GenBlockCount")
		} else if t == FieldMdHandleTypeDel {
			res = s.mdFieldGenBlockCount(so.GenBlockCount, false, true, false, so)
			errStr = fmt.Sprintf("fail to delete  sort or unique field  %v", "GenBlockCount")
		} else if t == FieldMdHandleTypeInsert {
			res = s.mdFieldGenBlockCount(so.GenBlockCount, false, false, true, so)
			errStr = fmt.Sprintf("fail to insert  sort or unique field  %v", "GenBlockCount")
		}
		if !res {
			return errors.New(errStr)
		}
	}
	if fields["PerTicketPrice"] {
		res := true
		if t == FieldMdHandleTypeCheck {
			res = s.mdFieldPerTicketPrice(so.PerTicketPrice, true, false, false, so)
			errStr = fmt.Sprintf("fail to modify exist value of %v", "PerTicketPrice")
		} else if t == FieldMdHandleTypeDel {
			res = s.mdFieldPerTicketPrice(so.PerTicketPrice, false, true, false, so)
			errStr = fmt.Sprintf("fail to delete  sort or unique field  %v", "PerTicketPrice")
		} else if t == FieldMdHandleTypeInsert {
			res = s.mdFieldPerTicketPrice(so.PerTicketPrice, false, false, true, so)
			errStr = fmt.Sprintf("fail to insert  sort or unique field  %v", "PerTicketPrice")
		}
		if !res {
			return errors.New(errStr)
		}
	}
	if fields["PerTicketWeight"] {
		res := true
		if t == FieldMdHandleTypeCheck {
			res = s.mdFieldPerTicketWeight(so.PerTicketWeight, true, false, false, so)
			errStr = fmt.Sprintf("fail to modify exist value of %v", "PerTicketWeight")
		} else if t == FieldMdHandleTypeDel {
			res = s.mdFieldPerTicketWeight(so.PerTicketWeight, false, true, false, so)
			errStr = fmt.Sprintf("fail to delete  sort or unique field  %v", "PerTicketWeight")
		} else if t == FieldMdHandleTypeInsert {
			res = s.mdFieldPerTicketWeight(so.PerTicketWeight, false, false, true, so)
			errStr = fmt.Sprintf("fail to insert  sort or unique field  %v", "PerTicketWeight")
		}
		if !res {
			return errors.New(errStr)
		}
	}
	if fields["ProposedStaminaFree"] {
		res := true
		if t == FieldMdHandleTypeCheck {
			res = s.mdFieldProposedStaminaFree(so.ProposedStaminaFree, true, false, false, so)
			errStr = fmt.Sprintf("fail to modify exist value of %v", "ProposedStaminaFree")
		} else if t == FieldMdHandleTypeDel {
			res = s.mdFieldProposedStaminaFree(so.ProposedStaminaFree, false, true, false, so)
			errStr = fmt.Sprintf("fail to delete  sort or unique field  %v", "ProposedStaminaFree")
		} else if t == FieldMdHandleTypeInsert {
			res = s.mdFieldProposedStaminaFree(so.ProposedStaminaFree, false, false, true, so)
			errStr = fmt.Sprintf("fail to insert  sort or unique field  %v", "ProposedStaminaFree")
		}
		if !res {
			return errors.New(errStr)
		}
	}
	if fields["SigningKey"] {
		res := true
		if t == FieldMdHandleTypeCheck {
			res = s.mdFieldSigningKey(so.SigningKey, true, false, false, so)
			errStr = fmt.Sprintf("fail to modify exist value of %v", "SigningKey")
		} else if t == FieldMdHandleTypeDel {
			res = s.mdFieldSigningKey(so.SigningKey, false, true, false, so)
			errStr = fmt.Sprintf("fail to delete  sort or unique field  %v", "SigningKey")
		} else if t == FieldMdHandleTypeInsert {
			res = s.mdFieldSigningKey(so.SigningKey, false, false, true, so)
			errStr = fmt.Sprintf("fail to insert  sort or unique field  %v", "SigningKey")
		}
		if !res {
			return errors.New(errStr)
		}
	}
	if fields["TopNAcquireFreeToken"] {
		res := true
		if t == FieldMdHandleTypeCheck {
			res = s.mdFieldTopNAcquireFreeToken(so.TopNAcquireFreeToken, true, false, false, so)
			errStr = fmt.Sprintf("fail to modify exist value of %v", "TopNAcquireFreeToken")
		} else if t == FieldMdHandleTypeDel {
			res = s.mdFieldTopNAcquireFreeToken(so.TopNAcquireFreeToken, false, true, false, so)
			errStr = fmt.Sprintf("fail to delete  sort or unique field  %v", "TopNAcquireFreeToken")
		} else if t == FieldMdHandleTypeInsert {
			res = s.mdFieldTopNAcquireFreeToken(so.TopNAcquireFreeToken, false, false, true, so)
			errStr = fmt.Sprintf("fail to insert  sort or unique field  %v", "TopNAcquireFreeToken")
		}
		if !res {
			return errors.New(errStr)
		}
	}
	if fields["TpsExpected"] {
		res := true
		if t == FieldMdHandleTypeCheck {
			res = s.mdFieldTpsExpected(so.TpsExpected, true, false, false, so)
			errStr = fmt.Sprintf("fail to modify exist value of %v", "TpsExpected")
		} else if t == FieldMdHandleTypeDel {
			res = s.mdFieldTpsExpected(so.TpsExpected, false, true, false, so)
			errStr = fmt.Sprintf("fail to delete  sort or unique field  %v", "TpsExpected")
		} else if t == FieldMdHandleTypeInsert {
			res = s.mdFieldTpsExpected(so.TpsExpected, false, false, true, so)
			errStr = fmt.Sprintf("fail to insert  sort or unique field  %v", "TpsExpected")
		}
		if !res {
			return errors.New(errStr)
		}
	}
	if fields["Url"] {
		res := true
		if t == FieldMdHandleTypeCheck {
			res = s.mdFieldUrl(so.Url, true, false, false, so)
			errStr = fmt.Sprintf("fail to modify exist value of %v", "Url")
		} else if t == FieldMdHandleTypeDel {
			res = s.mdFieldUrl(so.Url, false, true, false, so)
			errStr = fmt.Sprintf("fail to delete  sort or unique field  %v", "Url")
		} else if t == FieldMdHandleTypeInsert {
			res = s.mdFieldUrl(so.Url, false, false, true, so)
			errStr = fmt.Sprintf("fail to insert  sort or unique field  %v", "Url")
		}
		if !res {
			return errors.New(errStr)
		}
	}
	if fields["VoterCount"] {
		res := true
		if t == FieldMdHandleTypeCheck {
			res = s.mdFieldVoterCount(so.VoterCount, true, false, false, so)
			errStr = fmt.Sprintf("fail to modify exist value of %v", "VoterCount")
		} else if t == FieldMdHandleTypeDel {
			res = s.mdFieldVoterCount(so.VoterCount, false, true, false, so)
			errStr = fmt.Sprintf("fail to delete  sort or unique field  %v", "VoterCount")
		} else if t == FieldMdHandleTypeInsert {
			res = s.mdFieldVoterCount(so.VoterCount, false, false, true, so)
			errStr = fmt.Sprintf("fail to insert  sort or unique field  %v", "VoterCount")
		}
		if !res {
			return errors.New(errStr)
		}
	}
	return nil
}
func (s *SoBlockProducerWrap) delSortKeyOwner(sa *SoBlockProducer) bool {
	if s.dba == nil || s.mainKey == nil {
		return false
	}
	val := SoListBlockProducerByOwner{}
	if sa == nil {
		val.Owner = s.GetOwner()
	} else {
		val.Owner = sa.Owner
	}
	subBuf, err := val.OpeEncode()
	if err != nil {
		return false
	}
	ordErr := s.dba.Delete(subBuf)
	return ordErr == nil
}
func (s *SoBlockProducerWrap) insertSortKeyOwner(sa *SoBlockProducer) bool {
	if s.dba == nil || sa == nil {
		return false
	}
	val := SoListBlockProducerByOwner{}
	val.Owner = sa.Owner
	buf, err := proto.Marshal(&val)
	if err != nil {
		return false
	}
	subBuf, err := val.OpeEncode()
	if err != nil {
		return false
	}
	ordErr := s.dba.Put(subBuf, buf)
	return ordErr == nil
}
func (s *SoBlockProducerWrap) delSortKeyBpVest(sa *SoBlockProducer) bool {
	if s.dba == nil || s.mainKey == nil {
		return false
	}
	val := SoListBlockProducerByBpVest{}
	if sa == nil {
		val.BpVest = s.GetBpVest()
		val.Owner = s.mainKey
	} else {
		val.BpVest = sa.BpVest
		val.Owner = sa.Owner
	}
	subBuf, err := val.OpeEncode()
	if err != nil {
		return false
	}
	ordErr := s.dba.Delete(subBuf)
	return ordErr == nil
}
func (s *SoBlockProducerWrap) insertSortKeyBpVest(sa *SoBlockProducer) bool {
	if s.dba == nil || sa == nil {
		return false
	}
	val := SoListBlockProducerByBpVest{}
	val.Owner = sa.Owner
	val.BpVest = sa.BpVest
	buf, err := proto.Marshal(&val)
	if err != nil {
		return false
	}
	subBuf, err := val.OpeEncode()
	if err != nil {
		return false
	}
	ordErr := s.dba.Put(subBuf, buf)
	return ordErr == nil
}
func (s *SoBlockProducerWrap) delAllSortKeys(br bool, val *SoBlockProducer) bool {
	if s.dba == nil {
		return false
	}
	res := true
	if !s.delSortKeyOwner(val) {
		if br {
			return false
		} else {
			res = false
		}
	}
	if !s.delSortKeyBpVest(val) {
		if br {
			return false
		} else {
			res = false
		}
	}
	return res
}
func (s *SoBlockProducerWrap) insertAllSortKeys(val *SoBlockProducer) error {
	if s.dba == nil {
		return errors.New("insert sort Field fail,the db is nil ")
	}
	if val == nil {
		return errors.New("insert sort Field fail,get the SoBlockProducer fail ")
	}
	if !s.insertSortKeyOwner(val) {
		return errors.New("insert sort Field Owner fail while insert table ")
	}
	if !s.insertSortKeyBpVest(val) {
		return errors.New("insert sort Field BpVest fail while insert table ")
	}
	return nil
}