2014-02-14 22:56:09 +00:00
|
|
|
package ethutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Op codes
|
2014-02-21 11:36:41 +00:00
|
|
|
var OpCodes = map[string]byte{
|
|
|
|
"STOP": 0,
|
|
|
|
"ADD": 1,
|
|
|
|
"MUL": 2,
|
|
|
|
"SUB": 3,
|
|
|
|
"DIV": 4,
|
|
|
|
"SDIV": 5,
|
|
|
|
"MOD": 6,
|
|
|
|
"SMOD": 7,
|
|
|
|
"EXP": 8,
|
|
|
|
"NEG": 9,
|
|
|
|
"LT": 10,
|
|
|
|
"LE": 11,
|
|
|
|
"GT": 12,
|
|
|
|
"GE": 13,
|
|
|
|
"EQ": 14,
|
|
|
|
"NOT": 15,
|
|
|
|
"MYADDRESS": 16,
|
|
|
|
"TXSENDER": 17,
|
|
|
|
"TXVALUE": 18,
|
|
|
|
"TXFEE": 19,
|
|
|
|
"TXDATAN": 20,
|
|
|
|
"TXDATA": 21,
|
|
|
|
"BLK_PREVHASH": 22,
|
|
|
|
"BLK_COINBASE": 23,
|
|
|
|
"BLK_TIMESTAMP": 24,
|
|
|
|
"BLK_NUMBER": 25,
|
|
|
|
"BLK_DIFFICULTY": 26,
|
|
|
|
"BASEFEE": 27,
|
|
|
|
"SHA256": 32,
|
|
|
|
"RIPEMD160": 33,
|
|
|
|
"ECMUL": 34,
|
|
|
|
"ECADD": 35,
|
|
|
|
"ECSIGN": 36,
|
|
|
|
"ECRECOVER": 37,
|
|
|
|
"ECVALID": 38,
|
|
|
|
"SHA3": 39,
|
|
|
|
"PUSH": 48,
|
|
|
|
"POP": 49,
|
|
|
|
"DUP": 50,
|
|
|
|
"SWAP": 51,
|
|
|
|
"MLOAD": 52,
|
|
|
|
"MSTORE": 53,
|
|
|
|
"SLOAD": 54,
|
|
|
|
"SSTORE": 55,
|
|
|
|
"JMP": 56,
|
|
|
|
"JMPI": 57,
|
|
|
|
"IND": 58,
|
|
|
|
"EXTRO": 59,
|
|
|
|
"BALANCE": 60,
|
|
|
|
"MKTX": 61,
|
|
|
|
"SUICIDE": 62,
|
2014-02-14 22:56:09 +00:00
|
|
|
}
|
|
|
|
|
2014-02-21 11:36:41 +00:00
|
|
|
func IsOpCode(s string) bool {
|
|
|
|
for key, _ := range OpCodes {
|
|
|
|
if key == s {
|
|
|
|
return true
|
|
|
|
}
|
2014-02-14 22:56:09 +00:00
|
|
|
}
|
2014-02-21 11:36:41 +00:00
|
|
|
return false
|
|
|
|
}
|
2014-02-14 22:56:09 +00:00
|
|
|
|
2014-02-21 11:36:41 +00:00
|
|
|
func CompileInstr(s string) ([]byte, error) {
|
|
|
|
isOp := IsOpCode(s)
|
|
|
|
if isOp {
|
|
|
|
return []byte{OpCodes[s]}, nil
|
2014-02-14 22:56:09 +00:00
|
|
|
}
|
|
|
|
|
2014-02-21 11:36:41 +00:00
|
|
|
num := new(big.Int)
|
|
|
|
num.SetString(s, 0)
|
2014-02-14 22:56:09 +00:00
|
|
|
|
2014-02-21 11:36:41 +00:00
|
|
|
return num.Bytes(), nil
|
2014-02-14 22:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Instr(instr string) (int, []string, error) {
|
2014-02-21 11:36:41 +00:00
|
|
|
|
2014-02-14 22:56:09 +00:00
|
|
|
base := new(big.Int)
|
|
|
|
base.SetString(instr, 0)
|
|
|
|
|
|
|
|
args := make([]string, 7)
|
|
|
|
for i := 0; i < 7; i++ {
|
|
|
|
// int(int(val) / int(math.Pow(256,float64(i)))) % 256
|
|
|
|
exp := BigPow(256, i)
|
|
|
|
num := new(big.Int)
|
|
|
|
num.Div(base, exp)
|
|
|
|
|
|
|
|
args[i] = num.Mod(num, big.NewInt(256)).String()
|
|
|
|
}
|
|
|
|
op, _ := strconv.Atoi(args[0])
|
|
|
|
|
|
|
|
return op, args[1:7], nil
|
|
|
|
}
|