Skip to content

Commit

Permalink
Segwit support: parse transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
dvlop committed Apr 21, 2021
1 parent 81b0e4f commit 7b6f4d1
Showing 1 changed file with 55 additions and 9 deletions.
64 changes: 55 additions & 9 deletions parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,26 @@ static void parseInputs(
const Block *block,
const uint8_t *&p,
const uint8_t *txHash
#if defined(BITCOIN)
, uint64_t* witnessNum
#endif
) {
if(!skip) {
startInputs(p);
}

LOAD_VARINT(nbInputs, p);

#if defined(BITCOIN) // segwit support
*witnessNum = 0;
if(nbInputs == 0)
{
SKIP(unsigned char, flag, p); // flag is always 1 in current implementation
LOAD_VARINT(nbRealInputs, p);
nbInputs = nbRealInputs;
*witnessNum = nbRealInputs;
}
#endif
for(uint64_t inputIndex=0; inputIndex<nbInputs; ++inputIndex) {
parseInput<skip>(
block,
Expand All @@ -378,6 +392,28 @@ static void parseInputs(
}
}

#if defined(BITCOIN)
static void parseWitness(
const uint8_t* &p
) {
LOAD_VARINT(num, p);
for(uint64_t i=0; i<num; ++i)
{
LOAD_VARINT(num2, p);
p += num2; // just skip witness data for now
}
}


static void parseWitnesses(
const uint64_t witnessNum,
const uint8_t* &p
) {
for(uint64_t i=0; i<witnessNum; ++i)
parseWitness(p);
}
#endif

template<
bool skip
>
Expand Down Expand Up @@ -409,7 +445,12 @@ static void parseTX(
SKIP(uint32_t, nTime, p);
#endif

#if defined(BITCOIN)
uint64_t witnessNum = 0;
parseInputs<skip>(block, p, txHash, &witnessNum);
#else
parseInputs<skip>(block, p, txHash);
#endif

Chunk *txo = 0;
size_t txoOffset = -1;
Expand All @@ -422,6 +463,11 @@ static void parseTX(

parseOutputs<skip, false>(p, txHash);

#if defined(BITCOIN)
if(witnessNum)
parseWitnesses(witnessNum, p);
#endif

if(txo) {
size_t txoSize = p - outputsStart;
txo->init(
Expand Down Expand Up @@ -978,15 +1024,15 @@ static void findBlockFiles() {
}

auto fileSize = statBuf.st_size;
#if !defined(_WIN64)
auto r1 = posix_fadvise(fd, 0, fileSize, POSIX_FADV_NOREUSE);
if(r1<0) {
warning(
"failed to posix_fadvise on block chain file %s",
fileName.c_str()
);
}
#endif
#if !defined(_WIN64)
auto r1 = posix_fadvise(fd, 0, fileSize, POSIX_FADV_NOREUSE);
if(r1<0) {
warning(
"failed to posix_fadvise on block chain file %s",
fileName.c_str()
);
}
#endif

BlockFile blockFile;
blockFile.fd = fd;
Expand Down

0 comments on commit 7b6f4d1

Please sign in to comment.