Skip to content

Commit

Permalink
Fix TX payload for DO testnets (#9540)
Browse files Browse the repository at this point in the history
* Added print

* Fix unmarshall

* Fix unmarshalling

* Simplified steps to unmarshall

* minor

* Use 'encoding/hex'

* Forget about C, this is Go!

* gosec warning

* Set maximum payload size

* nosec annotation
  • Loading branch information
sergio-mena authored Oct 12, 2022
1 parent 387bf67 commit b42c439
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions test/loadtime/payload/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package payload
import (
"bytes"
"crypto/rand"
"errors"
"encoding/hex"
"fmt"
"math"

Expand All @@ -12,6 +12,7 @@ import (
)

const keyPrefix = "a="
const maxPayloadSize = 4 * 1024 * 1024

// NewBytes generates a new payload and returns the encoded representation of
// the payload as a slice of bytes. NewBytes uses the fields on the Options
Expand All @@ -25,10 +26,16 @@ func NewBytes(p *Payload) ([]byte, error) {
if err != nil {
return nil, err
}
if p.Size < uint64(us) {
return nil, fmt.Errorf("configured size %d not large enough to fit unpadded transaction of size %d", p.Size, us)
if p.Size > maxPayloadSize {
return nil, fmt.Errorf("configured size %d is too large (>%d)", p.Size, maxPayloadSize)
}
p.Padding = make([]byte, p.Size-uint64(us))
pSize := int(p.Size) // #nosec -- The "if" above makes this cast safe
if pSize < us {
return nil, fmt.Errorf("configured size %d not large enough to fit unpadded transaction of size %d", pSize, us)
}

// We halve the padding size because we transform the TX to hex
p.Padding = make([]byte, (pSize-us)/2)
_, err = rand.Read(p.Padding)
if err != nil {
return nil, err
Expand All @@ -37,22 +44,28 @@ func NewBytes(p *Payload) ([]byte, error) {
if err != nil {
return nil, err
}
h := []byte(hex.EncodeToString(b))

// prepend a single key so that the kv store only ever stores a single
// transaction instead of storing all tx and ballooning in size.
return append([]byte(keyPrefix), b...), nil
return append([]byte(keyPrefix), h...), nil
}

// FromBytes extracts a paylod from the byte representation of the payload.
// FromBytes leaves the padding untouched, returning it to the caller to handle
// or discard per their preference.
func FromBytes(b []byte) (*Payload, error) {
p := &Payload{}
tr := bytes.TrimPrefix(b, []byte(keyPrefix))
if bytes.Equal(b, tr) {
return nil, errors.New("payload bytes missing key prefix")
trH := bytes.TrimPrefix(b, []byte(keyPrefix))
if bytes.Equal(b, trH) {
return nil, fmt.Errorf("payload bytes missing key prefix '%s'", keyPrefix)
}
trB, err := hex.DecodeString(string(trH))
if err != nil {
return nil, err
}
err := proto.Unmarshal(tr, p)

p := &Payload{}
err = proto.Unmarshal(trB, p)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -83,5 +96,6 @@ func CalculateUnpaddedSize(p *Payload) (int, error) {
if err != nil {
return 0, err
}
return len(b) + len(keyPrefix), nil
h := []byte(hex.EncodeToString(b))
return len(h) + len(keyPrefix), nil
}

0 comments on commit b42c439

Please sign in to comment.