diff --git a/p2p/rlpx/buffer.go b/p2p/rlpx/buffer.go
new file mode 100644
index 000000000..bb38e1057
--- /dev/null
+++ b/p2p/rlpx/buffer.go
@@ -0,0 +1,127 @@
+// Copyright 2021 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see .
+
+package rlpx
+
+import (
+ "io"
+)
+
+// readBuffer implements buffering for network reads. This type is similar to bufio.Reader,
+// with two crucial differences: the buffer slice is exposed, and the buffer keeps all
+// read data available until reset.
+//
+// How to use this type:
+//
+// Keep a readBuffer b alongside the underlying network connection. When reading a packet
+// from the connection, first call b.reset(). This empties b.data. Now perform reads
+// through b.read() until the end of the packet is reached. The complete packet data is
+// now available in b.data.
+type readBuffer struct {
+ data []byte
+ end int
+}
+
+// reset removes all processed data which was read since the last call to reset.
+// After reset, len(b.data) is zero.
+func (b *readBuffer) reset() {
+ unprocessed := b.end - len(b.data)
+ copy(b.data[:unprocessed], b.data[len(b.data):b.end])
+ b.end = unprocessed
+ b.data = b.data[:0]
+}
+
+// read reads at least n bytes from r, returning the bytes.
+// The returned slice is valid until the next call to reset.
+func (b *readBuffer) read(r io.Reader, n int) ([]byte, error) {
+ offset := len(b.data)
+ have := b.end - len(b.data)
+
+ // If n bytes are available in the buffer, there is no need to read from r at all.
+ if have >= n {
+ b.data = b.data[:offset+n]
+ return b.data[offset : offset+n], nil
+ }
+
+ // Make buffer space available.
+ need := n - have
+ b.grow(need)
+
+ // Read.
+ rn, err := io.ReadAtLeast(r, b.data[b.end:cap(b.data)], need)
+ if err != nil {
+ return nil, err
+ }
+ b.end += rn
+ b.data = b.data[:offset+n]
+ return b.data[offset : offset+n], nil
+}
+
+// grow ensures the buffer has at least n bytes of unused space.
+func (b *readBuffer) grow(n int) {
+ if cap(b.data)-b.end >= n {
+ return
+ }
+ need := n - (cap(b.data) - b.end)
+ offset := len(b.data)
+ b.data = append(b.data[:cap(b.data)], make([]byte, need)...)
+ b.data = b.data[:offset]
+}
+
+// writeBuffer implements buffering for network writes. This is essentially
+// a convenience wrapper around a byte slice.
+type writeBuffer struct {
+ data []byte
+}
+
+func (b *writeBuffer) reset() {
+ b.data = b.data[:0]
+}
+
+func (b *writeBuffer) appendZero(n int) []byte {
+ offset := len(b.data)
+ b.data = append(b.data, make([]byte, n)...)
+ return b.data[offset : offset+n]
+}
+
+func (b *writeBuffer) Write(data []byte) (int, error) {
+ b.data = append(b.data, data...)
+ return len(data), nil
+}
+
+const maxUint24 = int(^uint32(0) >> 8)
+
+func readUint24(b []byte) uint32 {
+ return uint32(b[2]) | uint32(b[1])<<8 | uint32(b[0])<<16
+}
+
+func putUint24(v uint32, b []byte) {
+ b[0] = byte(v >> 16)
+ b[1] = byte(v >> 8)
+ b[2] = byte(v)
+}
+
+// growslice ensures b has the wanted length by either expanding it to its capacity
+// or allocating a new slice if b has insufficient capacity.
+func growslice(b []byte, wantLength int) []byte {
+ if len(b) >= wantLength {
+ return b
+ }
+ if cap(b) >= wantLength {
+ return b[:cap(b)]
+ }
+ return make([]byte, wantLength)
+}
diff --git a/p2p/rlpx/buffer_test.go b/p2p/rlpx/buffer_test.go
new file mode 100644
index 000000000..9fee4172b
--- /dev/null
+++ b/p2p/rlpx/buffer_test.go
@@ -0,0 +1,51 @@
+// Copyright 2021 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see .
+
+package rlpx
+
+import (
+ "bytes"
+ "testing"
+
+ "github.com/ethereum/go-ethereum/common/hexutil"
+ "github.com/stretchr/testify/assert"
+)
+
+func TestReadBufferReset(t *testing.T) {
+ reader := bytes.NewReader(hexutil.MustDecode("0x010202030303040505"))
+ var b readBuffer
+
+ s1, _ := b.read(reader, 1)
+ s2, _ := b.read(reader, 2)
+ s3, _ := b.read(reader, 3)
+
+ assert.Equal(t, []byte{1}, s1)
+ assert.Equal(t, []byte{2, 2}, s2)
+ assert.Equal(t, []byte{3, 3, 3}, s3)
+
+ b.reset()
+
+ s4, _ := b.read(reader, 1)
+ s5, _ := b.read(reader, 2)
+
+ assert.Equal(t, []byte{4}, s4)
+ assert.Equal(t, []byte{5, 5}, s5)
+
+ s6, err := b.read(reader, 2)
+
+ assert.EqualError(t, err, "EOF")
+ assert.Nil(t, s6)
+}
diff --git a/p2p/rlpx/rlpx.go b/p2p/rlpx/rlpx.go
index d6dd8c8c8..48d039f92 100644
--- a/p2p/rlpx/rlpx.go
+++ b/p2p/rlpx/rlpx.go
@@ -47,19 +47,45 @@ import (
// This type is not generally safe for concurrent use, but reading and writing of messages
// may happen concurrently after the handshake.
type Conn struct {
- dialDest *ecdsa.PublicKey
- conn net.Conn
- handshake *handshakeState
- snappy bool
+ dialDest *ecdsa.PublicKey
+ conn net.Conn
+ session *sessionState
+
+ // These are the buffers for snappy compression.
+ // Compression is enabled if they are non-nil.
+ snappyReadBuffer []byte
+ snappyWriteBuffer []byte
}
-type handshakeState struct {
+// sessionState contains the session keys.
+type sessionState struct {
enc cipher.Stream
dec cipher.Stream
- macCipher cipher.Block
- egressMAC hash.Hash
- ingressMAC hash.Hash
+ egressMAC hashMAC
+ ingressMAC hashMAC
+ rbuf readBuffer
+ wbuf writeBuffer
+}
+
+// hashMAC holds the state of the RLPx v4 MAC contraption.
+type hashMAC struct {
+ cipher cipher.Block
+ hash hash.Hash
+ aesBuffer [16]byte
+ hashBuffer [32]byte
+ seedBuffer [32]byte
+}
+
+func newHashMAC(cipher cipher.Block, h hash.Hash) hashMAC {
+ m := hashMAC{cipher: cipher, hash: h}
+ if cipher.BlockSize() != len(m.aesBuffer) {
+ panic(fmt.Errorf("invalid MAC cipher block size %d", cipher.BlockSize()))
+ }
+ if h.Size() != len(m.hashBuffer) {
+ panic(fmt.Errorf("invalid MAC digest size %d", h.Size()))
+ }
+ return m
}
// NewConn wraps the given network connection. If dialDest is non-nil, the connection
@@ -75,7 +101,13 @@ func NewConn(conn net.Conn, dialDest *ecdsa.PublicKey) *Conn {
// after the devp2p Hello message exchange when the negotiated version indicates that
// compression is available on both ends of the connection.
func (c *Conn) SetSnappy(snappy bool) {
- c.snappy = snappy
+ if snappy {
+ c.snappyReadBuffer = []byte{}
+ c.snappyWriteBuffer = []byte{}
+ } else {
+ c.snappyReadBuffer = nil
+ c.snappyWriteBuffer = nil
+ }
}
// SetReadDeadline sets the deadline for all future read operations.
@@ -94,12 +126,13 @@ func (c *Conn) SetDeadline(time time.Time) error {
}
// Read reads a message from the connection.
+// The returned data buffer is valid until the next call to Read.
func (c *Conn) Read() (code uint64, data []byte, wireSize int, err error) {
- if c.handshake == nil {
+ if c.session == nil {
panic("can't ReadMsg before handshake")
}
- frame, err := c.handshake.readFrame(c.conn)
+ frame, err := c.session.readFrame(c.conn)
if err != nil {
return 0, nil, 0, err
}
@@ -110,7 +143,7 @@ func (c *Conn) Read() (code uint64, data []byte, wireSize int, err error) {
wireSize = len(data)
// If snappy is enabled, verify and decompress message.
- if c.snappy {
+ if c.snappyReadBuffer != nil {
var actualSize int
actualSize, err = snappy.DecodedLen(data)
if err != nil {
@@ -119,51 +152,55 @@ func (c *Conn) Read() (code uint64, data []byte, wireSize int, err error) {
if actualSize > maxUint24 {
return code, nil, 0, errPlainMessageTooLarge
}
- data, err = snappy.Decode(nil, data)
+ c.snappyReadBuffer = growslice(c.snappyReadBuffer, actualSize)
+ data, err = snappy.Decode(c.snappyReadBuffer, data)
}
return code, data, wireSize, err
}
-func (h *handshakeState) readFrame(conn io.Reader) ([]byte, error) {
- // read the header
- headbuf := make([]byte, 32)
- if _, err := io.ReadFull(conn, headbuf); err != nil {
+func (h *sessionState) readFrame(conn io.Reader) ([]byte, error) {
+ h.rbuf.reset()
+
+ // Read the frame header.
+ header, err := h.rbuf.read(conn, 32)
+ if err != nil {
return nil, err
}
- // verify header mac
- shouldMAC := updateMAC(h.ingressMAC, h.macCipher, headbuf[:16])
- if !hmac.Equal(shouldMAC, headbuf[16:]) {
+ // Verify header MAC.
+ wantHeaderMAC := h.ingressMAC.computeHeader(header[:16])
+ if !hmac.Equal(wantHeaderMAC, header[16:]) {
return nil, errors.New("bad header MAC")
}
- h.dec.XORKeyStream(headbuf[:16], headbuf[:16]) // first half is now decrypted
- fsize := readInt24(headbuf)
- // ignore protocol type for now
- // read the frame content
- var rsize = fsize // frame size rounded up to 16 byte boundary
+ // Decrypt the frame header to get the frame size.
+ h.dec.XORKeyStream(header[:16], header[:16])
+ fsize := readUint24(header[:16])
+ // Frame size rounded up to 16 byte boundary for padding.
+ rsize := fsize
if padding := fsize % 16; padding > 0 {
rsize += 16 - padding
}
- framebuf := make([]byte, rsize)
- if _, err := io.ReadFull(conn, framebuf); err != nil {
+
+ // Read the frame content.
+ frame, err := h.rbuf.read(conn, int(rsize))
+ if err != nil {
return nil, err
}
- // read and validate frame MAC. we can re-use headbuf for that.
- h.ingressMAC.Write(framebuf)
- fmacseed := h.ingressMAC.Sum(nil)
- if _, err := io.ReadFull(conn, headbuf[:16]); err != nil {
+ // Validate frame MAC.
+ frameMAC, err := h.rbuf.read(conn, 16)
+ if err != nil {
return nil, err
}
- shouldMAC = updateMAC(h.ingressMAC, h.macCipher, fmacseed)
- if !hmac.Equal(shouldMAC, headbuf[:16]) {
+ wantFrameMAC := h.ingressMAC.computeFrame(frame)
+ if !hmac.Equal(wantFrameMAC, frameMAC) {
return nil, errors.New("bad frame MAC")
}
- // decrypt frame content
- h.dec.XORKeyStream(framebuf, framebuf)
- return framebuf[:fsize], nil
+ // Decrypt the frame data.
+ h.dec.XORKeyStream(frame, frame)
+ return frame[:fsize], nil
}
// Write writes a message to the connection.
@@ -171,83 +208,90 @@ func (h *handshakeState) readFrame(conn io.Reader) ([]byte, error) {
// Write returns the written size of the message data. This may be less than or equal to
// len(data) depending on whether snappy compression is enabled.
func (c *Conn) Write(code uint64, data []byte) (uint32, error) {
- if c.handshake == nil {
+ if c.session == nil {
panic("can't WriteMsg before handshake")
}
if len(data) > maxUint24 {
return 0, errPlainMessageTooLarge
}
- if c.snappy {
- data = snappy.Encode(nil, data)
+ if c.snappyWriteBuffer != nil {
+ // Ensure the buffer has sufficient size.
+ // Package snappy will allocate its own buffer if the provided
+ // one is smaller than MaxEncodedLen.
+ c.snappyWriteBuffer = growslice(c.snappyWriteBuffer, snappy.MaxEncodedLen(len(data)))
+ data = snappy.Encode(c.snappyWriteBuffer, data)
}
wireSize := uint32(len(data))
- err := c.handshake.writeFrame(c.conn, code, data)
+ err := c.session.writeFrame(c.conn, code, data)
return wireSize, err
}
-func (h *handshakeState) writeFrame(conn io.Writer, code uint64, data []byte) error {
- ptype, _ := rlp.EncodeToBytes(code)
+func (h *sessionState) writeFrame(conn io.Writer, code uint64, data []byte) error {
+ h.wbuf.reset()
- // write header
- headbuf := make([]byte, 32)
- fsize := len(ptype) + len(data)
+ // Write header.
+ fsize := rlp.IntSize(code) + len(data)
if fsize > maxUint24 {
return errPlainMessageTooLarge
}
- putInt24(uint32(fsize), headbuf)
- copy(headbuf[3:], zeroHeader)
- h.enc.XORKeyStream(headbuf[:16], headbuf[:16]) // first half is now encrypted
+ header := h.wbuf.appendZero(16)
+ putUint24(uint32(fsize), header)
+ copy(header[3:], zeroHeader)
+ h.enc.XORKeyStream(header, header)
- // write header MAC
- copy(headbuf[16:], updateMAC(h.egressMAC, h.macCipher, headbuf[:16]))
- if _, err := conn.Write(headbuf); err != nil {
- return err
- }
+ // Write header MAC.
+ h.wbuf.Write(h.egressMAC.computeHeader(header))
- // write encrypted frame, updating the egress MAC hash with
- // the data written to conn.
- tee := cipher.StreamWriter{S: h.enc, W: io.MultiWriter(conn, h.egressMAC)}
- if _, err := tee.Write(ptype); err != nil {
- return err
- }
- if _, err := tee.Write(data); err != nil {
- return err
- }
+ // Encode and encrypt the frame data.
+ offset := len(h.wbuf.data)
+ h.wbuf.data = rlp.AppendUint64(h.wbuf.data, code)
+ h.wbuf.Write(data)
if padding := fsize % 16; padding > 0 {
- if _, err := tee.Write(zero16[:16-padding]); err != nil {
- return err
- }
+ h.wbuf.appendZero(16 - padding)
}
+ framedata := h.wbuf.data[offset:]
+ h.enc.XORKeyStream(framedata, framedata)
+
+ // Write frame MAC.
+ h.wbuf.Write(h.egressMAC.computeFrame(framedata))
- // write frame MAC. egress MAC hash is up to date because
- // frame content was written to it as well.
- fmacseed := h.egressMAC.Sum(nil)
- mac := updateMAC(h.egressMAC, h.macCipher, fmacseed)
- _, err := conn.Write(mac)
+ _, err := conn.Write(h.wbuf.data)
return err
}
-func readInt24(b []byte) uint32 {
- return uint32(b[2]) | uint32(b[1])<<8 | uint32(b[0])<<16
+// computeHeader computes the MAC of a frame header.
+func (m *hashMAC) computeHeader(header []byte) []byte {
+ sum1 := m.hash.Sum(m.hashBuffer[:0])
+ return m.compute(sum1, header)
}
-func putInt24(v uint32, b []byte) {
- b[0] = byte(v >> 16)
- b[1] = byte(v >> 8)
- b[2] = byte(v)
+// computeFrame computes the MAC of framedata.
+func (m *hashMAC) computeFrame(framedata []byte) []byte {
+ m.hash.Write(framedata)
+ seed := m.hash.Sum(m.seedBuffer[:0])
+ return m.compute(seed, seed[:16])
}
-// updateMAC reseeds the given hash with encrypted seed.
-// it returns the first 16 bytes of the hash sum after seeding.
-func updateMAC(mac hash.Hash, block cipher.Block, seed []byte) []byte {
- aesbuf := make([]byte, aes.BlockSize)
- block.Encrypt(aesbuf, mac.Sum(nil))
- for i := range aesbuf {
- aesbuf[i] ^= seed[i]
+// compute computes the MAC of a 16-byte 'seed'.
+//
+// To do this, it encrypts the current value of the hash state, then XORs the ciphertext
+// with seed. The obtained value is written back into the hash state and hash output is
+// taken again. The first 16 bytes of the resulting sum are the MAC value.
+//
+// This MAC construction is a horrible, legacy thing.
+func (m *hashMAC) compute(sum1, seed []byte) []byte {
+ if len(seed) != len(m.aesBuffer) {
+ panic("invalid MAC seed")
}
- mac.Write(aesbuf)
- return mac.Sum(nil)[:16]
+
+ m.cipher.Encrypt(m.aesBuffer[:], sum1)
+ for i := range m.aesBuffer {
+ m.aesBuffer[i] ^= seed[i]
+ }
+ m.hash.Write(m.aesBuffer[:])
+ sum2 := m.hash.Sum(m.hashBuffer[:0])
+ return sum2[:16]
}
// Handshake performs the handshake. This must be called before any data is written
@@ -256,23 +300,26 @@ func (c *Conn) Handshake(prv *ecdsa.PrivateKey) (*ecdsa.PublicKey, error) {
var (
sec Secrets
err error
+ h handshakeState
)
if c.dialDest != nil {
- sec, err = initiatorEncHandshake(c.conn, prv, c.dialDest)
+ sec, err = h.runInitiator(c.conn, prv, c.dialDest)
} else {
- sec, err = receiverEncHandshake(c.conn, prv)
+ sec, err = h.runRecipient(c.conn, prv)
}
if err != nil {
return nil, err
}
c.InitWithSecrets(sec)
+ c.session.rbuf = h.rbuf
+ c.session.wbuf = h.wbuf
return sec.remote, err
}
// InitWithSecrets injects connection secrets as if a handshake had
// been performed. This cannot be called after the handshake.
func (c *Conn) InitWithSecrets(sec Secrets) {
- if c.handshake != nil {
+ if c.session != nil {
panic("can't handshake twice")
}
macc, err := aes.NewCipher(sec.MAC)
@@ -286,12 +333,11 @@ func (c *Conn) InitWithSecrets(sec Secrets) {
// we use an all-zeroes IV for AES because the key used
// for encryption is ephemeral.
iv := make([]byte, encc.BlockSize())
- c.handshake = &handshakeState{
+ c.session = &sessionState{
enc: cipher.NewCTR(encc, iv),
dec: cipher.NewCTR(encc, iv),
- macCipher: macc,
- egressMAC: sec.EgressMAC,
- ingressMAC: sec.IngressMAC,
+ egressMAC: newHashMAC(macc, sec.EgressMAC),
+ ingressMAC: newHashMAC(macc, sec.IngressMAC),
}
}
@@ -302,28 +348,18 @@ func (c *Conn) Close() error {
// Constants for the handshake.
const (
- maxUint24 = int(^uint32(0) >> 8)
-
sskLen = 16 // ecies.MaxSharedKeyLength(pubKey) / 2
sigLen = crypto.SignatureLength // elliptic S256
pubLen = 64 // 512 bit pubkey in uncompressed representation without format byte
shaLen = 32 // hash length (for nonce etc)
- authMsgLen = sigLen + shaLen + pubLen + shaLen + 1
- authRespLen = pubLen + shaLen + 1
-
eciesOverhead = 65 /* pubkey */ + 16 /* IV */ + 32 /* MAC */
-
- encAuthMsgLen = authMsgLen + eciesOverhead // size of encrypted pre-EIP-8 initiator handshake
- encAuthRespLen = authRespLen + eciesOverhead // size of encrypted pre-EIP-8 handshake reply
)
var (
// this is used in place of actual frame header data.
// TODO: replace this when Msg contains the protocol type code.
zeroHeader = []byte{0xC2, 0x80, 0x80}
- // sixteen zero bytes
- zero16 = make([]byte, 16)
// errPlainMessageTooLarge is returned if a decompressed message length exceeds
// the allowed 24 bits (i.e. length >= 16MB).
@@ -337,19 +373,20 @@ type Secrets struct {
remote *ecdsa.PublicKey
}
-// encHandshake contains the state of the encryption handshake.
-type encHandshake struct {
+// handshakeState contains the state of the encryption handshake.
+type handshakeState struct {
initiator bool
remote *ecies.PublicKey // remote-pubk
initNonce, respNonce []byte // nonce
randomPrivKey *ecies.PrivateKey // ecdhe-random
remoteRandomPub *ecies.PublicKey // ecdhe-random-pubk
+
+ rbuf readBuffer
+ wbuf writeBuffer
}
// RLPx v4 handshake auth (defined in EIP-8).
type authMsgV4 struct {
- gotPlain bool // whether read packet had plain format.
-
Signature [sigLen]byte
InitiatorPubkey [pubLen]byte
Nonce [shaLen]byte
@@ -369,17 +406,16 @@ type authRespV4 struct {
Rest []rlp.RawValue `rlp:"tail"`
}
-// receiverEncHandshake negotiates a session token on conn.
+// runRecipient negotiates a session token on conn.
// it should be called on the listening side of the connection.
//
// prv is the local client's private key.
-func receiverEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey) (s Secrets, err error) {
+func (h *handshakeState) runRecipient(conn io.ReadWriter, prv *ecdsa.PrivateKey) (s Secrets, err error) {
authMsg := new(authMsgV4)
- authPacket, err := readHandshakeMsg(authMsg, encAuthMsgLen, prv, conn)
+ authPacket, err := h.readMsg(authMsg, prv, conn)
if err != nil {
return s, err
}
- h := new(encHandshake)
if err := h.handleAuthMsg(authMsg, prv); err != nil {
return s, err
}
@@ -388,12 +424,7 @@ func receiverEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey) (s Secrets,
if err != nil {
return s, err
}
- var authRespPacket []byte
- if authMsg.gotPlain {
- authRespPacket, err = authRespMsg.sealPlain(h)
- } else {
- authRespPacket, err = sealEIP8(authRespMsg, h)
- }
+ authRespPacket, err := h.sealEIP8(authRespMsg)
if err != nil {
return s, err
}
@@ -403,7 +434,7 @@ func receiverEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey) (s Secrets,
return h.secrets(authPacket, authRespPacket)
}
-func (h *encHandshake) handleAuthMsg(msg *authMsgV4, prv *ecdsa.PrivateKey) error {
+func (h *handshakeState) handleAuthMsg(msg *authMsgV4, prv *ecdsa.PrivateKey) error {
// Import the remote identity.
rpub, err := importPublicKey(msg.InitiatorPubkey[:])
if err != nil {
@@ -437,7 +468,7 @@ func (h *encHandshake) handleAuthMsg(msg *authMsgV4, prv *ecdsa.PrivateKey) erro
// secrets is called after the handshake is completed.
// It extracts the connection secrets from the handshake values.
-func (h *encHandshake) secrets(auth, authResp []byte) (Secrets, error) {
+func (h *handshakeState) secrets(auth, authResp []byte) (Secrets, error) {
ecdheSecret, err := h.randomPrivKey.GenerateShared(h.remoteRandomPub, sskLen, sskLen)
if err != nil {
return Secrets{}, err
@@ -470,21 +501,22 @@ func (h *encHandshake) secrets(auth, authResp []byte) (Secrets, error) {
// staticSharedSecret returns the static shared secret, the result
// of key agreement between the local and remote static node key.
-func (h *encHandshake) staticSharedSecret(prv *ecdsa.PrivateKey) ([]byte, error) {
+func (h *handshakeState) staticSharedSecret(prv *ecdsa.PrivateKey) ([]byte, error) {
return ecies.ImportECDSA(prv).GenerateShared(h.remote, sskLen, sskLen)
}
-// initiatorEncHandshake negotiates a session token on conn.
+// runInitiator negotiates a session token on conn.
// it should be called on the dialing side of the connection.
//
// prv is the local client's private key.
-func initiatorEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey, remote *ecdsa.PublicKey) (s Secrets, err error) {
- h := &encHandshake{initiator: true, remote: ecies.ImportECDSAPublic(remote)}
+func (h *handshakeState) runInitiator(conn io.ReadWriter, prv *ecdsa.PrivateKey, remote *ecdsa.PublicKey) (s Secrets, err error) {
+ h.initiator = true
+ h.remote = ecies.ImportECDSAPublic(remote)
authMsg, err := h.makeAuthMsg(prv)
if err != nil {
return s, err
}
- authPacket, err := sealEIP8(authMsg, h)
+ authPacket, err := h.sealEIP8(authMsg)
if err != nil {
return s, err
}
@@ -494,7 +526,7 @@ func initiatorEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey, remote *ec
}
authRespMsg := new(authRespV4)
- authRespPacket, err := readHandshakeMsg(authRespMsg, encAuthRespLen, prv, conn)
+ authRespPacket, err := h.readMsg(authRespMsg, prv, conn)
if err != nil {
return s, err
}
@@ -505,7 +537,7 @@ func initiatorEncHandshake(conn io.ReadWriter, prv *ecdsa.PrivateKey, remote *ec
}
// makeAuthMsg creates the initiator handshake message.
-func (h *encHandshake) makeAuthMsg(prv *ecdsa.PrivateKey) (*authMsgV4, error) {
+func (h *handshakeState) makeAuthMsg(prv *ecdsa.PrivateKey) (*authMsgV4, error) {
// Generate random initiator nonce.
h.initNonce = make([]byte, shaLen)
_, err := rand.Read(h.initNonce)
@@ -537,13 +569,13 @@ func (h *encHandshake) makeAuthMsg(prv *ecdsa.PrivateKey) (*authMsgV4, error) {
return msg, nil
}
-func (h *encHandshake) handleAuthResp(msg *authRespV4) (err error) {
+func (h *handshakeState) handleAuthResp(msg *authRespV4) (err error) {
h.respNonce = msg.Nonce[:]
h.remoteRandomPub, err = importPublicKey(msg.RandomPubkey[:])
return err
}
-func (h *encHandshake) makeAuthResp() (msg *authRespV4, err error) {
+func (h *handshakeState) makeAuthResp() (msg *authRespV4, err error) {
// Generate random nonce.
h.respNonce = make([]byte, shaLen)
if _, err = rand.Read(h.respNonce); err != nil {
@@ -557,81 +589,53 @@ func (h *encHandshake) makeAuthResp() (msg *authRespV4, err error) {
return msg, nil
}
-func (msg *authMsgV4) decodePlain(input []byte) {
- n := copy(msg.Signature[:], input)
- n += shaLen // skip sha3(initiator-ephemeral-pubk)
- n += copy(msg.InitiatorPubkey[:], input[n:])
- copy(msg.Nonce[:], input[n:])
- msg.Version = 4
- msg.gotPlain = true
-}
+// readMsg reads an encrypted handshake message, decoding it into msg.
+func (h *handshakeState) readMsg(msg interface{}, prv *ecdsa.PrivateKey, r io.Reader) ([]byte, error) {
+ h.rbuf.reset()
+ h.rbuf.grow(512)
-func (msg *authRespV4) sealPlain(hs *encHandshake) ([]byte, error) {
- buf := make([]byte, authRespLen)
- n := copy(buf, msg.RandomPubkey[:])
- copy(buf[n:], msg.Nonce[:])
- return ecies.Encrypt(rand.Reader, hs.remote, buf, nil, nil)
-}
+ // Read the size prefix.
+ prefix, err := h.rbuf.read(r, 2)
+ if err != nil {
+ return nil, err
+ }
+ size := binary.BigEndian.Uint16(prefix)
-func (msg *authRespV4) decodePlain(input []byte) {
- n := copy(msg.RandomPubkey[:], input)
- copy(msg.Nonce[:], input[n:])
- msg.Version = 4
+ // Read the handshake packet.
+ packet, err := h.rbuf.read(r, int(size))
+ if err != nil {
+ return nil, err
+ }
+ dec, err := ecies.ImportECDSA(prv).Decrypt(packet, nil, prefix)
+ if err != nil {
+ return nil, err
+ }
+ // Can't use rlp.DecodeBytes here because it rejects
+ // trailing data (forward-compatibility).
+ s := rlp.NewStream(bytes.NewReader(dec), 0)
+ err = s.Decode(msg)
+ return h.rbuf.data[:len(prefix)+len(packet)], err
}
-var padSpace = make([]byte, 300)
+// sealEIP8 encrypts a handshake message.
+func (h *handshakeState) sealEIP8(msg interface{}) ([]byte, error) {
+ h.wbuf.reset()
-func sealEIP8(msg interface{}, h *encHandshake) ([]byte, error) {
- buf := new(bytes.Buffer)
- if err := rlp.Encode(buf, msg); err != nil {
+ // Write the message plaintext.
+ if err := rlp.Encode(&h.wbuf, msg); err != nil {
return nil, err
}
- // pad with random amount of data. the amount needs to be at least 100 bytes to make
+ // Pad with random amount of data. the amount needs to be at least 100 bytes to make
// the message distinguishable from pre-EIP-8 handshakes.
- pad := padSpace[:mrand.Intn(len(padSpace)-100)+100]
- buf.Write(pad)
+ h.wbuf.appendZero(mrand.Intn(100) + 100)
+
prefix := make([]byte, 2)
- binary.BigEndian.PutUint16(prefix, uint16(buf.Len()+eciesOverhead))
+ binary.BigEndian.PutUint16(prefix, uint16(len(h.wbuf.data)+eciesOverhead))
- enc, err := ecies.Encrypt(rand.Reader, h.remote, buf.Bytes(), nil, prefix)
+ enc, err := ecies.Encrypt(rand.Reader, h.remote, h.wbuf.data, nil, prefix)
return append(prefix, enc...), err
}
-type plainDecoder interface {
- decodePlain([]byte)
-}
-
-func readHandshakeMsg(msg plainDecoder, plainSize int, prv *ecdsa.PrivateKey, r io.Reader) ([]byte, error) {
- buf := make([]byte, plainSize)
- if _, err := io.ReadFull(r, buf); err != nil {
- return buf, err
- }
- // Attempt decoding pre-EIP-8 "plain" format.
- key := ecies.ImportECDSA(prv)
- if dec, err := key.Decrypt(buf, nil, nil); err == nil {
- msg.decodePlain(dec)
- return buf, nil
- }
- // Could be EIP-8 format, try that.
- prefix := buf[:2]
- size := binary.BigEndian.Uint16(prefix)
- if size < uint16(plainSize) {
- return buf, fmt.Errorf("size underflow, need at least %d bytes", plainSize)
- }
- buf = append(buf, make([]byte, size-uint16(plainSize)+2)...)
- if _, err := io.ReadFull(r, buf[plainSize:]); err != nil {
- return buf, err
- }
- dec, err := key.Decrypt(buf[2:], nil, prefix)
- if err != nil {
- return buf, err
- }
- // Can't use rlp.DecodeBytes here because it rejects
- // trailing data (forward-compatibility).
- s := rlp.NewStream(bytes.NewReader(dec), 0)
- return buf, s.Decode(msg)
-}
-
// importPublicKey unmarshals 512 bit public keys.
func importPublicKey(pubKey []byte) (*ecies.PublicKey, error) {
var pubKey65 []byte
diff --git a/p2p/rlpx/rlpx_test.go b/p2p/rlpx/rlpx_test.go
index 127a01816..28759f2b4 100644
--- a/p2p/rlpx/rlpx_test.go
+++ b/p2p/rlpx/rlpx_test.go
@@ -22,6 +22,7 @@ import (
"encoding/hex"
"fmt"
"io"
+ "math/rand"
"net"
"reflect"
"strings"
@@ -30,6 +31,7 @@ import (
"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies"
+ "github.com/ethereum/go-ethereum/p2p/simulations/pipes"
"github.com/ethereum/go-ethereum/rlp"
"github.com/stretchr/testify/assert"
)
@@ -124,7 +126,7 @@ func TestFrameReadWrite(t *testing.T) {
IngressMAC: hash,
EgressMAC: hash,
})
- h := conn.handshake
+ h := conn.session
golden := unhex(`
00828ddae471818bb0bfa6b551d1cb42
@@ -166,27 +168,11 @@ func (h fakeHash) Sum(b []byte) []byte { return append(b, h...) }
type handshakeAuthTest struct {
input string
- isPlain bool
wantVersion uint
wantRest []rlp.RawValue
}
var eip8HandshakeAuthTests = []handshakeAuthTest{
- // (Auth₁) RLPx v4 plain encoding
- {
- input: `
- 048ca79ad18e4b0659fab4853fe5bc58eb83992980f4c9cc147d2aa31532efd29a3d3dc6a3d89eaf
- 913150cfc777ce0ce4af2758bf4810235f6e6ceccfee1acc6b22c005e9e3a49d6448610a58e98744
- ba3ac0399e82692d67c1f58849050b3024e21a52c9d3b01d871ff5f210817912773e610443a9ef14
- 2e91cdba0bd77b5fdf0769b05671fc35f83d83e4d3b0b000c6b2a1b1bba89e0fc51bf4e460df3105
- c444f14be226458940d6061c296350937ffd5e3acaceeaaefd3c6f74be8e23e0f45163cc7ebd7622
- 0f0128410fd05250273156d548a414444ae2f7dea4dfca2d43c057adb701a715bf59f6fb66b2d1d2
- 0f2c703f851cbf5ac47396d9ca65b6260bd141ac4d53e2de585a73d1750780db4c9ee4cd4d225173
- a4592ee77e2bd94d0be3691f3b406f9bba9b591fc63facc016bfa8
- `,
- isPlain: true,
- wantVersion: 4,
- },
// (Auth₂) EIP-8 encoding
{
input: `
@@ -233,18 +219,6 @@ type handshakeAckTest struct {
}
var eip8HandshakeRespTests = []handshakeAckTest{
- // (Ack₁) RLPx v4 plain encoding
- {
- input: `
- 049f8abcfa9c0dc65b982e98af921bc0ba6e4243169348a236abe9df5f93aa69d99cadddaa387662
- b0ff2c08e9006d5a11a278b1b3331e5aaabf0a32f01281b6f4ede0e09a2d5f585b26513cb794d963
- 5a57563921c04a9090b4f14ee42be1a5461049af4ea7a7f49bf4c97a352d39c8d02ee4acc416388c
- 1c66cec761d2bc1c72da6ba143477f049c9d2dde846c252c111b904f630ac98e51609b3b1f58168d
- dca6505b7196532e5f85b259a20c45e1979491683fee108e9660edbf38f3add489ae73e3dda2c71b
- d1497113d5c755e942d1
- `,
- wantVersion: 4,
- },
// (Ack₂) EIP-8 encoding
{
input: `
@@ -287,10 +261,13 @@ var eip8HandshakeRespTests = []handshakeAckTest{
},
}
+var (
+ keyA, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
+ keyB, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
+)
+
func TestHandshakeForwardCompatibility(t *testing.T) {
var (
- keyA, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
- keyB, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
pubA = crypto.FromECDSAPub(&keyA.PublicKey)[1:]
pubB = crypto.FromECDSAPub(&keyB.PublicKey)[1:]
ephA, _ = crypto.HexToECDSA("869d6ecf5211f1cc60418a13b9d870b22959d0c16f02bec714c960dd2298a32d")
@@ -304,7 +281,7 @@ func TestHandshakeForwardCompatibility(t *testing.T) {
_ = authSignature
)
makeAuth := func(test handshakeAuthTest) *authMsgV4 {
- msg := &authMsgV4{Version: test.wantVersion, Rest: test.wantRest, gotPlain: test.isPlain}
+ msg := &authMsgV4{Version: test.wantVersion, Rest: test.wantRest}
copy(msg.Signature[:], authSignature)
copy(msg.InitiatorPubkey[:], pubA)
copy(msg.Nonce[:], nonceA)
@@ -319,9 +296,10 @@ func TestHandshakeForwardCompatibility(t *testing.T) {
// check auth msg parsing
for _, test := range eip8HandshakeAuthTests {
+ var h handshakeState
r := bytes.NewReader(unhex(test.input))
msg := new(authMsgV4)
- ciphertext, err := readHandshakeMsg(msg, encAuthMsgLen, keyB, r)
+ ciphertext, err := h.readMsg(msg, keyB, r)
if err != nil {
t.Errorf("error for input %x:\n %v", unhex(test.input), err)
continue
@@ -337,10 +315,11 @@ func TestHandshakeForwardCompatibility(t *testing.T) {
// check auth resp parsing
for _, test := range eip8HandshakeRespTests {
+ var h handshakeState
input := unhex(test.input)
r := bytes.NewReader(input)
msg := new(authRespV4)
- ciphertext, err := readHandshakeMsg(msg, encAuthRespLen, keyA, r)
+ ciphertext, err := h.readMsg(msg, keyA, r)
if err != nil {
t.Errorf("error for input %x:\n %v", input, err)
continue
@@ -356,14 +335,14 @@ func TestHandshakeForwardCompatibility(t *testing.T) {
// check derivation for (Auth₂, Ack₂) on recipient side
var (
- hs = &encHandshake{
+ hs = &handshakeState{
initiator: false,
respNonce: nonceB,
randomPrivKey: ecies.ImportECDSA(ephB),
}
- authCiphertext = unhex(eip8HandshakeAuthTests[1].input)
- authRespCiphertext = unhex(eip8HandshakeRespTests[1].input)
- authMsg = makeAuth(eip8HandshakeAuthTests[1])
+ authCiphertext = unhex(eip8HandshakeAuthTests[0].input)
+ authRespCiphertext = unhex(eip8HandshakeRespTests[0].input)
+ authMsg = makeAuth(eip8HandshakeAuthTests[0])
wantAES = unhex("80e8632c05fed6fc2a13b0f8d31a3cf645366239170ea067065aba8e28bac487")
wantMAC = unhex("2ea74ec5dae199227dff1af715362700e989d889d7a493cb0639691efb8e5f98")
wantFooIngressHash = unhex("0c7ec6340062cc46f5e9f1e3cf86f8c8c403c5a0964f5df0ebd34a75ddc86db5")
@@ -388,6 +367,74 @@ func TestHandshakeForwardCompatibility(t *testing.T) {
}
}
+func BenchmarkHandshakeRead(b *testing.B) {
+ var input = unhex(eip8HandshakeAuthTests[0].input)
+
+ for i := 0; i < b.N; i++ {
+ var (
+ h handshakeState
+ r = bytes.NewReader(input)
+ msg = new(authMsgV4)
+ )
+ if _, err := h.readMsg(msg, keyB, r); err != nil {
+ b.Fatal(err)
+ }
+ }
+}
+
+func BenchmarkThroughput(b *testing.B) {
+ pipe1, pipe2, err := pipes.TCPPipe()
+ if err != nil {
+ b.Fatal(err)
+ }
+
+ var (
+ conn1, conn2 = NewConn(pipe1, nil), NewConn(pipe2, &keyA.PublicKey)
+ handshakeDone = make(chan error, 1)
+ msgdata = make([]byte, 1024)
+ rand = rand.New(rand.NewSource(1337))
+ )
+ rand.Read(msgdata)
+
+ // Server side.
+ go func() {
+ defer conn1.Close()
+ // Perform handshake.
+ _, err := conn1.Handshake(keyA)
+ handshakeDone <- err
+ if err != nil {
+ return
+ }
+ conn1.SetSnappy(true)
+ // Keep sending messages until connection closed.
+ for {
+ if _, err := conn1.Write(0, msgdata); err != nil {
+ return
+ }
+ }
+ }()
+
+ // Set up client side.
+ defer conn2.Close()
+ if _, err := conn2.Handshake(keyB); err != nil {
+ b.Fatal("client handshake error:", err)
+ }
+ conn2.SetSnappy(true)
+ if err := <-handshakeDone; err != nil {
+ b.Fatal("server hanshake error:", err)
+ }
+
+ // Read N messages.
+ b.SetBytes(int64(len(msgdata)))
+ b.ReportAllocs()
+ for i := 0; i < b.N; i++ {
+ _, _, _, err := conn2.Read()
+ if err != nil {
+ b.Fatal("read error:", err)
+ }
+ }
+}
+
func unhex(str string) []byte {
r := strings.NewReplacer("\t", "", " ", "", "\n", "")
b, err := hex.DecodeString(r.Replace(str))
diff --git a/p2p/transport.go b/p2p/transport.go
index 2d5a9a117..b4841bcd1 100644
--- a/p2p/transport.go
+++ b/p2p/transport.go
@@ -26,6 +26,7 @@ import (
"sync"
"time"
+ "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/bitutil"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/p2p/rlpx"
@@ -63,6 +64,10 @@ func (t *rlpxTransport) ReadMsg() (Msg, error) {
t.conn.SetReadDeadline(time.Now().Add(frameReadTimeout))
code, data, wireSize, err := t.conn.Read()
if err == nil {
+ // Protocol messages are dispatched to subprotocol handlers asynchronously,
+ // but package rlpx may reuse the returned 'data' buffer on the next call
+ // to Read. Copy the message data to avoid this being an issue.
+ data = common.CopyBytes(data)
msg = Msg{
ReceivedAt: time.Now(),
Code: code,
diff --git a/rlp/decode.go b/rlp/decode.go
index 8e074ea48..e4ecaca00 100644
--- a/rlp/decode.go
+++ b/rlp/decode.go
@@ -220,20 +220,51 @@ func decodeBigIntNoPtr(s *Stream, val reflect.Value) error {
}
func decodeBigInt(s *Stream, val reflect.Value) error {
- b, err := s.Bytes()
- if err != nil {
+ var buffer []byte
+ kind, size, err := s.Kind()
+ switch {
+ case err != nil:
return wrapStreamError(err, val.Type())
+ case kind == List:
+ return wrapStreamError(ErrExpectedString, val.Type())
+ case kind == Byte:
+ buffer = s.uintbuf[:1]
+ buffer[0] = s.byteval
+ s.kind = -1 // re-arm Kind
+ case size == 0:
+ // Avoid zero-length read.
+ s.kind = -1
+ case size <= uint64(len(s.uintbuf)):
+ // For integers smaller than s.uintbuf, allocating a buffer
+ // can be avoided.
+ buffer = s.uintbuf[:size]
+ if err := s.readFull(buffer); err != nil {
+ return wrapStreamError(err, val.Type())
+ }
+ // Reject inputs where single byte encoding should have been used.
+ if size == 1 && buffer[0] < 128 {
+ return wrapStreamError(ErrCanonSize, val.Type())
+ }
+ default:
+ // For large integers, a temporary buffer is needed.
+ buffer = make([]byte, size)
+ if err := s.readFull(buffer); err != nil {
+ return wrapStreamError(err, val.Type())
+ }
+ }
+
+ // Reject leading zero bytes.
+ if len(buffer) > 0 && buffer[0] == 0 {
+ return wrapStreamError(ErrCanonInt, val.Type())
}
+
+ // Set the integer bytes.
i := val.Interface().(*big.Int)
if i == nil {
i = new(big.Int)
val.Set(reflect.ValueOf(i))
}
- // Reject leading zero bytes
- if len(b) > 0 && b[0] == 0 {
- return wrapStreamError(ErrCanonInt, val.Type())
- }
- i.SetBytes(b)
+ i.SetBytes(buffer)
return nil
}
@@ -245,7 +276,7 @@ func makeListDecoder(typ reflect.Type, tag tags) (decoder, error) {
}
return decodeByteSlice, nil
}
- etypeinfo := cachedTypeInfo1(etype, tags{})
+ etypeinfo := theTC.infoWhileGenerating(etype, tags{})
if etypeinfo.decoderErr != nil {
return nil, etypeinfo.decoderErr
}
@@ -348,25 +379,23 @@ func decodeByteArray(s *Stream, val reflect.Value) error {
if err != nil {
return err
}
- vlen := val.Len()
+ slice := byteArrayBytes(val, val.Len())
switch kind {
case Byte:
- if vlen == 0 {
+ if len(slice) == 0 {
return &decodeError{msg: "input string too long", typ: val.Type()}
- }
- if vlen > 1 {
+ } else if len(slice) > 1 {
return &decodeError{msg: "input string too short", typ: val.Type()}
}
- bv, _ := s.Uint()
- val.Index(0).SetUint(bv)
+ slice[0] = s.byteval
+ s.kind = -1
case String:
- if uint64(vlen) < size {
+ if uint64(len(slice)) < size {
return &decodeError{msg: "input string too long", typ: val.Type()}
}
- if uint64(vlen) > size {
+ if uint64(len(slice)) > size {
return &decodeError{msg: "input string too short", typ: val.Type()}
}
- slice := val.Slice(0, vlen).Interface().([]byte)
if err := s.readFull(slice); err != nil {
return err
}
@@ -410,7 +439,7 @@ func makeStructDecoder(typ reflect.Type) (decoder, error) {
// makePtrDecoder creates a decoder that decodes into the pointer's element type.
func makePtrDecoder(typ reflect.Type, tag tags) (decoder, error) {
etype := typ.Elem()
- etypeinfo := cachedTypeInfo1(etype, tags{})
+ etypeinfo := theTC.infoWhileGenerating(etype, tags{})
switch {
case etypeinfo.decoderErr != nil:
return nil, etypeinfo.decoderErr
@@ -504,7 +533,7 @@ func decodeDecoder(s *Stream, val reflect.Value) error {
}
// Kind represents the kind of value contained in an RLP stream.
-type Kind int
+type Kind int8
const (
Byte Kind = iota
@@ -547,22 +576,16 @@ type ByteReader interface {
type Stream struct {
r ByteReader
- // number of bytes remaining to be read from r.
- remaining uint64
- limited bool
-
- // auxiliary buffer for integer decoding
- uintbuf []byte
-
- kind Kind // kind of value ahead
- size uint64 // size of value ahead
- byteval byte // value of single byte in type tag
- kinderr error // error from last readKind
- stack []listpos
+ remaining uint64 // number of bytes remaining to be read from r
+ size uint64 // size of value ahead
+ kinderr error // error from last readKind
+ stack []uint64 // list sizes
+ uintbuf [32]byte // auxiliary buffer for integer decoding
+ kind Kind // kind of value ahead
+ byteval byte // value of single byte in type tag
+ limited bool // true if input limit is in effect
}
-type listpos struct{ pos, size uint64 }
-
// NewStream creates a new decoding stream reading from r.
//
// If r implements the ByteReader interface, Stream will
@@ -632,8 +655,8 @@ func (s *Stream) Raw() ([]byte, error) {
s.kind = -1 // rearm Kind
return []byte{s.byteval}, nil
}
- // the original header has already been read and is no longer
- // available. read content and put a new header in front of it.
+ // The original header has already been read and is no longer
+ // available. Read content and put a new header in front of it.
start := headsize(size)
buf := make([]byte, uint64(start)+size)
if err := s.readFull(buf[start:]); err != nil {
@@ -716,7 +739,14 @@ func (s *Stream) List() (size uint64, err error) {
if kind != List {
return 0, ErrExpectedList
}
- s.stack = append(s.stack, listpos{0, size})
+
+ // Remove size of inner list from outer list before pushing the new size
+ // onto the stack. This ensures that the remaining outer list size will
+ // be correct after the matching call to ListEnd.
+ if inList, limit := s.listLimit(); inList {
+ s.stack[len(s.stack)-1] = limit - size
+ }
+ s.stack = append(s.stack, size)
s.kind = -1
s.size = 0
return size, nil
@@ -725,17 +755,13 @@ func (s *Stream) List() (size uint64, err error) {
// ListEnd returns to the enclosing list.
// The input reader must be positioned at the end of a list.
func (s *Stream) ListEnd() error {
- if len(s.stack) == 0 {
+ // Ensure that no more data is remaining in the current list.
+ if inList, listLimit := s.listLimit(); !inList {
return errNotInList
- }
- tos := s.stack[len(s.stack)-1]
- if tos.pos != tos.size {
+ } else if listLimit > 0 {
return errNotAtEOL
}
s.stack = s.stack[:len(s.stack)-1] // pop
- if len(s.stack) > 0 {
- s.stack[len(s.stack)-1].pos += tos.size
- }
s.kind = -1
s.size = 0
return nil
@@ -763,7 +789,7 @@ func (s *Stream) Decode(val interface{}) error {
err = decoder(s, rval.Elem())
if decErr, ok := err.(*decodeError); ok && len(decErr.ctx) > 0 {
- // add decode target type to error so context has more meaning
+ // Add decode target type to error so context has more meaning.
decErr.ctx = append(decErr.ctx, fmt.Sprint("(", rtyp.Elem(), ")"))
}
return err
@@ -786,6 +812,9 @@ func (s *Stream) Reset(r io.Reader, inputLimit uint64) {
case *bytes.Reader:
s.remaining = uint64(br.Len())
s.limited = true
+ case *bytes.Buffer:
+ s.remaining = uint64(br.Len())
+ s.limited = true
case *strings.Reader:
s.remaining = uint64(br.Len())
s.limited = true
@@ -804,10 +833,8 @@ func (s *Stream) Reset(r io.Reader, inputLimit uint64) {
s.size = 0
s.kind = -1
s.kinderr = nil
- if s.uintbuf == nil {
- s.uintbuf = make([]byte, 8)
- }
s.byteval = 0
+ s.uintbuf = [32]byte{}
}
// Kind returns the kind and size of the next value in the
@@ -822,35 +849,29 @@ func (s *Stream) Reset(r io.Reader, inputLimit uint64) {
// the value. Subsequent calls to Kind (until the value is decoded)
// will not advance the input reader and return cached information.
func (s *Stream) Kind() (kind Kind, size uint64, err error) {
- var tos *listpos
- if len(s.stack) > 0 {
- tos = &s.stack[len(s.stack)-1]
- }
- if s.kind < 0 {
- s.kinderr = nil
- // Don't read further if we're at the end of the
- // innermost list.
- if tos != nil && tos.pos == tos.size {
- return 0, 0, EOL
- }
- s.kind, s.size, s.kinderr = s.readKind()
- if s.kinderr == nil {
- if tos == nil {
- // At toplevel, check that the value is smaller
- // than the remaining input length.
- if s.limited && s.size > s.remaining {
- s.kinderr = ErrValueTooLarge
- }
- } else {
- // Inside a list, check that the value doesn't overflow the list.
- if s.size > tos.size-tos.pos {
- s.kinderr = ErrElemTooLarge
- }
- }
+ if s.kind >= 0 {
+ return s.kind, s.size, s.kinderr
+ }
+
+ // Check for end of list. This needs to be done here because readKind
+ // checks against the list size, and would return the wrong error.
+ inList, listLimit := s.listLimit()
+ if inList && listLimit == 0 {
+ return 0, 0, EOL
+ }
+ // Read the actual size tag.
+ s.kind, s.size, s.kinderr = s.readKind()
+ if s.kinderr == nil {
+ // Check the data size of the value ahead against input limits. This
+ // is done here because many decoders require allocating an input
+ // buffer matching the value size. Checking it here protects those
+ // decoders from inputs declaring very large value size.
+ if inList && s.size > listLimit {
+ s.kinderr = ErrElemTooLarge
+ } else if s.limited && s.size > s.remaining {
+ s.kinderr = ErrValueTooLarge
}
}
- // Note: this might return a sticky error generated
- // by an earlier call to readKind.
return s.kind, s.size, s.kinderr
}
@@ -877,37 +898,35 @@ func (s *Stream) readKind() (kind Kind, size uint64, err error) {
s.byteval = b
return Byte, 0, nil
case b < 0xB8:
- // Otherwise, if a string is 0-55 bytes long,
- // the RLP encoding consists of a single byte with value 0x80 plus the
- // length of the string followed by the string. The range of the first
- // byte is thus [0x80, 0xB7].
+ // Otherwise, if a string is 0-55 bytes long, the RLP encoding consists
+ // of a single byte with value 0x80 plus the length of the string
+ // followed by the string. The range of the first byte is thus [0x80, 0xB7].
return String, uint64(b - 0x80), nil
case b < 0xC0:
- // If a string is more than 55 bytes long, the
- // RLP encoding consists of a single byte with value 0xB7 plus the length
- // of the length of the string in binary form, followed by the length of
- // the string, followed by the string. For example, a length-1024 string
- // would be encoded as 0xB90400 followed by the string. The range of
- // the first byte is thus [0xB8, 0xBF].
+ // If a string is more than 55 bytes long, the RLP encoding consists of a
+ // single byte with value 0xB7 plus the length of the length of the
+ // string in binary form, followed by the length of the string, followed
+ // by the string. For example, a length-1024 string would be encoded as
+ // 0xB90400 followed by the string. The range of the first byte is thus
+ // [0xB8, 0xBF].
size, err = s.readUint(b - 0xB7)
if err == nil && size < 56 {
err = ErrCanonSize
}
return String, size, err
case b < 0xF8:
- // If the total payload of a list
- // (i.e. the combined length of all its items) is 0-55 bytes long, the
- // RLP encoding consists of a single byte with value 0xC0 plus the length
- // of the list followed by the concatenation of the RLP encodings of the
- // items. The range of the first byte is thus [0xC0, 0xF7].
+ // If the total payload of a list (i.e. the combined length of all its
+ // items) is 0-55 bytes long, the RLP encoding consists of a single byte
+ // with value 0xC0 plus the length of the list followed by the
+ // concatenation of the RLP encodings of the items. The range of the
+ // first byte is thus [0xC0, 0xF7].
return List, uint64(b - 0xC0), nil
default:
- // If the total payload of a list is more than 55 bytes long,
- // the RLP encoding consists of a single byte with value 0xF7
- // plus the length of the length of the payload in binary
- // form, followed by the length of the payload, followed by
- // the concatenation of the RLP encodings of the items. The
- // range of the first byte is thus [0xF8, 0xFF].
+ // If the total payload of a list is more than 55 bytes long, the RLP
+ // encoding consists of a single byte with value 0xF7 plus the length of
+ // the length of the payload in binary form, followed by the length of
+ // the payload, followed by the concatenation of the RLP encodings of
+ // the items. The range of the first byte is thus [0xF8, 0xFF].
size, err = s.readUint(b - 0xF7)
if err == nil && size < 56 {
err = ErrCanonSize
@@ -925,23 +944,24 @@ func (s *Stream) readUint(size byte) (uint64, error) {
b, err := s.readByte()
return uint64(b), err
default:
- start := int(8 - size)
- for i := 0; i < start; i++ {
- s.uintbuf[i] = 0
+ buffer := s.uintbuf[:8]
+ for i := range buffer {
+ buffer[i] = 0
}
- if err := s.readFull(s.uintbuf[start:]); err != nil {
+ start := int(8 - size)
+ if err := s.readFull(buffer[start:]); err != nil {
return 0, err
}
- if s.uintbuf[start] == 0 {
- // Note: readUint is also used to decode integer
- // values. The error needs to be adjusted to become
- // ErrCanonInt in this case.
+ if buffer[start] == 0 {
+ // Note: readUint is also used to decode integer values.
+ // The error needs to be adjusted to become ErrCanonInt in this case.
return 0, ErrCanonSize
}
- return binary.BigEndian.Uint64(s.uintbuf), nil
+ return binary.BigEndian.Uint64(buffer[:]), nil
}
}
+// readFull reads into buf from the underlying stream.
func (s *Stream) readFull(buf []byte) (err error) {
if err := s.willRead(uint64(len(buf))); err != nil {
return err
@@ -974,16 +994,16 @@ func (s *Stream) readByte() (byte, error) {
return b, err
}
+// willRead is called before any read from the underlying stream. It checks
+// n against size limits, and updates the limits if n doesn't overflow them.
func (s *Stream) willRead(n uint64) error {
s.kind = -1 // rearm Kind
- if len(s.stack) > 0 {
- // check list overflow
- tos := s.stack[len(s.stack)-1]
- if n > tos.size-tos.pos {
+ if inList, limit := s.listLimit(); inList {
+ if n > limit {
return ErrElemTooLarge
}
- s.stack[len(s.stack)-1].pos += n
+ s.stack[len(s.stack)-1] = limit - n
}
if s.limited {
if n > s.remaining {
@@ -993,3 +1013,11 @@ func (s *Stream) willRead(n uint64) error {
}
return nil
}
+
+// listLimit returns the amount of data remaining in the innermost list.
+func (s *Stream) listLimit() (inList bool, limit uint64) {
+ if len(s.stack) == 0 {
+ return false, 0
+ }
+ return true, s.stack[len(s.stack)-1]
+}
diff --git a/rlp/decode_test.go b/rlp/decode_test.go
index d94c3969b..f8af3897c 100644
--- a/rlp/decode_test.go
+++ b/rlp/decode_test.go
@@ -26,6 +26,8 @@ import (
"reflect"
"strings"
"testing"
+
+ "github.com/ethereum/go-ethereum/common/math"
)
func TestStreamKind(t *testing.T) {
@@ -327,6 +329,11 @@ type recstruct struct {
Child *recstruct `rlp:"nil"`
}
+type bigIntStruct struct {
+ I *big.Int
+ B string
+}
+
type invalidNilTag struct {
X []byte `rlp:"nil"`
}
@@ -370,10 +377,11 @@ type intField struct {
}
var (
- veryBigInt = big.NewInt(0).Add(
+ veryBigInt = new(big.Int).Add(
big.NewInt(0).Lsh(big.NewInt(0xFFFFFFFFFFFFFF), 16),
big.NewInt(0xFFFF),
)
+ veryVeryBigInt = new(big.Int).Exp(veryBigInt, big.NewInt(8), nil)
)
type hasIgnoredField struct {
@@ -450,12 +458,15 @@ var decodeTests = []decodeTest{
{input: "C0", ptr: new(string), error: "rlp: expected input string or byte for string"},
// big ints
+ {input: "80", ptr: new(*big.Int), value: big.NewInt(0)},
{input: "01", ptr: new(*big.Int), value: big.NewInt(1)},
{input: "89FFFFFFFFFFFFFFFFFF", ptr: new(*big.Int), value: veryBigInt},
+ {input: "B848FFFFFFFFFFFFFFFFF800000000000000001BFFFFFFFFFFFFFFFFC8000000000000000045FFFFFFFFFFFFFFFFC800000000000000001BFFFFFFFFFFFFFFFFF8000000000000000001", ptr: new(*big.Int), value: veryVeryBigInt},
{input: "10", ptr: new(big.Int), value: *big.NewInt(16)}, // non-pointer also works
{input: "C0", ptr: new(*big.Int), error: "rlp: expected input string or byte for *big.Int"},
- {input: "820001", ptr: new(big.Int), error: "rlp: non-canonical integer (leading zero bytes) for *big.Int"},
- {input: "8105", ptr: new(big.Int), error: "rlp: non-canonical size information for *big.Int"},
+ {input: "00", ptr: new(*big.Int), error: "rlp: non-canonical integer (leading zero bytes) for *big.Int"},
+ {input: "820001", ptr: new(*big.Int), error: "rlp: non-canonical integer (leading zero bytes) for *big.Int"},
+ {input: "8105", ptr: new(*big.Int), error: "rlp: non-canonical size information for *big.Int"},
// structs
{
@@ -468,6 +479,13 @@ var decodeTests = []decodeTest{
ptr: new(recstruct),
value: recstruct{1, &recstruct{2, &recstruct{3, nil}}},
},
+ {
+ // This checks that empty big.Int works correctly in struct context. It's easy to
+ // miss the update of s.kind for this case, so it needs its own test.
+ input: "C58083343434",
+ ptr: new(bigIntStruct),
+ value: bigIntStruct{new(big.Int), "444"},
+ },
// struct errors
{
@@ -898,7 +916,7 @@ func ExampleStream() {
// [102 111 111 98 97 114]
}
-func BenchmarkDecode(b *testing.B) {
+func BenchmarkDecodeUints(b *testing.B) {
enc := encodeTestSlice(90000)
b.SetBytes(int64(len(enc)))
b.ReportAllocs()
@@ -913,7 +931,7 @@ func BenchmarkDecode(b *testing.B) {
}
}
-func BenchmarkDecodeIntSliceReuse(b *testing.B) {
+func BenchmarkDecodeUintsReused(b *testing.B) {
enc := encodeTestSlice(100000)
b.SetBytes(int64(len(enc)))
b.ReportAllocs()
@@ -928,6 +946,44 @@ func BenchmarkDecodeIntSliceReuse(b *testing.B) {
}
}
+func BenchmarkDecodeByteArrayStruct(b *testing.B) {
+ enc, err := EncodeToBytes(&byteArrayStruct{})
+ if err != nil {
+ b.Fatal(err)
+ }
+ b.SetBytes(int64(len(enc)))
+ b.ReportAllocs()
+ b.ResetTimer()
+
+ var out byteArrayStruct
+ for i := 0; i < b.N; i++ {
+ if err := DecodeBytes(enc, &out); err != nil {
+ b.Fatal(err)
+ }
+ }
+}
+
+func BenchmarkDecodeBigInts(b *testing.B) {
+ ints := make([]*big.Int, 200)
+ for i := range ints {
+ ints[i] = math.BigPow(2, int64(i))
+ }
+ enc, err := EncodeToBytes(ints)
+ if err != nil {
+ b.Fatal(err)
+ }
+ b.SetBytes(int64(len(enc)))
+ b.ReportAllocs()
+ b.ResetTimer()
+
+ var out []*big.Int
+ for i := 0; i < b.N; i++ {
+ if err := DecodeBytes(enc, &out); err != nil {
+ b.Fatal(err)
+ }
+ }
+}
+
func encodeTestSlice(n uint) []byte {
s := make([]uint, n)
for i := uint(0); i < n; i++ {
diff --git a/rlp/encode.go b/rlp/encode.go
index abf25e42c..c94a31ba1 100644
--- a/rlp/encode.go
+++ b/rlp/encode.go
@@ -124,19 +124,15 @@ func puthead(buf []byte, smalltag, largetag byte, size uint64) int {
}
type encbuf struct {
- str []byte // string data, contains everything except list headers
- lheads []listhead // all list headers
- lhsize int // sum of sizes of all encoded list headers
- sizebuf [9]byte // auxiliary buffer for uint encoding
- bufvalue reflect.Value // used in writeByteArrayCopy
+ str []byte // string data, contains everything except list headers
+ lheads []listhead // all list headers
+ lhsize int // sum of sizes of all encoded list headers
+ sizebuf [9]byte // auxiliary buffer for uint encoding
}
// encbufs are pooled.
var encbufPool = sync.Pool{
- New: func() interface{} {
- var bytes []byte
- return &encbuf{bufvalue: reflect.ValueOf(&bytes).Elem()}
- },
+ New: func() interface{} { return new(encbuf) },
}
func (w *encbuf) reset() {
@@ -429,21 +425,27 @@ func writeBytes(val reflect.Value, w *encbuf) error {
return nil
}
-var byteType = reflect.TypeOf(byte(0))
-
func makeByteArrayWriter(typ reflect.Type) writer {
- length := typ.Len()
- if length == 0 {
+ switch typ.Len() {
+ case 0:
return writeLengthZeroByteArray
- } else if length == 1 {
+ case 1:
return writeLengthOneByteArray
- }
- if typ.Elem() != byteType {
- return writeNamedByteArray
- }
- return func(val reflect.Value, w *encbuf) error {
- writeByteArrayCopy(length, val, w)
- return nil
+ default:
+ length := typ.Len()
+ return func(val reflect.Value, w *encbuf) error {
+ if !val.CanAddr() {
+ // Getting the byte slice of val requires it to be addressable. Make it
+ // addressable by copying.
+ copy := reflect.New(val.Type()).Elem()
+ copy.Set(val)
+ val = copy
+ }
+ slice := byteArrayBytes(val, length)
+ w.encodeStringHeader(len(slice))
+ w.str = append(w.str, slice...)
+ return nil
+ }
}
}
@@ -462,32 +464,6 @@ func writeLengthOneByteArray(val reflect.Value, w *encbuf) error {
return nil
}
-// writeByteArrayCopy encodes byte arrays using reflect.Copy. This is
-// the fast path for [N]byte where N > 1.
-func writeByteArrayCopy(length int, val reflect.Value, w *encbuf) {
- w.encodeStringHeader(length)
- offset := len(w.str)
- w.str = append(w.str, make([]byte, length)...)
- w.bufvalue.SetBytes(w.str[offset:])
- reflect.Copy(w.bufvalue, val)
-}
-
-// writeNamedByteArray encodes byte arrays with named element type.
-// This exists because reflect.Copy can't be used with such types.
-func writeNamedByteArray(val reflect.Value, w *encbuf) error {
- if !val.CanAddr() {
- // Slice requires the value to be addressable.
- // Make it addressable by copying.
- copy := reflect.New(val.Type()).Elem()
- copy.Set(val)
- val = copy
- }
- size := val.Len()
- slice := val.Slice(0, size).Bytes()
- w.encodeString(slice)
- return nil
-}
-
func writeString(val reflect.Value, w *encbuf) error {
s := val.String()
if len(s) == 1 && s[0] <= 0x7f {
@@ -517,23 +493,43 @@ func writeInterface(val reflect.Value, w *encbuf) error {
}
func makeSliceWriter(typ reflect.Type, ts tags) (writer, error) {
- etypeinfo := cachedTypeInfo1(typ.Elem(), tags{})
+ etypeinfo := theTC.infoWhileGenerating(typ.Elem(), tags{})
if etypeinfo.writerErr != nil {
return nil, etypeinfo.writerErr
}
- writer := func(val reflect.Value, w *encbuf) error {
- if !ts.tail {
- defer w.listEnd(w.list())
+
+ var wfn writer
+ if ts.tail {
+ // This is for struct tail slices.
+ // w.list is not called for them.
+ wfn = func(val reflect.Value, w *encbuf) error {
+ vlen := val.Len()
+ for i := 0; i < vlen; i++ {
+ if err := etypeinfo.writer(val.Index(i), w); err != nil {
+ return err
+ }
+ }
+ return nil
}
- vlen := val.Len()
- for i := 0; i < vlen; i++ {
- if err := etypeinfo.writer(val.Index(i), w); err != nil {
- return err
+ } else {
+ // This is for regular slices and arrays.
+ wfn = func(val reflect.Value, w *encbuf) error {
+ vlen := val.Len()
+ if vlen == 0 {
+ w.str = append(w.str, 0xC0)
+ return nil
}
+ listOffset := w.list()
+ for i := 0; i < vlen; i++ {
+ if err := etypeinfo.writer(val.Index(i), w); err != nil {
+ return err
+ }
+ }
+ w.listEnd(listOffset)
+ return nil
}
- return nil
}
- return writer, nil
+ return wfn, nil
}
func makeStructWriter(typ reflect.Type) (writer, error) {
@@ -559,12 +555,8 @@ func makeStructWriter(typ reflect.Type) (writer, error) {
return writer, nil
}
-func makePtrWriter(typ reflect.Type, ts tags) (writer, error) {
- etypeinfo := cachedTypeInfo1(typ.Elem(), tags{})
- if etypeinfo.writerErr != nil {
- return nil, etypeinfo.writerErr
- }
- // Determine how to encode nil pointers.
+// nilEncoding returns the encoded value of a nil pointer.
+func nilEncoding(typ reflect.Type, ts tags) uint8 {
var nilKind Kind
if ts.nilOK {
nilKind = ts.nilKind // use struct tag if provided
@@ -572,16 +564,29 @@ func makePtrWriter(typ reflect.Type, ts tags) (writer, error) {
nilKind = defaultNilKind(typ.Elem())
}
+ switch nilKind {
+ case String:
+ return 0x80
+ case List:
+ return 0xC0
+ default:
+ panic(fmt.Errorf("rlp: invalid nil kind %d", nilKind))
+ }
+}
+
+func makePtrWriter(typ reflect.Type, ts tags) (writer, error) {
+ etypeinfo := theTC.infoWhileGenerating(typ.Elem(), tags{})
+ if etypeinfo.writerErr != nil {
+ return nil, etypeinfo.writerErr
+ }
+ nilEncoding := nilEncoding(typ, ts)
+
writer := func(val reflect.Value, w *encbuf) error {
- if val.IsNil() {
- if nilKind == String {
- w.str = append(w.str, 0x80)
- } else {
- w.listEnd(w.list())
- }
- return nil
+ if ev := val.Elem(); ev.IsValid() {
+ return etypeinfo.writer(ev, w)
}
- return etypeinfo.writer(val.Elem(), w)
+ w.str = append(w.str, nilEncoding)
+ return nil
}
return writer, nil
}
diff --git a/rlp/encode_test.go b/rlp/encode_test.go
index 4e95a28d8..65a41fbb6 100644
--- a/rlp/encode_test.go
+++ b/rlp/encode_test.go
@@ -22,6 +22,7 @@ import (
"fmt"
"io"
"math/big"
+ "runtime"
"sync"
"testing"
@@ -129,6 +130,14 @@ var encTests = []encTest{
val: big.NewInt(0).SetBytes(unhex("010000000000000000000000000000000000000000000000000000000000000000")),
output: "A1010000000000000000000000000000000000000000000000000000000000000000",
},
+ {
+ val: veryBigInt,
+ output: "89FFFFFFFFFFFFFFFFFF",
+ },
+ {
+ val: veryVeryBigInt,
+ output: "B848FFFFFFFFFFFFFFFFF800000000000000001BFFFFFFFFFFFFFFFFC8000000000000000045FFFFFFFFFFFFFFFFC800000000000000001BFFFFFFFFFFFFFFFFF8000000000000000001",
+ },
// non-pointer big.Int
{val: *big.NewInt(0), output: "80"},
@@ -461,3 +470,82 @@ func BenchmarkEncodeBigInts(b *testing.B) {
}
}
}
+
+func BenchmarkEncodeConcurrentInterface(b *testing.B) {
+ type struct1 struct {
+ A string
+ B *big.Int
+ C [20]byte
+ }
+ value := []interface{}{
+ uint(999),
+ &struct1{A: "hello", B: big.NewInt(0xFFFFFFFF)},
+ [10]byte{1, 2, 3, 4, 5, 6},
+ []string{"yeah", "yeah", "yeah"},
+ }
+
+ var wg sync.WaitGroup
+ for cpu := 0; cpu < runtime.NumCPU(); cpu++ {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+
+ var buffer bytes.Buffer
+ for i := 0; i < b.N; i++ {
+ buffer.Reset()
+ err := Encode(&buffer, value)
+ if err != nil {
+ panic(err)
+ }
+ }
+ }()
+ }
+ wg.Wait()
+}
+
+type byteArrayStruct struct {
+ A [20]byte
+ B [32]byte
+ C [32]byte
+}
+
+func BenchmarkEncodeByteArrayStruct(b *testing.B) {
+ var out bytes.Buffer
+ var value byteArrayStruct
+
+ b.ReportAllocs()
+ for i := 0; i < b.N; i++ {
+ out.Reset()
+ if err := Encode(&out, &value); err != nil {
+ b.Fatal(err)
+ }
+ }
+}
+
+type structSliceElem struct {
+ X uint64
+ Y uint64
+ Z uint64
+}
+
+type structPtrSlice []*structSliceElem
+
+func BenchmarkEncodeStructPtrSlice(b *testing.B) {
+ var out bytes.Buffer
+ var value = structPtrSlice{
+ &structSliceElem{1, 1, 1},
+ &structSliceElem{2, 2, 2},
+ &structSliceElem{3, 3, 3},
+ &structSliceElem{5, 5, 5},
+ &structSliceElem{6, 6, 6},
+ &structSliceElem{7, 7, 7},
+ }
+
+ b.ReportAllocs()
+ for i := 0; i < b.N; i++ {
+ out.Reset()
+ if err := Encode(&out, &value); err != nil {
+ b.Fatal(err)
+ }
+ }
+}
diff --git a/rlp/raw.go b/rlp/raw.go
index 3071e99ca..f355efc14 100644
--- a/rlp/raw.go
+++ b/rlp/raw.go
@@ -34,6 +34,14 @@ func ListSize(contentSize uint64) uint64 {
return uint64(headsize(contentSize)) + contentSize
}
+// IntSize returns the encoded size of the integer x.
+func IntSize(x uint64) int {
+ if x < 0x80 {
+ return 1
+ }
+ return 1 + intsize(x)
+}
+
// Split returns the content of first RLP value and any
// bytes after the value as subslices of b.
func Split(b []byte) (k Kind, content, rest []byte, err error) {
diff --git a/rlp/raw_test.go b/rlp/raw_test.go
index c976c4f73..185e269d0 100644
--- a/rlp/raw_test.go
+++ b/rlp/raw_test.go
@@ -263,6 +263,12 @@ func TestAppendUint64(t *testing.T) {
if !bytes.Equal(x, unhex(test.output)) {
t.Errorf("AppendUint64(%v, %d): got %x, want %s", test.slice, test.input, x, test.output)
}
+
+ // Check that IntSize returns the appended size.
+ length := len(x) - len(test.slice)
+ if s := IntSize(test.input); s != length {
+ t.Errorf("IntSize(%d): got %d, want %d", test.input, s, length)
+ }
}
}
diff --git a/rlp/safe.go b/rlp/safe.go
new file mode 100644
index 000000000..3c910337b
--- /dev/null
+++ b/rlp/safe.go
@@ -0,0 +1,27 @@
+// Copyright 2021 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see .
+
+//go:build nacl || js || !cgo
+// +build nacl js !cgo
+
+package rlp
+
+import "reflect"
+
+// byteArrayBytes returns a slice of the byte array v.
+func byteArrayBytes(v reflect.Value, length int) []byte {
+ return v.Slice(0, length).Bytes()
+}
diff --git a/rlp/typecache.go b/rlp/typecache.go
index 6026e1a64..4d6860d59 100644
--- a/rlp/typecache.go
+++ b/rlp/typecache.go
@@ -21,13 +21,10 @@ import (
"reflect"
"strings"
"sync"
+ "sync/atomic"
)
-var (
- typeCacheMutex sync.RWMutex
- typeCache = make(map[typekey]*typeinfo)
-)
-
+// typeinfo is an entry in the type cache.
type typeinfo struct {
decoder decoder
decoderErr error // error from makeDecoder
@@ -64,41 +61,76 @@ type decoder func(*Stream, reflect.Value) error
type writer func(reflect.Value, *encbuf) error
+var theTC = newTypeCache()
+
+type typeCache struct {
+ cur atomic.Value
+
+ // This lock synchronizes writers.
+ mu sync.Mutex
+ next map[typekey]*typeinfo
+}
+
+func newTypeCache() *typeCache {
+ c := new(typeCache)
+ c.cur.Store(make(map[typekey]*typeinfo))
+ return c
+}
+
func cachedDecoder(typ reflect.Type) (decoder, error) {
- info := cachedTypeInfo(typ, tags{})
+ info := theTC.info(typ)
return info.decoder, info.decoderErr
}
func cachedWriter(typ reflect.Type) (writer, error) {
- info := cachedTypeInfo(typ, tags{})
+ info := theTC.info(typ)
return info.writer, info.writerErr
}
-func cachedTypeInfo(typ reflect.Type, tags tags) *typeinfo {
- typeCacheMutex.RLock()
- info := typeCache[typekey{typ, tags}]
- typeCacheMutex.RUnlock()
- if info != nil {
+func (c *typeCache) info(typ reflect.Type) *typeinfo {
+ key := typekey{Type: typ}
+ if info := c.cur.Load().(map[typekey]*typeinfo)[key]; info != nil {
return info
}
- // not in the cache, need to generate info for this type.
- typeCacheMutex.Lock()
- defer typeCacheMutex.Unlock()
- return cachedTypeInfo1(typ, tags)
+
+ // Not in the cache, need to generate info for this type.
+ return c.generate(typ, tags{})
+}
+
+func (c *typeCache) generate(typ reflect.Type, tags tags) *typeinfo {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+
+ cur := c.cur.Load().(map[typekey]*typeinfo)
+ if info := cur[typekey{typ, tags}]; info != nil {
+ return info
+ }
+
+ // Copy cur to next.
+ c.next = make(map[typekey]*typeinfo, len(cur)+1)
+ for k, v := range cur {
+ c.next[k] = v
+ }
+
+ // Generate.
+ info := c.infoWhileGenerating(typ, tags)
+
+ // next -> cur
+ c.cur.Store(c.next)
+ c.next = nil
+ return info
}
-func cachedTypeInfo1(typ reflect.Type, tags tags) *typeinfo {
+func (c *typeCache) infoWhileGenerating(typ reflect.Type, tags tags) *typeinfo {
key := typekey{typ, tags}
- info := typeCache[key]
- if info != nil {
- // another goroutine got the write lock first
+ if info := c.next[key]; info != nil {
return info
}
- // put a dummy value into the cache before generating.
- // if the generator tries to lookup itself, it will get
+ // Put a dummy value into the cache before generating.
+ // If the generator tries to lookup itself, it will get
// the dummy value and won't call itself recursively.
- info = new(typeinfo)
- typeCache[key] = info
+ info := new(typeinfo)
+ c.next[key] = info
info.generate(typ, tags)
return info
}
@@ -119,7 +151,7 @@ func structFields(typ reflect.Type) (fields []field, err error) {
if tags.ignored {
continue
}
- info := cachedTypeInfo1(f.Type, tags)
+ info := theTC.infoWhileGenerating(f.Type, tags)
fields = append(fields, field{i, info})
}
}
diff --git a/rlp/unsafe.go b/rlp/unsafe.go
new file mode 100644
index 000000000..10868caaf
--- /dev/null
+++ b/rlp/unsafe.go
@@ -0,0 +1,30 @@
+// Copyright 2021 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see .
+
+//go:build !nacl && !js && cgo
+// +build !nacl,!js,cgo
+
+package rlp
+
+import (
+ "reflect"
+ "unsafe"
+)
+
+// byteArrayBytes returns a slice of the byte array v.
+func byteArrayBytes(v reflect.Value, length int) []byte {
+ return unsafe.Slice((*byte)(unsafe.Pointer(v.UnsafeAddr())), length)
+}