Refactoring and added documentation comments

pull/150/head
obscuren 11 years ago
parent 16e52327a4
commit 338b698091
  1. 55
      ethutil/asm.go
  2. 0
      ethutil/asm_test.go
  3. 31
      ethutil/big.go
  4. 13
      ethutil/bytes.go
  5. 12
      ethutil/common.go
  6. 9
      ethutil/config.go
  7. 10
      ethutil/rlp.go

@ -79,6 +79,10 @@ var OpCodes = map[string]byte{
"SUICIDE": 0x7f,
}
// Is op code
//
// Check whether the given string matches anything in
// the OpCode list
func IsOpCode(s string) bool {
for key, _ := range OpCodes {
if key == s {
@ -88,6 +92,10 @@ func IsOpCode(s string) bool {
return false
}
// Compile instruction
//
// Attempts to compile and parse the given instruction in "s"
// and returns the byte sequence
func CompileInstr(s interface{}) ([]byte, error) {
switch s.(type) {
case string:
@ -119,8 +127,9 @@ func CompileInstr(s interface{}) ([]byte, error) {
return nil, nil
}
// Script compilation functions
// Compiles strings to machine code
// Assemble
//
// Assembles the given instructions and returns EVM byte code
func Assemble(instructions ...interface{}) (script []byte) {
//script = make([]string, len(instructions))
@ -134,38 +143,22 @@ func Assemble(instructions ...interface{}) (script []byte) {
return
}
/*
Prepocessing function that takes init and main apart:
init() {
// something
}
main() {
// main something
}
// Pre process script
//
// Take data apart and attempt to find the "init" section and
// "main" section. `main { } init { }`
func PreProcess(data string) (mainInput, initInput string) {
reg := "\\(\\)\\s*{([\\d\\w\\W\\n\\s]+?)}"
mainReg := regexp.MustCompile("main" + reg)
initReg := regexp.MustCompile("init" + reg)
main := mainReg.FindStringSubmatch(data)
if len(main) > 0 {
mainInput = main[1]
} else {
mainInput = getCodeSectionFor("main", data)
if mainInput == "" {
mainInput = data
}
init := initReg.FindStringSubmatch(data)
if len(init) > 0 {
initInput = init[1]
}
initInput = getCodeSectionFor("init", data)
return
}
*/
// Very, very dumb parser. Heed no attention :-)
func FindFor(blockMatcher, input string) string {
func getCodeSectionFor(blockMatcher, input string) string {
curCount := -1
length := len(blockMatcher)
matchfst := rune(blockMatcher[0])
@ -198,13 +191,3 @@ func FindFor(blockMatcher, input string) string {
return currStr
}
func PreProcess(data string) (mainInput, initInput string) {
mainInput = FindFor("main", data)
if mainInput == "" {
mainInput = data
}
initInput = FindFor("init", data)
return
}

@ -12,7 +12,9 @@ var BigTrue *big.Int = big.NewInt(1)
// False
var BigFalse *big.Int = big.NewInt(0)
// Returns the power of two integers
// Big pow
//
// Returns the power of two big integers
func BigPow(a, b int) *big.Int {
c := new(big.Int)
c.Exp(big.NewInt(int64(a)), big.NewInt(int64(b)), big.NewInt(0))
@ -20,7 +22,9 @@ func BigPow(a, b int) *big.Int {
return c
}
// Like big.NewInt(uint64); this takes a string instead.
// Big
//
// Shortcut for new(big.Int).SetString(..., 0)
func Big(num string) *big.Int {
n := new(big.Int)
n.SetString(num, 0)
@ -28,7 +32,9 @@ func Big(num string) *big.Int {
return n
}
// Like big.NewInt(uint64); this takes a byte buffer instead.
// BigD
//
// Shortcut for new(big.Int).SetBytes(...)
func BigD(data []byte) *big.Int {
n := new(big.Int)
n.SetBytes(data)
@ -36,21 +42,26 @@ func BigD(data []byte) *big.Int {
return n
}
// Big to bytes
//
// Returns the bytes of a big integer with the size specified by **base**
// Attempts to pad the byte array with zeros.
func BigToBytes(num *big.Int, base int) []byte {
ret := make([]byte, base/8)
return append(ret[:len(ret)-len(num.Bytes())], num.Bytes()...)
}
// Functions like the build in "copy" function
// but works on big integers
func BigCopy(src *big.Int) (ret *big.Int) {
ret = new(big.Int)
ret.Add(ret, src)
return
// Big copy
//
// Creates a copy of the given big integer
func BigCopy(src *big.Int) *big.Int {
return new(big.Int).Set(src)
}
// Big max
//
// Returns the maximum size big integer
func BigMax(x, y *big.Int) *big.Int {
if x.Cmp(y) <= 0 {
return x

@ -6,6 +6,9 @@ import (
"fmt"
)
// Number to bytes
//
// Returns the number in bytes with the specified base
func NumberToBytes(num interface{}, bits int) []byte {
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, num)
@ -16,6 +19,9 @@ func NumberToBytes(num interface{}, bits int) []byte {
return buf.Bytes()[buf.Len()-(bits/8):]
}
// Bytes to number
//
// Attempts to cast a byte slice to a unsigned integer
func BytesToNumber(b []byte) uint64 {
var number uint64
@ -32,7 +38,9 @@ func BytesToNumber(b []byte) uint64 {
return number
}
// Read variable integer in big endian
// Read variable int
//
// Read a variable length number in big endian byte order
func ReadVarint(reader *bytes.Reader) (ret uint64) {
if reader.Len() == 8 {
var num uint64
@ -55,6 +63,9 @@ func ReadVarint(reader *bytes.Reader) (ret uint64) {
return ret
}
// Binary length
//
// Returns the true binary length of the given number
func BinaryLength(num int) int {
if num == 0 {
return 0

@ -5,16 +5,20 @@ import (
"math/big"
)
// The different number of units
var (
Ether = BigPow(10, 18)
Finney = BigPow(10, 15)
Szabo = BigPow(10, 12)
Vito = BigPow(10, 9)
Vita = BigPow(10, 9)
Turing = BigPow(10, 6)
Eins = BigPow(10, 3)
Wei = big.NewInt(1)
)
// Currency to string
//
// Returns a string representing a human readable format
func CurrencyToString(num *big.Int) string {
switch {
case num.Cmp(Ether) >= 0:
@ -23,8 +27,8 @@ func CurrencyToString(num *big.Int) string {
return fmt.Sprintf("%v Finney", new(big.Int).Div(num, Finney))
case num.Cmp(Szabo) >= 0:
return fmt.Sprintf("%v Szabo", new(big.Int).Div(num, Szabo))
case num.Cmp(Vito) >= 0:
return fmt.Sprintf("%v Vito", new(big.Int).Div(num, Vito))
case num.Cmp(Vita) >= 0:
return fmt.Sprintf("%v Vita", new(big.Int).Div(num, Vita))
case num.Cmp(Turing) >= 0:
return fmt.Sprintf("%v Turing", new(big.Int).Div(num, Turing))
case num.Cmp(Eins) >= 0:
@ -34,6 +38,7 @@ func CurrencyToString(num *big.Int) string {
return fmt.Sprintf("%v Wei", num)
}
// Common big integers often used
var (
Big1 = big.NewInt(1)
Big2 = big.NewInt(1)
@ -42,6 +47,7 @@ var (
Big256 = big.NewInt(0xff)
)
// Creates an ethereum address given the bytes and the nonce
func CreateAddress(b []byte, nonce *big.Int) []byte {
addrBytes := append(b, nonce.Bytes()...)

@ -9,6 +9,7 @@ import (
"runtime"
)
// Log types available
type LogType byte
const (
@ -16,7 +17,7 @@ const (
LogTypeFile = 2
)
// Config struct isn't exposed
// Config struct
type config struct {
Db Database
@ -31,7 +32,9 @@ type config struct {
var Config *config
// Read config doesn't read anything yet.
// Read config
//
// Initialize the global Config variable with default settings
func ReadConfig(base string) *config {
if Config == nil {
usr, _ := user.Current()
@ -56,6 +59,8 @@ func ReadConfig(base string) *config {
return Config
}
// Set client string
//
func (c *config) SetClientString(str string) {
Config.ClientString = fmt.Sprintf("%s nv%s/%s", str, c.Ver, runtime.GOOS)
}

@ -26,16 +26,6 @@ func (coder *RlpEncoder) EncodeData(rlpData interface{}) []byte {
return Encode(rlpData)
}
/*
func FromBin(data []byte) uint64 {
if len(data) == 0 {
return 0
}
return FromBin(data[:len(data)-1])*256 + uint64(data[len(data)-1])
}
*/
const (
RlpEmptyList = 0x80
RlpEmptyStr = 0x40

Loading…
Cancel
Save