MCPcopy Index your code
hub / github.com/CovenantSQL/CovenantSQL / updateBilling

Method updateBilling

blockproducer/metastate.go:910–999  ·  view source on GitHub ↗
(tx *types.UpdateBilling)

Source from the content-addressed store, hash-verified

908}
909
910func (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

Callers 1

applyTransactionMethod · 0.95

Calls 7

loadSQLChainObjectMethod · 0.95
DebugfFunction · 0.92
WithFieldsFunction · 0.92
DatabaseIDMethod · 0.80
WarningMethod · 0.80
WithErrorMethod · 0.80
GetAccountAddressMethod · 0.65

Tested by

no test coverage detected