Skip to content

Commit

Permalink
consensus,gateway,rhp,types: add limits to all prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Jun 6, 2024
1 parent 9d2a157 commit 3c5c687
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 98 deletions.
14 changes: 8 additions & 6 deletions consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"go.sia.tech/core/types"
)

const maxTransactionElements = 10000

// Pool for reducing heap allocations when hashing. This is only necessary
// because blake2b.New256 returns a hash.Hash interface, which prevents the
// compiler from doing escape analysis. Can be removed if we switch to an
Expand Down Expand Up @@ -713,19 +715,19 @@ func (ts V1TransactionSupplement) EncodeTo(e *types.Encoder) {

// DecodeFrom implements types.DecoderFrom.
func (ts *V1TransactionSupplement) DecodeFrom(d *types.Decoder) {
ts.SiacoinInputs = make([]types.SiacoinElement, d.ReadPrefix())
ts.SiacoinInputs = make([]types.SiacoinElement, d.ReadPrefix(maxTransactionElements))
for i := range ts.SiacoinInputs {
ts.SiacoinInputs[i].DecodeFrom(d)
}
ts.SiafundInputs = make([]types.SiafundElement, d.ReadPrefix())
ts.SiafundInputs = make([]types.SiafundElement, d.ReadPrefix(maxTransactionElements))
for i := range ts.SiafundInputs {
ts.SiafundInputs[i].DecodeFrom(d)
}
ts.RevisedFileContracts = make([]types.FileContractElement, d.ReadPrefix())
ts.RevisedFileContracts = make([]types.FileContractElement, d.ReadPrefix(maxTransactionElements))
for i := range ts.RevisedFileContracts {
ts.RevisedFileContracts[i].DecodeFrom(d)
}
ts.ValidFileContracts = make([]types.FileContractElement, d.ReadPrefix())
ts.ValidFileContracts = make([]types.FileContractElement, d.ReadPrefix(maxTransactionElements))
for i := range ts.ValidFileContracts {
ts.ValidFileContracts[i].DecodeFrom(d)
}
Expand Down Expand Up @@ -795,11 +797,11 @@ func (bs V1BlockSupplement) EncodeTo(e *types.Encoder) {

// DecodeFrom implements types.DecoderFrom.
func (bs *V1BlockSupplement) DecodeFrom(d *types.Decoder) {
bs.Transactions = make([]V1TransactionSupplement, d.ReadPrefix())
bs.Transactions = make([]V1TransactionSupplement, d.ReadPrefix(maxTransactionElements))
for i := range bs.Transactions {
bs.Transactions[i].DecodeFrom(d)
}
bs.ExpiringFileContracts = make([]types.FileContractElement, d.ReadPrefix())
bs.ExpiringFileContracts = make([]types.FileContractElement, d.ReadPrefix(1000000)) // arbitrary
for i := range bs.ExpiringFileContracts {
bs.ExpiringFileContracts[i].DecodeFrom(d)
}
Expand Down
24 changes: 12 additions & 12 deletions gateway/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func withV1Encoder(w io.Writer, fn func(*types.Encoder)) error {

func withV1Decoder(r io.Reader, maxLen int, fn func(*types.Decoder)) error {
d := types.NewDecoder(io.LimitedReader{R: r, N: int64(8 + maxLen)})
d.ReadPrefix() // ignored
d.ReadPrefix(int64(maxLen)) // ignored
fn(d)
return d.Err()
}
Expand Down Expand Up @@ -128,13 +128,13 @@ func (ob *V2BlockOutline) decodeFrom(d *types.Decoder) {
ob.Timestamp = d.ReadTime()
ob.MinerAddress.DecodeFrom(d)

txns := make([]types.Transaction, d.ReadPrefix())
txns := make([]types.Transaction, d.ReadPrefix(10000))
for i := range txns {
txns[i].DecodeFrom(d)
}
var v2txns types.V2TransactionsMultiproof
v2txns.DecodeFrom(d)
hashes := make([]types.Hash256, d.ReadPrefix())
hashes := make([]types.Hash256, d.ReadPrefix(100000))
for i := range hashes {
hashes[i].DecodeFrom(d)
}
Expand Down Expand Up @@ -205,7 +205,7 @@ func (r *RPCShareNodes) encodeResponse(e *types.Encoder) {
}
}
func (r *RPCShareNodes) decodeResponse(d *types.Decoder) {
r.Peers = make([]string, d.ReadPrefix())
r.Peers = make([]string, d.ReadPrefix(100))
for i := range r.Peers {
r.Peers[i] = d.ReadString()
}
Expand Down Expand Up @@ -247,7 +247,7 @@ func (r *RPCSendBlocks) encodeResponse(e *types.Encoder) {
}
}
func (r *RPCSendBlocks) decodeResponse(d *types.Decoder) {
r.Blocks = make([]types.Block, d.ReadPrefix())
r.Blocks = make([]types.Block, d.ReadPrefix(1000))
for i := range r.Blocks {
(*types.V1Block)(&r.Blocks[i]).DecodeFrom(d)
}
Expand Down Expand Up @@ -304,7 +304,7 @@ func (r *RPCRelayTransactionSet) encodeRequest(e *types.Encoder) {
}
}
func (r *RPCRelayTransactionSet) decodeRequest(d *types.Decoder) {
r.Transactions = make([]types.Transaction, d.ReadPrefix())
r.Transactions = make([]types.Transaction, d.ReadPrefix(1000))
for i := range r.Transactions {
r.Transactions[i].DecodeFrom(d)
}
Expand All @@ -327,7 +327,7 @@ func (r *RPCSendV2Blocks) encodeRequest(e *types.Encoder) {
e.WriteUint64(r.Max)
}
func (r *RPCSendV2Blocks) decodeRequest(d *types.Decoder) {
r.History = make([]types.BlockID, d.ReadPrefix())
r.History = make([]types.BlockID, d.ReadPrefix(10000))
for i := range r.History {
r.History[i].DecodeFrom(d)
}
Expand All @@ -343,7 +343,7 @@ func (r *RPCSendV2Blocks) encodeResponse(e *types.Encoder) {
e.WriteUint64(r.Remaining)
}
func (r *RPCSendV2Blocks) decodeResponse(d *types.Decoder) {
r.Blocks = make([]types.Block, d.ReadPrefix())
r.Blocks = make([]types.Block, d.ReadPrefix(1000))
for i := range r.Blocks {
(*types.V2Block)(&r.Blocks[i]).DecodeFrom(d)
}
Expand All @@ -369,7 +369,7 @@ func (r *RPCSendTransactions) encodeRequest(e *types.Encoder) {
}
func (r *RPCSendTransactions) decodeRequest(d *types.Decoder) {
r.Index.DecodeFrom(d)
r.Hashes = make([]types.Hash256, d.ReadPrefix())
r.Hashes = make([]types.Hash256, d.ReadPrefix(1000))
for i := range r.Hashes {
r.Hashes[i].DecodeFrom(d)
}
Expand All @@ -387,11 +387,11 @@ func (r *RPCSendTransactions) encodeResponse(e *types.Encoder) {
}
}
func (r *RPCSendTransactions) decodeResponse(d *types.Decoder) {
r.Transactions = make([]types.Transaction, d.ReadPrefix())
r.Transactions = make([]types.Transaction, d.ReadPrefix(10000))
for i := range r.Transactions {
r.Transactions[i].DecodeFrom(d)
}
r.V2Transactions = make([]types.V2Transaction, d.ReadPrefix())
r.V2Transactions = make([]types.V2Transaction, d.ReadPrefix(10000))
for i := range r.V2Transactions {
r.V2Transactions[i].DecodeFrom(d)
}
Expand Down Expand Up @@ -456,7 +456,7 @@ func (r *RPCRelayV2TransactionSet) encodeRequest(e *types.Encoder) {
}
func (r *RPCRelayV2TransactionSet) decodeRequest(d *types.Decoder) {
r.Index.DecodeFrom(d)
r.Transactions = make([]types.V2Transaction, d.ReadPrefix())
r.Transactions = make([]types.V2Transaction, d.ReadPrefix(5000))
for i := range r.Transactions {
r.Transactions[i].DecodeFrom(d)
}
Expand Down
50 changes: 25 additions & 25 deletions rhp/v2/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (r *loopKeyExchangeRequest) EncodeTo(e *types.Encoder) {
func (r *loopKeyExchangeRequest) DecodeFrom(d *types.Decoder) {
new(types.Specifier).DecodeFrom(d) // loopEnter
d.Read(r.PublicKey[:])
r.Ciphers = make([]types.Specifier, d.ReadPrefix())
r.Ciphers = make([]types.Specifier, d.ReadPrefix(10))
for i := range r.Ciphers {
r.Ciphers[i].DecodeFrom(d)
}
Expand Down Expand Up @@ -92,7 +92,7 @@ func (r *RPCFormContractRequest) EncodeTo(e *types.Encoder) {

// DecodeFrom implements ProtocolObject.
func (r *RPCFormContractRequest) DecodeFrom(d *types.Decoder) {
r.Transactions = make([]types.Transaction, d.ReadPrefix())
r.Transactions = make([]types.Transaction, d.ReadPrefix(100))
for i := range r.Transactions {
r.Transactions[i].DecodeFrom(d)
}
Expand All @@ -117,15 +117,15 @@ func (r *RPCFormContractAdditions) EncodeTo(e *types.Encoder) {

// DecodeFrom implements ProtocolObject.
func (r *RPCFormContractAdditions) DecodeFrom(d *types.Decoder) {
r.Parents = make([]types.Transaction, d.ReadPrefix())
r.Parents = make([]types.Transaction, d.ReadPrefix(100))
for i := range r.Parents {
r.Parents[i].DecodeFrom(d)
}
r.Inputs = make([]types.SiacoinInput, d.ReadPrefix())
r.Inputs = make([]types.SiacoinInput, d.ReadPrefix(100))
for i := range r.Inputs {
r.Inputs[i].DecodeFrom(d)
}
r.Outputs = make([]types.SiacoinOutput, d.ReadPrefix())
r.Outputs = make([]types.SiacoinOutput, d.ReadPrefix(100))
for i := range r.Outputs {
(*types.V1SiacoinOutput)(&r.Outputs[i]).DecodeFrom(d)
}
Expand All @@ -142,7 +142,7 @@ func (r *RPCFormContractSignatures) EncodeTo(e *types.Encoder) {

// DecodeFrom implements ProtocolObject.
func (r *RPCFormContractSignatures) DecodeFrom(d *types.Decoder) {
r.ContractSignatures = make([]types.TransactionSignature, d.ReadPrefix())
r.ContractSignatures = make([]types.TransactionSignature, d.ReadPrefix(100))
for i := range r.ContractSignatures {
r.ContractSignatures[i].DecodeFrom(d)
}
Expand Down Expand Up @@ -170,16 +170,16 @@ func (r *RPCRenewAndClearContractRequest) EncodeTo(e *types.Encoder) {

// DecodeFrom implements ProtocolObject.
func (r *RPCRenewAndClearContractRequest) DecodeFrom(d *types.Decoder) {
r.Transactions = make([]types.Transaction, d.ReadPrefix())
r.Transactions = make([]types.Transaction, d.ReadPrefix(100))
for i := range r.Transactions {
r.Transactions[i].DecodeFrom(d)
}
r.RenterKey.DecodeFrom(d)
r.FinalValidProofValues = make([]types.Currency, d.ReadPrefix())
r.FinalValidProofValues = make([]types.Currency, d.ReadPrefix(6))
for i := range r.FinalValidProofValues {
(*types.V1Currency)(&r.FinalValidProofValues[i]).DecodeFrom(d)
}
r.FinalMissedProofValues = make([]types.Currency, d.ReadPrefix())
r.FinalMissedProofValues = make([]types.Currency, d.ReadPrefix(6))
for i := range r.FinalMissedProofValues {
(*types.V1Currency)(&r.FinalMissedProofValues[i]).DecodeFrom(d)
}
Expand All @@ -197,7 +197,7 @@ func (r *RPCRenewAndClearContractSignatures) EncodeTo(e *types.Encoder) {

// DecodeFrom implements ProtocolObject.
func (r *RPCRenewAndClearContractSignatures) DecodeFrom(d *types.Decoder) {
r.ContractSignatures = make([]types.TransactionSignature, d.ReadPrefix())
r.ContractSignatures = make([]types.TransactionSignature, d.ReadPrefix(100))
for i := range r.ContractSignatures {
r.ContractSignatures[i].DecodeFrom(d)
}
Expand Down Expand Up @@ -237,7 +237,7 @@ func (r *RPCLockResponse) DecodeFrom(d *types.Decoder) {
r.Acquired = d.ReadBool()
d.Read(r.NewChallenge[:])
r.Revision.DecodeFrom(d)
r.Signatures = make([]types.TransactionSignature, d.ReadPrefix())
r.Signatures = make([]types.TransactionSignature, d.ReadPrefix(100))
for i := range r.Signatures {
r.Signatures[i].DecodeFrom(d)
}
Expand Down Expand Up @@ -268,19 +268,19 @@ func (r *RPCReadRequest) EncodeTo(e *types.Encoder) {

// DecodeFrom implements ProtocolObject.
func (r *RPCReadRequest) DecodeFrom(d *types.Decoder) {
r.Sections = make([]RPCReadRequestSection, d.ReadPrefix())
r.Sections = make([]RPCReadRequestSection, d.ReadPrefix(50))
for i := range r.Sections {
d.Read(r.Sections[i].MerkleRoot[:])
r.Sections[i].Offset = d.ReadUint64()
r.Sections[i].Length = d.ReadUint64()
}
r.MerkleProof = d.ReadBool()
r.RevisionNumber = d.ReadUint64()
r.ValidProofValues = make([]types.Currency, d.ReadPrefix())
r.ValidProofValues = make([]types.Currency, d.ReadPrefix(6))
for i := range r.ValidProofValues {
(*types.V1Currency)(&r.ValidProofValues[i]).DecodeFrom(d)
}
r.MissedProofValues = make([]types.Currency, d.ReadPrefix())
r.MissedProofValues = make([]types.Currency, d.ReadPrefix(6))
for i := range r.MissedProofValues {
(*types.V1Currency)(&r.MissedProofValues[i]).DecodeFrom(d)
}
Expand All @@ -306,14 +306,14 @@ func (r *RPCReadResponse) DecodeFrom(d *types.Decoder) {
//
// NOTE: for maximum efficiency, we should be doing this for every slice,
// but in most cases the extra performance isn't worth the aliasing issues.
dataLen := d.ReadPrefix()
dataLen := d.ReadPrefix(SectorSize)
if cap(r.Data) < dataLen {
r.Data = make([]byte, dataLen)
}
r.Data = r.Data[:dataLen]
d.Read(r.Data)

r.MerkleProof = make([]types.Hash256, d.ReadPrefix())
r.MerkleProof = make([]types.Hash256, d.ReadPrefix(10000))
for i := range r.MerkleProof {
d.Read(r.MerkleProof[i][:])
}
Expand Down Expand Up @@ -342,11 +342,11 @@ func (r *RPCSectorRootsRequest) DecodeFrom(d *types.Decoder) {
r.RootOffset = d.ReadUint64()
r.NumRoots = d.ReadUint64()
r.RevisionNumber = d.ReadUint64()
r.ValidProofValues = make([]types.Currency, d.ReadPrefix())
r.ValidProofValues = make([]types.Currency, d.ReadPrefix(6))
for i := range r.ValidProofValues {
(*types.V1Currency)(&r.ValidProofValues[i]).DecodeFrom(d)
}
r.MissedProofValues = make([]types.Currency, d.ReadPrefix())
r.MissedProofValues = make([]types.Currency, d.ReadPrefix(6))
for i := range r.MissedProofValues {
(*types.V1Currency)(&r.MissedProofValues[i]).DecodeFrom(d)
}
Expand All @@ -369,11 +369,11 @@ func (r *RPCSectorRootsResponse) EncodeTo(e *types.Encoder) {
// DecodeFrom implements ProtocolObject.
func (r *RPCSectorRootsResponse) DecodeFrom(d *types.Decoder) {
copy(r.Signature[:], d.ReadBytes())
r.SectorRoots = make([]types.Hash256, d.ReadPrefix())
r.SectorRoots = make([]types.Hash256, d.ReadPrefix(15000000)) // ~50 TB of data or 500 MB of sector roots
for i := range r.SectorRoots {
d.Read(r.SectorRoots[i][:])
}
r.MerkleProof = make([]types.Hash256, d.ReadPrefix())
r.MerkleProof = make([]types.Hash256, d.ReadPrefix(1_000_000))
for i := range r.MerkleProof {
d.Read(r.MerkleProof[i][:])
}
Expand Down Expand Up @@ -416,7 +416,7 @@ func (r *RPCWriteRequest) EncodeTo(e *types.Encoder) {

// DecodeFrom implements ProtocolObject.
func (r *RPCWriteRequest) DecodeFrom(d *types.Decoder) {
r.Actions = make([]RPCWriteAction, d.ReadPrefix())
r.Actions = make([]RPCWriteAction, d.ReadPrefix(50))
for i := range r.Actions {
d.Read(r.Actions[i].Type[:])
r.Actions[i].A = d.ReadUint64()
Expand All @@ -425,11 +425,11 @@ func (r *RPCWriteRequest) DecodeFrom(d *types.Decoder) {
}
r.MerkleProof = d.ReadBool()
r.RevisionNumber = d.ReadUint64()
r.ValidProofValues = make([]types.Currency, d.ReadPrefix())
r.ValidProofValues = make([]types.Currency, d.ReadPrefix(6))
for i := range r.ValidProofValues {
(*types.V1Currency)(&r.ValidProofValues[i]).DecodeFrom(d)
}
r.MissedProofValues = make([]types.Currency, d.ReadPrefix())
r.MissedProofValues = make([]types.Currency, d.ReadPrefix(6))
for i := range r.MissedProofValues {
(*types.V1Currency)(&r.MissedProofValues[i]).DecodeFrom(d)
}
Expand All @@ -450,11 +450,11 @@ func (r *RPCWriteMerkleProof) EncodeTo(e *types.Encoder) {

// DecodeFrom implements ProtocolObject.
func (r *RPCWriteMerkleProof) DecodeFrom(d *types.Decoder) {
r.OldSubtreeHashes = make([]types.Hash256, d.ReadPrefix())
r.OldSubtreeHashes = make([]types.Hash256, d.ReadPrefix(10000))
for i := range r.OldSubtreeHashes {
d.Read(r.OldSubtreeHashes[i][:])
}
r.OldLeafHashes = make([]types.Hash256, d.ReadPrefix())
r.OldLeafHashes = make([]types.Hash256, d.ReadPrefix(10000))
for i := range r.OldLeafHashes {
d.Read(r.OldLeafHashes[i][:])
}
Expand Down
Loading

0 comments on commit 3c5c687

Please sign in to comment.