aboutsummaryrefslogtreecommitdiffstats
path: root/ethutil
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-04-27 23:15:44 +0800
committerobscuren <geffobscura@gmail.com>2014-04-27 23:16:53 +0800
commit338b6980915c990c6e6287a7249ddd98e6be20eb (patch)
treed5c33a03c473dab1de0daade37a2d12986084faa /ethutil
parent16e52327a4baa5547c38965fce53b3ff40b98173 (diff)
downloaddexon-338b6980915c990c6e6287a7249ddd98e6be20eb.tar.gz
dexon-338b6980915c990c6e6287a7249ddd98e6be20eb.tar.zst
dexon-338b6980915c990c6e6287a7249ddd98e6be20eb.zip
Refactoring and added documentation comments
Diffstat (limited to 'ethutil')
-rw-r--r--ethutil/asm.go (renamed from ethutil/parsing.go)55
-rw-r--r--ethutil/asm_test.go (renamed from ethutil/parsing_test.go)0
-rw-r--r--ethutil/big.go31
-rw-r--r--ethutil/bytes.go13
-rw-r--r--ethutil/common.go12
-rw-r--r--ethutil/config.go9
-rw-r--r--ethutil/rlp.go10
7 files changed, 68 insertions, 62 deletions
diff --git a/ethutil/parsing.go b/ethutil/asm.go
index 514fd92cd..a547d3ac1 100644
--- a/ethutil/parsing.go
+++ b/ethutil/asm.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
-}
diff --git a/ethutil/parsing_test.go b/ethutil/asm_test.go
index a9ad347dd..a9ad347dd 100644
--- a/ethutil/parsing_test.go
+++ b/ethutil/asm_test.go
diff --git a/ethutil/big.go b/ethutil/big.go
index c0488a71f..891d476ad 100644
--- a/ethutil/big.go
+++ b/ethutil/big.go
@@ -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
diff --git a/ethutil/bytes.go b/ethutil/bytes.go
index 40903a5f1..957fa254a 100644
--- a/ethutil/bytes.go
+++ b/ethutil/bytes.go
@@ -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
diff --git a/ethutil/common.go b/ethutil/common.go
index d0ee7b538..983ea5d1b 100644
--- a/ethutil/common.go
+++ b/ethutil/common.go
@@ -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()...)
diff --git a/ethutil/config.go b/ethutil/config.go
index 86c0a855d..323773ba7 100644
--- a/ethutil/config.go
+++ b/ethutil/config.go
@@ -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)
}
diff --git a/ethutil/rlp.go b/ethutil/rlp.go
index d95ace425..69f80a0a6 100644
--- a/ethutil/rlp.go
+++ b/ethutil/rlp.go
@@ -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