Exemplo n.º 1
0
func TestRun2(t *testing.T) {
	ethutil.ReadConfig("")

	db, _ := ethdb.NewMemDatabase()
	state := NewState(ethutil.NewTrie(db, ""))

	script := Compile([]string{
		"PUSH", "0",
		"PUSH", "0",
		"TXSENDER",
		"PUSH", "10000000",
		"MKTX",
	})
	fmt.Println(ethutil.NewValue(script))

	tx := NewTransaction(ContractAddr, ethutil.Big("100000000000000000000000000000000000000000000000000"), script)
	fmt.Printf("contract addr %x\n", tx.Hash()[12:])
	contract := MakeContract(tx, state)
	vm := &Vm{}

	vm.Process(contract, state, RuntimeVars{
		address:     tx.Hash()[12:],
		blockNumber: 1,
		sender:      ethutil.FromHex("cd1722f3947def4cf144679da39c4c32bdc35681"),
		prevHash:    ethutil.FromHex("5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6"),
		coinbase:    ethutil.FromHex("2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"),
		time:        1,
		diff:        big.NewInt(256),
		txValue:     tx.Value,
		txData:      tx.Data,
	})
}
Exemplo n.º 2
0
func initEthereum() *eth.Ethereum {
	// Create flag for datadir folder
	flag.StringVar(&Datadir, "datadir", ".ethdns", "specifies the datadir to use. Takes precedence over config file.")
	flag.StringVar(&Port, "port", "20202", "specifies the Ethereum port to use.")
	flag.StringVar(&DnsPort, "dnsport", "8053", "specifies the DNS port to use.")
	flag.IntVar(&MaxPeer, "maxpeer", 10, "maximum desired peers")

	flag.Parse()

	// Set logging flags
	var lt ethutil.LoggerType
	lt = ethutil.LogFile | ethutil.LogStd

	// Read config if any
	ethutil.ReadConfig(Datadir, lt, nil, "DnsEth")

	// Create Ethereum object
	ethereum, err := eth.New(eth.CapDefault, false)

	if err != nil {
		fmt.Println("Could not start the Ethereum-core:", err)
		return nil
	}
	// Make sure we have an public key to identify ourselves
	utils.CreateKeyPair(false)

	// Set the port
	ethereum.Port = Port

	// Set the max peers
	ethereum.MaxPeers = MaxPeer

	return ethereum
}
Exemplo n.º 3
0
func TestVm(t *testing.T) {
	ethlog.AddLogSystem(ethlog.NewStdLogSystem(os.Stdout, log.LstdFlags, ethlog.LogLevel(4)))

	ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "")

	stateObject := ethstate.NewStateObject([]byte{'j', 'e', 'f', 'f'})
	callerClosure := NewClosure(stateObject, stateObject, []byte{0x60, 0x01}, big.NewInt(1000000), big.NewInt(0))

	vm := New(TestEnv{})
	vm.Verbose = true

	ret, _, e := callerClosure.Call(vm, nil)
	if e != nil {
		fmt.Println("error", e)
	}
	fmt.Println(ret)
}
Exemplo n.º 4
0
func NewTestManager() *TestManager {
	ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "ETH")

	db, err := ethdb.NewMemDatabase()
	if err != nil {
		fmt.Println("Could not create mem-db, failing")
		return nil
	}
	ethutil.Config.Db = db

	testManager := &TestManager{}
	testManager.reactor = ethreact.New()

	testManager.txPool = NewTxPool(testManager)
	testManager.blockChain = NewBlockChain(testManager)
	testManager.stateManager = NewStateManager(testManager)

	// Start the tx pool
	testManager.txPool.Start()

	return testManager
}
Exemplo n.º 5
0
func TestVm(t *testing.T) {
	InitFees()
	ethutil.ReadConfig("")

	db, _ := ethdb.NewMemDatabase()
	ethutil.Config.Db = db
	bm := NewBlockManager(nil)

	block := bm.bc.genesisBlock
	script := Compile([]string{
		"PUSH",
		"1",
		"PUSH",
		"2",
	})
	tx := NewTransaction(ContractAddr, big.NewInt(200000000), script)
	addr := tx.Hash()[12:]
	bm.ApplyTransactions(block, []*Transaction{tx})

	tx2 := NewTransaction(addr, big.NewInt(1e17), nil)
	tx2.Sign([]byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
	bm.ApplyTransactions(block, []*Transaction{tx2})
}
Exemplo n.º 6
0
func TestSnapshot(t *testing.T) {
	db, _ := ethdb.NewMemDatabase()
	ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "")
	ethutil.Config.Db = db

	state := New(ethtrie.New(db, ""))

	stateObject := state.GetOrNewStateObject([]byte("aa"))

	stateObject.SetStorage(ethutil.Big("0"), ethutil.NewValue(42))

	snapshot := state.Copy()

	stateObject = state.GetStateObject([]byte("aa"))
	stateObject.SetStorage(ethutil.Big("0"), ethutil.NewValue(43))

	state.Set(snapshot)

	stateObject = state.GetStateObject([]byte("aa"))
	res := stateObject.GetStorage(ethutil.Big("0"))
	if !res.Cmp(ethutil.NewValue(42)) {
		t.Error("Expected storage 0 to be 42", res)
	}
}
Exemplo n.º 7
0
func main() {
	Init()

	runtime.GOMAXPROCS(runtime.NumCPU())

	ethchain.InitFees()
	ethutil.ReadConfig(DataDir)
	ethutil.Config.Seed = UseSeed

	// Instantiated a eth stack
	ethereum, err := eth.New(eth.CapDefault, UseUPnP)
	if err != nil {
		log.Println("eth start err:", err)
		return
	}
	ethereum.Port = OutboundPort

	if GenAddr {
		fmt.Println("This action overwrites your old private key. Are you sure? (y/n)")

		var r string
		fmt.Scanln(&r)
		for ; ; fmt.Scanln(&r) {
			if r == "n" || r == "y" {
				break
			} else {
				fmt.Printf("Yes or no?", r)
			}
		}

		if r == "y" {
			utils.CreateKeyPair(true)
		}
		os.Exit(0)
	} else {
		if len(ImportKey) > 0 {
			fmt.Println("This action overwrites your old private key. Are you sure? (y/n)")
			var r string
			fmt.Scanln(&r)
			for ; ; fmt.Scanln(&r) {
				if r == "n" || r == "y" {
					break
				} else {
					fmt.Printf("Yes or no?", r)
				}
			}

			if r == "y" {
				utils.ImportPrivateKey(ImportKey)
				os.Exit(0)
			}
		} else {
			utils.CreateKeyPair(false)
		}
	}

	if ExportKey {
		key := ethutil.Config.Db.GetKeys()[0]
		fmt.Printf("%x\n", key.PrivateKey)
		os.Exit(0)
	}

	if ShowGenesis {
		fmt.Println(ethereum.BlockChain().Genesis())
		os.Exit(0)
	}

	log.Printf("Starting Ethereum v%s\n", ethutil.Config.Ver)

	// Set the max peers
	ethereum.MaxPeers = MaxPeer

	if StartConsole {
		err := os.Mkdir(ethutil.Config.ExecPath, os.ModePerm)
		// Error is OK if the error is ErrExist
		if err != nil && !os.IsExist(err) {
			log.Panic("Unable to create EXECPATH:", err)
		}

		console := NewConsole(ethereum)
		go console.Start()
	}

	RegisterInterupts(ethereum)
	ethereum.Start()

	if StartMining {
		log.Printf("Miner started\n")

		// Fake block mining. It broadcasts a new block every 5 seconds
		go func() {
			pow := &ethchain.EasyPow{}
			data, _ := ethutil.Config.Db.Get([]byte("KeyRing"))
			keyRing := ethutil.NewValueFromBytes(data)
			addr := keyRing.Get(1).Bytes()

			for {
				txs := ethereum.TxPool().Flush()
				// Create a new block which we're going to mine
				block := ethereum.BlockChain().NewBlock(addr, txs)
				log.Println("Mining on new block. Includes", len(block.Transactions()), "transactions")
				// Apply all transactions to the block
				ethereum.StateManager().ApplyTransactions(block, block.Transactions())

				ethereum.StateManager().Prepare(block.State(), block.State())
				ethereum.StateManager().AccumelateRewards(block)

				// Search the nonce
				block.Nonce = pow.Search(block)
				ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{block.Value().Val})

				ethereum.StateManager().PrepareDefault(block)
				err := ethereum.StateManager().ProcessBlock(block)
				if err != nil {
					log.Println(err)
				} else {
					log.Println("\n+++++++ MINED BLK +++++++\n", ethereum.BlockChain().CurrentBlock)
					log.Printf("🔨  Mined block %x\n", block.Hash())
				}
			}
		}()
	}

	// Wait for shutdown
	ethereum.WaitForShutdown()
}
Exemplo n.º 8
0
func InitConfig(ConfigFile string, Datadir string, EnvPrefix string) *ethutil.ConfigManager {
	InitDataDir(Datadir)
	return ethutil.ReadConfig(ConfigFile, Datadir, EnvPrefix)
}
Exemplo n.º 9
0
func main() {
	Init()

	qml.Init(nil)

	runtime.GOMAXPROCS(runtime.NumCPU())

	ethchain.InitFees()
	ethutil.ReadConfig(DataDir)
	ethutil.Config.Seed = UseSeed

	// Instantiated a eth stack
	ethereum, err := eth.New(eth.CapDefault, UseUPnP)
	if err != nil {
		log.Println("eth start err:", err)
		return
	}
	ethereum.Port = OutboundPort

	if GenAddr {
		fmt.Println("This action overwrites your old private key. Are you sure? (y/n)")

		var r string
		fmt.Scanln(&r)
		for ; ; fmt.Scanln(&r) {
			if r == "n" || r == "y" {
				break
			} else {
				fmt.Printf("Yes or no?", r)
			}
		}

		if r == "y" {
			utils.CreateKeyPair(true)
		}
		os.Exit(0)
	} else {
		if len(ImportKey) > 0 {
			fmt.Println("This action overwrites your old private key. Are you sure? (y/n)")
			var r string
			fmt.Scanln(&r)
			for ; ; fmt.Scanln(&r) {
				if r == "n" || r == "y" {
					break
				} else {
					fmt.Printf("Yes or no?", r)
				}
			}

			if r == "y" {
				utils.ImportPrivateKey(ImportKey)
				os.Exit(0)
			}
		} else {
			utils.CreateKeyPair(false)
		}
	}

	if ExportKey {
		key := ethutil.Config.Db.GetKeys()[0]
		fmt.Printf("%x\n", key.PrivateKey)
		os.Exit(0)
	}

	if ShowGenesis {
		fmt.Println(ethereum.BlockChain().Genesis())
		os.Exit(0)
	}

	log.Printf("Starting Ethereum GUI v%s\n", ethutil.Config.Ver)

	// Set the max peers
	ethereum.MaxPeers = MaxPeer

	gui := ethui.New(ethereum)
	gui.Start(AssetPath)
}