Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
8ac47db2
Unverified
Commit
8ac47db2
authored
Apr 28, 2022
by
Mark Tyneway
Committed by
GitHub
Apr 28, 2022
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2381 from ethereum-optimism/feat/gasusage-compression-ratio
tasks: compute calldata cost diff in `fetch-batches`
parents
f1586e3a
5cb3a5f7
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
67 additions
and
11 deletions
+67
-11
tasty-adults-explode.md
.changeset/tasty-adults-explode.md
+5
-0
fetch-batches.ts
packages/contracts/tasks/fetch-batches.ts
+26
-8
fees.ts
packages/core-utils/src/optimism/fees.ts
+16
-2
fees.spec.ts
packages/core-utils/test/fees.spec.ts
+20
-1
No files found.
.changeset/tasty-adults-explode.md
0 → 100644
View file @
8ac47db2
---
'
@eth-optimism/core-utils'
:
patch
---
Add a
`calldataCost`
function that computes the cost of calldata
packages/contracts/tasks/fetch-batches.ts
View file @
8ac47db2
import
{
ethers
}
from
'
ethers
'
import
{
task
}
from
'
hardhat/config
'
import
*
as
types
from
'
hardhat/internal/core/params/argumentTypes
'
import
{
BatchType
,
SequencerBatch
}
from
'
@eth-optimism/core-utils
'
import
{
BatchType
,
SequencerBatch
,
calldataCost
,
}
from
'
@eth-optimism/core-utils
'
import
{
names
}
from
'
../src/address-names
'
import
{
getContractFromArtifact
}
from
'
../src/deploy-utils
'
...
...
@@ -52,28 +56,42 @@ task('fetch-batches')
const
tx
=
await
provider
.
getTransaction
(
event
.
transactionHash
)
const
batch
=
(
SequencerBatch
as
any
).
fromHex
(
tx
.
data
)
// Add
an extra field
to the resulting json
// so that the serialization sizes can be observed
// Add
extra fields
to the resulting json
// so that the serialization sizes
and gas usage
can be observed
const
json
=
batch
.
toJSON
()
json
.
sizes
=
{
legacy
:
0
,
zlib
:
0
,
}
json
.
gasUsage
=
{
legacy
:
0
,
zlib
:
0
,
}
// Create a copy of the batch to serialize in
// the alternative format
const
copy
=
(
SequencerBatch
as
any
).
fromHex
(
tx
.
data
)
let
legacy
:
Buffer
let
zlib
:
Buffer
if
(
batch
.
type
===
BatchType
.
ZLIB
)
{
copy
.
type
=
BatchType
.
LEGACY
json
.
sizes
.
legacy
=
copy
.
encode
().
length
json
.
sizes
.
zlib
=
batch
.
encode
().
length
legacy
=
copy
.
encode
()
zlib
=
batch
.
encode
()
}
else
{
copy
.
type
=
BatchType
.
ZLIB
json
.
sizes
.
zlib
=
copy
.
encode
().
length
json
.
sizes
.
legacy
=
batch
.
encode
().
length
zlib
=
copy
.
encode
()
legacy
=
batch
.
encode
()
}
json
.
compressionRatio
=
json
.
sizes
.
zlib
/
json
.
sizes
.
legacy
json
.
sizes
.
legacy
=
legacy
.
length
json
.
sizes
.
zlib
=
zlib
.
length
json
.
sizes
.
compressionRatio
=
json
.
sizes
.
zlib
/
json
.
sizes
.
legacy
json
.
gasUsage
.
legacy
=
calldataCost
(
legacy
).
toNumber
()
json
.
gasUsage
.
zlib
=
calldataCost
(
zlib
).
toNumber
()
json
.
gasUsage
.
compressionRatio
=
json
.
gasUsage
.
zlib
/
json
.
gasUsage
.
legacy
batches
.
push
(
json
)
}
...
...
packages/core-utils/src/optimism/fees.ts
View file @
8ac47db2
...
...
@@ -6,8 +6,8 @@ import { BigNumber } from 'ethers'
import
{
remove0x
}
from
'
../common
'
const
txDataZeroGas
=
4
const
txDataNonZeroGasEIP2028
=
16
export
const
txDataZeroGas
=
4
export
const
txDataNonZeroGasEIP2028
=
16
const
big10
=
BigNumber
.
from
(
10
)
export
const
scaleDecimals
=
(
...
...
@@ -63,3 +63,17 @@ export const zeroesAndOnes = (data: Buffer | string): Array<number> => {
}
return
[
zeros
,
ones
]
}
/**
* Computes the L1 calldata cost of bytes based
* on the London hardfork.
*
* @param data {Buffer|string} Bytes
* @returns {BigNumber} Gas consumed by the bytes
*/
export
const
calldataCost
=
(
data
:
Buffer
|
string
):
BigNumber
=>
{
const
[
zeros
,
ones
]
=
zeroesAndOnes
(
data
)
const
zeroCost
=
BigNumber
.
from
(
zeros
).
mul
(
txDataZeroGas
)
const
nonZeroCost
=
BigNumber
.
from
(
ones
).
mul
(
txDataNonZeroGasEIP2028
)
return
zeroCost
.
add
(
nonZeroCost
)
}
packages/core-utils/test/fees.spec.ts
View file @
8ac47db2
import
{
zeroesAndOnes
}
from
'
../src
'
import
'
./setup
'
import
{
BigNumber
}
from
'
ethers
'
import
{
zeroesAndOnes
,
calldataCost
}
from
'
../src
'
describe
(
'
Fees
'
,
()
=>
{
it
(
'
should count zeros and ones
'
,
()
=>
{
...
...
@@ -15,4 +19,19 @@ describe('Fees', () => {
ones
.
should
.
eq
(
test
.
ones
)
}
})
it
(
'
should compute calldata costs
'
,
()
=>
{
const
cases
=
[
{
input
:
'
0x
'
,
output
:
BigNumber
.
from
(
0
)
},
{
input
:
'
0x00
'
,
output
:
BigNumber
.
from
(
4
)
},
{
input
:
'
0xff
'
,
output
:
BigNumber
.
from
(
16
)
},
{
input
:
Buffer
.
alloc
(
32
),
output
:
BigNumber
.
from
(
4
*
32
)
},
{
input
:
Buffer
.
alloc
(
32
,
0xff
),
output
:
BigNumber
.
from
(
16
*
32
)
},
]
for
(
const
test
of
cases
)
{
const
cost
=
calldataCost
(
test
.
input
)
cost
.
should
.
deep
.
eq
(
test
.
output
)
}
})
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment