github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/store/mainchain/extstorages/league_maintx_used.go (about) 1 package extstorages 2 3 import ( 4 "bytes" 5 6 "github.com/sixexorg/magnetic-ring/common" 7 "github.com/sixexorg/magnetic-ring/common/serialization" 8 "github.com/sixexorg/magnetic-ring/store/db" 9 dbcommon "github.com/sixexorg/magnetic-ring/store/mainchain/common" 10 ) 11 12 type MainTxUsed struct { 13 dbDir string 14 store *db.LevelDBStore 15 } 16 17 func NewMainTxUsed(dbDir string) (*MainTxUsed, error) { 18 store, err := db.NewLevelDBStore(dbDir) 19 if err != nil { 20 return nil, err 21 } 22 el := &MainTxUsed{ 23 dbDir: dbDir, 24 store: store, 25 } 26 return el, nil 27 } 28 29 //NewBatch start a commit batch 30 func (this *MainTxUsed) NewBatch() { 31 this.store.NewBatch() 32 } 33 func (this *MainTxUsed) CommitTo() error { 34 return this.store.BatchCommit() 35 } 36 func (this *MainTxUsed) Exist(txHash common.Hash) bool { 37 key := this.getKey(txHash) 38 bl, err := this.store.Has(key) 39 if err != nil { 40 return false 41 } 42 return bl 43 } 44 45 func (this *MainTxUsed) BatchSave(txHashes common.HashArray, height uint64) error { 46 buff := bytes.NewBuffer(nil) 47 err := serialization.WriteUint64(buff, height) 48 if err != nil { 49 return err 50 } 51 for _, v := range txHashes { 52 key := this.getKey(v) 53 this.store.BatchPut(key, buff.Bytes()) 54 } 55 return nil 56 } 57 58 func (this *MainTxUsed) getKey(txHash common.Hash) []byte { 59 buff := make([]byte, 1+common.HashLength) 60 buff[0] = byte(dbcommon.EXT_LEAGUE_MAIN_USED) 61 copy(buff[1:common.AddrLength+1], txHash[:]) 62 return buff 63 }