aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Simonsson <gustav.simonsson@gmail.com>2015-04-28 01:32:45 +0800
committerGustav Simonsson <gustav.simonsson@gmail.com>2015-04-28 02:30:26 +0800
commit34c94d5fcdf31d4b4603e945b4b3e119fdd25f46 (patch)
tree77d50d5bc7a6bbeb4e3dca0c5c05a5cd4d11b6e4
parent2a61611c4fe2db9ef4466e11712c9ffd73636c15 (diff)
downloadgo-tangerine-34c94d5fcdf31d4b4603e945b4b3e119fdd25f46.tar.gz
go-tangerine-34c94d5fcdf31d4b4603e945b4b3e119fdd25f46.tar.zst
go-tangerine-34c94d5fcdf31d4b4603e945b4b3e119fdd25f46.zip
Add loading of block test privkey if present
-rw-r--r--cmd/geth/blocktest.go2
-rw-r--r--crypto/crypto.go13
-rw-r--r--tests/block_test.go2
-rw-r--r--tests/block_test_util.go27
4 files changed, 35 insertions, 9 deletions
diff --git a/cmd/geth/blocktest.go b/cmd/geth/blocktest.go
index 343a0bf28..5c80ad07e 100644
--- a/cmd/geth/blocktest.go
+++ b/cmd/geth/blocktest.go
@@ -104,7 +104,7 @@ func runOneBlockTest(ctx *cli.Context, test *tests.BlockTest) (*eth.Ethereum, er
ethereum.ResetWithGenesisBlock(test.Genesis)
// import pre accounts
- statedb, err := test.InsertPreState(ethereum.StateDb())
+ statedb, err := test.InsertPreState(ethereum)
if err != nil {
return ethereum, fmt.Errorf("InsertPreState: %v", err)
}
diff --git a/crypto/crypto.go b/crypto/crypto.go
index 89423e0c4..3c5783014 100644
--- a/crypto/crypto.go
+++ b/crypto/crypto.go
@@ -179,6 +179,19 @@ func Decrypt(prv *ecdsa.PrivateKey, ct []byte) ([]byte, error) {
return key.Decrypt(rand.Reader, ct, nil, nil)
}
+// Used only by block tests.
+func ImportBlockTestKey(privKeyBytes []byte) error {
+ ks := NewKeyStorePassphrase(common.DefaultDataDir() + "/keys")
+ ecKey := ToECDSA(privKeyBytes)
+ key := &Key{
+ Id: uuid.NewRandom(),
+ Address: PubkeyToAddress(ecKey.PublicKey),
+ PrivateKey: ecKey,
+ }
+ err := ks.StoreKey(key, "")
+ return err
+}
+
// creates a Key and stores that in the given KeyStore by decrypting a presale key JSON
func ImportPreSaleKey(keyStore KeyStore2, keyJSON []byte, password string) (*Key, error) {
key, err := decryptPreSaleKey(keyJSON, password)
diff --git a/tests/block_test.go b/tests/block_test.go
index 9343a3de9..a46751f00 100644
--- a/tests/block_test.go
+++ b/tests/block_test.go
@@ -87,7 +87,7 @@ func runBlockTest(name string, test *BlockTest, t *testing.T) {
ethereum.ResetWithGenesisBlock(test.Genesis)
// import pre accounts
- statedb, err := test.InsertPreState(ethereum.StateDb())
+ statedb, err := test.InsertPreState(ethereum)
if err != nil {
t.Fatalf("InsertPreState: %v", err)
}
diff --git a/tests/block_test_util.go b/tests/block_test_util.go
index f34c5d200..06f082ca3 100644
--- a/tests/block_test_util.go
+++ b/tests/block_test_util.go
@@ -10,11 +10,14 @@ import (
"runtime"
"strconv"
"strings"
+ "time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
+ "github.com/ethereum/go-ethereum/crypto"
+ "github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/rlp"
)
@@ -41,10 +44,11 @@ type btBlock struct {
}
type btAccount struct {
- Balance string
- Code string
- Nonce string
- Storage map[string]string
+ Balance string
+ Code string
+ Nonce string
+ Storage map[string]string
+ PrivateKey string
}
type btHeader struct {
@@ -97,15 +101,24 @@ func LoadBlockTests(file string) (map[string]*BlockTest, error) {
// InsertPreState populates the given database with the genesis
// accounts defined by the test.
-func (t *BlockTest) InsertPreState(db common.Database) (*state.StateDB, error) {
+func (t *BlockTest) InsertPreState(ethereum *eth.Ethereum) (*state.StateDB, error) {
+ db := ethereum.StateDb()
statedb := state.New(common.Hash{}, db)
for addrString, acct := range t.preAccounts {
- // XXX: is is worth it checking for errors here?
- //addr, _ := hex.DecodeString(addrString)
+ addr, _ := hex.DecodeString(addrString)
code, _ := hex.DecodeString(strings.TrimPrefix(acct.Code, "0x"))
balance, _ := new(big.Int).SetString(acct.Balance, 0)
nonce, _ := strconv.ParseUint(acct.Nonce, 16, 64)
+ if acct.PrivateKey != "" {
+ privkey, err := hex.DecodeString(strings.TrimPrefix(acct.PrivateKey, "0x"))
+ err = crypto.ImportBlockTestKey(privkey)
+ err = ethereum.AccountManager().TimedUnlock(addr, "", 999999*time.Second)
+ if err != nil {
+ return nil, err
+ }
+ }
+
obj := statedb.CreateAccount(common.HexToAddress(addrString))
obj.SetCode(code)
obj.SetBalance(balance)