github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/store/mainchain/extstorages/league_fulltx.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/common/sink" 9 "github.com/sixexorg/magnetic-ring/core/orgchain/types" 10 "github.com/sixexorg/magnetic-ring/store/db" 11 scom "github.com/sixexorg/magnetic-ring/store/mainchain/common" 12 ) 13 14 type ExtFullTXStore struct { 15 dbDir string 16 store *db.LevelDBStore 17 } 18 19 func NewExtFullTX(dbDir string) (*ExtFullTXStore, error) { 20 store, err := db.NewLevelDBStore(dbDir) 21 if err != nil { 22 return nil, err 23 } 24 el := new(ExtFullTXStore) 25 26 el.dbDir = dbDir 27 el.store = store 28 return el, nil 29 } 30 func (this *ExtFullTXStore) GetTx(leagueId common.Address, hash common.Hash) (tx *types.Transaction, height uint64, err error) { 31 key := this.getKey(leagueId, hash) 32 val, err := this.store.Get(key) 33 if err != nil { 34 return nil, 0, err 35 } 36 tx = &types.Transaction{} 37 buff := bytes.NewBuffer(val) 38 err = tx.Deserialize(buff) 39 if err != nil { 40 return nil, 0, err 41 } 42 height, err = serialization.ReadUint64(buff) 43 if err != nil { 44 return nil, 0, err 45 } 46 return tx, height, nil 47 } 48 49 func (this *ExtFullTXStore) SaveTxs(leagueId common.Address, height uint64, txs types.Transactions) error { 50 this.store.NewBatch() 51 for _, v := range txs { 52 key := this.getKey(leagueId, v.Hash()) 53 sk := sink.NewZeroCopySink(nil) 54 err := v.Serialization(sk) 55 if err != nil { 56 return err 57 } 58 sk.WriteUint64(height) 59 this.store.BatchPut(key, sk.Bytes()) 60 } 61 err := this.store.BatchCommit() 62 return err 63 } 64 65 func (this *ExtFullTXStore) getKey(leagueId common.Address, hash common.Hash) []byte { 66 buff := make([]byte, 1+common.AddrLength+common.HashLength) 67 buff[0] = byte(scom.EXT_VOTE_TX) 68 copy(buff[1:common.AddrLength+1], leagueId[:]) 69 copy(buff[common.AddrLength+1:], hash[:]) 70 return buff 71 }