diff --git a/ethereum.go b/ethereum.go index ac9690e41d..b10415fd1b 100644 --- a/ethereum.go +++ b/ethereum.go @@ -5,13 +5,16 @@ import ( "os" "os/signal" "flag" + "runtime" ) const Debug = true var StartDBQueryInterface bool +var StartMining bool func Init() { flag.BoolVar(&StartDBQueryInterface, "db", false, "start db query interface") + flag.BoolVar(&StartMining, "mine", false, "start dagger mining") flag.Parse() } @@ -24,7 +27,7 @@ func RegisterInterupts(s *Server) { signal.Notify(c, os.Interrupt) go func() { for sig := range c { - fmt.Println("Shutting down (%v) ... \n", sig) + fmt.Printf("Shutting down (%v) ... \n", sig) s.Stop() } @@ -32,6 +35,8 @@ func RegisterInterupts(s *Server) { } func main() { + runtime.GOMAXPROCS(runtime.NumCPU()) + InitFees() Init() @@ -39,7 +44,25 @@ func main() { if StartDBQueryInterface { dbInterface := NewDBInterface() dbInterface.Start() + } else if StartMining { + dagger := &Dagger{} + seed := dagger.Search(BigPow(2, 36)) + + fmt.Println("dagger res = ", seed) } 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() } } diff --git a/memory_database.go b/memory_database.go index fc40f76f33..a8c74bb46a 100644 --- a/memory_database.go +++ b/memory_database.go @@ -1,6 +1,7 @@ package main import ( + "fmt" ) /* @@ -23,3 +24,11 @@ func (db *MemDatabase) Put(key []byte, value []byte) { func (db *MemDatabase) Get(key []byte) ([]byte, error) { 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) + } +} diff --git a/server.go b/server.go index c8fb492d5b..d7718a8a6c 100644 --- a/server.go +++ b/server.go @@ -5,11 +5,15 @@ import ( "time" ) +var Db *LDBDatabase + type Server struct { // Channel for shutting down the server shutdownChan chan bool // DB interface db *LDBDatabase + // Block manager for processing new blocks and managing the block chain + blockManager *BlockManager // Peers (NYI) peers *list.List } @@ -20,8 +24,11 @@ func NewServer() (*Server, error) { return nil, err } + Db = db + server := &Server{ shutdownChan: make(chan bool), + blockManager: NewBlockManager(), db: db, peers: list.New(), } @@ -32,9 +39,11 @@ func NewServer() (*Server, error) { // Start the server func (s *Server) Start() { // For now this function just blocks the main thread - for { - time.Sleep( time.Second ) - } + go func() { + for { + time.Sleep( time.Second ) + } + }() } func (s *Server) Stop() { diff --git a/testing.go b/testing.go index 3762e4dc57..9b7b7b3ce8 100644 --- a/testing.go +++ b/testing.go @@ -1,7 +1,8 @@ package main +/* import ( - "fmt" + _"fmt" ) // This will eventually go away @@ -15,18 +16,17 @@ func Testing() { tx := NewTransaction("\x00", 20, []string{"PUSH"}) txData := tx.MarshalRlp() - fmt.Printf("%q\n", txData) + //fmt.Printf("%q\n", txData) copyTx := &Transaction{} copyTx.UnmarshalRlp(txData) - fmt.Println(tx) - fmt.Println(copyTx) + //fmt.Println(tx) + //fmt.Println(copyTx) tx2 := NewTransaction("\x00", 20, []string{"SET 10 6", "LD 10 10"}) blck := CreateTestBlock([]*Transaction{tx2, tx}) bm.ProcessBlock( blck ) - - fmt.Println("GenesisBlock:", GenisisBlock, "hash", string(GenisisBlock.Hash())) } +*/ diff --git a/trie.go b/trie.go index 442cc08b8b..d66699eeb6 100644 --- a/trie.go +++ b/trie.go @@ -36,6 +36,8 @@ func DecodeNode(data []byte) []string { } return strSlice + } else { + fmt.Printf("It wasn't a []. It's a %T\n", dec) } return nil @@ -70,16 +72,6 @@ func (t *Trie) Get(key string) string { * 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) func (t *Trie) PrintNode(n string) { data, _ := t.db.Get([]byte(n)) @@ -133,6 +125,16 @@ func (t *Trie) UpdateState(node string, key []int, value string) string { 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 { if len(key) == 0 { diff --git a/util.go b/util.go index 512a6f3d79..24e5455f45 100644 --- a/util.go +++ b/util.go @@ -6,6 +6,7 @@ import ( "encoding/hex" _"fmt" _"math" + "github.com/obscuren/sha3" ) func Uitoa(i uint32) string { @@ -24,6 +25,14 @@ func Sha256Bin(data []byte) []byte { return hash[:] } +func Sha3Bin(data []byte) []byte { + d := sha3.NewKeccak224() + d.Reset() + d.Write(data) + + return d.Sum(nil) +} + // Helper function for comparing slices func CompareIntSlice(a, b []int) bool { if len(a) != len(b) { @@ -48,3 +57,7 @@ func MatchingNibbleLength(a, b []int) int { return i } + +func Hex(d []byte) string { + return hex.EncodeToString(d) +} diff --git a/vm.go b/vm.go index a727d58e57..5b70f35442 100644 --- a/vm.go +++ b/vm.go @@ -11,29 +11,55 @@ import ( // Op codes const ( - oSTOP int = 0x00 - oADD int = 0x01 - oMUL int = 0x02 - oSUB int = 0x03 - oDIV int = 0x04 - oSDIV int = 0x05 - oMOD int = 0x06 - oSMOD int = 0x07 - oEXP int = 0x08 - oNEG int = 0x09 - oLT int = 0x0a - oLE int = 0x0b - oGT int = 0x0c - oGE int = 0x0d - oEQ int = 0x0e - oNOT int = 0x0f - oMYADDRESS int = 0x10 - oTXSENDER int = 0x11 - - - oPUSH int = 0x30 - oPOP int = 0x31 - oLOAD int = 0x36 + oSTOP int = 0x00 + oADD int = 0x01 + oMUL int = 0x02 + oSUB int = 0x03 + oDIV int = 0x04 + oSDIV int = 0x05 + oMOD int = 0x06 + oSMOD int = 0x07 + oEXP int = 0x08 + oNEG int = 0x09 + oLT int = 0x0a + oLE int = 0x0b + oGT int = 0x0c + oGE int = 0x0d + oEQ int = 0x0e + oNOT int = 0x0f + oMYADDRESS int = 0x10 + oTXSENDER int = 0x11 + oTXVALUE int = 0x12 + oTXFEE int = 0x13 + oTXDATAN int = 0x14 + oTXDATA int = 0x15 + 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