aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cmd/evm/main.go4
-rw-r--r--cmd/geth/js.go17
-rw-r--r--cmd/geth/main.go5
-rw-r--r--cmd/utils/cmd.go13
-rw-r--r--cmd/utils/flags.go17
-rw-r--r--core/execution.go9
-rw-r--r--core/genesis.go3
-rw-r--r--core/vm/environment.go2
-rw-r--r--core/vm/jit_test.go4
-rw-r--r--core/vm_env.go4
-rw-r--r--crypto/crypto.go15
-rw-r--r--crypto/crypto_test.go181
-rw-r--r--tests/util.go6
13 files changed, 206 insertions, 74 deletions
diff --git a/cmd/evm/main.go b/cmd/evm/main.go
index bf24da982..e170dc190 100644
--- a/cmd/evm/main.go
+++ b/cmd/evm/main.go
@@ -217,8 +217,8 @@ func (self *VMEnv) AddLog(log *vm.Log) {
func (self *VMEnv) CanTransfer(from common.Address, balance *big.Int) bool {
return self.state.GetBalance(from).Cmp(balance) >= 0
}
-func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
- return core.Transfer(from, to, amount)
+func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) {
+ core.Transfer(from, to, amount)
}
func (self *VMEnv) Call(caller vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
diff --git a/cmd/geth/js.go b/cmd/geth/js.go
index 3e3600705..b5ec82b57 100644
--- a/cmd/geth/js.go
+++ b/cmd/geth/js.go
@@ -145,7 +145,7 @@ func apiWordCompleter(line string, pos int) (head string, completions []string,
return begin, completionWords, end
}
-func newLightweightJSRE(libPath string, client comms.EthereumClient, interactive bool) *jsre {
+func newLightweightJSRE(libPath string, client comms.EthereumClient, datadir string, interactive bool) *jsre {
js := &jsre{ps1: "> "}
js.wait = make(chan *big.Int)
js.client = client
@@ -161,14 +161,14 @@ func newLightweightJSRE(libPath string, client comms.EthereumClient, interactive
js.prompter = dumbterm{bufio.NewReader(os.Stdin)}
} else {
lr := liner.NewLiner()
- js.withHistory(func(hist *os.File) { lr.ReadHistory(hist) })
+ js.withHistory(datadir, func(hist *os.File) { lr.ReadHistory(hist) })
lr.SetCtrlCAborts(true)
js.loadAutoCompletion()
lr.SetWordCompleter(apiWordCompleter)
lr.SetTabCompletionStyle(liner.TabPrints)
js.prompter = lr
js.atexit = func() {
- js.withHistory(func(hist *os.File) { hist.Truncate(0); lr.WriteHistory(hist) })
+ js.withHistory(datadir, func(hist *os.File) { hist.Truncate(0); lr.WriteHistory(hist) })
lr.Close()
close(js.wait)
}
@@ -203,14 +203,14 @@ func newJSRE(ethereum *eth.Ethereum, libPath, corsDomain string, client comms.Et
js.prompter = dumbterm{bufio.NewReader(os.Stdin)}
} else {
lr := liner.NewLiner()
- js.withHistory(func(hist *os.File) { lr.ReadHistory(hist) })
+ js.withHistory(ethereum.DataDir, func(hist *os.File) { lr.ReadHistory(hist) })
lr.SetCtrlCAborts(true)
js.loadAutoCompletion()
lr.SetWordCompleter(apiWordCompleter)
lr.SetTabCompletionStyle(liner.TabPrints)
js.prompter = lr
js.atexit = func() {
- js.withHistory(func(hist *os.File) { hist.Truncate(0); lr.WriteHistory(hist) })
+ js.withHistory(ethereum.DataDir, func(hist *os.File) { hist.Truncate(0); lr.WriteHistory(hist) })
lr.Close()
close(js.wait)
}
@@ -433,12 +433,7 @@ func hidepassword(input string) string {
}
}
-func (self *jsre) withHistory(op func(*os.File)) {
- datadir := common.DefaultDataDir()
- if self.ethereum != nil {
- datadir = self.ethereum.DataDir
- }
-
+func (self *jsre) withHistory(datadir string, op func(*os.File)) {
hist, err := os.OpenFile(filepath.Join(datadir, "history"), os.O_RDWR|os.O_CREATE, os.ModePerm)
if err != nil {
fmt.Printf("unable to open history file: %v\n", err)
diff --git a/cmd/geth/main.go b/cmd/geth/main.go
index 2c4cb5ae1..1eb078201 100644
--- a/cmd/geth/main.go
+++ b/cmd/geth/main.go
@@ -341,6 +341,7 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
}
app.Before = func(ctx *cli.Context) error {
utils.SetupLogger(ctx)
+ utils.SetupNetwork(ctx)
utils.SetupVM(ctx)
utils.SetupEth(ctx)
if ctx.GlobalBool(utils.PProfEanbledFlag.Name) {
@@ -392,9 +393,6 @@ func makeDefaultExtra() []byte {
func run(ctx *cli.Context) {
utils.CheckLegalese(utils.MustDataDir(ctx))
- if ctx.GlobalBool(utils.OlympicFlag.Name) {
- utils.InitOlympic()
- }
cfg := utils.MakeEthConfig(ClientIdentifier, nodeNameVersion, ctx)
cfg.ExtraData = makeExtra(ctx)
@@ -430,6 +428,7 @@ func attach(ctx *cli.Context) {
repl := newLightweightJSRE(
ctx.GlobalString(utils.JSpathFlag.Name),
client,
+ ctx.GlobalString(utils.DataDirFlag.Name),
true,
)
diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go
index 5e4bfc937..1fbd96dc8 100644
--- a/cmd/utils/cmd.go
+++ b/cmd/utils/cmd.go
@@ -21,8 +21,6 @@ import (
"bufio"
"fmt"
"io"
- "math"
- "math/big"
"os"
"os/signal"
"regexp"
@@ -34,7 +32,6 @@ import (
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
- "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/peterh/liner"
)
@@ -146,16 +143,6 @@ func StartEthereum(ethereum *eth.Ethereum) {
}()
}
-func InitOlympic() {
- params.DurationLimit = big.NewInt(8)
- params.GenesisGasLimit = big.NewInt(3141592)
- params.MinGasLimit = big.NewInt(125000)
- params.MaximumExtraDataSize = big.NewInt(1024)
- NetworkIdFlag.Value = 0
- core.BlockReward = big.NewInt(1.5e+18)
- core.ExpDiffPeriod = big.NewInt(math.MaxInt64)
-}
-
func FormatTransactionData(data string) []byte {
d := common.StringToByteFunc(data, func(s string) (ret []byte) {
slice := regexp.MustCompile("\\n|\\s").Split(s, 1000000000)
diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go
index 67f8f0c57..53faeefdc 100644
--- a/cmd/utils/flags.go
+++ b/cmd/utils/flags.go
@@ -20,6 +20,7 @@ import (
"crypto/ecdsa"
"fmt"
"log"
+ "math"
"math/big"
"net"
"net/http"
@@ -42,6 +43,7 @@ import (
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/p2p/nat"
+ "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc/api"
"github.com/ethereum/go-ethereum/rpc/codec"
"github.com/ethereum/go-ethereum/rpc/comms"
@@ -503,6 +505,20 @@ func SetupLogger(ctx *cli.Context) {
glog.SetLogDir(ctx.GlobalString(LogFileFlag.Name))
}
+// SetupNetwork configures the system for either the main net or some test network.
+func SetupNetwork(ctx *cli.Context) {
+ switch {
+ case ctx.GlobalBool(OlympicFlag.Name):
+ params.DurationLimit = big.NewInt(8)
+ params.GenesisGasLimit = big.NewInt(3141592)
+ params.MinGasLimit = big.NewInt(125000)
+ params.MaximumExtraDataSize = big.NewInt(1024)
+ NetworkIdFlag.Value = 0
+ core.BlockReward = big.NewInt(1.5e+18)
+ core.ExpDiffPeriod = big.NewInt(math.MaxInt64)
+ }
+}
+
// SetupVM configured the VM package's global settings
func SetupVM(ctx *cli.Context) {
vm.EnableJit = ctx.GlobalBool(VMEnableJitFlag.Name)
@@ -532,7 +548,6 @@ func MakeChain(ctx *cli.Context) (chain *core.BlockChain, chainDb ethdb.Database
Fatalf("Could not open database: %v", err)
}
if ctx.GlobalBool(OlympicFlag.Name) {
- InitOlympic()
_, err := core.WriteTestNetGenesisBlock(chainDb, 42)
if err != nil {
glog.Fatalln(err)
diff --git a/core/execution.go b/core/execution.go
index e3c00a2ea..fd8464f6e 100644
--- a/core/execution.go
+++ b/core/execution.go
@@ -17,7 +17,6 @@
package core
import (
- "errors"
"math/big"
"github.com/ethereum/go-ethereum/common"
@@ -108,13 +107,7 @@ func exec(env vm.Environment, caller vm.ContractRef, address, codeAddr *common.A
}
// generic transfer method
-func Transfer(from, to vm.Account, amount *big.Int) error {
- if from.Balance().Cmp(amount) < 0 {
- return errors.New("Insufficient balance in account")
- }
-
+func Transfer(from, to vm.Account, amount *big.Int) {
from.SubBalance(amount)
to.AddBalance(amount)
-
- return nil
}
diff --git a/core/genesis.go b/core/genesis.go
index bfa8696af..4c5c17f60 100644
--- a/core/genesis.go
+++ b/core/genesis.go
@@ -102,6 +102,9 @@ func WriteGenesisBlock(chainDb ethdb.Database, reader io.Reader) (*types.Block,
if err := WriteBlock(chainDb, block); err != nil {
return nil, err
}
+ if err := PutBlockReceipts(chainDb, block, nil); err != nil {
+ return nil, err
+ }
if err := WriteCanonicalHash(chainDb, block.Hash(), block.NumberU64()); err != nil {
return nil, err
}
diff --git a/core/vm/environment.go b/core/vm/environment.go
index f8e19baea..ec739b26c 100644
--- a/core/vm/environment.go
+++ b/core/vm/environment.go
@@ -51,7 +51,7 @@ type Environment interface {
// Determines whether it's possible to transact
CanTransfer(from common.Address, balance *big.Int) bool
// Transfers amount from one account to the other
- Transfer(from, to Account, amount *big.Int) error
+ Transfer(from, to Account, amount *big.Int)
// Adds a LOG to the state
AddLog(*Log)
// Adds a structured log to the env
diff --git a/core/vm/jit_test.go b/core/vm/jit_test.go
index 8c45f2ce7..cb09e179d 100644
--- a/core/vm/jit_test.go
+++ b/core/vm/jit_test.go
@@ -152,9 +152,7 @@ func (self *Env) SetDepth(i int) { self.depth = i }
func (self *Env) CanTransfer(from common.Address, balance *big.Int) bool {
return true
}
-func (self *Env) Transfer(from, to Account, amount *big.Int) error {
- return nil
-}
+func (self *Env) Transfer(from, to Account, amount *big.Int) {}
func (self *Env) Call(caller ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
return nil, nil
}
diff --git a/core/vm_env.go b/core/vm_env.go
index 467e34c6b..715fde52f 100644
--- a/core/vm_env.go
+++ b/core/vm_env.go
@@ -81,8 +81,8 @@ func (self *VMEnv) SetSnapshot(copy vm.Database) {
self.state.Set(copy.(*state.StateDB))
}
-func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
- return Transfer(from, to, amount)
+func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) {
+ Transfer(from, to, amount)
}
func (self *VMEnv) Call(me vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
diff --git a/crypto/crypto.go b/crypto/crypto.go
index b3a8d730b..272050106 100644
--- a/crypto/crypto.go
+++ b/crypto/crypto.go
@@ -172,10 +172,10 @@ func GenerateKey() (*ecdsa.PrivateKey, error) {
}
func ValidateSignatureValues(v byte, r, s *big.Int) bool {
- vint := uint32(v)
- if r.Cmp(common.Big0) == 0 || s.Cmp(common.Big0) == 0 {
+ if r.Cmp(common.Big1) < 0 || s.Cmp(common.Big1) < 0 {
return false
}
+ vint := uint32(v)
if r.Cmp(secp256k1n) < 0 && s.Cmp(secp256k1n) < 0 && (vint == 27 || vint == 28) {
return true
} else {
@@ -302,17 +302,6 @@ func aesCBCDecrypt(key, cipherText, iv []byte) ([]byte, error) {
}
// From https://leanpub.com/gocrypto/read#leanpub-auto-block-cipher-modes
-func PKCS7Pad(in []byte) []byte {
- padding := 16 - (len(in) % 16)
- if padding == 0 {
- padding = 16
- }
- for i := 0; i < padding; i++ {
- in = append(in, byte(padding))
- }
- return in
-}
-
func PKCS7Unpad(in []byte) []byte {
if len(in) == 0 {
return nil
diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go
index b891f41a9..fdd9c1ee8 100644
--- a/crypto/crypto_test.go
+++ b/crypto/crypto_test.go
@@ -18,8 +18,12 @@ package crypto
import (
"bytes"
+ "crypto/ecdsa"
"encoding/hex"
"fmt"
+ "io/ioutil"
+ "math/big"
+ "os"
"testing"
"time"
@@ -27,10 +31,12 @@ import (
"github.com/ethereum/go-ethereum/crypto/secp256k1"
)
+var testAddrHex = "970e8128ab834e8eac17ab8e3812f010678cf791"
+var testPrivHex = "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032"
+
// These tests are sanity checks.
// They should ensure that we don't e.g. use Sha3-224 instead of Sha3-256
// and that the sha3 library uses keccak-f permutation.
-
func TestSha3(t *testing.T) {
msg := []byte("abc")
exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
@@ -55,13 +61,6 @@ func TestRipemd160(t *testing.T) {
checkhash(t, "Ripemd160", Ripemd160, msg, exp)
}
-func checkhash(t *testing.T, name string, f func([]byte) []byte, msg, exp []byte) {
- sum := f(msg)
- if bytes.Compare(exp, sum) != 0 {
- t.Errorf("hash %s returned wrong result.\ngot: %x\nwant: %x", name, sum, exp)
- }
-}
-
func BenchmarkSha3(b *testing.B) {
a := []byte("hello world")
amount := 1000000
@@ -74,13 +73,41 @@ func BenchmarkSha3(b *testing.B) {
}
func Test0Key(t *testing.T) {
- t.Skip()
- key := common.Hex2Bytes("1111111111111111111111111111111111111111111111111111111111111111")
+ key := common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")
+ _, err := secp256k1.GeneratePubKey(key)
+ if err == nil {
+ t.Errorf("expected error due to zero privkey")
+ }
+}
+
+func TestSign(t *testing.T) {
+ key, _ := HexToECDSA(testPrivHex)
+ addr := common.HexToAddress(testAddrHex)
+
+ msg := Sha3([]byte("foo"))
+ sig, err := Sign(msg, key)
+ if err != nil {
+ t.Errorf("Sign error: %s", err)
+ }
+ recoveredPub, err := Ecrecover(msg, sig)
+ if err != nil {
+ t.Errorf("ECRecover error: %s", err)
+ }
+ recoveredAddr := PubkeyToAddress(*ToECDSAPub(recoveredPub))
+ if addr != recoveredAddr {
+ t.Errorf("Address mismatch: want: %x have: %x", addr, recoveredAddr)
+ }
+
+ // should be equal to SigToPub
+ recoveredPub2, err := SigToPub(msg, sig)
+ if err != nil {
+ t.Errorf("ECRecover error: %s", err)
+ }
+ recoveredAddr2 := PubkeyToAddress(*recoveredPub2)
+ if addr != recoveredAddr2 {
+ t.Errorf("Address mismatch: want: %x have: %x", addr, recoveredAddr2)
+ }
- p, err := secp256k1.GeneratePubKey(key)
- addr := Sha3(p[1:])[12:]
- fmt.Printf("%x\n", p)
- fmt.Printf("%v %x\n", err, addr)
}
func TestInvalidSign(t *testing.T) {
@@ -94,3 +121,129 @@ func TestInvalidSign(t *testing.T) {
t.Errorf("expected sign with hash 33 byte to error")
}
}
+
+func TestNewContractAddress(t *testing.T) {
+ key, _ := HexToECDSA(testPrivHex)
+ addr := common.HexToAddress(testAddrHex)
+ genAddr := PubkeyToAddress(key.PublicKey)
+ // sanity check before using addr to create contract address
+ checkAddr(t, genAddr, addr)
+
+ caddr0 := CreateAddress(addr, 0)
+ caddr1 := CreateAddress(addr, 1)
+ caddr2 := CreateAddress(addr, 2)
+ checkAddr(t, common.HexToAddress("333c3310824b7c685133f2bedb2ca4b8b4df633d"), caddr0)
+ checkAddr(t, common.HexToAddress("8bda78331c916a08481428e4b07c96d3e916d165"), caddr1)
+ checkAddr(t, common.HexToAddress("c9ddedf451bc62ce88bf9292afb13df35b670699"), caddr2)
+}
+
+func TestLoadECDSAFile(t *testing.T) {
+ keyBytes := common.FromHex(testPrivHex)
+ fileName0 := "test_key0"
+ fileName1 := "test_key1"
+ checkKey := func(k *ecdsa.PrivateKey) {
+ checkAddr(t, PubkeyToAddress(k.PublicKey), common.HexToAddress(testAddrHex))
+ loadedKeyBytes := FromECDSA(k)
+ if !bytes.Equal(loadedKeyBytes, keyBytes) {
+ t.Fatalf("private key mismatch: want: %x have: %x", keyBytes, loadedKeyBytes)
+ }
+ }
+
+ ioutil.WriteFile(fileName0, []byte(testPrivHex), 0600)
+ defer os.Remove(fileName0)
+
+ key0, err := LoadECDSA(fileName0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ checkKey(key0)
+
+ // again, this time with SaveECDSA instead of manual save:
+ err = SaveECDSA(fileName1, key0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.Remove(fileName1)
+
+ key1, err := LoadECDSA(fileName1)
+ if err != nil {
+ t.Fatal(err)
+ }
+ checkKey(key1)
+}
+
+func TestValidateSignatureValues(t *testing.T) {
+ check := func(expected bool, v byte, r, s *big.Int) {
+ if ValidateSignatureValues(v, r, s) != expected {
+ t.Errorf("mismatch for v: %d r: %d s: %d want: %v", v, r, s, expected)
+ }
+ }
+ minusOne := big.NewInt(-1)
+ one := common.Big1
+ zero := common.Big0
+ secp256k1nMinus1 := new(big.Int).Sub(secp256k1n, common.Big1)
+
+ // correct v,r,s
+ check(true, 27, one, one)
+ check(true, 28, one, one)
+ // incorrect v, correct r,s,
+ check(false, 30, one, one)
+ check(false, 26, one, one)
+
+ // incorrect v, combinations of incorrect/correct r,s at lower limit
+ check(false, 0, zero, zero)
+ check(false, 0, zero, one)
+ check(false, 0, one, zero)
+ check(false, 0, one, one)
+
+ // correct v for any combination of incorrect r,s
+ check(false, 27, zero, zero)
+ check(false, 27, zero, one)
+ check(false, 27, one, zero)
+
+ check(false, 28, zero, zero)
+ check(false, 28, zero, one)
+ check(false, 28, one, zero)
+
+ // correct sig with max r,s
+ check(true, 27, secp256k1nMinus1, secp256k1nMinus1)
+ // correct v, combinations of incorrect r,s at upper limit
+ check(false, 27, secp256k1n, secp256k1nMinus1)
+ check(false, 27, secp256k1nMinus1, secp256k1n)
+ check(false, 27, secp256k1n, secp256k1n)
+
+ // current callers ensures r,s cannot be negative, but let's test for that too
+ // as crypto package could be used stand-alone
+ check(false, 27, minusOne, one)
+ check(false, 27, one, minusOne)
+}
+
+func checkhash(t *testing.T, name string, f func([]byte) []byte, msg, exp []byte) {
+ sum := f(msg)
+ if bytes.Compare(exp, sum) != 0 {
+ t.Fatalf("hash %s mismatch: want: %x have: %x", name, exp, sum)
+ }
+}
+
+func checkAddr(t *testing.T, addr0, addr1 common.Address) {
+ if addr0 != addr1 {
+ t.Fatalf("address mismatch: want: %x have: %x", addr0, addr1)
+ }
+}
+
+// test to help Python team with integration of libsecp256k1
+// skip but keep it after they are done
+func TestPythonIntegration(t *testing.T) {
+ kh := "289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032"
+ k0, _ := HexToECDSA(kh)
+ k1 := FromECDSA(k0)
+
+ msg0 := Sha3([]byte("foo"))
+ sig0, _ := secp256k1.Sign(msg0, k1)
+
+ msg1 := common.FromHex("00000000000000000000000000000000")
+ sig1, _ := secp256k1.Sign(msg0, k1)
+
+ fmt.Printf("msg: %x, privkey: %x sig: %x\n", msg0, k1, sig0)
+ fmt.Printf("msg: %x, privkey: %x sig: %x\n", msg1, k1, sig1)
+}
diff --git a/tests/util.go b/tests/util.go
index fb9e518c8..bbc671169 100644
--- a/tests/util.go
+++ b/tests/util.go
@@ -209,11 +209,11 @@ func (self *Env) SetSnapshot(copy vm.Database) {
self.state.Set(copy.(*state.StateDB))
}
-func (self *Env) Transfer(from, to vm.Account, amount *big.Int) error {
+func (self *Env) Transfer(from, to vm.Account, amount *big.Int) {
if self.skipTransfer {
- return nil
+ return
}
- return core.Transfer(from, to, amount)
+ core.Transfer(from, to, amount)
}
func (self *Env) Call(caller vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {