Skip to content

Commit

Permalink
Remove all custom serializers
Browse files Browse the repository at this point in the history
Use MarshalBinary and UnmarshalBinary
  • Loading branch information
nytzuga committed Dec 15, 2023
1 parent 2efae3d commit 4591ccd
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 59 deletions.
21 changes: 15 additions & 6 deletions commontype/fee_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
32 changes: 20 additions & 12 deletions precompile/allowlist/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
30 changes: 26 additions & 4 deletions precompile/contracts/deployerallowlist/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,47 @@ 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
}

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
Expand Down
40 changes: 33 additions & 7 deletions precompile/contracts/feemanager/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 100 in precompile/contracts/feemanager/config.go

View workflow job for this annotation

GitHub Actions / Lint

unnecessary trailing newline (whitespace)
if p.Err != nil {
return nil, p.Err
}
p.PackBytes(upgrade)
if p.Err != nil {
return nil, p.Err
}

if c.InitialFeeConfig == nil {
Expand All @@ -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
}
}
Expand Down
25 changes: 21 additions & 4 deletions precompile/contracts/nativeminter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
58 changes: 44 additions & 14 deletions precompile/contracts/rewardmanager/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -145,46 +152,69 @@ 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 {
return nil, p.Err
}

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
}
Loading

0 comments on commit 4591ccd

Please sign in to comment.