Add support for EIP2718 (enveloppe) and EIP2930 (acess list tx); Display chain ID when different from 1 (ethereum mainnet)

This commit is contained in:
pscott
2021-04-21 16:56:17 +02:00
parent 5dd99c3d48
commit 970f0355dd
14 changed files with 419 additions and 146 deletions

View File

@@ -20,6 +20,7 @@
#include "ethUstream.h"
#include "ethUtils.h"
#include "utils.h"
#define MAX_INT256 32
#define MAX_ADDRESS 20
@@ -30,12 +31,15 @@ void initTx(txContext_t *context,
txContent_t *content,
ustreamProcess_t customProcessor,
void *extra) {
uint8_t save = context->txType;
memset(context, 0, sizeof(txContext_t));
context->txType = save;
context->sha3 = sha3;
context->content = content;
context->customProcessor = customProcessor;
context->extra = extra;
context->currentField = TX_RLP_CONTENT;
context->currentField = RLP_NONE + 1;
cx_keccak_init(context->sha3, 256);
}
@@ -86,6 +90,22 @@ static void processContent(txContext_t *context) {
context->processingField = false;
}
static void processAccessList(txContext_t *context) {
if (!context->currentFieldIsList) {
PRINTF("Invalid type for RLP_DATA\n");
THROW(EXCEPTION);
}
if (context->currentFieldPos < context->currentFieldLength) {
uint32_t copySize =
MIN(context->commandLength, context->currentFieldLength - context->currentFieldPos);
copyTxData(context, NULL, copySize);
}
if (context->currentFieldPos == context->currentFieldLength) {
context->currentField++;
context->processingField = false;
}
}
static void processType(txContext_t *context) {
if (context->currentFieldIsList) {
PRINTF("Invalid type for RLP_TYPE\n");
@@ -106,6 +126,28 @@ static void processType(txContext_t *context) {
}
}
static void processChainID(txContext_t *context) {
if (context->currentFieldIsList) {
PRINTF("Invalid type for RLP_CHAINID\n");
THROW(EXCEPTION);
}
if (context->currentFieldLength > MAX_INT256) {
PRINTF("Invalid length for RLP_CHAINID\n");
THROW(EXCEPTION);
}
if (context->currentFieldPos < context->currentFieldLength) {
uint32_t copySize =
MIN(context->commandLength, context->currentFieldLength - context->currentFieldPos);
copyTxData(context, context->content->chainID.value, copySize);
}
if (context->currentFieldPos == context->currentFieldLength) {
context->content->chainID.length = context->currentFieldLength;
context->currentField++;
context->processingField = false;
}
PRINTF("chainID: %.*H\n", context->content->chainID.length, context->content->chainID.value);
}
static void processNonce(txContext_t *context) {
if (context->currentFieldIsList) {
PRINTF("Invalid type for RLP_NONCE\n");
@@ -148,6 +190,11 @@ static void processStartGas(txContext_t *context) {
}
}
// Alias over `processStartGas()`.
static void processGasLimit(txContext_t *context) {
processStartGas(context);
}
static void processGasprice(txContext_t *context) {
if (context->currentFieldIsList) {
PRINTF("Invalid type for RLP_GASPRICE\n");
@@ -248,68 +295,167 @@ static void processV(txContext_t *context) {
}
}
static bool processEIP2930Tx(txContext_t *context) {
switch (context->currentField) {
case EIP2930_RLP_CONTENT:
processContent(context);
if ((context->processingFlags & TX_FLAG_TYPE) == 0) {
context->currentField++;
}
break;
case EIP2930_RLP_TYPE:
processType(context);
break;
case EIP2930_RLP_CHAINID:
processChainID(context);
break;
case EIP2930_RLP_NONCE:
processNonce(context);
break;
case EIP2930_RLP_GASPRICE:
processGasprice(context);
break;
case EIP2930_RLP_GASLIMIT:
processGasLimit(context);
break;
case EIP2930_RLP_TO:
processTo(context);
break;
case EIP2930_RLP_VALUE:
processValue(context);
break;
case EIP2930_RLP_YPARITY:
processV(context);
break;
case EIP2930_RLP_ACCESS_LIST:
processAccessList(context);
break;
case EIP2930_RLP_DATA:
case EIP2930_RLP_SENDER_R:
case EIP2930_RLP_SENDER_S:
processData(context);
break;
default:
PRINTF("Invalid RLP decoder context\n");
return true;
}
return false;
}
static bool processLegacyTx(txContext_t *context) {
PRINTF("Processing legacy\n");
switch (context->currentField) {
case LEGACY_RLP_CONTENT:
processContent(context);
if ((context->processingFlags & TX_FLAG_TYPE) == 0) {
context->currentField++;
}
break;
case LEGACY_RLP_TYPE:
processType(context);
break;
case LEGACY_RLP_NONCE:
processNonce(context);
break;
case LEGACY_RLP_GASPRICE:
processGasprice(context);
break;
case LEGACY_RLP_STARTGAS:
processStartGas(context);
break;
case LEGACY_RLP_TO:
processTo(context);
break;
case LEGACY_RLP_VALUE:
processValue(context);
break;
case LEGACY_RLP_DATA:
case LEGACY_RLP_R:
case LEGACY_RLP_S:
processData(context);
break;
case LEGACY_RLP_V:
processV(context);
break;
default:
PRINTF("Invalid RLP decoder context\n");
return true;
}
return false;
}
static parserStatus_e parseRLP(txContext_t *context) {
bool canDecode = false;
uint32_t offset;
while (context->commandLength != 0) {
bool valid;
// Feed the RLP buffer until the length can be decoded
context->rlpBuffer[context->rlpBufferPos++] = readTxByte(context);
if (rlpCanDecode(context->rlpBuffer, context->rlpBufferPos, &valid)) {
// Can decode now, if valid
if (!valid) {
PRINTF("RLP pre-decode error\n");
return USTREAM_FAULT;
}
canDecode = true;
break;
}
// Cannot decode yet
// Sanity check
if (context->rlpBufferPos == sizeof(context->rlpBuffer)) {
PRINTF("RLP pre-decode logic error\n");
return USTREAM_FAULT;
}
}
if (!canDecode) {
PRINTF("Can't decode\n");
return USTREAM_PROCESSING;
}
// Ready to process this field
if (!rlpDecodeLength(context->rlpBuffer,
context->rlpBufferPos,
&context->currentFieldLength,
&offset,
&context->currentFieldIsList)) {
PRINTF("RLP decode error\n");
return USTREAM_FAULT;
}
if (offset == 0) {
// Hack for single byte, self encoded
context->workBuffer--;
context->commandLength++;
context->fieldSingleByte = true;
} else {
context->fieldSingleByte = false;
}
context->currentFieldPos = 0;
context->rlpBufferPos = 0;
context->processingField = true;
return USTREAM_CONTINUE;
}
static parserStatus_e processTxInternal(txContext_t *context) {
for (;;) {
customStatus_e customStatus = CUSTOM_NOT_HANDLED;
// EIP 155 style transaction
if (context->currentField == TX_RLP_DONE) {
if (IS_PARSING_DONE(context)) {
return USTREAM_FINISHED;
}
// Old style transaction
if ((context->currentField == TX_RLP_V) && (context->commandLength == 0)) {
if ((context->currentField == LEGACY_RLP_V ||
context->currentField == EIP2930_RLP_YPARITY) &&
(context->commandLength == 0)) {
context->content->vLength = 0;
return USTREAM_FINISHED;
}
if (context->commandLength == 0) {
return USTREAM_PROCESSING;
}
if (!context->processingField) {
bool canDecode = false;
uint32_t offset;
while (context->commandLength != 0) {
bool valid;
// Feed the RLP buffer until the length can be decoded
context->rlpBuffer[context->rlpBufferPos++] = readTxByte(context);
if (rlpCanDecode(context->rlpBuffer, context->rlpBufferPos, &valid)) {
// Can decode now, if valid
if (!valid) {
PRINTF("RLP pre-decode error\n");
return USTREAM_FAULT;
}
canDecode = true;
break;
}
// Cannot decode yet
// Sanity check
if (context->rlpBufferPos == sizeof(context->rlpBuffer)) {
PRINTF("RLP pre-decode logic error\n");
return USTREAM_FAULT;
}
parserStatus_e status = parseRLP(context);
if (status != USTREAM_CONTINUE) {
return status;
}
if (!canDecode) {
return USTREAM_PROCESSING;
}
// Ready to process this field
if (!rlpDecodeLength(context->rlpBuffer,
context->rlpBufferPos,
&context->currentFieldLength,
&offset,
&context->currentFieldIsList)) {
PRINTF("RLP decode error\n");
return USTREAM_FAULT;
}
if (offset == 0) {
// Hack for single byte, self encoded
context->workBuffer--;
context->commandLength++;
context->fieldSingleByte = true;
} else {
context->fieldSingleByte = false;
}
context->currentFieldPos = 0;
context->rlpBufferPos = 0;
context->processingField = true;
}
if (context->customProcessor != NULL) {
customStatus = context->customProcessor(context);
@@ -328,41 +474,25 @@ static parserStatus_e processTxInternal(txContext_t *context) {
}
}
if (customStatus == CUSTOM_NOT_HANDLED) {
switch (context->currentField) {
case TX_RLP_CONTENT:
processContent(context);
if ((context->processingFlags & TX_FLAG_TYPE) == 0) {
context->currentField++;
PRINTF("Current field: %u\n", context->currentField);
switch (context->txType) {
bool fault;
case LEGACY:
fault = processLegacyTx(context);
if (fault) {
return USTREAM_FAULT;
} else {
break;
}
case EIP2930:
fault = processEIP2930Tx(context);
if (fault) {
return USTREAM_FAULT;
} else {
break;
}
break;
case TX_RLP_TYPE:
processType(context);
break;
case TX_RLP_NONCE:
processNonce(context);
break;
case TX_RLP_GASPRICE:
processGasprice(context);
break;
case TX_RLP_STARTGAS:
processStartGas(context);
break;
case TX_RLP_VALUE:
processValue(context);
break;
case TX_RLP_TO:
processTo(context);
break;
case TX_RLP_DATA:
case TX_RLP_R:
case TX_RLP_S:
processData(context);
break;
case TX_RLP_V:
processV(context);
break;
default:
PRINTF("Invalid RLP decoder context\n");
PRINTF("Transaction type %u is not supported\n", context->txType);
return USTREAM_FAULT;
}
}

View File

@@ -37,35 +37,63 @@ typedef customStatus_e (*ustreamProcess_t)(struct txContext_t *context);
#define TX_FLAG_TYPE 0x01
typedef enum rlpTxField_e {
TX_RLP_NONE = 0,
TX_RLP_CONTENT,
TX_RLP_TYPE,
TX_RLP_NONCE,
TX_RLP_GASPRICE,
TX_RLP_STARTGAS,
TX_RLP_TO,
TX_RLP_VALUE,
TX_RLP_DATA,
TX_RLP_V,
TX_RLP_R,
TX_RLP_S,
TX_RLP_DONE
} rlpTxField_e;
// First variant of every Tx enum.
#define RLP_NONE 0
#define IS_PARSING_DONE(ctx) \
((ctx->txType == LEGACY && ctx->currentField == LEGACY_RLP_DONE) || \
(ctx->txType == EIP2930 && ctx->currentField == EIP2930_RLP_DONE))
typedef enum rlpLegacyTxField_e {
LEGACY_RLP_NONE = RLP_NONE,
LEGACY_RLP_CONTENT,
LEGACY_RLP_TYPE,
LEGACY_RLP_NONCE,
LEGACY_RLP_GASPRICE,
LEGACY_RLP_STARTGAS,
LEGACY_RLP_TO,
LEGACY_RLP_VALUE,
LEGACY_RLP_DATA,
LEGACY_RLP_V,
LEGACY_RLP_R,
LEGACY_RLP_S,
LEGACY_RLP_DONE
} rlpLegacyTxField_e;
typedef enum rlpEIP2930TxField_e {
EIP2930_RLP_NONE = RLP_NONE,
EIP2930_RLP_CONTENT,
EIP2930_RLP_TYPE,
EIP2930_RLP_CHAINID,
EIP2930_RLP_NONCE,
EIP2930_RLP_GASPRICE,
EIP2930_RLP_GASLIMIT,
EIP2930_RLP_TO,
EIP2930_RLP_VALUE,
EIP2930_RLP_DATA,
EIP2930_RLP_ACCESS_LIST,
EIP2930_RLP_YPARITY,
EIP2930_RLP_SENDER_R,
EIP2930_RLP_SENDER_S,
EIP2930_RLP_DONE
} rlpEIP2930TxField_e;
#define MIN_TX_TYPE 0x00
#define MAX_TX_TYPE 0x7f
// EIP 2718 TransactionType
// Valid transaction types should be in [0x00, 0x7f]
typedef enum txType_e {
LEGACY_TX = 1,
EIP2930 = 0x01,
LEGACY = 0xc0 // Legacy tx are greater than or equal to 0xc0.
} txType_e;
typedef enum parserStatus_e {
USTREAM_PROCESSING,
USTREAM_SUSPENDED,
USTREAM_FINISHED,
USTREAM_FAULT
USTREAM_PROCESSING, // Parsing is in progress
USTREAM_SUSPENDED, // Parsing has been suspended
USTREAM_FINISHED, // Parsing is done
USTREAM_FAULT, // An error was encountered while parsing
USTREAM_CONTINUE // Used internally to signify we can keep on parsing
} parserStatus_e;
typedef struct txInt256_t {
@@ -78,6 +106,7 @@ typedef struct txContent_t {
txInt256_t startgas;
txInt256_t value;
txInt256_t nonce;
txInt256_t chainID;
uint8_t destination[20];
uint8_t destinationLength;
uint8_t v[4];
@@ -85,7 +114,7 @@ typedef struct txContent_t {
} txContent_t;
typedef struct txContext_t {
rlpTxField_e currentField;
uint8_t currentField;
cx_sha3_t *sha3;
uint32_t currentFieldLength;
uint32_t currentFieldPos;