OSDN Git Service

add free gas (#295)
[bytom/vapor.git] / protocol / validation / tx.go
index 9364455..d360854 100644 (file)
@@ -58,12 +58,15 @@ func (g *GasState) setGas(BTMValue int64, txSize int64) error {
        }
 
        g.BTMValue = uint64(BTMValue)
-
        var ok bool
        if g.GasLeft, ok = checked.DivInt64(BTMValue, consensus.ActiveNetParams.VMGasRate); !ok {
                return errors.Wrap(ErrGasCalculate, "setGas calc gas amount")
        }
 
+       if g.GasLeft, ok = checked.AddInt64(g.GasLeft, consensus.ActiveNetParams.DefaultGasCredit); !ok {
+               return errors.Wrap(ErrGasCalculate, "setGas calc free gas")
+       }
+
        if g.GasLeft > consensus.ActiveNetParams.MaxGasAmount {
                g.GasLeft = consensus.ActiveNetParams.MaxGasAmount
        }
@@ -172,16 +175,19 @@ func checkValid(vs *validationState, e bc.Entry) (err error) {
                        parity[*dest.Value.AssetId] = diff
                }
 
+               btmAmount := int64(0)
                for assetID, amount := range parity {
                        if assetID == *consensus.BTMAssetID {
-                               if err = vs.gasStatus.setGas(amount, int64(vs.tx.SerializedSize)); err != nil {
-                                       return err
-                               }
+                               btmAmount = amount
                        } else if amount != 0 {
                                return errors.WithDetailf(ErrUnbalanced, "asset %x sources - destinations = %d (should be 0)", assetID.Bytes(), amount)
                        }
                }
 
+               if err = vs.gasStatus.setGas(btmAmount, int64(vs.tx.SerializedSize)); err != nil {
+                       return err
+               }
+
                for _, BTMInputID := range vs.tx.GasInputIDs {
                        e, ok := vs.tx.Entries[BTMInputID]
                        if !ok {