(tx *types.UpdateBilling)
| 908 | } |
| 909 | |
| 910 | func (s *metaState) updateBilling(tx *types.UpdateBilling) (err error) { |
| 911 | newProfile, loaded := s.loadSQLChainObject(tx.Receiver.DatabaseID()) |
| 912 | if !loaded { |
| 913 | err = errors.Wrap(ErrDatabaseNotFound, "update billing failed") |
| 914 | return |
| 915 | } |
| 916 | |
| 917 | if tx.Version > 0 && (tx.Range.From >= tx.Range.To || newProfile.LastUpdatedHeight != tx.Range.From) { |
| 918 | err = errors.Wrapf(ErrInvalidRange, |
| 919 | "update billing within range %d:(%d, %d]", |
| 920 | newProfile.LastUpdatedHeight, tx.Range.From, tx.Range.To) |
| 921 | return |
| 922 | } |
| 923 | log.Debugf("update billing addr: %s, user: %d, tx: %v", tx.GetAccountAddress(), len(tx.Users), tx) |
| 924 | |
| 925 | if newProfile.GasPrice == 0 { |
| 926 | return |
| 927 | } |
| 928 | |
| 929 | var ( |
| 930 | costMap = make(map[proto.AccountAddress]uint64) |
| 931 | userMap = make(map[proto.AccountAddress]map[proto.AccountAddress]uint64) |
| 932 | minerAddr = tx.GetAccountAddress() |
| 933 | isMiner = false |
| 934 | ) |
| 935 | for _, miner := range newProfile.Miners { |
| 936 | isMiner = isMiner || (miner.Address == minerAddr) |
| 937 | miner.ReceivedIncome += miner.PendingIncome |
| 938 | miner.PendingIncome = 0 |
| 939 | } |
| 940 | if !isMiner { |
| 941 | err = ErrInvalidSender |
| 942 | log.WithFields(log.Fields{ |
| 943 | "miner_addr": minerAddr, |
| 944 | "miners": newProfile.Miners, |
| 945 | }).WithError(err).Warning("sender does not exists in sqlchain (updateBilling)") |
| 946 | return |
| 947 | } |
| 948 | |
| 949 | for _, userCost := range tx.Users { |
| 950 | log.Debugf("update billing user cost: %s, cost: %d", userCost.User, userCost.Cost) |
| 951 | costMap[userCost.User] = userCost.Cost |
| 952 | if _, ok := userMap[userCost.User]; !ok { |
| 953 | userMap[userCost.User] = make(map[proto.AccountAddress]uint64) |
| 954 | } |
| 955 | for _, minerIncome := range userCost.Miners { |
| 956 | userMap[userCost.User][minerIncome.Miner] += minerIncome.Income |
| 957 | } |
| 958 | } |
| 959 | for _, user := range newProfile.Users { |
| 960 | if user.AdvancePayment >= costMap[user.Address]*newProfile.GasPrice { |
| 961 | user.AdvancePayment -= costMap[user.Address] * newProfile.GasPrice |
| 962 | for _, miner := range newProfile.Miners { |
| 963 | miner.PendingIncome += userMap[user.Address][miner.Address] * newProfile.GasPrice |
| 964 | } |
| 965 | } else { |
| 966 | rate := float64(user.AdvancePayment) / float64(costMap[user.Address]*newProfile.GasPrice) |
| 967 | user.AdvancePayment = 0 |
no test coverage detected