Refactoring and added documentation comments

This commit is contained in:
obscuren 2014-04-27 17:15:44 +02:00
parent 16e52327a4
commit 338b698091
7 changed files with 68 additions and 62 deletions

View File

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

View File

@ -12,7 +12,9 @@ var BigTrue *big.Int = big.NewInt(1)
// False // False
var BigFalse *big.Int = big.NewInt(0) 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 { func BigPow(a, b int) *big.Int {
c := new(big.Int) c := new(big.Int)
c.Exp(big.NewInt(int64(a)), big.NewInt(int64(b)), big.NewInt(0)) 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 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 { func Big(num string) *big.Int {
n := new(big.Int) n := new(big.Int)
n.SetString(num, 0) n.SetString(num, 0)
@ -28,7 +32,9 @@ func Big(num string) *big.Int {
return n 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 { func BigD(data []byte) *big.Int {
n := new(big.Int) n := new(big.Int)
n.SetBytes(data) n.SetBytes(data)
@ -36,21 +42,26 @@ func BigD(data []byte) *big.Int {
return n 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 { func BigToBytes(num *big.Int, base int) []byte {
ret := make([]byte, base/8) ret := make([]byte, base/8)
return append(ret[:len(ret)-len(num.Bytes())], num.Bytes()...) return append(ret[:len(ret)-len(num.Bytes())], num.Bytes()...)
} }
// Functions like the build in "copy" function // Big copy
// but works on big integers //
func BigCopy(src *big.Int) (ret *big.Int) { // Creates a copy of the given big integer
ret = new(big.Int) func BigCopy(src *big.Int) *big.Int {
ret.Add(ret, src) return new(big.Int).Set(src)
return
} }
// Big max
//
// Returns the maximum size big integer
func BigMax(x, y *big.Int) *big.Int { func BigMax(x, y *big.Int) *big.Int {
if x.Cmp(y) <= 0 { if x.Cmp(y) <= 0 {
return x return x

View File

@ -6,6 +6,9 @@ import (
"fmt" "fmt"
) )
// Number to bytes
//
// Returns the number in bytes with the specified base
func NumberToBytes(num interface{}, bits int) []byte { func NumberToBytes(num interface{}, bits int) []byte {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, num) 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):] 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 { func BytesToNumber(b []byte) uint64 {
var number uint64 var number uint64
@ -32,7 +38,9 @@ func BytesToNumber(b []byte) uint64 {
return number 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) { func ReadVarint(reader *bytes.Reader) (ret uint64) {
if reader.Len() == 8 { if reader.Len() == 8 {
var num uint64 var num uint64
@ -55,6 +63,9 @@ func ReadVarint(reader *bytes.Reader) (ret uint64) {
return ret return ret
} }
// Binary length
//
// Returns the true binary length of the given number
func BinaryLength(num int) int { func BinaryLength(num int) int {
if num == 0 { if num == 0 {
return 0 return 0

View File

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

View File

@ -9,6 +9,7 @@ import (
"runtime" "runtime"
) )
// Log types available
type LogType byte type LogType byte
const ( const (
@ -16,7 +17,7 @@ const (
LogTypeFile = 2 LogTypeFile = 2
) )
// Config struct isn't exposed // Config struct
type config struct { type config struct {
Db Database Db Database
@ -31,7 +32,9 @@ type config struct {
var Config *config 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 { func ReadConfig(base string) *config {
if Config == nil { if Config == nil {
usr, _ := user.Current() usr, _ := user.Current()
@ -56,6 +59,8 @@ func ReadConfig(base string) *config {
return Config return Config
} }
// Set client string
//
func (c *config) SetClientString(str string) { func (c *config) SetClientString(str string) {
Config.ClientString = fmt.Sprintf("%s nv%s/%s", str, c.Ver, runtime.GOOS) Config.ClientString = fmt.Sprintf("%s nv%s/%s", str, c.Ver, runtime.GOOS)
} }

View File

@ -26,16 +26,6 @@ func (coder *RlpEncoder) EncodeData(rlpData interface{}) []byte {
return Encode(rlpData) 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 ( const (
RlpEmptyList = 0x80 RlpEmptyList = 0x80
RlpEmptyStr = 0x40 RlpEmptyStr = 0x40