Minor update

This commit is contained in:
obscuren 2014-01-08 23:43:20 +01:00
parent 9f42835a02
commit 92b6667bd1
7 changed files with 126 additions and 44 deletions

View File

@ -5,13 +5,16 @@ import (
"os" "os"
"os/signal" "os/signal"
"flag" "flag"
"runtime"
) )
const Debug = true const Debug = true
var StartDBQueryInterface bool var StartDBQueryInterface bool
var StartMining bool
func Init() { func Init() {
flag.BoolVar(&StartDBQueryInterface, "db", false, "start db query interface") flag.BoolVar(&StartDBQueryInterface, "db", false, "start db query interface")
flag.BoolVar(&StartMining, "mine", false, "start dagger mining")
flag.Parse() flag.Parse()
} }
@ -24,7 +27,7 @@ func RegisterInterupts(s *Server) {
signal.Notify(c, os.Interrupt) signal.Notify(c, os.Interrupt)
go func() { go func() {
for sig := range c { for sig := range c {
fmt.Println("Shutting down (%v) ... \n", sig) fmt.Printf("Shutting down (%v) ... \n", sig)
s.Stop() s.Stop()
} }
@ -32,6 +35,8 @@ func RegisterInterupts(s *Server) {
} }
func main() { func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
InitFees() InitFees()
Init() Init()
@ -39,7 +44,25 @@ func main() {
if StartDBQueryInterface { if StartDBQueryInterface {
dbInterface := NewDBInterface() dbInterface := NewDBInterface()
dbInterface.Start() dbInterface.Start()
} else if StartMining {
dagger := &Dagger{}
seed := dagger.Search(BigPow(2, 36))
fmt.Println("dagger res = ", seed)
} else { } else {
Testing() fmt.Println("[DBUG]: Starting Ethereum")
server, err := NewServer()
if err != nil {
fmt.Println("error NewServer:", err)
return
}
RegisterInterupts(server)
server.Start()
// Wait for shutdown
server.WaitForShutdown()
} }
} }

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
) )
/* /*
@ -23,3 +24,11 @@ func (db *MemDatabase) Put(key []byte, value []byte) {
func (db *MemDatabase) Get(key []byte) ([]byte, error) { func (db *MemDatabase) Get(key []byte) ([]byte, error) {
return db.db[string(key)], nil return db.db[string(key)], nil
} }
func (db *MemDatabase) Print() {
for key, val := range db.db {
fmt.Printf("%x(%d):", key, len(key))
decoded := DecodeNode(val)
PrintSlice(decoded)
}
}

View File

@ -5,11 +5,15 @@ import (
"time" "time"
) )
var Db *LDBDatabase
type Server struct { type Server struct {
// Channel for shutting down the server // Channel for shutting down the server
shutdownChan chan bool shutdownChan chan bool
// DB interface // DB interface
db *LDBDatabase db *LDBDatabase
// Block manager for processing new blocks and managing the block chain
blockManager *BlockManager
// Peers (NYI) // Peers (NYI)
peers *list.List peers *list.List
} }
@ -20,8 +24,11 @@ func NewServer() (*Server, error) {
return nil, err return nil, err
} }
Db = db
server := &Server{ server := &Server{
shutdownChan: make(chan bool), shutdownChan: make(chan bool),
blockManager: NewBlockManager(),
db: db, db: db,
peers: list.New(), peers: list.New(),
} }
@ -32,9 +39,11 @@ func NewServer() (*Server, error) {
// Start the server // Start the server
func (s *Server) Start() { func (s *Server) Start() {
// For now this function just blocks the main thread // For now this function just blocks the main thread
for { go func() {
time.Sleep( time.Second ) for {
} time.Sleep( time.Second )
}
}()
} }
func (s *Server) Stop() { func (s *Server) Stop() {

View File

@ -1,7 +1,8 @@
package main package main
/*
import ( import (
"fmt" _"fmt"
) )
// This will eventually go away // This will eventually go away
@ -15,18 +16,17 @@ func Testing() {
tx := NewTransaction("\x00", 20, []string{"PUSH"}) tx := NewTransaction("\x00", 20, []string{"PUSH"})
txData := tx.MarshalRlp() txData := tx.MarshalRlp()
fmt.Printf("%q\n", txData) //fmt.Printf("%q\n", txData)
copyTx := &Transaction{} copyTx := &Transaction{}
copyTx.UnmarshalRlp(txData) copyTx.UnmarshalRlp(txData)
fmt.Println(tx) //fmt.Println(tx)
fmt.Println(copyTx) //fmt.Println(copyTx)
tx2 := NewTransaction("\x00", 20, []string{"SET 10 6", "LD 10 10"}) tx2 := NewTransaction("\x00", 20, []string{"SET 10 6", "LD 10 10"})
blck := CreateTestBlock([]*Transaction{tx2, tx}) blck := CreateTestBlock([]*Transaction{tx2, tx})
bm.ProcessBlock( blck ) bm.ProcessBlock( blck )
fmt.Println("GenesisBlock:", GenisisBlock, "hash", string(GenisisBlock.Hash()))
} }
*/

22
trie.go
View File

@ -36,6 +36,8 @@ func DecodeNode(data []byte) []string {
} }
return strSlice return strSlice
} else {
fmt.Printf("It wasn't a []. It's a %T\n", dec)
} }
return nil return nil
@ -70,16 +72,6 @@ func (t *Trie) Get(key string) string {
* State functions (shouldn't be needed directly). * State functions (shouldn't be needed directly).
*/ */
// Wrapper around the regular db "Put" which generates a key and value
func (t *Trie) Put(node interface{}) []byte {
enc := Encode(node)
sha := Sha256Bin(enc)
t.db.Put([]byte(sha), enc)
return sha
}
// Helper function for printing a node (using fetch, decode and slice printing) // Helper function for printing a node (using fetch, decode and slice printing)
func (t *Trie) PrintNode(n string) { func (t *Trie) PrintNode(n string) {
data, _ := t.db.Get([]byte(n)) data, _ := t.db.Get([]byte(n))
@ -133,6 +125,16 @@ func (t *Trie) UpdateState(node string, key []int, value string) string {
return "" return ""
} }
// Wrapper around the regular db "Put" which generates a key and value
func (t *Trie) Put(node interface{}) []byte {
enc := Encode(node)
var sha []byte
sha = Sha256Bin(enc)
t.db.Put([]byte(sha), enc)
return sha
}
func (t *Trie) InsertState(node string, key []int, value string) string { func (t *Trie) InsertState(node string, key []int, value string) string {
if len(key) == 0 { if len(key) == 0 {

13
util.go
View File

@ -6,6 +6,7 @@ import (
"encoding/hex" "encoding/hex"
_"fmt" _"fmt"
_"math" _"math"
"github.com/obscuren/sha3"
) )
func Uitoa(i uint32) string { func Uitoa(i uint32) string {
@ -24,6 +25,14 @@ func Sha256Bin(data []byte) []byte {
return hash[:] return hash[:]
} }
func Sha3Bin(data []byte) []byte {
d := sha3.NewKeccak224()
d.Reset()
d.Write(data)
return d.Sum(nil)
}
// Helper function for comparing slices // Helper function for comparing slices
func CompareIntSlice(a, b []int) bool { func CompareIntSlice(a, b []int) bool {
if len(a) != len(b) { if len(a) != len(b) {
@ -48,3 +57,7 @@ func MatchingNibbleLength(a, b []int) int {
return i return i
} }
func Hex(d []byte) string {
return hex.EncodeToString(d)
}

72
vm.go
View File

@ -11,29 +11,55 @@ import (
// Op codes // Op codes
const ( const (
oSTOP int = 0x00 oSTOP int = 0x00
oADD int = 0x01 oADD int = 0x01
oMUL int = 0x02 oMUL int = 0x02
oSUB int = 0x03 oSUB int = 0x03
oDIV int = 0x04 oDIV int = 0x04
oSDIV int = 0x05 oSDIV int = 0x05
oMOD int = 0x06 oMOD int = 0x06
oSMOD int = 0x07 oSMOD int = 0x07
oEXP int = 0x08 oEXP int = 0x08
oNEG int = 0x09 oNEG int = 0x09
oLT int = 0x0a oLT int = 0x0a
oLE int = 0x0b oLE int = 0x0b
oGT int = 0x0c oGT int = 0x0c
oGE int = 0x0d oGE int = 0x0d
oEQ int = 0x0e oEQ int = 0x0e
oNOT int = 0x0f oNOT int = 0x0f
oMYADDRESS int = 0x10 oMYADDRESS int = 0x10
oTXSENDER int = 0x11 oTXSENDER int = 0x11
oTXVALUE int = 0x12
oTXFEE int = 0x13
oPUSH int = 0x30 oTXDATAN int = 0x14
oPOP int = 0x31 oTXDATA int = 0x15
oLOAD int = 0x36 oBLK_PREVHASH int = 0x16
oBLK_COINBASE int = 0x17
oBLK_TIMESTAMP int = 0x18
oBLK_NUMBER int = 0x19
oBLK_DIFFICULTY int = 0x1a
oSHA256 int = 0x20
oRIPEMD160 int = 0x21
oECMUL int = 0x22
oECADD int = 0x23
oECSIGN int = 0x24
oECRECOVER int = 0x25
oECVALID int = 0x26
oPUSH int = 0x30
oPOP int = 0x31
oDUP int = 0x32
oDUPN int = 0x33
oSWAP int = 0x34
oSWAPN int = 0x35
oLOAD int = 0x36
oSTORE int = 0x37
oJMP int = 0x40
oJMPI int = 0x41
oIND int = 0x42
oEXTRO int = 0x50
oBALANCE int = 0x51
oMKTX int = 0x60
oSUICIDE int = 0xff
) )
type OpType int type OpType int