From 79d5e5593fc86190a5f67d9c48b3de365075c9cd Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Fri, 8 Dec 2017 10:06:16 +0100 Subject: [PATCH 01/12] consensus/ethash: relax requirements when determining future-blocks --- consensus/ethash/consensus.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go index 775419e067..2bdb18677e 100644 --- a/consensus/ethash/consensus.go +++ b/consensus/ethash/consensus.go @@ -36,9 +36,10 @@ import ( // Ethash proof-of-work protocol constants. var ( - FrontierBlockReward *big.Int = big.NewInt(5e+18) // Block reward in wei for successfully mining a block - ByzantiumBlockReward *big.Int = big.NewInt(3e+18) // Block reward in wei for successfully mining a block upward from Byzantium - maxUncles = 2 // Maximum number of uncles allowed in a single block + FrontierBlockReward *big.Int = big.NewInt(5e+18) // Block reward in wei for successfully mining a block + ByzantiumBlockReward *big.Int = big.NewInt(3e+18) // Block reward in wei for successfully mining a block upward from Byzantium + maxUncles = 2 // Maximum number of uncles allowed in a single block + allowedFutureBlockTime = 15 * time.Second // Max time from current time allowed for blocks, before they're considered future blocks ) // Various error messages to mark blocks invalid. These should be private to @@ -231,7 +232,7 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent * return errLargeBlockTime } } else { - if header.Time.Cmp(big.NewInt(time.Now().Unix())) > 0 { + if header.Time.Cmp(big.NewInt(time.Now().Add(allowedFutureBlockTime).Unix())) > 0 { return consensus.ErrFutureBlock } } From e9971d356bf977d2a3f63e50296d7410ade2d075 Mon Sep 17 00:00:00 2001 From: rhaps107 Date: Thu, 14 Dec 2017 15:24:34 +0300 Subject: [PATCH 02/12] internal/ethapi: don't crash for missing receipts Fixes #15408 Fixes #14432 --- internal/ethapi/api.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index fe0ed81707..76a7306e41 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -1003,9 +1003,12 @@ func (s *PublicTransactionPoolAPI) GetRawTransactionByHash(ctx context.Context, func (s *PublicTransactionPoolAPI) GetTransactionReceipt(hash common.Hash) (map[string]interface{}, error) { tx, blockHash, blockNumber, index := core.GetTransaction(s.b.ChainDb(), hash) if tx == nil { - return nil, nil + return nil, errors.New("unknown transaction") } receipt, _, _, _ := core.GetReceipt(s.b.ChainDb(), hash) // Old receipts don't have the lookup data available + if receipt == nil { + return nil, errors.New("unknown receipt") + } var signer types.Signer = types.FrontierSigner{} if tx.Protected() { From 7bb2a489b27eb862458ff2017c317f0e1187aacc Mon Sep 17 00:00:00 2001 From: George Ornbo Date: Thu, 14 Dec 2017 21:55:18 +0000 Subject: [PATCH 03/12] crypto: Fix comment typo --- crypto/crypto.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto/crypto.go b/crypto/crypto.go index 3a98bfb503..e51726e624 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -79,7 +79,7 @@ func ToECDSA(d []byte) (*ecdsa.PrivateKey, error) { return toECDSA(d, true) } -// ToECDSAUnsafe blidly converts a binary blob to a private key. It should almost +// ToECDSAUnsafe blindly converts a binary blob to a private key. It should almost // never be used unless you are sure the input is valid and want to avoid hitting // errors due to bad origin encoding (0 prefixes cut off). func ToECDSAUnsafe(d []byte) *ecdsa.PrivateKey { From c6069a627c42c21fc02d0770d39db9a9be45b180 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Fri, 15 Dec 2017 10:40:09 +0100 Subject: [PATCH 04/12] crypto, crypto/secp256k1: add CompressPubkey (#15626) This adds the inverse to DecompressPubkey and improves a few minor details in crypto/secp256k1. --- crypto/secp256k1/curve.go | 46 +++++++++++++++---------------------- crypto/secp256k1/ext.h | 40 ++++++++++++++++++-------------- crypto/secp256k1/secp256.go | 36 ++++++++++++++++++++++------- crypto/signature_cgo.go | 5 ++++ crypto/signature_nocgo.go | 5 ++++ crypto/signature_test.go | 38 ++++++++++++++++++++++++++++++ 6 files changed, 118 insertions(+), 52 deletions(-) diff --git a/crypto/secp256k1/curve.go b/crypto/secp256k1/curve.go index ec6d266cec..df80481857 100644 --- a/crypto/secp256k1/curve.go +++ b/crypto/secp256k1/curve.go @@ -34,7 +34,6 @@ package secp256k1 import ( "crypto/elliptic" "math/big" - "sync" "unsafe" "github.com/ethereum/go-ethereum/common/math" @@ -42,7 +41,7 @@ import ( /* #include "libsecp256k1/include/secp256k1.h" -extern int secp256k1_pubkey_scalar_mul(const secp256k1_context* ctx, const unsigned char *point, const unsigned char *scalar); +extern int secp256k1_ext_scalar_mul(const secp256k1_context* ctx, const unsigned char *point, const unsigned char *scalar); */ import "C" @@ -236,7 +235,7 @@ func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, math.ReadBits(By, point[32:]) pointPtr := (*C.uchar)(unsafe.Pointer(&point[0])) scalarPtr := (*C.uchar)(unsafe.Pointer(&scalar[0])) - res := C.secp256k1_pubkey_scalar_mul(context, pointPtr, scalarPtr) + res := C.secp256k1_ext_scalar_mul(context, pointPtr, scalarPtr) // Unpack the result and clear temporaries. x := new(big.Int).SetBytes(point[:32]) @@ -263,14 +262,10 @@ func (BitCurve *BitCurve) ScalarBaseMult(k []byte) (*big.Int, *big.Int) { // X9.62. func (BitCurve *BitCurve) Marshal(x, y *big.Int) []byte { byteLen := (BitCurve.BitSize + 7) >> 3 - ret := make([]byte, 1+2*byteLen) - ret[0] = 4 // uncompressed point - - xBytes := x.Bytes() - copy(ret[1+byteLen-len(xBytes):], xBytes) - yBytes := y.Bytes() - copy(ret[1+2*byteLen-len(yBytes):], yBytes) + ret[0] = 4 // uncompressed point flag + math.ReadBits(x, ret[1:1+byteLen]) + math.ReadBits(y, ret[1+byteLen:]) return ret } @@ -289,24 +284,21 @@ func (BitCurve *BitCurve) Unmarshal(data []byte) (x, y *big.Int) { return } -var ( - initonce sync.Once - theCurve *BitCurve -) +var theCurve = new(BitCurve) -// S256 returns a BitCurve which implements secp256k1 (see SEC 2 section 2.7.1) +func init() { + // See SEC 2 section 2.7.1 + // curve parameters taken from: + // http://www.secg.org/collateral/sec2_final.pdf + theCurve.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16) + theCurve.N, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16) + theCurve.B, _ = new(big.Int).SetString("0000000000000000000000000000000000000000000000000000000000000007", 16) + theCurve.Gx, _ = new(big.Int).SetString("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16) + theCurve.Gy, _ = new(big.Int).SetString("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 16) + theCurve.BitSize = 256 +} + +// S256 returns a BitCurve which implements secp256k1. func S256() *BitCurve { - initonce.Do(func() { - // See SEC 2 section 2.7.1 - // curve parameters taken from: - // http://www.secg.org/collateral/sec2_final.pdf - theCurve = new(BitCurve) - theCurve.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16) - theCurve.N, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16) - theCurve.B, _ = new(big.Int).SetString("0000000000000000000000000000000000000000000000000000000000000007", 16) - theCurve.Gx, _ = new(big.Int).SetString("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16) - theCurve.Gy, _ = new(big.Int).SetString("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 16) - theCurve.BitSize = 256 - }) return theCurve } diff --git a/crypto/secp256k1/ext.h b/crypto/secp256k1/ext.h index b0f30b73c6..9b043c724e 100644 --- a/crypto/secp256k1/ext.h +++ b/crypto/secp256k1/ext.h @@ -19,7 +19,7 @@ static secp256k1_context* secp256k1_context_create_sign_verify() { return secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); } -// secp256k1_ecdsa_recover_pubkey recovers the public key of an encoded compact signature. +// secp256k1_ext_ecdsa_recover recovers the public key of an encoded compact signature. // // Returns: 1: recovery was successful // 0: recovery was not successful @@ -27,7 +27,7 @@ static secp256k1_context* secp256k1_context_create_sign_verify() { // Out: pubkey_out: the serialized 65-byte public key of the signer (cannot be NULL) // In: sigdata: pointer to a 65-byte signature with the recovery id at the end (cannot be NULL) // msgdata: pointer to a 32-byte message (cannot be NULL) -static int secp256k1_ecdsa_recover_pubkey( +static int secp256k1_ext_ecdsa_recover( const secp256k1_context* ctx, unsigned char *pubkey_out, const unsigned char *sigdata, @@ -46,7 +46,7 @@ static int secp256k1_ecdsa_recover_pubkey( return secp256k1_ec_pubkey_serialize(ctx, pubkey_out, &outputlen, &pubkey, SECP256K1_EC_UNCOMPRESSED); } -// secp256k1_ecdsa_verify_enc verifies an encoded compact signature. +// secp256k1_ext_ecdsa_verify verifies an encoded compact signature. // // Returns: 1: signature is valid // 0: signature is invalid @@ -55,7 +55,7 @@ static int secp256k1_ecdsa_recover_pubkey( // msgdata: pointer to a 32-byte message (cannot be NULL) // pubkeydata: pointer to public key data (cannot be NULL) // pubkeylen: length of pubkeydata -static int secp256k1_ecdsa_verify_enc( +static int secp256k1_ext_ecdsa_verify( const secp256k1_context* ctx, const unsigned char *sigdata, const unsigned char *msgdata, @@ -74,28 +74,34 @@ static int secp256k1_ecdsa_verify_enc( return secp256k1_ecdsa_verify(ctx, &sig, msgdata, &pubkey); } -// secp256k1_decompress_pubkey decompresses a public key. +// secp256k1_ext_reencode_pubkey decodes then encodes a public key. It can be used to +// convert between public key formats. The input/output formats are chosen depending on the +// length of the input/output buffers. // -// Returns: 1: public key is valid -// 0: public key is invalid +// Returns: 1: conversion successful +// 0: conversion unsuccessful // Args: ctx: pointer to a context object (cannot be NULL) -// Out: pubkey_out: the serialized 65-byte public key (cannot be NULL) -// In: pubkeydata: pointer to 33 bytes of compressed public key data (cannot be NULL) -static int secp256k1_decompress_pubkey( +// Out: out: output buffer that will contain the reencoded key (cannot be NULL) +// In: outlen: length of out (33 for compressed keys, 65 for uncompressed keys) +// pubkeydata: the input public key (cannot be NULL) +// pubkeylen: length of pubkeydata +static int secp256k1_ext_reencode_pubkey( const secp256k1_context* ctx, - unsigned char *pubkey_out, - const unsigned char *pubkeydata + unsigned char *out, + size_t outlen, + const unsigned char *pubkeydata, + size_t pubkeylen ) { secp256k1_pubkey pubkey; - if (!secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeydata, 33)) { + if (!secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeydata, pubkeylen)) { return 0; } - size_t outputlen = 65; - return secp256k1_ec_pubkey_serialize(ctx, pubkey_out, &outputlen, &pubkey, SECP256K1_EC_UNCOMPRESSED); + unsigned int flag = (outlen == 33) ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED; + return secp256k1_ec_pubkey_serialize(ctx, out, &outlen, &pubkey, flag); } -// secp256k1_pubkey_scalar_mul multiplies a point by a scalar in constant time. +// secp256k1_ext_scalar_mul multiplies a point by a scalar in constant time. // // Returns: 1: multiplication was successful // 0: scalar was invalid (zero or overflow) @@ -104,7 +110,7 @@ static int secp256k1_decompress_pubkey( // In: point: pointer to a 64-byte public point, // encoded as two 256bit big-endian numbers. // scalar: a 32-byte scalar with which to multiply the point -int secp256k1_pubkey_scalar_mul(const secp256k1_context* ctx, unsigned char *point, const unsigned char *scalar) { +int secp256k1_ext_scalar_mul(const secp256k1_context* ctx, unsigned char *point, const unsigned char *scalar) { int ret = 0; int overflow = 0; secp256k1_fe feX, feY; diff --git a/crypto/secp256k1/secp256.go b/crypto/secp256k1/secp256.go index 00a1f8aaa4..eefbb99ee4 100644 --- a/crypto/secp256k1/secp256.go +++ b/crypto/secp256k1/secp256.go @@ -115,7 +115,7 @@ func RecoverPubkey(msg []byte, sig []byte) ([]byte, error) { sigdata = (*C.uchar)(unsafe.Pointer(&sig[0])) msgdata = (*C.uchar)(unsafe.Pointer(&msg[0])) ) - if C.secp256k1_ecdsa_recover_pubkey(context, (*C.uchar)(unsafe.Pointer(&pubkey[0])), sigdata, msgdata) == 0 { + if C.secp256k1_ext_ecdsa_recover(context, (*C.uchar)(unsafe.Pointer(&pubkey[0])), sigdata, msgdata) == 0 { return nil, ErrRecoverFailed } return pubkey, nil @@ -130,22 +130,42 @@ func VerifySignature(pubkey, msg, signature []byte) bool { sigdata := (*C.uchar)(unsafe.Pointer(&signature[0])) msgdata := (*C.uchar)(unsafe.Pointer(&msg[0])) keydata := (*C.uchar)(unsafe.Pointer(&pubkey[0])) - return C.secp256k1_ecdsa_verify_enc(context, sigdata, msgdata, keydata, C.size_t(len(pubkey))) != 0 + return C.secp256k1_ext_ecdsa_verify(context, sigdata, msgdata, keydata, C.size_t(len(pubkey))) != 0 } // DecompressPubkey parses a public key in the 33-byte compressed format. // It returns non-nil coordinates if the public key is valid. -func DecompressPubkey(pubkey []byte) (X, Y *big.Int) { +func DecompressPubkey(pubkey []byte) (x, y *big.Int) { if len(pubkey) != 33 { return nil, nil } - buf := make([]byte, 65) - bufdata := (*C.uchar)(unsafe.Pointer(&buf[0])) - pubkeydata := (*C.uchar)(unsafe.Pointer(&pubkey[0])) - if C.secp256k1_decompress_pubkey(context, bufdata, pubkeydata) == 0 { + var ( + pubkeydata = (*C.uchar)(unsafe.Pointer(&pubkey[0])) + pubkeylen = C.size_t(len(pubkey)) + out = make([]byte, 65) + outdata = (*C.uchar)(unsafe.Pointer(&out[0])) + outlen = C.size_t(len(out)) + ) + if C.secp256k1_ext_reencode_pubkey(context, outdata, outlen, pubkeydata, pubkeylen) == 0 { return nil, nil } - return new(big.Int).SetBytes(buf[1:33]), new(big.Int).SetBytes(buf[33:]) + return new(big.Int).SetBytes(out[1:33]), new(big.Int).SetBytes(out[33:]) +} + +// CompressPubkey encodes a public key to 33-byte compressed format. +func CompressPubkey(x, y *big.Int) []byte { + var ( + pubkey = S256().Marshal(x, y) + pubkeydata = (*C.uchar)(unsafe.Pointer(&pubkey[0])) + pubkeylen = C.size_t(len(pubkey)) + out = make([]byte, 33) + outdata = (*C.uchar)(unsafe.Pointer(&out[0])) + outlen = C.size_t(len(out)) + ) + if C.secp256k1_ext_reencode_pubkey(context, outdata, outlen, pubkeydata, pubkeylen) == 0 { + panic("libsecp256k1 error") + } + return out } func checkSignature(sig []byte) error { diff --git a/crypto/signature_cgo.go b/crypto/signature_cgo.go index 381d8a1bbc..340bfc221e 100644 --- a/crypto/signature_cgo.go +++ b/crypto/signature_cgo.go @@ -76,6 +76,11 @@ func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error) { return &ecdsa.PublicKey{X: x, Y: y, Curve: S256()}, nil } +// CompressPubkey encodes a public key to the 33-byte compressed format. +func CompressPubkey(pubkey *ecdsa.PublicKey) []byte { + return secp256k1.CompressPubkey(pubkey.X, pubkey.Y) +} + // S256 returns an instance of the secp256k1 curve. func S256() elliptic.Curve { return secp256k1.S256() diff --git a/crypto/signature_nocgo.go b/crypto/signature_nocgo.go index 17fd613b21..78b99c02b0 100644 --- a/crypto/signature_nocgo.go +++ b/crypto/signature_nocgo.go @@ -102,6 +102,11 @@ func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error) { return key.ToECDSA(), nil } +// CompressPubkey encodes a public key to the 33-byte compressed format. +func CompressPubkey(pubkey *ecdsa.PublicKey) []byte { + return (*btcec.PublicKey)(pubkey).SerializeCompressed() +} + // S256 returns an instance of the secp256k1 curve. func S256() elliptic.Curve { return btcec.S256() diff --git a/crypto/signature_test.go b/crypto/signature_test.go index abcab425b5..5e2efc7e05 100644 --- a/crypto/signature_test.go +++ b/crypto/signature_test.go @@ -18,10 +18,13 @@ package crypto import ( "bytes" + "crypto/ecdsa" + "reflect" "testing" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/common/math" ) var ( @@ -65,6 +68,11 @@ func TestVerifySignature(t *testing.T) { if VerifySignature(testpubkey, testmsg, sig[:len(sig)-2]) { t.Errorf("signature valid even though it's incomplete") } + wrongkey := common.CopyBytes(testpubkey) + wrongkey[10]++ + if VerifySignature(wrongkey, testmsg, sig) { + t.Errorf("signature valid with with wrong public key") + } } func TestDecompressPubkey(t *testing.T) { @@ -86,6 +94,36 @@ func TestDecompressPubkey(t *testing.T) { } } +func TestCompressPubkey(t *testing.T) { + key := &ecdsa.PublicKey{ + Curve: S256(), + X: math.MustParseBig256("0xe32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a"), + Y: math.MustParseBig256("0x0a2b2667f7e725ceea70c673093bf67663e0312623c8e091b13cf2c0f11ef652"), + } + compressed := CompressPubkey(key) + if !bytes.Equal(compressed, testpubkeyc) { + t.Errorf("wrong public key result: got %x, want %x", compressed, testpubkeyc) + } +} + +func TestPubkeyRandom(t *testing.T) { + const runs = 200 + + for i := 0; i < runs; i++ { + key, err := GenerateKey() + if err != nil { + t.Fatalf("iteration %d: %v", i, err) + } + pubkey2, err := DecompressPubkey(CompressPubkey(&key.PublicKey)) + if err != nil { + t.Fatalf("iteration %d: %v", i, err) + } + if !reflect.DeepEqual(key.PublicKey, *pubkey2) { + t.Fatalf("iteration %d: keys not equal", i) + } + } +} + func BenchmarkEcrecoverSignature(b *testing.B) { for i := 0; i < b.N; i++ { if _, err := Ecrecover(testmsg, testsig); err != nil { From fb5f25eeee6091ab4f70506a9b0ff36affe4d879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 14 Dec 2017 13:28:44 +0100 Subject: [PATCH 05/12] core/vm: Remove snapshot param from Interpreter.Run() --- core/vm/evm.go | 14 +++++++------- core/vm/interpreter.go | 6 +++--- internal/ethapi/tracer_test.go | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/core/vm/evm.go b/core/vm/evm.go index 344435f73c..cbb5a03ce7 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -38,7 +38,7 @@ type ( ) // run runs the given contract and takes care of running precompiles with a fallback to the byte code interpreter. -func run(evm *EVM, snapshot int, contract *Contract, input []byte) ([]byte, error) { +func run(evm *EVM, contract *Contract, input []byte) ([]byte, error) { if contract.CodeAddr != nil { precompiles := PrecompiledContractsHomestead if evm.ChainConfig().IsByzantium(evm.BlockNumber) { @@ -48,7 +48,7 @@ func run(evm *EVM, snapshot int, contract *Contract, input []byte) ([]byte, erro return RunPrecompiledContract(p, input, contract) } } - return evm.interpreter.Run(snapshot, contract, input) + return evm.interpreter.Run(contract, input) } // Context provides the EVM with auxiliary information. Once provided @@ -171,7 +171,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas contract := NewContract(caller, to, value, gas) contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr)) - ret, err = run(evm, snapshot, contract, input) + ret, err = run(evm, contract, input) // When an error was returned by the EVM or when setting the creation code // above we revert to the snapshot and consume any gas remaining. Additionally // when we're in homestead this also counts for code storage gas errors. @@ -215,7 +215,7 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte, contract := NewContract(caller, to, value, gas) contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr)) - ret, err = run(evm, snapshot, contract, input) + ret, err = run(evm, contract, input) if err != nil { evm.StateDB.RevertToSnapshot(snapshot) if err != errExecutionReverted { @@ -248,7 +248,7 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by contract := NewContract(caller, to, nil, gas).AsDelegate() contract.SetCallCode(&addr, evm.StateDB.GetCodeHash(addr), evm.StateDB.GetCode(addr)) - ret, err = run(evm, snapshot, contract, input) + ret, err = run(evm, contract, input) if err != nil { evm.StateDB.RevertToSnapshot(snapshot) if err != errExecutionReverted { @@ -291,7 +291,7 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte // When an error was returned by the EVM or when setting the creation code // above we revert to the snapshot and consume any gas remaining. Additionally // when we're in Homestead this also counts for code storage gas errors. - ret, err = run(evm, snapshot, contract, input) + ret, err = run(evm, contract, input) if err != nil { evm.StateDB.RevertToSnapshot(snapshot) if err != errExecutionReverted { @@ -338,7 +338,7 @@ func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.I if evm.vmConfig.NoRecursion && evm.depth > 0 { return nil, contractAddr, gas, nil } - ret, err = run(evm, snapshot, contract, nil) + ret, err = run(evm, contract, nil) // check whether the max code size has been exceeded maxCodeSizeExceeded := evm.ChainConfig().IsEIP158(evm.BlockNumber) && len(ret) > params.MaxCodeSize // if the contract creation ran successfully and no errors were returned diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go index ac6000f97d..455f970ddb 100644 --- a/core/vm/interpreter.go +++ b/core/vm/interpreter.go @@ -107,9 +107,9 @@ func (in *Interpreter) enforceRestrictions(op OpCode, operation operation, stack // the return byte-slice and an error if one occurred. // // It's important to note that any errors returned by the interpreter should be -// considered a revert-and-consume-all-gas operation. No error specific checks -// should be handled to reduce complexity and errors further down the in. -func (in *Interpreter) Run(snapshot int, contract *Contract, input []byte) (ret []byte, err error) { +// considered a revert-and-consume-all-gas operation except for +// errExecutionReverted which means revert-and-keep-gas-left. +func (in *Interpreter) Run(contract *Contract, input []byte) (ret []byte, err error) { // Increment the call depth which is restricted to 1024 in.evm.depth++ defer func() { in.evm.depth-- }() diff --git a/internal/ethapi/tracer_test.go b/internal/ethapi/tracer_test.go index 5093dafd6e..0ef450ce32 100644 --- a/internal/ethapi/tracer_test.go +++ b/internal/ethapi/tracer_test.go @@ -48,7 +48,7 @@ func runTrace(tracer *JavascriptTracer) (interface{}, error) { contract := vm.NewContract(account{}, account{}, big.NewInt(0), 10000) contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0} - _, err := env.Interpreter().Run(0, contract, []byte{}) + _, err := env.Interpreter().Run(contract, []byte{}) if err != nil { return nil, err } From 1d7d7f57d0fefa8797fbb269f1de76bca3e73dc3 Mon Sep 17 00:00:00 2001 From: Sorin Neacsu Date: Fri, 15 Dec 2017 13:31:10 -0800 Subject: [PATCH 06/12] cmd/geth: add support for geth --rinkeby attach --- cmd/geth/consolecmd.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cmd/geth/consolecmd.go b/cmd/geth/consolecmd.go index 2c6b166875..9d5cc38a1b 100644 --- a/cmd/geth/consolecmd.go +++ b/cmd/geth/consolecmd.go @@ -120,8 +120,12 @@ func remoteConsole(ctx *cli.Context) error { if ctx.GlobalIsSet(utils.DataDirFlag.Name) { path = ctx.GlobalString(utils.DataDirFlag.Name) } - if path != "" && ctx.GlobalBool(utils.TestnetFlag.Name) { - path = filepath.Join(path, "testnet") + if path != "" { + if ctx.GlobalBool(utils.TestnetFlag.Name) { + path = filepath.Join(path, "testnet") + } else if ctx.GlobalBool(utils.RinkebyFlag.Name) { + path = filepath.Join(path, "rinkeby") + } } endpoint = fmt.Sprintf("%s/geth.ipc", path) } From afa3c72c40eee45dfbf3cbc40505b78cb2c3c6b2 Mon Sep 17 00:00:00 2001 From: ferhat elmas Date: Mon, 18 Dec 2017 04:03:48 +0100 Subject: [PATCH 07/12] p2p/discover: fix leaked goroutine in data expiration --- p2p/discover/database.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/p2p/discover/database.go b/p2p/discover/database.go index 7206a63c63..b136609f24 100644 --- a/p2p/discover/database.go +++ b/p2p/discover/database.go @@ -226,14 +226,14 @@ func (db *nodeDB) ensureExpirer() { // expirer should be started in a go routine, and is responsible for looping ad // infinitum and dropping stale data from the database. func (db *nodeDB) expirer() { - tick := time.Tick(nodeDBCleanupCycle) + tick := time.NewTicker(nodeDBCleanupCycle) + defer tick.Stop() for { select { - case <-tick: + case <-tick.C: if err := db.expireNodes(); err != nil { log.Error("Failed to expire nodedb items", "err", err) } - case <-db.quit: return } From afc2039f220821155796b64de5f08ccd09fd8412 Mon Sep 17 00:00:00 2001 From: Armin Date: Mon, 18 Dec 2017 12:28:26 +0100 Subject: [PATCH 08/12] accounts/keystore: Improved error message * Fix for #15668 --- accounts/keystore/presale.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/accounts/keystore/presale.go b/accounts/keystore/presale.go index ed900ad085..1554294e14 100644 --- a/accounts/keystore/presale.go +++ b/accounts/keystore/presale.go @@ -58,6 +58,9 @@ func decryptPreSaleKey(fileContent []byte, password string) (key *Key, err error if err != nil { return nil, errors.New("invalid hex in encSeed") } + if len(encSeedBytes) < 16 { + return nil, errors.New("invalid encSeed, too short") + } iv := encSeedBytes[:16] cipherText := encSeedBytes[16:] /* From 8c33ac10bff32d082facfd274188334a3236a4e7 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Mon, 18 Dec 2017 12:50:21 +0100 Subject: [PATCH 09/12] internal/ethapi: support "input" in transaction args (#15640) The tx data field is called "input" in returned objects and "data" in argument objects. Make it so "input" can be used, but bail if both are set. --- internal/ethapi/api.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 76a7306e41..d07b2e693a 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -17,6 +17,7 @@ package ethapi import ( + "bytes" "context" "errors" "fmt" @@ -1070,8 +1071,11 @@ type SendTxArgs struct { Gas *hexutil.Big `json:"gas"` GasPrice *hexutil.Big `json:"gasPrice"` Value *hexutil.Big `json:"value"` - Data hexutil.Bytes `json:"data"` Nonce *hexutil.Uint64 `json:"nonce"` + // We accept "data" and "input" for backwards-compatibility reasons. "input" is the + // newer name and should be preferred by clients. + Data *hexutil.Bytes `json:"data"` + Input *hexutil.Bytes `json:"input"` } // setDefaults is a helper function that fills in default values for unspecified tx fields. @@ -1096,14 +1100,23 @@ func (args *SendTxArgs) setDefaults(ctx context.Context, b Backend) error { } args.Nonce = (*hexutil.Uint64)(&nonce) } + if args.Data != nil && args.Input != nil && !bytes.Equal(*args.Data, *args.Input) { + return errors.New(`Both "data" and "input" are set and not equal. Please use "input" to pass transaction call data.`) + } return nil } func (args *SendTxArgs) toTransaction() *types.Transaction { - if args.To == nil { - return types.NewContractCreation(uint64(*args.Nonce), (*big.Int)(args.Value), (*big.Int)(args.Gas), (*big.Int)(args.GasPrice), args.Data) + var input []byte + if args.Data != nil { + input = *args.Data + } else if args.Input != nil { + input = *args.Input } - return types.NewTransaction(uint64(*args.Nonce), *args.To, (*big.Int)(args.Value), (*big.Int)(args.Gas), (*big.Int)(args.GasPrice), args.Data) + if args.To == nil { + return types.NewContractCreation(uint64(*args.Nonce), (*big.Int)(args.Value), (*big.Int)(args.Gas), (*big.Int)(args.GasPrice), input) + } + return types.NewTransaction(uint64(*args.Nonce), *args.To, (*big.Int)(args.Value), (*big.Int)(args.Gas), (*big.Int)(args.GasPrice), input) } // submitTransaction is a helper function that submits tx to txPool and logs a message. From 48648bc2f8396984a775ead38ead77155c291f67 Mon Sep 17 00:00:00 2001 From: Zsolt Felfoldi Date: Sun, 17 Dec 2017 22:40:39 +0100 Subject: [PATCH 10/12] contracts/release: do not print error log if les backend has no peers --- contracts/release/release.go | 2 +- light/trie.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/release/release.go b/contracts/release/release.go index 28a35381d4..4442ce3e0d 100644 --- a/contracts/release/release.go +++ b/contracts/release/release.go @@ -137,7 +137,7 @@ func (r *ReleaseService) checkVersion() { if err != nil { if err == bind.ErrNoCode { log.Debug("Release oracle not found", "contract", r.config.Oracle) - } else { + } else if err != les.ErrNoPeers { log.Error("Failed to retrieve current release", "err", err) } return diff --git a/light/trie.go b/light/trie.go index 7502b6e5dd..7a9c86b98d 100644 --- a/light/trie.go +++ b/light/trie.go @@ -151,7 +151,7 @@ func (t *odrTrie) do(key []byte, fn func() error) error { } r := &TrieRequest{Id: t.id, Key: key} if err := t.db.backend.Retrieve(t.db.ctx, r); err != nil { - return fmt.Errorf("can't fetch trie key %x: %v", key, err) + return err } } } From c786f75389121c29d4380c53a013759254457776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jano=C5=A1=20Gulja=C5=A1?= Date: Tue, 19 Dec 2017 09:49:30 +0100 Subject: [PATCH 11/12] swarm: bzz-list, bzz-raw and bzz-immutable schemes (#15667) * swarm/api: url scheme bzz-list for getting list of files from manifest Replace query parameter list=true for listing all files contained in a swarm manifest with a new URL scheme bzz-list. * swarm: replaace bzzr and bzzi schemes with bzz-raw and bzz-immutable New URI Shemes are added and old ones are deprecated, but not removed. Old Schemes bzzr and bzzi are functional for backward compatibility. * swarm/api: completely remove bzzr and bzzi schemes Remove old schemes in favour of bzz-raw and bzz-immutable. * swarm/api: revert "completely remove bzzr and bzzi schemes" Keep bzzr and bzzi schemes for backward compatibility. At least until 0.3 swarm release. --- swarm/api/api.go | 2 +- swarm/api/api_test.go | 2 +- swarm/api/client/client.go | 6 +- swarm/api/http/roundtripper.go | 4 +- swarm/api/http/server.go | 37 ++++++----- swarm/api/http/server_test.go | 98 +++++++++++++++++++++++++++-- swarm/api/http/templates.go | 2 +- swarm/api/uri.go | 27 ++++++-- swarm/api/uri_test.go | 72 ++++++++++++++++----- swarm/dev/scripts/random-uploads.sh | 2 +- 10 files changed, 203 insertions(+), 49 deletions(-) diff --git a/swarm/api/api.go b/swarm/api/api.go index 79de29a1cf..8c4bca2ec0 100644 --- a/swarm/api/api.go +++ b/swarm/api/api.go @@ -83,7 +83,7 @@ func (self *Api) Resolve(uri *URI) (storage.Key, error) { // if the URI is immutable, check if the address is a hash isHash := hashMatcher.MatchString(uri.Addr) - if uri.Immutable() { + if uri.Immutable() || uri.DeprecatedImmutable() { if !isHash { return nil, fmt.Errorf("immutable address not a content hash: %q", uri.Addr) } diff --git a/swarm/api/api_test.go b/swarm/api/api_test.go index f9caed27f5..e673f76c42 100644 --- a/swarm/api/api_test.go +++ b/swarm/api/api_test.go @@ -216,7 +216,7 @@ func TestAPIResolve(t *testing.T) { api := &Api{dns: x.dns} uri := &URI{Addr: x.addr, Scheme: "bzz"} if x.immutable { - uri.Scheme = "bzzi" + uri.Scheme = "bzz-immutable" } res, err := api.Resolve(uri) if err == nil { diff --git a/swarm/api/client/client.go b/swarm/api/client/client.go index 7952d3fb68..8165d52d7e 100644 --- a/swarm/api/client/client.go +++ b/swarm/api/client/client.go @@ -57,7 +57,7 @@ func (c *Client) UploadRaw(r io.Reader, size int64) (string, error) { if size <= 0 { return "", errors.New("data size must be greater than zero") } - req, err := http.NewRequest("POST", c.Gateway+"/bzzr:/", r) + req, err := http.NewRequest("POST", c.Gateway+"/bzz-raw:/", r) if err != nil { return "", err } @@ -79,7 +79,7 @@ func (c *Client) UploadRaw(r io.Reader, size int64) (string, error) { // DownloadRaw downloads raw data from swarm func (c *Client) DownloadRaw(hash string) (io.ReadCloser, error) { - uri := c.Gateway + "/bzzr:/" + hash + uri := c.Gateway + "/bzz-raw:/" + hash res, err := http.DefaultClient.Get(uri) if err != nil { return nil, err @@ -269,7 +269,7 @@ func (c *Client) DownloadManifest(hash string) (*api.Manifest, error) { // // where entries ending with "/" are common prefixes. func (c *Client) List(hash, prefix string) (*api.ManifestList, error) { - res, err := http.DefaultClient.Get(c.Gateway + "/bzz:/" + hash + "/" + prefix + "?list=true") + res, err := http.DefaultClient.Get(c.Gateway + "/bzz-list:/" + hash + "/" + prefix) if err != nil { return nil, err } diff --git a/swarm/api/http/roundtripper.go b/swarm/api/http/roundtripper.go index 328177a218..0194317716 100644 --- a/swarm/api/http/roundtripper.go +++ b/swarm/api/http/roundtripper.go @@ -35,8 +35,8 @@ import ( client := httpclient.New() // for (private) swarm proxy running locally client.RegisterScheme("bzz", &http.RoundTripper{Port: port}) -client.RegisterScheme("bzzi", &http.RoundTripper{Port: port}) -client.RegisterScheme("bzzr", &http.RoundTripper{Port: port}) +client.RegisterScheme("bzz-immutable", &http.RoundTripper{Port: port}) +client.RegisterScheme("bzz-raw", &http.RoundTripper{Port: port}) The port you give the Roundtripper is the port the swarm proxy is listening on. If Host is left empty, localhost is assumed. diff --git a/swarm/api/http/server.go b/swarm/api/http/server.go index 65f6afab72..3872cbc4f0 100644 --- a/swarm/api/http/server.go +++ b/swarm/api/http/server.go @@ -86,7 +86,7 @@ type Request struct { uri *api.URI } -// HandlePostRaw handles a POST request to a raw bzzr:/ URI, stores the request +// HandlePostRaw handles a POST request to a raw bzz-raw:/ URI, stores the request // body in swarm and returns the resulting storage key as a text/plain response func (s *Server) HandlePostRaw(w http.ResponseWriter, r *Request) { if r.uri.Path != "" { @@ -290,7 +290,7 @@ func (s *Server) HandleDelete(w http.ResponseWriter, r *Request) { fmt.Fprint(w, newKey) } -// HandleGetRaw handles a GET request to bzzr:// and responds with +// HandleGetRaw handles a GET request to bzz-raw:// and responds with // the raw content stored at the given storage key func (s *Server) HandleGetRaw(w http.ResponseWriter, r *Request) { key, err := s.api.Resolve(r.uri) @@ -424,14 +424,13 @@ func (s *Server) HandleGetFiles(w http.ResponseWriter, r *Request) { } } -// HandleGetList handles a GET request to bzz:// which has -// the "list" query parameter set to "true" and returns a list of all files -// contained in under grouped into common prefixes using -// "/" as a delimiter +// HandleGetList handles a GET request to bzz-list:// and returns +// a list of all files contained in under grouped into +// common prefixes using "/" as a delimiter func (s *Server) HandleGetList(w http.ResponseWriter, r *Request) { // ensure the root path has a trailing slash so that relative URLs work if r.uri.Path == "" && !strings.HasSuffix(r.URL.Path, "/") { - http.Redirect(w, &r.Request, r.URL.Path+"/?list=true", http.StatusMovedPermanently) + http.Redirect(w, &r.Request, r.URL.Path+"/", http.StatusMovedPermanently) return } @@ -453,7 +452,11 @@ func (s *Server) HandleGetList(w http.ResponseWriter, r *Request) { if strings.Contains(r.Header.Get("Accept"), "text/html") { w.Header().Set("Content-Type", "text/html") err := htmlListTemplate.Execute(w, &htmlListData{ - URI: r.uri, + URI: &api.URI{ + Scheme: "bzz", + Addr: r.uri.Addr, + Path: r.uri.Path, + }, List: &list, }) if err != nil { @@ -589,7 +592,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch r.Method { case "POST": - if uri.Raw() { + if uri.Raw() || uri.DeprecatedRaw() { s.HandlePostRaw(w, req) } else { s.HandlePostFiles(w, req) @@ -601,7 +604,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { // new manifest leaving the existing one intact, so it isn't // strictly a traditional PUT request which replaces content // at a URI, and POST is more ubiquitous) - if uri.Raw() { + if uri.Raw() || uri.DeprecatedRaw() { ShowError(w, r, fmt.Sprintf("No PUT to %s allowed.", uri), http.StatusBadRequest) return } else { @@ -609,28 +612,28 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { } case "DELETE": - if uri.Raw() { + if uri.Raw() || uri.DeprecatedRaw() { ShowError(w, r, fmt.Sprintf("No DELETE to %s allowed.", uri), http.StatusBadRequest) return } s.HandleDelete(w, req) case "GET": - if uri.Raw() { + if uri.Raw() || uri.DeprecatedRaw() { s.HandleGetRaw(w, req) return } + if uri.List() { + s.HandleGetList(w, req) + return + } + if r.Header.Get("Accept") == "application/x-tar" { s.HandleGetFiles(w, req) return } - if r.URL.Query().Get("list") == "true" { - s.HandleGetList(w, req) - return - } - s.HandleGetFile(w, req) default: diff --git a/swarm/api/http/server_test.go b/swarm/api/http/server_test.go index ffeaf6e0d8..dbbfa3b071 100644 --- a/swarm/api/http/server_test.go +++ b/swarm/api/http/server_test.go @@ -70,7 +70,7 @@ func TestBzzrGetPath(t *testing.T) { wg.Wait() } - _, err = http.Get(srv.URL + "/bzzr:/" + common.ToHex(key[0])[2:] + "/a") + _, err = http.Get(srv.URL + "/bzz-raw:/" + common.ToHex(key[0])[2:] + "/a") if err != nil { t.Fatalf("Failed to connect to proxy: %v", err) } @@ -79,7 +79,7 @@ func TestBzzrGetPath(t *testing.T) { var resp *http.Response var respbody []byte - url := srv.URL + "/bzzr:/" + url := srv.URL + "/bzz-raw:/" if k[:] != "" { url += common.ToHex(key[0])[2:] + "/" + k[1:] + "?content_type=text/plain" } @@ -104,16 +104,106 @@ func TestBzzrGetPath(t *testing.T) { } } + for _, c := range []struct { + path string + json string + html string + }{ + { + path: "/", + json: `{"common_prefixes":["a/"]}`, + html: "\n\n\n \n \n Swarm index of bzz:/262e5c08c03c2789b6daef487dfa14b4d132f5340d781a3ecb1d5122ab65640c/\n\n\n\n

Swarm index of bzz:/262e5c08c03c2789b6daef487dfa14b4d132f5340d781a3ecb1d5122ab65640c/

\n
\n \n \n \n\t\n\t\n\t\n \n \n\n \n \n\t\n\t \n\t \n\t \n\t\n \n\n \n
PathTypeSize
a/DIR-
\n
\n\n", + }, + { + path: "/a/", + json: `{"common_prefixes":["a/b/"],"entries":[{"hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","path":"a/a","mod_time":"0001-01-01T00:00:00Z"}]}`, + html: "\n\n\n \n \n Swarm index of bzz:/262e5c08c03c2789b6daef487dfa14b4d132f5340d781a3ecb1d5122ab65640c/a/\n\n\n\n

Swarm index of bzz:/262e5c08c03c2789b6daef487dfa14b4d132f5340d781a3ecb1d5122ab65640c/a/

\n
\n \n \n \n\t\n\t\n\t\n \n \n\n \n \n\t\n\t \n\t \n\t \n\t\n \n\n \n\t\n\t \n\t \n\t \n\t\n \n
PathTypeSize
b/DIR-
a0
\n
\n\n", + }, + { + path: "/a/b/", + json: `{"entries":[{"hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","path":"a/b/b","mod_time":"0001-01-01T00:00:00Z"},{"hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","path":"a/b/c","mod_time":"0001-01-01T00:00:00Z"}]}`, + html: "\n\n\n \n \n Swarm index of bzz:/262e5c08c03c2789b6daef487dfa14b4d132f5340d781a3ecb1d5122ab65640c/a/b/\n\n\n\n

Swarm index of bzz:/262e5c08c03c2789b6daef487dfa14b4d132f5340d781a3ecb1d5122ab65640c/a/b/

\n
\n \n \n \n\t\n\t\n\t\n \n \n\n \n \n\n \n\t\n\t \n\t \n\t \n\t\n \n\t\n\t \n\t \n\t \n\t\n \n
PathTypeSize
b0
c0
\n
\n\n", + }, + { + path: "/x", + }, + { + path: "", + }, + } { + k := c.path + url := srv.URL + "/bzz-list:/" + if k[:] != "" { + url += common.ToHex(key[0])[2:] + "/" + k[1:] + } + t.Run("json list "+c.path, func(t *testing.T) { + resp, err := http.Get(url) + if err != nil { + t.Fatalf("HTTP request: %v", err) + } + defer resp.Body.Close() + respbody, err := ioutil.ReadAll(resp.Body) + if err != nil { + t.Fatalf("Read response body: %v", err) + } + + body := strings.TrimSpace(string(respbody)) + if body != c.json { + isexpectedfailrequest := false + + for _, r := range expectedfailrequests { + if k[:] == r { + isexpectedfailrequest = true + } + } + if !isexpectedfailrequest { + t.Errorf("Response list body %q does not match, expected: %v, got %v", k, c.json, body) + } + } + }) + t.Run("html list "+c.path, func(t *testing.T) { + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + t.Fatalf("New request: %v", err) + } + req.Header.Set("Accept", "text/html") + resp, err := http.DefaultClient.Do(req) + if err != nil { + t.Fatalf("HTTP request: %v", err) + } + defer resp.Body.Close() + respbody, err := ioutil.ReadAll(resp.Body) + if err != nil { + t.Fatalf("Read response body: %v", err) + } + + if string(respbody) != c.html { + isexpectedfailrequest := false + + for _, r := range expectedfailrequests { + if k[:] == r { + isexpectedfailrequest = true + } + } + if !isexpectedfailrequest { + t.Errorf("Response list body %q does not match, expected: %q, got %q", k, c.html, string(respbody)) + } + } + }) + } + nonhashtests := []string{ srv.URL + "/bzz:/name", - srv.URL + "/bzzi:/nonhash", - srv.URL + "/bzzr:/nonhash", + srv.URL + "/bzz-immutable:/nonhash", + srv.URL + "/bzz-raw:/nonhash", + srv.URL + "/bzz-list:/nonhash", } nonhashresponses := []string{ "error resolving name: no DNS to resolve name: "name"", "error resolving nonhash: immutable address not a content hash: "nonhash"", "error resolving nonhash: no DNS to resolve name: "nonhash"", + "error resolving nonhash: no DNS to resolve name: "nonhash"", } for i, url := range nonhashtests { diff --git a/swarm/api/http/templates.go b/swarm/api/http/templates.go index 9ae434a7ed..53ce7b5a22 100644 --- a/swarm/api/http/templates.go +++ b/swarm/api/http/templates.go @@ -52,7 +52,7 @@ var htmlListTemplate = template.Must(template.New("html-list").Funcs(template.Fu {{ range .List.CommonPrefixes }} - {{ basename . }}/ + {{ basename . }}/ DIR - diff --git a/swarm/api/uri.go b/swarm/api/uri.go index caed4212d5..af1dc74453 100644 --- a/swarm/api/uri.go +++ b/swarm/api/uri.go @@ -26,7 +26,13 @@ import ( type URI struct { // Scheme has one of the following values: // - // * bzz - an entry in a swarm manifest + // * bzz - an entry in a swarm manifest + // * bzz-raw - raw swarm content + // * bzz-immutable - immutable URI of an entry in a swarm manifest + // (address is not resolved) + // * bzz-list - list of all files contained in a swarm manifest + // + // Deprecated Schemes: // * bzzr - raw swarm content // * bzzi - immutable URI of an entry in a swarm manifest // (address is not resolved) @@ -50,7 +56,8 @@ type URI struct { // * :// // * :/// // -// with scheme one of bzz, bzzr or bzzi +// with scheme one of bzz, bzz-raw, bzz-immutable or bzz-list +// or deprecated ones bzzr and bzzi func Parse(rawuri string) (*URI, error) { u, err := url.Parse(rawuri) if err != nil { @@ -60,7 +67,7 @@ func Parse(rawuri string) (*URI, error) { // check the scheme is valid switch uri.Scheme { - case "bzz", "bzzi", "bzzr": + case "bzz", "bzz-raw", "bzz-immutable", "bzz-list", "bzzr", "bzzi": default: return nil, fmt.Errorf("unknown scheme %q", u.Scheme) } @@ -84,10 +91,22 @@ func Parse(rawuri string) (*URI, error) { } func (u *URI) Raw() bool { - return u.Scheme == "bzzr" + return u.Scheme == "bzz-raw" } func (u *URI) Immutable() bool { + return u.Scheme == "bzz-immutable" +} + +func (u *URI) List() bool { + return u.Scheme == "bzz-list" +} + +func (u *URI) DeprecatedRaw() bool { + return u.Scheme == "bzzr" +} + +func (u *URI) DeprecatedImmutable() bool { return u.Scheme == "bzzi" } diff --git a/swarm/api/uri_test.go b/swarm/api/uri_test.go index 7d4160601d..babb2834e9 100644 --- a/swarm/api/uri_test.go +++ b/swarm/api/uri_test.go @@ -23,11 +23,14 @@ import ( func TestParseURI(t *testing.T) { type test struct { - uri string - expectURI *URI - expectErr bool - expectRaw bool - expectImmutable bool + uri string + expectURI *URI + expectErr bool + expectRaw bool + expectImmutable bool + expectList bool + expectDeprecatedRaw bool + expectDeprecatedImmutable bool } tests := []test{ { @@ -47,13 +50,13 @@ func TestParseURI(t *testing.T) { expectURI: &URI{Scheme: "bzz"}, }, { - uri: "bzzi:", - expectURI: &URI{Scheme: "bzzi"}, + uri: "bzz-immutable:", + expectURI: &URI{Scheme: "bzz-immutable"}, expectImmutable: true, }, { - uri: "bzzr:", - expectURI: &URI{Scheme: "bzzr"}, + uri: "bzz-raw:", + expectURI: &URI{Scheme: "bzz-raw"}, expectRaw: true, }, { @@ -69,18 +72,18 @@ func TestParseURI(t *testing.T) { expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"}, }, { - uri: "bzzr:/", - expectURI: &URI{Scheme: "bzzr"}, + uri: "bzz-raw:/", + expectURI: &URI{Scheme: "bzz-raw"}, expectRaw: true, }, { - uri: "bzzr:/abc123", - expectURI: &URI{Scheme: "bzzr", Addr: "abc123"}, + uri: "bzz-raw:/abc123", + expectURI: &URI{Scheme: "bzz-raw", Addr: "abc123"}, expectRaw: true, }, { - uri: "bzzr:/abc123/path/to/entry", - expectURI: &URI{Scheme: "bzzr", Addr: "abc123", Path: "path/to/entry"}, + uri: "bzz-raw:/abc123/path/to/entry", + expectURI: &URI{Scheme: "bzz-raw", Addr: "abc123", Path: "path/to/entry"}, expectRaw: true, }, { @@ -95,6 +98,36 @@ func TestParseURI(t *testing.T) { uri: "bzz://abc123/path/to/entry", expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"}, }, + { + uri: "bzz-list:", + expectURI: &URI{Scheme: "bzz-list"}, + expectList: true, + }, + { + uri: "bzz-list:/", + expectURI: &URI{Scheme: "bzz-list"}, + expectList: true, + }, + { + uri: "bzzr:", + expectURI: &URI{Scheme: "bzzr"}, + expectDeprecatedRaw: true, + }, + { + uri: "bzzr:/", + expectURI: &URI{Scheme: "bzzr"}, + expectDeprecatedRaw: true, + }, + { + uri: "bzzi:", + expectURI: &URI{Scheme: "bzzi"}, + expectDeprecatedImmutable: true, + }, + { + uri: "bzzi:/", + expectURI: &URI{Scheme: "bzzi"}, + expectDeprecatedImmutable: true, + }, } for _, x := range tests { actual, err := Parse(x.uri) @@ -116,5 +149,14 @@ func TestParseURI(t *testing.T) { if actual.Immutable() != x.expectImmutable { t.Fatalf("expected %s immutable to be %t, got %t", x.uri, x.expectImmutable, actual.Immutable()) } + if actual.List() != x.expectList { + t.Fatalf("expected %s list to be %t, got %t", x.uri, x.expectList, actual.List()) + } + if actual.DeprecatedRaw() != x.expectDeprecatedRaw { + t.Fatalf("expected %s deprecated raw to be %t, got %t", x.uri, x.expectDeprecatedRaw, actual.DeprecatedRaw()) + } + if actual.DeprecatedImmutable() != x.expectDeprecatedImmutable { + t.Fatalf("expected %s deprecated immutable to be %t, got %t", x.uri, x.expectDeprecatedImmutable, actual.DeprecatedImmutable()) + } } } diff --git a/swarm/dev/scripts/random-uploads.sh b/swarm/dev/scripts/random-uploads.sh index db7887e3c0..563a51befc 100755 --- a/swarm/dev/scripts/random-uploads.sh +++ b/swarm/dev/scripts/random-uploads.sh @@ -46,7 +46,7 @@ main() { } do_random_upload() { - curl -fsSL -X POST --data-binary "$(random_data)" "http://${addr}/bzzr:/" + curl -fsSL -X POST --data-binary "$(random_data)" "http://${addr}/bzz-raw:/" } random_data() { From 50df2b78be038fceb6975582a5f07052de939cbc Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Tue, 19 Dec 2017 13:21:03 +0100 Subject: [PATCH 12/12] console: create datadir at startup (#15700) Fixes #15672 by creating the datadir when creating the console. This prevents failing to save the history if no datadir exists. --- console/console.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/console/console.go b/console/console.go index 1ecbfd0b0f..52fe1f542a 100644 --- a/console/console.go +++ b/console/console.go @@ -92,6 +92,9 @@ func New(config Config) (*Console, error) { printer: config.Printer, histPath: filepath.Join(config.DataDir, HistoryFile), } + if err := os.MkdirAll(config.DataDir, 0700); err != nil { + return nil, err + } if err := console.init(config.Preload); err != nil { return nil, err } @@ -423,7 +426,7 @@ func (c *Console) Execute(path string) error { return c.jsre.Exec(path) } -// Stop cleans up the console and terminates the runtime envorinment. +// Stop cleans up the console and terminates the runtime environment. func (c *Console) Stop(graceful bool) error { if err := ioutil.WriteFile(c.histPath, []byte(strings.Join(c.history, "\n")), 0600); err != nil { return err