remove all Encoders from encoding.go

parent 8d425847
......@@ -19,7 +19,17 @@ func (b Block) MarshalSia(w io.Writer) error {
w.Write(b.ParentID[:])
w.Write(b.Nonce[:])
encoding.WriteUint64(w, uint64(b.Timestamp))
return encoding.NewEncoder(w).EncodeAll(b.MinerPayouts, b.Transactions)
encoding.WriteInt(w, len(b.MinerPayouts))
for i := range b.MinerPayouts {
b.MinerPayouts[i].MarshalSia(w)
}
encoding.WriteInt(w, len(b.Transactions))
for i := range b.Transactions {
if err := b.Transactions[i].MarshalSia(w); err != nil {
return err
}
}
return nil
}
// UnmarshalSia implements the encoding.SiaUnmarshaler interface.
......@@ -49,7 +59,11 @@ func (bid *BlockID) UnmarshalJSON(b []byte) error {
// MarshalSia implements the encoding.SiaMarshaler interface.
func (cf CoveredFields) MarshalSia(w io.Writer) error {
encoding.NewEncoder(w).Encode(cf.WholeTransaction)
if cf.WholeTransaction {
w.Write([]byte{1})
} else {
w.Write([]byte{0})
}
fields := [][]uint64{
cf.SiacoinInputs,
cf.SiacoinOutputs,
......@@ -431,9 +445,21 @@ func (s *Specifier) UnmarshalJSON(b []byte) error {
return nil
}
// MarshalSia implements the encoding.SiaMarshaler interface.
func (sp *StorageProof) MarshalSia(w io.Writer) error {
w.Write(sp.ParentID[:])
w.Write(sp.Segment[:])
encoding.WriteInt(w, len(sp.HashSet))
for i := range sp.HashSet {
if _, err := w.Write(sp.HashSet[i][:]); err != nil {
return err
}
}
return nil
}
// MarshalSia implements the encoding.SiaMarshaler interface.
func (t Transaction) MarshalSia(w io.Writer) error {
enc := encoding.NewEncoder(w)
encoding.WriteInt(w, len((t.SiacoinInputs)))
for i := range t.SiacoinInputs {
t.SiacoinInputs[i].MarshalSia(w)
......@@ -452,11 +478,11 @@ func (t Transaction) MarshalSia(w io.Writer) error {
}
encoding.WriteInt(w, len((t.StorageProofs)))
for i := range t.StorageProofs {
enc.Encode(t.StorageProofs[i])
t.StorageProofs[i].MarshalSia(w)
}
encoding.WriteInt(w, len((t.SiafundInputs)))
for i := range t.SiafundInputs {
enc.Encode(t.SiafundInputs[i])
t.SiafundInputs[i].MarshalSia(w)
}
encoding.WriteInt(w, len((t.SiafundOutputs)))
for i := range t.SiafundOutputs {
......@@ -480,6 +506,47 @@ func (t Transaction) MarshalSia(w io.Writer) error {
return nil
}
// marshalSiaNoSignatures is a helper function for calculating certain hashes
// that do not include the transaction's signatures.
func (t Transaction) marshalSiaNoSignatures(w io.Writer) {
encoding.WriteInt(w, len((t.SiacoinInputs)))
for i := range t.SiacoinInputs {
t.SiacoinInputs[i].MarshalSia(w)
}
encoding.WriteInt(w, len((t.SiacoinOutputs)))
for i := range t.SiacoinOutputs {
t.SiacoinOutputs[i].MarshalSia(w)
}
encoding.WriteInt(w, len((t.FileContracts)))
for i := range t.FileContracts {
t.FileContracts[i].MarshalSia(w)
}
encoding.WriteInt(w, len((t.FileContractRevisions)))
for i := range t.FileContractRevisions {
t.FileContractRevisions[i].MarshalSia(w)
}
encoding.WriteInt(w, len((t.StorageProofs)))
for i := range t.StorageProofs {
t.StorageProofs[i].MarshalSia(w)
}
encoding.WriteInt(w, len((t.SiafundInputs)))
for i := range t.SiafundInputs {
t.SiafundInputs[i].MarshalSia(w)
}
encoding.WriteInt(w, len((t.SiafundOutputs)))
for i := range t.SiafundOutputs {
t.SiafundOutputs[i].MarshalSia(w)
}
encoding.WriteInt(w, len((t.MinerFees)))
for i := range t.MinerFees {
t.MinerFees[i].MarshalSia(w)
}
encoding.WriteInt(w, len((t.ArbitraryData)))
for i := range t.ArbitraryData {
encoding.WritePrefix(w, t.ArbitraryData[i])
}
}
// MarshalSiaSize returns the encoded size of t.
func (t Transaction) MarshalSiaSize() (size int) {
size += 8
......@@ -502,9 +569,9 @@ func (t Transaction) MarshalSiaSize() (size int) {
}
size += 8
for _, sp := range t.StorageProofs {
size += 8 + len(sp.HashSet)*crypto.HashSize
size += len(sp.ParentID)
size += 8 + len(sp.Segment)
size += len(sp.Segment)
size += 8 + len(sp.HashSet)*crypto.HashSize
}
size += 8
for _, sfi := range t.SiafundInputs {
......@@ -530,9 +597,11 @@ func (t Transaction) MarshalSiaSize() (size int) {
for _, ts := range t.TransactionSignatures {
size += len(ts.ParentID)
size += 8 // ts.PublicKeyIndex
size += 8 // ts.Timelock
size += ts.CoveredFields.MarshalSiaSize()
size += 8 + len(ts.Signature)
}
// Sanity check against the slower method.
if build.DEBUG {
expectedSize := len(encoding.Marshal(t))
......
......@@ -154,54 +154,43 @@ func (uc UnlockConditions) UnlockHash() UnlockHash {
func (t Transaction) SigHash(i int) (hash crypto.Hash) {
cf := t.TransactionSignatures[i].CoveredFields
h := crypto.NewHash()
enc := encoding.NewEncoder(h)
if cf.WholeTransaction {
enc.EncodeAll(
t.SiacoinInputs,
t.SiacoinOutputs,
t.FileContracts,
t.FileContractRevisions,
t.StorageProofs,
t.SiafundInputs,
t.SiafundOutputs,
t.MinerFees,
t.ArbitraryData,
t.TransactionSignatures[i].ParentID,
t.TransactionSignatures[i].PublicKeyIndex,
t.TransactionSignatures[i].Timelock,
)
t.marshalSiaNoSignatures(h)
h.Write(t.TransactionSignatures[i].ParentID[:])
encoding.WriteUint64(h, t.TransactionSignatures[i].PublicKeyIndex)
encoding.WriteUint64(h, uint64(t.TransactionSignatures[i].Timelock))
} else {
for _, input := range cf.SiacoinInputs {
enc.Encode(t.SiacoinInputs[input])
t.SiacoinInputs[input].MarshalSia(h)
}
for _, output := range cf.SiacoinOutputs {
enc.Encode(t.SiacoinOutputs[output])
t.SiacoinOutputs[output].MarshalSia(h)
}
for _, contract := range cf.FileContracts {
enc.Encode(t.FileContracts[contract])
t.FileContracts[contract].MarshalSia(h)
}
for _, revision := range cf.FileContractRevisions {
enc.Encode(t.FileContractRevisions[revision])
t.FileContractRevisions[revision].MarshalSia(h)
}
for _, storageProof := range cf.StorageProofs {
enc.Encode(t.StorageProofs[storageProof])
t.StorageProofs[storageProof].MarshalSia(h)
}
for _, siafundInput := range cf.SiafundInputs {
enc.Encode(t.SiafundInputs[siafundInput])
t.SiafundInputs[siafundInput].MarshalSia(h)
}
for _, siafundOutput := range cf.SiafundOutputs {
enc.Encode(t.SiafundOutputs[siafundOutput])
t.SiafundOutputs[siafundOutput].MarshalSia(h)
}
for _, minerFee := range cf.MinerFees {
enc.Encode(t.MinerFees[minerFee])
t.MinerFees[minerFee].MarshalSia(h)
}
for _, arbData := range cf.ArbitraryData {
enc.Encode(t.ArbitraryData[arbData])
encoding.WritePrefix(h, t.ArbitraryData[arbData])
}
}
for _, sig := range cf.TransactionSignatures {
enc.Encode(t.TransactionSignatures[sig])
t.TransactionSignatures[sig].MarshalSia(h)
}
h.Sum(hash[:0])
......
......@@ -7,7 +7,6 @@ package types
import (
"errors"
"io"
"github.com/NebulousLabs/Sia/build"
"github.com/NebulousLabs/Sia/crypto"
......@@ -143,47 +142,6 @@ type (
UnlockHash crypto.Hash
)
func (t Transaction) marshalSiaNoSignatures(w io.Writer) {
enc := encoding.NewEncoder(w)
encoding.WriteInt(w, len((t.SiacoinInputs)))
for i := range t.SiacoinInputs {
t.SiacoinInputs[i].MarshalSia(w)
}
encoding.WriteInt(w, len((t.SiacoinOutputs)))
for i := range t.SiacoinOutputs {
t.SiacoinOutputs[i].MarshalSia(w)
}
encoding.WriteInt(w, len((t.FileContracts)))
for i := range t.FileContracts {
enc.Encode(t.FileContracts[i])
}
encoding.WriteInt(w, len((t.FileContractRevisions)))
for i := range t.FileContractRevisions {
enc.Encode(t.FileContractRevisions[i])
}
encoding.WriteInt(w, len((t.StorageProofs)))
for i := range t.StorageProofs {
enc.Encode(t.StorageProofs[i])
}
encoding.WriteInt(w, len((t.SiafundInputs)))
for i := range t.SiafundInputs {
enc.Encode(t.SiafundInputs[i])
}
encoding.WriteInt(w, len((t.SiafundOutputs)))
for i := range t.SiafundOutputs {
t.SiafundOutputs[i].MarshalSia(w)
}
encoding.WriteInt(w, len((t.MinerFees)))
for i := range t.MinerFees {
t.MinerFees[i].MarshalSia(w)
}
encoding.WriteInt(w, len((t.ArbitraryData)))
for i := range t.ArbitraryData {
encoding.WritePrefix(w, t.ArbitraryData[i])
}
}
// ID returns the id of a transaction, which is taken by marshalling all of the
// fields except for the signatures and taking the hash of the result.
func (t Transaction) ID() TransactionID {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment