OSDN Git Service

edit code for elegant (#1983)
[bytom/bytom.git] / protocol / vm / introspection.go
index 02580b1..099655f 100644 (file)
@@ -1,10 +1,11 @@
 package vm
 
-import "math"
+import (
+       "github.com/holiman/uint256"
+)
 
 func opCheckOutput(vm *virtualMachine) error {
-       err := vm.applyCost(16)
-       if err != nil {
+       if err := vm.applyCost(16); err != nil {
                return err
        }
 
@@ -12,41 +13,37 @@ func opCheckOutput(vm *virtualMachine) error {
        if err != nil {
                return err
        }
-       vmVersion, err := vm.popInt64(true)
+
+       vmVersion, err := vm.popBigInt(true)
        if err != nil {
                return err
        }
-       if vmVersion < 0 {
-               return ErrBadValue
-       }
+
        assetID, err := vm.pop(true)
        if err != nil {
                return err
        }
-       amount, err := vm.popInt64(true)
+
+       amountInt, err := vm.popBigInt(true)
        if err != nil {
                return err
        }
-       if amount < 0 {
+
+       amount, overflow := amountInt.Uint64WithOverflow()
+       if overflow {
                return ErrBadValue
        }
-       data, err := vm.pop(true)
-       if err != nil {
-               return err
-       }
-       index, err := vm.popInt64(true)
+
+       index, err := vm.popBigInt(true)
        if err != nil {
                return err
        }
-       if index < 0 {
-               return ErrBadValue
-       }
 
        if vm.context.CheckOutput == nil {
                return ErrContext
        }
 
-       ok, err := vm.context.CheckOutput(uint64(index), data, uint64(amount), assetID, uint64(vmVersion), code, vm.expansionReserved)
+       ok, err := vm.context.CheckOutput(uint64(index.Uint64()), amount, assetID, uint64(vmVersion.Uint64()), code, vm.altStack, vm.expansionReserved)
        if err != nil {
                return err
        }
@@ -54,144 +51,74 @@ func opCheckOutput(vm *virtualMachine) error {
 }
 
 func opAsset(vm *virtualMachine) error {
-       err := vm.applyCost(1)
-       if err != nil {
+       if err := vm.applyCost(1); err != nil {
                return err
        }
 
        if vm.context.AssetID == nil {
                return ErrContext
        }
-       return vm.push(*vm.context.AssetID, true)
+       return vm.pushDataStack(*vm.context.AssetID, true)
 }
 
 func opAmount(vm *virtualMachine) error {
-       err := vm.applyCost(1)
-       if err != nil {
+       if err := vm.applyCost(1); err != nil {
                return err
        }
 
        if vm.context.Amount == nil {
                return ErrContext
        }
-       return vm.pushInt64(int64(*vm.context.Amount), true)
-}
-
-func opProgram(vm *virtualMachine) error {
-       err := vm.applyCost(1)
-       if err != nil {
-               return err
-       }
-
-       return vm.push(vm.context.Code, true)
-}
-
-func opMinTime(vm *virtualMachine) error {
-       err := vm.applyCost(1)
-       if err != nil {
-               return err
-       }
-
-       if vm.context.MinTimeMS == nil {
-               return ErrContext
-       }
-       return vm.pushInt64(int64(*vm.context.MinTimeMS), true)
-}
-
-func opMaxTime(vm *virtualMachine) error {
-       err := vm.applyCost(1)
-       if err != nil {
-               return err
-       }
-
-       if vm.context.MaxTimeMS == nil {
-               return ErrContext
-       }
-       maxTimeMS := *vm.context.MaxTimeMS
-       if maxTimeMS == 0 || maxTimeMS > math.MaxInt64 {
-               maxTimeMS = uint64(math.MaxInt64)
-       }
-
-       return vm.pushInt64(int64(maxTimeMS), true)
-}
-
-func opEntryData(vm *virtualMachine) error {
-       err := vm.applyCost(1)
-       if err != nil {
-               return err
-       }
-
-       if vm.context.EntryData == nil {
-               return ErrContext
-       }
 
-       return vm.push(*vm.context.EntryData, true)
+       return vm.pushBigInt(uint256.NewInt().SetUint64(*vm.context.Amount), true)
 }
 
-func opTxData(vm *virtualMachine) error {
-       err := vm.applyCost(1)
-       if err != nil {
+func opProgram(vm *virtualMachine) error {
+       if err := vm.applyCost(1); err != nil {
                return err
        }
 
-       if vm.context.TxData == nil {
-               return ErrContext
-       }
-       return vm.push(*vm.context.TxData, true)
+       return vm.pushDataStack(vm.context.Code, true)
 }
 
 func opIndex(vm *virtualMachine) error {
-       err := vm.applyCost(1)
-       if err != nil {
+       if err := vm.applyCost(1); err != nil {
                return err
        }
 
        if vm.context.DestPos == nil {
                return ErrContext
        }
-       return vm.pushInt64(int64(*vm.context.DestPos), true)
+
+       return vm.pushBigInt(uint256.NewInt().SetUint64(*vm.context.DestPos), true)
 }
 
 func opEntryID(vm *virtualMachine) error {
-       err := vm.applyCost(1)
-       if err != nil {
+       if err := vm.applyCost(1); err != nil {
                return err
        }
-       return vm.push(vm.context.EntryID, true)
+       return vm.pushDataStack(vm.context.EntryID, true)
 }
 
 func opOutputID(vm *virtualMachine) error {
-       err := vm.applyCost(1)
-       if err != nil {
+       if err := vm.applyCost(1); err != nil {
                return err
        }
 
        if vm.context.SpentOutputID == nil {
                return ErrContext
        }
-       return vm.push(*vm.context.SpentOutputID, true)
+       return vm.pushDataStack(*vm.context.SpentOutputID, true)
 }
 
-func opNonce(vm *virtualMachine) error {
-       err := vm.applyCost(1)
-       if err != nil {
+func opBlockHeight(vm *virtualMachine) error {
+       if err := vm.applyCost(1); err != nil {
                return err
        }
 
-       if vm.context.AnchorID == nil {
+       if vm.context.BlockHeight == nil {
                return ErrContext
        }
-       return vm.push(*vm.context.AnchorID, true)
-}
-
-func opBlockHeigh(vm *virtualMachine) error {
-       err := vm.applyCost(1)
-       if err != nil {
-               return err
-       }
 
-       if vm.context.BlockHeigh == nil {
-               return ErrContext
-       }
-       return vm.pushInt64(int64(*vm.context.BlockHeigh), true)
+       return vm.pushBigInt(uint256.NewInt().SetUint64(*vm.context.BlockHeight), true)
 }