github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/core/vm/common.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:35</date>
    10  //</624342621057191936>
    11  
    12  
    13  package vm
    14  
    15  import (
    16  	"math/big"
    17  
    18  	"github.com/ethereum/go-ethereum/common"
    19  	"github.com/ethereum/go-ethereum/common/math"
    20  )
    21  
    22  //计算步骤所需的内存大小
    23  func calcMemSize(off, l *big.Int) *big.Int {
    24  	if l.Sign() == 0 {
    25  		return common.Big0
    26  	}
    27  
    28  	return new(big.Int).Add(off, l)
    29  }
    30  
    31  //GetData根据起始点、大小和焊盘从数据返回一个切片。
    32  //最大大小为零。此函数是溢出安全的。
    33  func getData(data []byte, start uint64, size uint64) []byte {
    34  	length := uint64(len(data))
    35  	if start > length {
    36  		start = length
    37  	}
    38  	end := start + size
    39  	if end > length {
    40  		end = length
    41  	}
    42  	return common.RightPadBytes(data[start:end], int(size))
    43  }
    44  
    45  //GetDataBig根据开始、大小和焊盘从数据返回一个切片
    46  //最大大小为零。此函数是溢出安全的。
    47  func getDataBig(data []byte, start *big.Int, size *big.Int) []byte {
    48  	dlen := big.NewInt(int64(len(data)))
    49  
    50  	s := math.BigMin(start, dlen)
    51  	e := math.BigMin(new(big.Int).Add(s, size), dlen)
    52  	return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64()))
    53  }
    54  
    55  //bigunt64返回强制转换为uint64的整数,并返回它是否
    56  //过程中溢出。
    57  func bigUint64(v *big.Int) (uint64, bool) {
    58  	return v.Uint64(), v.BitLen() > 64
    59  }
    60  
    61  //TowordSize返回内存扩展所需的接收字大小。
    62  func toWordSize(size uint64) uint64 {
    63  	if size > math.MaxUint64-31 {
    64  		return math.MaxUint64/32 + 1
    65  	}
    66  
    67  	return (size + 31) / 32
    68  }
    69  
    70  func allZero(b []byte) bool {
    71  	for _, byte := range b {
    72  		if byte != 0 {
    73  			return false
    74  		}
    75  	}
    76  	return true
    77  }
    78