Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handshake codec #762

Draft
wants to merge 57 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
0f72644
Add handshake codec
nytzuga Jul 28, 2023
89bf82c
Use 8 bytes to hash UpgradeConfig
nytzuga Aug 14, 2023
19e0d18
Fixed linting issues
nytzuga Sep 8, 2023
30b8aaa
Addressed PR comments
nytzuga Sep 14, 2023
4a59b8a
Improve API usage
nytzuga Sep 14, 2023
acc8195
Rewrote configs
nytzuga Oct 13, 2023
a0ceae8
WIP: Optional upgrades tests
nytzuga Oct 20, 2023
c5d86a4
Upgrade to the latest version
nytzuga Nov 2, 2023
b6f8a33
Merge remote-tracking branch 'origin/master' into handshake-codec
nytzuga Nov 20, 2023
453c84d
Remove unknown field
nytzuga Nov 20, 2023
7c507ac
Address comments from PR
nytzuga Nov 20, 2023
9c954d5
Update precompile template
nytzuga Nov 20, 2023
a27ecc8
Merge remote-tracking branch 'origin/master' into handshake-codec
nytzuga Nov 27, 2023
771d511
Improve template
nytzuga Nov 27, 2023
c261ce2
Add more test cases
nytzuga Nov 27, 2023
6fff967
Linting
nytzuga Nov 27, 2023
e3601b0
Remove Test config
nytzuga Nov 27, 2023
cade28f
Use whole hash instead of the first 8 bytes
nytzuga Nov 28, 2023
6b96e44
Merge remote-tracking branch 'origin/master' into handshake-codec
nytzuga Nov 29, 2023
a2848d7
Merge remote-tracking branch 'origin/master' into handshake-codec
nytzuga Nov 30, 2023
d996f99
Update how optional upgrades are going to be represented internally
nytzuga Dec 4, 2023
758441f
Merge remote-tracking branch 'origin/master' into handshake-codec
nytzuga Dec 4, 2023
634e9b5
Add ToBytes() and FromBytes() to the Config interface
nytzuga Dec 12, 2023
d1b37c4
Merge remote-tracking branch 'origin/master' into handshake-codec
nytzuga Dec 12, 2023
eef61dd
Update template
nytzuga Dec 12, 2023
541acd7
Update mocks
nytzuga Dec 13, 2023
2309af5
Remove serialize tag
nytzuga Dec 13, 2023
74e25d5
Implement custom marshal/unmarshal for all types
nytzuga Dec 13, 2023
f1d3d07
Use standard Marshal/Unmarshaller
nytzuga Dec 14, 2023
55f4b95
Implement UpgradeConfig Marshal/Unmarshal
nytzuga Dec 14, 2023
d9851ca
Added Marshal / Unmarshal to OptionalNetworkUpgrades
nytzuga Dec 14, 2023
1946de4
Remove sorting from AllowListConfig
nytzuga Dec 15, 2023
2efae3d
Move tests to their own file
nytzuga Dec 15, 2023
6785dd9
Remove all custom serializers
nytzuga Dec 15, 2023
32486fe
WIP
nytzuga Dec 28, 2023
8bc4f87
Merge remote-tracking branch 'origin/master' into handshake-codec
nytzuga Jan 4, 2024
2de9095
Do not use double pointers
nytzuga Jan 4, 2024
3a1bd28
Apply suggestions from code review
nytzuga Jan 4, 2024
b2020cb
Update precompile/allowlist/config.go
nytzuga Jan 4, 2024
beb7ab8
Update precompile/allowlist/config.go
nytzuga Jan 4, 2024
8192cb2
Bring back getBigIntToSerialize() until a better solution can be found
nytzuga Jan 4, 2024
b86226d
Merge remote-tracking branch 'origin/handshake-codec' into handshake-…
nytzuga Jan 4, 2024
8935259
Remove all magic bytes
nytzuga Jan 4, 2024
5c88c08
Use fixed bytes for addresses
nytzuga Jan 4, 2024
1819327
Improve serialization and testing
nytzuga Jan 4, 2024
e079719
Use fixed bytes for addresses
nytzuga Jan 4, 2024
17bf11e
Minor changes
nytzuga Jan 4, 2024
91bdff5
Move UnpackAddresses/PackAddresses to utils
nytzuga Jan 4, 2024
a593a68
Fix typo
nytzuga Jan 4, 2024
987017b
Update template
nytzuga Jan 4, 2024
19b3073
Move serialization to utils
nytzuga Jan 4, 2024
e8ffb13
Fixed typo
nytzuga Jan 4, 2024
550f7c0
Fixed typo
nytzuga Jan 4, 2024
a08d746
Fix typo
nytzuga Jan 4, 2024
e0b4b61
Merge remote-tracking branch 'origin/master' into handshake-codec
nytzuga Jan 5, 2024
1bf4664
Rebase with master
nytzuga Jan 5, 2024
ac0e945
Add comments back
nytzuga Jan 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions accounts/abi/bind/precompilebind/precompile_config_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ package {{.Package}}

import (
"github.com/ava-labs/subnet-evm/precompile/precompileconfig"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/docker/docker/pkg/units"

{{- if .Contract.AllowList}}
"github.com/ava-labs/subnet-evm/precompile/allowlist"

Expand Down Expand Up @@ -48,6 +51,50 @@ func NewConfig(blockTimestamp *uint64{{if .Contract.AllowList}}, admins []common
}
}

func (c * Config) MarshalBinary() ([]byte, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add some comments here why these are important, how these can be implemented etc?

p := wrappers.Packer {
Bytes: []byte{},
MaxSize: 1 * units.MiB,
}
{{- if .Contract.AllowList}}
allowBytes, err := c.AllowListConfig.MarshalBinary()
if err != nil {
return nil, err
}
p.PackBytes(allowBytes)
if p.Err != nil {
return nil, p.Err
}
{{- end}}
upgradeBytes, err := c.Upgrade.MarshalBinary()
if err != nil {
return nil, err
}
p.PackBytes(upgradeBytes)
return p.Bytes, p.Err
}

func (c * Config) UnmarshalBinary(bytes []byte) error {
p := wrappers.Packer {
Bytes: bytes,
}
{{- if .Contract.AllowList}}
allowList := p.UnpackBytes()
if p.Err != nil {
return p.Err
}
if err := c.AllowListConfig.UnmarshalBinary(allowList); err != nil {
return err
}
{{- end}}
upgrade := p.UnpackBytes()
if p.Err != nil {
return p.Err
}

return c.Upgrade.UnmarshalBinary(upgrade)
}

// NewDisableConfig returns config for a network upgrade at [blockTimestamp]
// that disables {{.Contract.Type}}.
func NewDisableConfig(blockTimestamp *uint64) *Config {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package {{.Package}}
import (
"testing"

"github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/precompile/precompileconfig"
"github.com/ava-labs/subnet-evm/precompile/testutils"
"github.com/ava-labs/subnet-evm/utils"
Expand All @@ -22,6 +23,7 @@ import (
"github.com/ethereum/go-ethereum/common"
{{- end}}
"go.uber.org/mock/gomock"
"github.com/stretchr/testify/require"
)

// TestVerify tests the verification of Config.
Expand Down Expand Up @@ -61,6 +63,35 @@ func TestVerify(t *testing.T) {
{{- end}}
}

func TestSerialize(t *testing.T) {
var t0 uint64 = 2
var t1 uint64 = 1001

config := params.UpgradeConfig{
PrecompileUpgrades: []params.PrecompileUpgrade{
{
Config: NewConfig(&t0,
{{- if .Contract.AllowList}}
[]common.Address{
common.BytesToAddress(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")),
common.BytesToAddress(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000030")),
}, []common.Address{
common.BytesToAddress(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")),
}, []common.Address{
common.BytesToAddress(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000050")),
},
{{- end}}
),
},
{
Config: NewDisableConfig(&t1), // disable at timestamp 1
},
},
}
require.NotNil(t, config)
params.AssertConfigHashesAndSerialization(t, &config)
}

// TestEqual tests the equality of Config with other precompile configs.
func TestEqual(t *testing.T) {
{{- if .Contract.AllowList}}
Expand Down
94 changes: 94 additions & 0 deletions commontype/fee_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"fmt"
"math/big"

"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/subnet-evm/utils"
"github.com/docker/docker/pkg/units"
"github.com/ethereum/go-ethereum/common"
)

Expand Down Expand Up @@ -144,6 +146,98 @@ func (f *FeeConfig) checkByteLens() error {
return nil
}

func (c *FeeConfig) MarshalBinary() ([]byte, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add the following test cases for the serialization of FeeConfig:

  1. serialize and de-serialize valid config and get the same value
  2. hardcoded test vector and require it results in the same value
  3. unmarshal should fail if there are insufficient bytes to unpack big int values

p := wrappers.Packer{
Bytes: []byte{},
MaxSize: 1 * units.MiB,
}

if err := utils.PackBigInt(&p, c.GasLimit); err != nil {
return nil, err
}

if err := utils.PackBigInt(&p, c.MinBaseFee); err != nil {
return nil, err
}

if err := utils.PackBigInt(&p, c.TargetGas); err != nil {
return nil, err
}

if err := utils.PackBigInt(&p, c.BaseFeeChangeDenominator); err != nil {
return nil, err
}

if err := utils.PackBigInt(&p, c.MinBlockGasCost); err != nil {
return nil, err
}

if err := utils.PackBigInt(&p, c.MaxBlockGasCost); err != nil {
return nil, err
}

if err := utils.PackBigInt(&p, c.BlockGasCostStep); err != nil {
return nil, err
}

p.PackLong(c.TargetBlockRate)
if p.Err != nil {
return nil, p.Err
}

return p.Bytes, nil
}

func (c *FeeConfig) UnmarshalBinary(data []byte) error {
p := wrappers.Packer{
Bytes: data,
}

var err error

c.GasLimit, err = utils.UnpackBigInt(&p)
if err != nil {
return err
}

c.MinBaseFee, err = utils.UnpackBigInt(&p)
if err != nil {
return err
}

c.TargetGas, err = utils.UnpackBigInt(&p)
if err != nil {
return err
}

c.BaseFeeChangeDenominator, err = utils.UnpackBigInt(&p)
if err != nil {
return err
}

c.MinBlockGasCost, err = utils.UnpackBigInt(&p)
if err != nil {
return err
}

c.MaxBlockGasCost, err = utils.UnpackBigInt(&p)
if err != nil {
return err
}

c.BlockGasCostStep, err = utils.UnpackBigInt(&p)
if err != nil {
return err
}

c.TargetBlockRate = p.UnpackLong()
if p.Err != nil {
return p.Err
}

return nil
}

func isBiggerThanHashLen(bigint *big.Int) bool {
buf := bigint.Bytes()
isBigger := len(buf) > common.HashLength
Expand Down
Loading
Loading