github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/store/mainchain/storages/league_birth_cert.go (about)

     1  package storages
     2  
     3  import (
     4  	"github.com/sixexorg/magnetic-ring/common"
     5  	"github.com/sixexorg/magnetic-ring/store/db"
     6  	mcom "github.com/sixexorg/magnetic-ring/store/mainchain/common"
     7  )
     8  
     9  type LeagueBirthCertStore struct {
    10  	dbDir string
    11  	store *db.LevelDBStore
    12  }
    13  
    14  func NewLeagueBirthCertStore(dbDir string) (*LeagueBirthCertStore, error) {
    15  	var err error
    16  	store, err := db.NewLevelDBStore(dbDir)
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  	model := &LeagueBirthCertStore{
    21  		dbDir: dbDir,
    22  		store: store,
    23  	}
    24  	return model, nil
    25  }
    26  func (this *LeagueBirthCertStore) NewBatch() {
    27  	this.store.NewBatch()
    28  }
    29  func (this *LeagueBirthCertStore) CommitTo() error {
    30  	return this.store.BatchCommit()
    31  }
    32  func (this *LeagueBirthCertStore) BatchPutBirthCert(leagueId common.Address, txHash common.Hash) {
    33  	key := this.getKey(leagueId)
    34  	this.store.BatchPut(key, txHash.ToBytes())
    35  }
    36  func (this *LeagueBirthCertStore) GetBirthCert(leagueId common.Address) (common.Hash, error) {
    37  	key := this.getKey(leagueId)
    38  	val, err := this.store.Get(key)
    39  	if err != nil {
    40  		return common.Hash{}, err
    41  	}
    42  	return common.ParseHashFromBytes(val)
    43  }
    44  func (this *LeagueBirthCertStore) getKey(leagueId common.Address) []byte {
    45  	buff := make([]byte, 1+common.AddrLength)
    46  	buff[0] = byte(mcom.LEAGUE_BIRTH_CERT)
    47  	copy(buff[1:common.AddrLength+1], leagueId[:])
    48  	return buff
    49  }