Skip to content

Commit

Permalink
Add pegin input serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenroose committed Jan 2, 2019
1 parent ac1eadd commit ae54d4a
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/core_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,24 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry,
o.pushKV("asm", ScriptToAsmStr(txin.scriptSig, true));
o.pushKV("hex", HexStr(txin.scriptSig.begin(), txin.scriptSig.end()));
in.pushKV("scriptSig", o);

if (!tx.vin[i].scriptWitness.IsNull()) {
UniValue txinwitness(UniValue::VARR);
for (const auto& item : tx.vin[i].scriptWitness.stack) {
txinwitness.push_back(HexStr(item.begin(), item.end()));
}
in.pushKV("txinwitness", txinwitness);
}

// ELEMENTS:
in.pushKV("is_pegin", txin.m_is_pegin);
if (!tx.vin[i].m_pegin_witness.IsNull()) {
UniValue pegin_witness(UniValue::VARR);
for (const auto& item : tx.vin[i].m_pegin_witness.stack) {
pegin_witness.push_back(HexStr(item.begin(), item.end()));
}
in.pushKV("pegin_witness", pegin_witness);
}
}
in.pushKV("sequence", (int64_t)txin.nSequence);
vin.push_back(in);
Expand Down
108 changes: 107 additions & 1 deletion src/primitives/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ class COutPoint
uint256 hash;
uint32_t n;

//
// ELEMENTS flags:

/* If this flag is set, the CTxIn including this COutPoint has a
* CAssetIssuance object. */
//TODO(rebase) CA
//static const uint32_t OUTPOINT_ISSUANCE_FLAG = (1 << 31);

/* If this flag is set, the CTxIn including this COutPoint
* is a peg-in input. */
static const uint32_t OUTPOINT_PEGIN_FLAG = (1 << 30);

/* The inverse of the combination of the preceeding flags. Used to
* extract the original meaning of `n` as the index into the
* transaction's output array. */
static const uint32_t OUTPOINT_INDEX_MASK = 0x3fffffff;

// END ELEMENTS
//

COutPoint(): n((uint32_t) -1) { }
COutPoint(const uint256& hashIn, uint32_t nIn): hash(hashIn), n(nIn) { }

Expand Down Expand Up @@ -66,6 +86,18 @@ class CTxIn
uint32_t nSequence;
CScriptWitness scriptWitness; //! Only serialized through CTransaction

//
// ELEMENTS:

/* If this is set to true, the input is interpreted as a
* peg-in claim and processed as such */
bool m_is_pegin = false;
// Re-use script witness struct to include its own witness
CScriptWitness m_pegin_witness;

// END ELEMENTS
//

/* Setting nSequence to this value for every input in a transaction
* disables nLockTime. */
static const uint32_t SEQUENCE_FINAL = 0xffffffff;
Expand Down Expand Up @@ -105,7 +137,69 @@ class CTxIn

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(prevout);

//
// ELEMENTS:

//TODO(rebase) CA/CT
//bool fHasAssetIssuance;
COutPoint outpoint;
if (!ser_action.ForRead()) {
if (prevout.n == (uint32_t) -1) {
// Coinbase inputs do not have asset issuances attached
// to them.
// fHasAssetIssuance = false;
outpoint = prevout;
} else {
// The issuance and pegin bits can't be set as it is used to indicate
// the presence of the asset issuance or pegin objects. They should
// never be set anyway as that would require a parent
// transaction with over one billion outputs.
assert(!(prevout.n & ~COutPoint::OUTPOINT_INDEX_MASK));
// The assetIssuance object is used to represent both new
// asset generation and reissuance of existing asset types.
// fHasAssetIssuance = !assetIssuance.IsNull();
// The mode is placed in the upper bits of the outpoint's
// index field. The IssuanceMode enum values are chosen to
// make this as simple as a bitwise-OR.
outpoint.hash = prevout.hash;
outpoint.n = prevout.n & COutPoint::OUTPOINT_INDEX_MASK;
// if (fHasAssetIssuance) {
// outpoint.n |= COutPoint::OUTPOINT_ISSUANCE_FLAG;
// }
if (m_is_pegin) {
outpoint.n |= COutPoint::OUTPOINT_PEGIN_FLAG;
}
}
}

READWRITE(outpoint);

if (ser_action.ForRead()) {
if (outpoint.n == (uint32_t) -1) {
// No asset issuance for Coinbase inputs.
// fHasAssetIssuance = false;
prevout = outpoint;
m_is_pegin = false;
} else {
// The presence of the asset issuance object is indicated by
// a bit set in the outpoint index field.
// fHasAssetIssuance = !!(outpoint.n & COutPoint::OUTPOINT_ISSUANCE_FLAG);
// The interpretation of this input as a peg-in is indicated by
// a bit set in the outpoint index field.
m_is_pegin = !!(outpoint.n & COutPoint::OUTPOINT_PEGIN_FLAG);
// The mode, if set, must be masked out of the outpoint so
// that the in-memory index field retains its traditional
// meaning of identifying the index into the output array
// of the previous transaction.
prevout.hash = outpoint.hash;
prevout.n = outpoint.n & COutPoint::OUTPOINT_INDEX_MASK;
}
}

// END ELEMENTS
//

READWRITE(scriptSig);
READWRITE(nSequence);
}
Expand Down Expand Up @@ -219,6 +313,10 @@ inline void UnserializeTransaction(TxType& tx, Stream& s) {
flags ^= 1;
for (size_t i = 0; i < tx.vin.size(); i++) {
s >> tx.vin[i].scriptWitness.stack;
// ELEMENTS:
if (tx.vin[i].m_is_pegin) {
s >> tx.vin[i].m_pegin_witness.stack;
}
}
}
if (flags) {
Expand Down Expand Up @@ -252,6 +350,10 @@ inline void SerializeTransaction(const TxType& tx, Stream& s) {
if (flags & 1) {
for (size_t i = 0; i < tx.vin.size(); i++) {
s << tx.vin[i].scriptWitness.stack;
// ELEMENTS:
if (tx.vin[i].m_is_pegin) {
s << tx.vin[i].m_pegin_witness.stack;
}
}
}
s << tx.nLockTime;
Expand Down Expand Up @@ -351,6 +453,10 @@ class CTransaction
if (!vin[i].scriptWitness.IsNull()) {
return true;
}
// ELEMENTS:
if (!vin[i].m_pegin_witness.IsNull()) {
return true;
}
}
return false;
}
Expand Down
20 changes: 20 additions & 0 deletions test/util/data/tt-delin1-out.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"asm": "3046022100b4251ecd63778a3dde0155abe4cd162947620ae9ee45a874353551092325b116022100db307baf4ff3781ec520bd18f387948cedd15dc27bafe17c894b0fe6ffffcafa[ALL] 03091137f3ef23f4acfc19a5953a68b2074fae942ad3563ef28c33b0cac9a93adc",
"hex": "493046022100b4251ecd63778a3dde0155abe4cd162947620ae9ee45a874353551092325b116022100db307baf4ff3781ec520bd18f387948cedd15dc27bafe17c894b0fe6ffffcafa012103091137f3ef23f4acfc19a5953a68b2074fae942ad3563ef28c33b0cac9a93adc"
},
"is_pegin": false,
"sequence": 4294967295
},
{
Expand All @@ -23,6 +24,7 @@
"asm": "3044022079bd62ee09621a3be96b760c39e8ef78170101d46313923c6b07ae60a95c90670220238e51ea29fc70b04b65508450523caedbb11cb4dd5aa608c81487de798925ba[ALL] 027a759be8df971a6a04fafcb4f6babf75dc811c5cdaa0734cddbe9b942ce75b34",
"hex": "473044022079bd62ee09621a3be96b760c39e8ef78170101d46313923c6b07ae60a95c90670220238e51ea29fc70b04b65508450523caedbb11cb4dd5aa608c81487de798925ba0121027a759be8df971a6a04fafcb4f6babf75dc811c5cdaa0734cddbe9b942ce75b34"
},
"is_pegin": false,
"sequence": 4294967295
},
{
Expand All @@ -32,6 +34,7 @@
"asm": "304502207722d6f9038673c86a1019b1c4de2d687ae246477cd4ca7002762be0299de385022100e594a11e3a313942595f7666dcf7078bcb14f1330f4206b95c917e7ec0e82fac[ALL] 03091137f3ef23f4acfc19a5953a68b2074fae942ad3563ef28c33b0cac9a93adc",
"hex": "48304502207722d6f9038673c86a1019b1c4de2d687ae246477cd4ca7002762be0299de385022100e594a11e3a313942595f7666dcf7078bcb14f1330f4206b95c917e7ec0e82fac012103091137f3ef23f4acfc19a5953a68b2074fae942ad3563ef28c33b0cac9a93adc"
},
"is_pegin": false,
"sequence": 4294967295
},
{
Expand All @@ -41,6 +44,7 @@
"asm": "3045022100a63a4788027b79b65c6f9d9e054f68cf3b4eed19efd82a2d53f70dcbe64683390220526f243671425b2bd05745fcf2729361f985cfe84ea80c7cfc817b93d8134374[ALL] 03a621f08be22d1bbdcbe4e527ee4927006aa555fc65e2aafa767d4ea2fe9dfa52",
"hex": "483045022100a63a4788027b79b65c6f9d9e054f68cf3b4eed19efd82a2d53f70dcbe64683390220526f243671425b2bd05745fcf2729361f985cfe84ea80c7cfc817b93d8134374012103a621f08be22d1bbdcbe4e527ee4927006aa555fc65e2aafa767d4ea2fe9dfa52"
},
"is_pegin": false,
"sequence": 4294967295
},
{
Expand All @@ -50,6 +54,7 @@
"asm": "3046022100b200ac6db16842f76dab9abe807ce423c992805879bc50abd46ed8275a59d9cf022100c0d518e85dd345b3c29dd4dc47b9a420d3ce817b18720e94966d2fe23413a408[ALL] 03091137f3ef23f4acfc19a5953a68b2074fae942ad3563ef28c33b0cac9a93adc",
"hex": "493046022100b200ac6db16842f76dab9abe807ce423c992805879bc50abd46ed8275a59d9cf022100c0d518e85dd345b3c29dd4dc47b9a420d3ce817b18720e94966d2fe23413a408012103091137f3ef23f4acfc19a5953a68b2074fae942ad3563ef28c33b0cac9a93adc"
},
"is_pegin": false,
"sequence": 4294967295
},
{
Expand All @@ -59,6 +64,7 @@
"asm": "3045022100ededc441c3103a6f2bd6cab7639421af0f6ec5e60503bce1e603cf34f00aee1c02205cb75f3f519a13fb348783b21db3085cb5ec7552c59e394fdbc3e1feea43f967[ALL] 03a621f08be22d1bbdcbe4e527ee4927006aa555fc65e2aafa767d4ea2fe9dfa52",
"hex": "483045022100ededc441c3103a6f2bd6cab7639421af0f6ec5e60503bce1e603cf34f00aee1c02205cb75f3f519a13fb348783b21db3085cb5ec7552c59e394fdbc3e1feea43f967012103a621f08be22d1bbdcbe4e527ee4927006aa555fc65e2aafa767d4ea2fe9dfa52"
},
"is_pegin": false,
"sequence": 4294967295
},
{
Expand All @@ -68,6 +74,7 @@
"asm": "3045022100d9eed5413d2a4b4b98625aa6e3169edc4fb4663e7862316d69224454e70cd8ca022061e506521d5ced51dd0ea36496e75904d756a4c4f9fb111568555075d5f68d9a[ALL] 03f1575d6124ac78be398c25b31146d08313c6072d23a4d7df5ac6a9f87346c64c",
"hex": "483045022100d9eed5413d2a4b4b98625aa6e3169edc4fb4663e7862316d69224454e70cd8ca022061e506521d5ced51dd0ea36496e75904d756a4c4f9fb111568555075d5f68d9a012103f1575d6124ac78be398c25b31146d08313c6072d23a4d7df5ac6a9f87346c64c"
},
"is_pegin": false,
"sequence": 4294967295
},
{
Expand All @@ -77,6 +84,7 @@
"asm": "304502207e84b27139c4c19c828cb1e30c349bba88e4d9b59be97286960793b5ddc0a2af0221008cdc7a951e7f31c20953ed5635fbabf228e80b7047f32faaa0313e7693005177[ALL] 03f1575d6124ac78be398c25b31146d08313c6072d23a4d7df5ac6a9f87346c64c",
"hex": "48304502207e84b27139c4c19c828cb1e30c349bba88e4d9b59be97286960793b5ddc0a2af0221008cdc7a951e7f31c20953ed5635fbabf228e80b7047f32faaa0313e7693005177012103f1575d6124ac78be398c25b31146d08313c6072d23a4d7df5ac6a9f87346c64c"
},
"is_pegin": false,
"sequence": 4294967295
},
{
Expand All @@ -86,6 +94,7 @@
"asm": "30440220426540dfed9c4ab5812e5f06df705b8bcf307dd7d20f7fa6512298b2a6314f420220064055096e3ca62f6c7352c66a5447767c53f946acdf35025ab3807ddb2fa404[ALL] 03f1575d6124ac78be398c25b31146d08313c6072d23a4d7df5ac6a9f87346c64c",
"hex": "4730440220426540dfed9c4ab5812e5f06df705b8bcf307dd7d20f7fa6512298b2a6314f420220064055096e3ca62f6c7352c66a5447767c53f946acdf35025ab3807ddb2fa404012103f1575d6124ac78be398c25b31146d08313c6072d23a4d7df5ac6a9f87346c64c"
},
"is_pegin": false,
"sequence": 4294967295
},
{
Expand All @@ -95,6 +104,7 @@
"asm": "304402200a5e673996f2fc88e21cc8613611f08a650bc0370338803591d85d0ec5663764022040b6664a0d1ec83a7f01975b8fde5232992b8ca58bf48af6725d2f92a936ab2e[ALL] 03f1575d6124ac78be398c25b31146d08313c6072d23a4d7df5ac6a9f87346c64c",
"hex": "47304402200a5e673996f2fc88e21cc8613611f08a650bc0370338803591d85d0ec5663764022040b6664a0d1ec83a7f01975b8fde5232992b8ca58bf48af6725d2f92a936ab2e012103f1575d6124ac78be398c25b31146d08313c6072d23a4d7df5ac6a9f87346c64c"
},
"is_pegin": false,
"sequence": 4294967295
},
{
Expand All @@ -104,6 +114,7 @@
"asm": "3046022100d93b30219c5735f673be5c3b4688366d96f545561c74cb62c6958c00f6960806022100ec8200adcb028f2184fa2a4f6faac7f8bb57cb4503bb7584ac11051fece31b3d[ALL] 03091137f3ef23f4acfc19a5953a68b2074fae942ad3563ef28c33b0cac9a93adc",
"hex": "493046022100d93b30219c5735f673be5c3b4688366d96f545561c74cb62c6958c00f6960806022100ec8200adcb028f2184fa2a4f6faac7f8bb57cb4503bb7584ac11051fece31b3d012103091137f3ef23f4acfc19a5953a68b2074fae942ad3563ef28c33b0cac9a93adc"
},
"is_pegin": false,
"sequence": 4294967295
},
{
Expand All @@ -113,6 +124,7 @@
"asm": "3045022100a13934e68d3f5b22b130c4cb33f4da468cffc52323a47fbfbe06b64858162246022047081e0a70ff770e64a2e2d31e5d520d9102268b57a47009a72fe73ec7669018[ALL] 0234b9d9413f247bb78cd3293b7b65a2c38018ba5621ea9ee737f3a6a3523fb4cd",
"hex": "483045022100a13934e68d3f5b22b130c4cb33f4da468cffc52323a47fbfbe06b64858162246022047081e0a70ff770e64a2e2d31e5d520d9102268b57a47009a72fe73ec766901801210234b9d9413f247bb78cd3293b7b65a2c38018ba5621ea9ee737f3a6a3523fb4cd"
},
"is_pegin": false,
"sequence": 4294967295
},
{
Expand All @@ -122,6 +134,7 @@
"asm": "304602210097f1f35d5bdc1a3a60390a1b015b8e7c4f916aa3847aafd969e04975e15bbe70022100a9052eb25517d481f1fda1b129eb1b534da50ea1a51f3ee012dca3601c11b86a[ALL] 027a759be8df971a6a04fafcb4f6babf75dc811c5cdaa0734cddbe9b942ce75b34",
"hex": "49304602210097f1f35d5bdc1a3a60390a1b015b8e7c4f916aa3847aafd969e04975e15bbe70022100a9052eb25517d481f1fda1b129eb1b534da50ea1a51f3ee012dca3601c11b86a0121027a759be8df971a6a04fafcb4f6babf75dc811c5cdaa0734cddbe9b942ce75b34"
},
"is_pegin": false,
"sequence": 4294967295
},
{
Expand All @@ -131,6 +144,7 @@
"asm": "3045022012b3138c591bf7154b6fef457f2c4a3c7162225003788ac0024a99355865ff13022100b71b125ae1ffb2e1d1571f580cd3ebc8cd049a2d7a8a41f138ba94aeb982106f[ALL] 03091137f3ef23f4acfc19a5953a68b2074fae942ad3563ef28c33b0cac9a93adc",
"hex": "483045022012b3138c591bf7154b6fef457f2c4a3c7162225003788ac0024a99355865ff13022100b71b125ae1ffb2e1d1571f580cd3ebc8cd049a2d7a8a41f138ba94aeb982106f012103091137f3ef23f4acfc19a5953a68b2074fae942ad3563ef28c33b0cac9a93adc"
},
"is_pegin": false,
"sequence": 4294967295
},
{
Expand All @@ -140,6 +154,7 @@
"asm": "3045022100f834ccc8b22ee72712a3e5e6ef4acb8b2fb791b5385b70e2cd4332674d6667f4022024fbda0a997e0c253503f217501f508a4d56edce2c813ecdd9ad796dbeba9074[ALL] 0234b9d9413f247bb78cd3293b7b65a2c38018ba5621ea9ee737f3a6a3523fb4cd",
"hex": "483045022100f834ccc8b22ee72712a3e5e6ef4acb8b2fb791b5385b70e2cd4332674d6667f4022024fbda0a997e0c253503f217501f508a4d56edce2c813ecdd9ad796dbeba907401210234b9d9413f247bb78cd3293b7b65a2c38018ba5621ea9ee737f3a6a3523fb4cd"
},
"is_pegin": false,
"sequence": 4294967295
},
{
Expand All @@ -149,6 +164,7 @@
"asm": "304502203b2fd1e39ae0e469d7a15768f262661b0de41470daf0fe8c4fd0c26542a0870002210081c57e331f9a2d214457d953e3542904727ee412c63028113635d7224da3dccc[ALL] 03f1575d6124ac78be398c25b31146d08313c6072d23a4d7df5ac6a9f87346c64c",
"hex": "48304502203b2fd1e39ae0e469d7a15768f262661b0de41470daf0fe8c4fd0c26542a0870002210081c57e331f9a2d214457d953e3542904727ee412c63028113635d7224da3dccc012103f1575d6124ac78be398c25b31146d08313c6072d23a4d7df5ac6a9f87346c64c"
},
"is_pegin": false,
"sequence": 4294967295
},
{
Expand All @@ -158,6 +174,7 @@
"asm": "304502206947a9c54f0664ece4430fd4ae999891dc50bb6126bc36b6a15a3189f29d25e9022100a86cfc4e2fdd9e39a20e305cfd1b76509c67b3e313e0f118229105caa0e823c9[ALL] 03f1575d6124ac78be398c25b31146d08313c6072d23a4d7df5ac6a9f87346c64c",
"hex": "48304502206947a9c54f0664ece4430fd4ae999891dc50bb6126bc36b6a15a3189f29d25e9022100a86cfc4e2fdd9e39a20e305cfd1b76509c67b3e313e0f118229105caa0e823c9012103f1575d6124ac78be398c25b31146d08313c6072d23a4d7df5ac6a9f87346c64c"
},
"is_pegin": false,
"sequence": 4294967295
},
{
Expand All @@ -167,6 +184,7 @@
"asm": "3045022100c7128fe10b2d38744ae8177776054c29fc8ec13f07207723e70766ab7164847402201d2cf09009b9596de74c0183d1ab832e5edddb7a9965880bb400097e850850f8[ALL] 03f1575d6124ac78be398c25b31146d08313c6072d23a4d7df5ac6a9f87346c64c",
"hex": "483045022100c7128fe10b2d38744ae8177776054c29fc8ec13f07207723e70766ab7164847402201d2cf09009b9596de74c0183d1ab832e5edddb7a9965880bb400097e850850f8012103f1575d6124ac78be398c25b31146d08313c6072d23a4d7df5ac6a9f87346c64c"
},
"is_pegin": false,
"sequence": 4294967295
},
{
Expand All @@ -176,6 +194,7 @@
"asm": "304502203b89a71628a28cc3703d170ca3be77786cff6b867e38a18b719705f8a326578f022100b2a9879e1acf621faa6466c207746a7f3eb4c8514c1482969aba3f2a957f1321[ALL] 03f1575d6124ac78be398c25b31146d08313c6072d23a4d7df5ac6a9f87346c64c",
"hex": "48304502203b89a71628a28cc3703d170ca3be77786cff6b867e38a18b719705f8a326578f022100b2a9879e1acf621faa6466c207746a7f3eb4c8514c1482969aba3f2a957f1321012103f1575d6124ac78be398c25b31146d08313c6072d23a4d7df5ac6a9f87346c64c"
},
"is_pegin": false,
"sequence": 4294967295
},
{
Expand All @@ -185,6 +204,7 @@
"asm": "3046022100ef794a8ef7fd6752d2a183c18866ff6e8dc0f5bd889a63e2c21cf303a6302461022100c1b09662d9e92988c3f9fcf17d1bcc79b5403647095d7212b9f8a1278a532d68[ALL] 03091137f3ef23f4acfc19a5953a68b2074fae942ad3563ef28c33b0cac9a93adc",
"hex": "493046022100ef794a8ef7fd6752d2a183c18866ff6e8dc0f5bd889a63e2c21cf303a6302461022100c1b09662d9e92988c3f9fcf17d1bcc79b5403647095d7212b9f8a1278a532d68012103091137f3ef23f4acfc19a5953a68b2074fae942ad3563ef28c33b0cac9a93adc"
},
"is_pegin": false,
"sequence": 4294967295
}
],
Expand Down

0 comments on commit ae54d4a

Please sign in to comment.