From 4591ccdbe89fd1f58fb176fd743aeabccb0b1d1d Mon Sep 17 00:00:00 2001 From: Cesar Date: Fri, 15 Dec 2023 18:02:45 -0300 Subject: [PATCH] Remove all custom serializers Use MarshalBinary and UnmarshalBinary --- commontype/fee_config.go | 21 +++++-- precompile/allowlist/config.go | 32 ++++++---- .../contracts/deployerallowlist/config.go | 30 ++++++++-- precompile/contracts/feemanager/config.go | 40 ++++++++++--- precompile/contracts/nativeminter/config.go | 25 ++++++-- precompile/contracts/rewardmanager/config.go | 58 ++++++++++++++----- precompile/contracts/txallowlist/config.go | 26 +++++++-- precompile/precompileconfig/upgradeable.go | 18 ++++-- x/warp/config.go | 13 ++++- 9 files changed, 204 insertions(+), 59 deletions(-) diff --git a/commontype/fee_config.go b/commontype/fee_config.go index aca3a5cd40..cf1867d385 100644 --- a/commontype/fee_config.go +++ b/commontype/fee_config.go @@ -9,6 +9,7 @@ import ( "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" ) @@ -152,28 +153,36 @@ func (c *FeeConfig) getBigIntToSerialize() []**big.Int { } } -func (c *FeeConfig) ToBytesWithPacker(p *wrappers.Packer) error { +func (c *FeeConfig) MarshalBinary() ([]byte, error) { + p := wrappers.Packer{ + Bytes: []byte{}, + MaxSize: 1 * units.MiB, + } + for _, bigint := range c.getBigIntToSerialize() { p.PackBool(*bigint == nil) if p.Err != nil { - return p.Err + return nil, p.Err } if bigint != nil { p.PackBytes((*bigint).Bytes()) if p.Err != nil { - return p.Err + return nil, p.Err } } } p.PackLong(c.TargetBlockRate) if p.Err != nil { - return p.Err + return nil, p.Err } - return nil + return p.Bytes, nil } -func (c *FeeConfig) FromBytesWithPacker(p *wrappers.Packer) error { +func (c *FeeConfig) UnmarshalBinary(data []byte) error { + p := wrappers.Packer{ + Bytes: data, + } for _, bigint := range c.getBigIntToSerialize() { isNil := p.UnpackBool() if p.Err != nil { diff --git a/precompile/allowlist/config.go b/precompile/allowlist/config.go index 6487b9f47d..044e8650ba 100644 --- a/precompile/allowlist/config.go +++ b/precompile/allowlist/config.go @@ -9,6 +9,7 @@ import ( "github.com/ava-labs/avalanchego/utils/wrappers" "github.com/ava-labs/subnet-evm/precompile/contract" "github.com/ava-labs/subnet-evm/precompile/precompileconfig" + "github.com/docker/docker/pkg/units" "github.com/ethereum/go-ethereum/common" ) @@ -145,29 +146,36 @@ func (c *AllowListConfig) Verify(chainConfig precompileconfig.ChainConfig, upgra return nil } -func (c *AllowListConfig) ToBytesWithPacker(p *wrappers.Packer) error { - if err := c.packAddresses(c.AdminAddresses, p); err != nil { - return err +func (c *AllowListConfig) MarshalBinary() ([]byte, error) { + p := wrappers.Packer{ + Bytes: []byte{}, + MaxSize: 1 * units.MiB, } - if err := c.packAddresses(c.ManagerAddresses, p); err != nil { - return err + if err := c.packAddresses(c.AdminAddresses, &p); err != nil { + return nil, err } - if err := c.packAddresses(c.EnabledAddresses, p); err != nil { - return err + if err := c.packAddresses(c.ManagerAddresses, &p); err != nil { + return nil, err } - return nil + if err := c.packAddresses(c.EnabledAddresses, &p); err != nil { + return nil, err + } + return p.Bytes, nil } -func (c *AllowListConfig) FromBytesWithPacker(p *wrappers.Packer) error { - admins, err := c.unpackAddresses(p) +func (c *AllowListConfig) UnmarshalBinary(data []byte) error { + p := wrappers.Packer{ + Bytes: data, + } + admins, err := c.unpackAddresses(&p) if err != nil { return err } - managers, err := c.unpackAddresses(p) + managers, err := c.unpackAddresses(&p) if err != nil { return err } - enableds, err := c.unpackAddresses(p) + enableds, err := c.unpackAddresses(&p) if err != nil { return err } diff --git a/precompile/contracts/deployerallowlist/config.go b/precompile/contracts/deployerallowlist/config.go index 061d99ace5..288cc16de9 100644 --- a/precompile/contracts/deployerallowlist/config.go +++ b/precompile/contracts/deployerallowlist/config.go @@ -66,14 +66,26 @@ func (c *Config) MarshalBinary() ([]byte, error) { MaxSize: 1 * units.MiB, } - if err := c.AllowListConfig.ToBytesWithPacker(&p); err != nil { + bytes, err := c.AllowListConfig.MarshalBinary() + if err != nil { return nil, err } - if err := c.Upgrade.ToBytesWithPacker(&p); err != nil { + p.PackBytes(bytes) + if p.Err != nil { + return nil, p.Err + } + + bytes, err = c.Upgrade.MarshalBinary() + if err != nil { return nil, err } + p.PackBytes(bytes) + if p.Err != nil { + return nil, p.Err + } + return p.Bytes, nil } @@ -81,10 +93,20 @@ func (c *Config) UnmarshalBinary(bytes []byte) error { p := wrappers.Packer{ Bytes: bytes, } - if err := c.AllowListConfig.FromBytesWithPacker(&p); err != nil { + + allowList := p.UnpackBytes() + if p.Err != nil { + return p.Err + } + upgrade := p.UnpackBytes() + if p.Err != nil { + return p.Err + } + + if err := c.AllowListConfig.UnmarshalBinary(allowList); err != nil { return err } - if err := c.Upgrade.FromBytesWithPacker(&p); err != nil { + if err := c.Upgrade.UnmarshalBinary(upgrade); err != nil { return err } return nil diff --git a/precompile/contracts/feemanager/config.go b/precompile/contracts/feemanager/config.go index f047f693fe..4f4edcd593 100644 --- a/precompile/contracts/feemanager/config.go +++ b/precompile/contracts/feemanager/config.go @@ -86,12 +86,24 @@ func (c *Config) MarshalBinary() ([]byte, error) { MaxSize: 1 * units.MiB, } - if err := c.AllowListConfig.ToBytesWithPacker(&p); err != nil { + allowList, err := c.AllowListConfig.MarshalBinary() + if err != nil { return nil, err } - if err := c.Upgrade.ToBytesWithPacker(&p); err != nil { + upgrade, err := c.Upgrade.MarshalBinary() + if err != nil { return nil, err + + } + + p.PackBytes(allowList) + if p.Err != nil { + return nil, p.Err + } + p.PackBytes(upgrade) + if p.Err != nil { + return nil, p.Err } if c.InitialFeeConfig == nil { @@ -104,28 +116,42 @@ func (c *Config) MarshalBinary() ([]byte, error) { if p.Err != nil { return nil, p.Err } - if err := c.InitialFeeConfig.ToBytesWithPacker(&p); err != nil { + bytes, err := c.InitialFeeConfig.MarshalBinary() + if err != nil { return nil, err } + p.PackBytes(bytes) } - return p.Bytes, nil + return p.Bytes, p.Err } func (c *Config) UnmarshalBinary(bytes []byte) error { p := wrappers.Packer{ Bytes: bytes, } - if err := c.AllowListConfig.FromBytesWithPacker(&p); err != nil { + allowList := p.UnpackBytes() + if p.Err != nil { + return p.Err + } + upgrade := p.UnpackBytes() + if p.Err != nil { + return p.Err + } + if err := c.AllowListConfig.UnmarshalBinary(allowList); err != nil { return err } - if err := c.Upgrade.FromBytesWithPacker(&p); err != nil { + if err := c.Upgrade.UnmarshalBinary(upgrade); err != nil { return err } isNil := p.UnpackBool() if !isNil { c.InitialFeeConfig = &commontype.FeeConfig{} - if err := c.InitialFeeConfig.FromBytesWithPacker(&p); err != nil { + bytes := p.UnpackBytes() + if p.Err != nil { + return p.Err + } + if err := c.InitialFeeConfig.UnmarshalBinary(bytes); err != nil { return err } } diff --git a/precompile/contracts/nativeminter/config.go b/precompile/contracts/nativeminter/config.go index 99703a8469..8dc92435c7 100644 --- a/precompile/contracts/nativeminter/config.go +++ b/precompile/contracts/nativeminter/config.go @@ -115,14 +115,23 @@ func (c *Config) MarshalBinary() ([]byte, error) { MaxSize: 1 * units.MiB, } - if err := c.AllowListConfig.ToBytesWithPacker(&p); err != nil { + bytes, err := c.AllowListConfig.MarshalBinary() + if err != nil { return nil, err } - if err := c.Upgrade.ToBytesWithPacker(&p); err != nil { + p.PackBytes(bytes) + if p.Err != nil { + return nil, p.Err + } + + bytes, err = c.Upgrade.MarshalBinary() + if err != nil { return nil, err } + p.PackBytes(bytes) + p.PackInt(uint32(len(keys))) if p.Err != nil { return nil, p.Err @@ -146,10 +155,18 @@ func (c *Config) UnmarshalBinary(bytes []byte) error { p := wrappers.Packer{ Bytes: bytes, } - if err := c.AllowListConfig.FromBytesWithPacker(&p); err != nil { + allowList := p.UnpackBytes() + if p.Err != nil { + return p.Err + } + upgrade := p.UnpackBytes() + if p.Err != nil { + return p.Err + } + if err := c.AllowListConfig.UnmarshalBinary(allowList); err != nil { return err } - if err := c.Upgrade.FromBytesWithPacker(&p); err != nil { + if err := c.Upgrade.UnmarshalBinary(upgrade); err != nil { return err } len := p.UnpackInt() diff --git a/precompile/contracts/rewardmanager/config.go b/precompile/contracts/rewardmanager/config.go index 0d3a6bf914..d10897cc3a 100644 --- a/precompile/contracts/rewardmanager/config.go +++ b/precompile/contracts/rewardmanager/config.go @@ -22,16 +22,23 @@ type InitialRewardConfig struct { RewardAddress common.Address `json:"rewardAddress,omitempty"` } -func (u *InitialRewardConfig) ToBytesWithPacker(p *wrappers.Packer) error { +func (u *InitialRewardConfig) MarshalBinary() ([]byte, error) { + p := wrappers.Packer{ + Bytes: []byte{}, + MaxSize: 1 * units.MiB, + } p.PackBool(u.AllowFeeRecipients) if p.Err != nil { - return p.Err + return nil, p.Err } p.PackBytes(u.RewardAddress[:]) - return p.Err + return p.Bytes, p.Err } -func (u *InitialRewardConfig) FromBytesWithPacker(p *wrappers.Packer) error { +func (u *InitialRewardConfig) UnmarshalBinary(data []byte) error { + p := wrappers.Packer{ + Bytes: data, + } u.AllowFeeRecipients = p.UnpackBool() if p.Err != nil { return p.Err @@ -145,13 +152,24 @@ func (c *Config) MarshalBinary() ([]byte, error) { MaxSize: 1 * units.MiB, } - if err := c.AllowListConfig.ToBytesWithPacker(&p); err != nil { + bytes, err := c.AllowListConfig.MarshalBinary() + if err != nil { return nil, err } - if err := c.Upgrade.ToBytesWithPacker(&p); err != nil { + p.PackBytes(bytes) + if p.Err != nil { + return nil, p.Err + } + + bytes, err = c.Upgrade.MarshalBinary() + if err != nil { return nil, err } + p.PackBytes(bytes) + if p.Err != nil { + return nil, p.Err + } p.PackBool(c.InitialRewardConfig == nil) if p.Err != nil { @@ -159,32 +177,44 @@ func (c *Config) MarshalBinary() ([]byte, error) { } if c.InitialRewardConfig != nil { - if err := c.InitialRewardConfig.ToBytesWithPacker(&p); err != nil { + bytes, err := c.InitialRewardConfig.MarshalBinary() + if err != nil { return nil, err } + p.PackBytes(bytes) } - return p.Bytes, nil + return p.Bytes, p.Err } func (c *Config) UnmarshalBinary(bytes []byte) error { p := wrappers.Packer{ Bytes: bytes, } - if err := c.AllowListConfig.FromBytesWithPacker(&p); err != nil { + allowList := p.UnpackBytes() + if p.Err != nil { + return p.Err + } + upgrade := p.UnpackBytes() + if p.Err != nil { + return p.Err + } + if err := c.AllowListConfig.UnmarshalBinary(allowList); err != nil { return err } - if err := c.Upgrade.FromBytesWithPacker(&p); err != nil { + if err := c.Upgrade.UnmarshalBinary(upgrade); err != nil { return err } isNil := p.UnpackBool() - if !isNil { + if p.Err == nil && !isNil { c.InitialRewardConfig = &InitialRewardConfig{} - if err := c.InitialRewardConfig.FromBytesWithPacker(&p); err != nil { - return err + bytes := p.UnpackBytes() + if p.Err != nil { + return p.Err } + return c.InitialRewardConfig.UnmarshalBinary(bytes) } - return nil + return p.Err } diff --git a/precompile/contracts/txallowlist/config.go b/precompile/contracts/txallowlist/config.go index 859d61db34..89acb47af7 100644 --- a/precompile/contracts/txallowlist/config.go +++ b/precompile/contracts/txallowlist/config.go @@ -66,14 +66,23 @@ func (c *Config) MarshalBinary() ([]byte, error) { MaxSize: 1 * units.MiB, } - if err := c.AllowListConfig.ToBytesWithPacker(&p); err != nil { + bytes, err := c.AllowListConfig.MarshalBinary() + if err != nil { return nil, err } - if err := c.Upgrade.ToBytesWithPacker(&p); err != nil { + p.PackBytes(bytes) + if p.Err != nil { + return nil, p.Err + } + + bytes, err = c.Upgrade.MarshalBinary() + if err != nil { return nil, err } + p.PackBytes(bytes) + return p.Bytes, nil } @@ -82,9 +91,16 @@ func (c *Config) UnmarshalBinary(bytes []byte) error { Bytes: bytes, } - if err := c.AllowListConfig.FromBytesWithPacker(&p); err != nil { + allowList := p.UnpackBytes() + if p.Err != nil { + return p.Err + } + upgrade := p.UnpackBytes() + if p.Err != nil { + return p.Err + } + if err := c.AllowListConfig.UnmarshalBinary(allowList); err != nil { return err } - - return c.Upgrade.FromBytesWithPacker(&p) + return c.Upgrade.UnmarshalBinary(upgrade) } diff --git a/precompile/precompileconfig/upgradeable.go b/precompile/precompileconfig/upgradeable.go index 8ae28860cd..a2e005592d 100644 --- a/precompile/precompileconfig/upgradeable.go +++ b/precompile/precompileconfig/upgradeable.go @@ -6,6 +6,7 @@ package precompileconfig import ( "github.com/ava-labs/avalanchego/utils/wrappers" "github.com/ava-labs/subnet-evm/utils" + "github.com/docker/docker/pkg/units" ) // Upgrade contains the timestamp for the upgrade along with @@ -35,24 +36,31 @@ func (u *Upgrade) Equal(other *Upgrade) bool { return u.Disable == other.Disable && utils.Uint64PtrEqual(u.BlockTimestamp, other.BlockTimestamp) } -func (u *Upgrade) ToBytesWithPacker(p *wrappers.Packer) error { +func (u *Upgrade) MarshalBinary() ([]byte, error) { + p := wrappers.Packer{ + Bytes: []byte{}, + MaxSize: 1 * units.MiB, + } if u.BlockTimestamp == nil { p.PackBool(true) } else { p.PackBool(false) if p.Err != nil { - return p.Err + return nil, p.Err } p.PackLong(*u.BlockTimestamp) } if p.Err != nil { - return p.Err + return nil, p.Err } p.PackBool(u.Disable) - return p.Err + return p.Bytes, p.Err } -func (u *Upgrade) FromBytesWithPacker(p *wrappers.Packer) error { +func (u *Upgrade) UnmarshalBinary(data []byte) error { + p := wrappers.Packer{ + Bytes: data, + } isNil := p.UnpackBool() if !isNil { timestamp := p.UnpackLong() diff --git a/x/warp/config.go b/x/warp/config.go index 53731132e3..4e92c1e64c 100644 --- a/x/warp/config.go +++ b/x/warp/config.go @@ -219,9 +219,14 @@ func (c *Config) MarshalBinary() ([]byte, error) { MaxSize: 1 * units.MiB, } - if err := c.Upgrade.ToBytesWithPacker(&p); err != nil { + bytes, err := c.Upgrade.MarshalBinary() + if err != nil { return nil, err } + p.PackBytes(bytes) + if p.Err != nil { + return nil, p.Err + } p.PackLong(c.QuorumNumerator) if p.Err != nil { @@ -235,7 +240,11 @@ func (c *Config) UnmarshalBinary(bytes []byte) error { p := wrappers.Packer{ Bytes: bytes, } - if err := c.Upgrade.FromBytesWithPacker(&p); err != nil { + upgrade := p.UnpackBytes() + if p.Err != nil { + return p.Err + } + if err := c.Upgrade.UnmarshalBinary(upgrade); err != nil { return err }