github.com/turingchain2020/turingchain@v1.1.21/system/dapp/coins/executor/coinsdb.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package executor 6 7 /* 8 coins 是一个货币的exec。内置货币的执行器。 9 10 主要提供两种操作: 11 EventTransfer -> 转移资产 12 */ 13 14 // package none execer for unknow execer 15 // all none transaction exec ok, execept nofee 16 // nofee transaction will not pack into block 17 18 import ( 19 "fmt" 20 21 dbm "github.com/turingchain2020/turingchain/common/db" 22 "github.com/turingchain2020/turingchain/types" 23 ) 24 25 // calcAddrKey store information on the receiving address 26 func calcAddrKey(addr string) []byte { 27 return []byte(fmt.Sprintf("LODB-coins-Addr:%s", addr)) 28 } 29 30 func geAddrReciverKV(addr string, reciverAmount int64) *types.KeyValue { 31 reciver := &types.Int64{Data: reciverAmount} 32 amountbytes := types.Encode(reciver) 33 kv := &types.KeyValue{Key: calcAddrKey(addr), Value: amountbytes} 34 return kv 35 } 36 37 func getAddrReciver(db dbm.KVDB, addr string) (int64, error) { 38 reciver := types.Int64{} 39 addrReciver, err := db.Get(calcAddrKey(addr)) 40 if err != nil && err != types.ErrNotFound { 41 return 0, err 42 } 43 if len(addrReciver) == 0 { 44 return 0, nil 45 } 46 err = types.Decode(addrReciver, &reciver) 47 if err != nil { 48 return 0, err 49 } 50 return reciver.Data, nil 51 } 52 53 func setAddrReciver(db dbm.KVDB, addr string, reciverAmount int64) error { 54 kv := geAddrReciverKV(addr, reciverAmount) 55 return db.Set(kv.Key, kv.Value) 56 } 57 58 func updateAddrReciver(cachedb dbm.KVDB, addr string, amount int64, isadd bool) (*types.KeyValue, error) { 59 recv, err := getAddrReciver(cachedb, addr) 60 if err != nil && err != types.ErrNotFound { 61 return nil, err 62 } 63 if isadd { 64 recv += amount 65 } else { 66 recv -= amount 67 } 68 err = setAddrReciver(cachedb, addr, recv) 69 if err != nil { 70 return nil, err 71 } 72 //keyvalue 73 return geAddrReciverKV(addr, recv), nil 74 }