Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanio committed Jul 13, 2020
1 parent 3f2e313 commit 0f6cf99
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 32 deletions.
11 changes: 7 additions & 4 deletions packages/block/src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,13 @@ export class Block {
for (let i = 0; i < rawTransactions.length; i++) {
// TODO: Pass the common object instead of the options. It can't be implemented right now
// because the hardfork may be `null`. Read the above TODO for more info.
const tx = Transaction.fromValuesArray(
rawTransactions[i] as Buffer[],
chainOptions as TxOptions,
)
const txData = rawTransactions[i]
let tx
if (Array.isArray(txData)) {
tx = Transaction.fromValuesArray(txData as Buffer[], chainOptions as TxOptions)
} else {
tx = Transaction.fromRlpSerializedTx(txData as Buffer, chainOptions as TxOptions)
}
this.transactions.push(tx)
}
}
Expand Down
25 changes: 13 additions & 12 deletions packages/tx/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ export default class Transaction {

return new Transaction(
{
nonce: values[0] || new Buffer([]),
gasPrice: values[1] || new Buffer([]),
gasLimit: values[2] || new Buffer([]),
to: values[3] || new Buffer([]),
value: values[4] || new Buffer([]),
data: values[5] || new Buffer([]),
v: values[6] || new Buffer([]),
r: values[7] || new Buffer([]),
s: values[8] || new Buffer([]),
nonce: values[0] || Buffer.from([]),
gasPrice: values[1] || Buffer.from([]),
gasLimit: values[2] || Buffer.from([]),
to: values[3] || Buffer.from([]),
value: values[4] || Buffer.from([]),
data: values[5] || Buffer.from([]),
v: values[6] || Buffer.from([]),
r: values[7] || Buffer.from([]),
s: values[8] || Buffer.from([]),
},
opts,
)
Expand Down Expand Up @@ -232,7 +232,7 @@ export default class Transaction {
}

/**
* the minimum amount of gas the tx must have (DataFee + TxFee + Creation Fee)
* The minimum amount of gas the tx must have (DataFee + TxFee + Creation Fee)
*/
getBaseFee(): BN {
const fee = this.getDataFee().iaddn(this._common.param('gasPrices', 'tx'))
Expand All @@ -243,7 +243,7 @@ export default class Transaction {
}

/**
* the up front amount that an account must have for this transaction to be valid
* The up front amount that an account must have for this transaction to be valid
*/
getUpfrontCost(): BN {
return new BN(this.gasLimit).imul(new BN(this.gasPrice)).iadd(new BN(this.value))
Expand Down Expand Up @@ -306,7 +306,8 @@ export default class Transaction {
}

public isSigned(): boolean {
return this.v.length > 0 && this.r.length > 0 && this.s.length > 0
const { v, r, s } = this
return v.length > 0 && r.length > 0 && s.length > 0
}

get nonce(): Buffer {
Expand Down
20 changes: 13 additions & 7 deletions packages/vm/tests/api/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ tape('VM events', (t) => {
st.end()
})

t.test('should the Transaction before running it', async (st) => {
t.test('should emit the Transaction before running it', async (st) => {
const vm = new VM()

let emitted
Expand All @@ -57,7 +57,8 @@ tape('VM events', (t) => {
})

const tx = Transaction.fromTxData({
gas: 200000,
gasPrice: 40000,
gasLimit: 90000,
to: '0x1111111111111111111111111111111111111111',
})
tx.sign(util.toBuffer('0xa5737ecdc1b89ca0091647e727ba082ed8953f29182e94adc397210dda643b07'))
Expand All @@ -77,7 +78,8 @@ tape('VM events', (t) => {
})

const tx = Transaction.fromTxData({
gas: 200000,
gasPrice: 40000,
gasLimit: 90000,
to: '0x1111111111111111111111111111111111111111',
value: 1,
})
Expand All @@ -98,7 +100,8 @@ tape('VM events', (t) => {
})

const tx = Transaction.fromTxData({
gas: 200000,
gasPrice: 40000,
gasLimit: 90000,
to: '0x1111111111111111111111111111111111111111',
value: 1,
})
Expand All @@ -120,7 +123,8 @@ tape('VM events', (t) => {
})

const tx = Transaction.fromTxData({
gas: 200000,
gasPrice: 40000,
gasLimit: 90000,
to: '0x1111111111111111111111111111111111111111',
value: 1,
})
Expand All @@ -144,7 +148,8 @@ tape('VM events', (t) => {
// the stack, stores that in memory, and then returns the first byte from memory.
// This deploys a contract which a single byte of code, 0x41.
const tx = Transaction.fromTxData({
gas: 200000,
gasPrice: 40000,
gasLimit: 90000,
data: '0x7f410000000000000000000000000000000000000000000000000000000000000060005260016000f3',
})
tx.sign(util.toBuffer('0xa5737ecdc1b89ca0091647e727ba082ed8953f29182e94adc397210dda643b07'))
Expand All @@ -167,7 +172,8 @@ tape('VM events', (t) => {
// the stack, stores that in memory, and then returns the first byte from memory.
// This deploys a contract which a single byte of code, 0x41.
const tx = Transaction.fromTxData({
gas: 200000,
gasPrice: 40000,
gasLimit: 90000,
data: '0x7f410000000000000000000000000000000000000000000000000000000000000060005260016000f3',
})
tx.sign(util.toBuffer('0xa5737ecdc1b89ca0091647e727ba082ed8953f29182e94adc397210dda643b07'))
Expand Down
22 changes: 13 additions & 9 deletions packages/vm/tests/api/runTx.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ tape('runTx', (t) => {
t.test('should fail to run without signature', async (st) => {
const tx = getTransaction(false, true)
shouldFail(st, suite.runTx({ tx }), (e) =>
st.ok(e.message.toLowerCase().includes('signature'), 'should fail with appropriate error'),
st.ok(
e.message.includes('This transaction is not signed'),
'should fail with appropriate error',
),
)
st.end()
})
Expand All @@ -68,7 +71,7 @@ tape('should run simple tx without errors', async (t) => {

const tx = getTransaction(true, true)
const acc = createAccount()
await suite.putAccount(tx.from.toString('hex'), acc)
await suite.putAccount(tx.getSenderAddress().toString('hex'), acc)

let res = await suite.runTx({ tx, populateCache: true })
t.true(res.gasUsed.gt(0), 'should have used some gas')
Expand All @@ -83,7 +86,7 @@ tape('should fail when account balance overflows (call)', async (t) => {
const tx = getTransaction(true, true, '0x01')
const from = createAccount()
const to = createAccount('0x00', ethUtil.MAX_INTEGER)
await suite.putAccount(tx.from.toString('hex'), from)
await suite.putAccount(tx.getSenderAddress().toString('hex'), from)
await suite.putAccount(tx.to, to)

const res = await suite.runTx({ tx })
Expand All @@ -101,8 +104,9 @@ tape('should fail when account balance overflows (create)', async (t) => {
const tx = getTransaction(true, true, '0x01', true)
const from = createAccount()
const to = createAccount('0x00', ethUtil.MAX_INTEGER)
await suite.putAccount(tx.from.toString('hex'), from)
await suite.putAccount(tx.getSenderAddress().toString('hex'), from)
await suite.putAccount(contractAddress, to)
tx.to = contractAddress

const res = await suite.runTx({ tx })

Expand All @@ -120,7 +124,7 @@ tape('should fail when account balance overflows (create)', async (t) => {
const tx = getTransaction(true, true)
const acc = createAccount()
await suite.putAccount(tx.from.toString('hex'), acc)
await suite.putAccount(tx.getSenderAddress().toString('hex'), acc)
await suite.cacheFlush()
suite.vm.stateManager.cache.clear()
Expand Down Expand Up @@ -160,10 +164,10 @@ function getTransaction(
const txParams = {
nonce: '0x00',
gasPrice: 100,
gasLimit: 1000,
to: to,
value: value,
data: data,
gasLimit: 90000,
to,
value,
data,
chainId: 3,
}

Expand Down

0 comments on commit 0f6cf99

Please sign in to comment.