private void SerializeTxn(BitcoinStream stream, bool witSupported) { byte flags = 0; var version = (witSupported && (this.Inputs.Count == 0 && this.Outputs.Count > 0)) ? this.Version | NoDummyInput : this.Version; stream.ReadWrite(ref version); // POS Timestamp var time = this.Time; stream.ReadWrite(ref time); if (witSupported) { // Check whether witnesses need to be serialized. if (HasWitness) { flags |= 1; } } if (flags != 0) { // Use extended format in case witnesses are to be serialized. TxInList vinDummy = new TxInList(); stream.ReadWrite <TxInList, TxIn>(ref vinDummy); stream.ReadWrite(ref flags); } TxInList vin = this.Inputs; stream.ReadWrite <TxInList, TxIn>(ref vin); vin.Transaction = this; TxOutList vout = this.Outputs; stream.ReadWrite <TxOutList, TxOut>(ref vout); vout.Transaction = this; if ((flags & 1) != 0) { StratisWitness wit = new StratisWitness(this.Inputs); wit.ReadWrite(stream); } LockTime lockTime = this.LockTime; stream.ReadWriteStruct(ref lockTime); }
private void DeserializeTxn(BitcoinStream stream, bool witSupported) { byte flags = 0; UInt32 nVersionTemp = 0; stream.ReadWrite(ref nVersionTemp); // POS time stamp uint nTimeTemp = 0; stream.ReadWrite(ref nTimeTemp); TxInList vinTemp = new TxInList(); TxOutList voutTemp = new TxOutList(); // Try to read the vin. In case the dummy is there, this will be read as an empty vector. stream.ReadWrite <TxInList, TxIn>(ref vinTemp); var hasNoDummy = (nVersionTemp & NoDummyInput) != 0 && vinTemp.Count == 0; if (witSupported && hasNoDummy) { nVersionTemp = nVersionTemp & ~NoDummyInput; } if (vinTemp.Count == 0 && witSupported && !hasNoDummy) { // We read a dummy or an empty vin. stream.ReadWrite(ref flags); if (flags != 0) { // Assume we read a dummy and a flag. stream.ReadWrite <TxInList, TxIn>(ref vinTemp); vinTemp.Transaction = this; stream.ReadWrite <TxOutList, TxOut>(ref voutTemp); voutTemp.Transaction = this; } else { // Assume read a transaction without output. voutTemp = new TxOutList(); voutTemp.Transaction = this; } } else { // We read a non-empty vin. Assume a normal vout follows. stream.ReadWrite <TxOutList, TxOut>(ref voutTemp); voutTemp.Transaction = this; } if (((flags & 1) != 0) && witSupported) { // The witness flag is present, and we support witnesses. flags ^= 1; StratisWitness wit = new StratisWitness(vinTemp); wit.ReadWrite(stream); } if (flags != 0) { // Unknown flag in the serialization throw new FormatException("Unknown transaction optional data"); } LockTime lockTimeTemp = LockTime.Zero; stream.ReadWriteStruct(ref lockTimeTemp); this.Version = nVersionTemp; this.Time = nTimeTemp; // POS Timestamp vinTemp.ForEach(i => this.AddInput(i)); voutTemp.ForEach(i => this.AddOutput(i)); this.LockTime = lockTimeTemp; }