Skip to content

Commit

Permalink
Improve API usage
Browse files Browse the repository at this point in the history
  • Loading branch information
nytzuga committed Sep 14, 2023
1 parent a986442 commit 41d7776
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 38 deletions.
2 changes: 1 addition & 1 deletion plugin/evm/message/handshake/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {

errs := wrappers.Errs{}
errs.Add(
c.RegisterType(upgradeConfigMessage{}),
c.RegisterType(networkUpgradeConfigMessage{}),

Codec.RegisterCodec(Version, c),
)
Expand Down
65 changes: 34 additions & 31 deletions plugin/evm/message/handshake/upgrade_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,30 @@ type rawPrecompileUpgrade struct {
Bytes []byte `serialize:"true"`
}

type upgradeConfigMessage struct {
type networkUpgradeConfigMessage struct {
OptionalNetworkUpgrades []params.Fork `serialize:"true"`

// Config for modifying state as a network upgrade.
StateUpgrades []params.StateUpgrade `serialize:"true"`

// Config for enabling and disabling precompiles as network upgrades.
PrecompileUpgrades []rawPrecompileUpgrade `serialize:"true"`
config params.UpgradeConfig
bytes []byte
}

func ParseUpgradeConfig(bytes []byte) (*upgradeConfigMessage, error) {
var config upgradeConfigMessage
type UpgradeConfigMessage struct {
Bytes []byte
Hash []byte
}

// Attempts to parse a networkUpgradeConfigMessage from a []byte
//
// This function attempts to parse a stream of bytes as a
// networkUpgradeConfigMessage (as serialized from
// UpgradeConfigToNetworkMessage).
//
// The function returns a reference of *params.UpgradeConfig
func ParseUpgradeConfigMessage(bytes []byte) (*params.UpgradeConfig, error) {
var config networkUpgradeConfigMessage
version, err := Codec.Unmarshal(bytes, &config)
if err != nil {
return nil, err
Expand All @@ -37,14 +47,12 @@ func ParseUpgradeConfig(bytes []byte) (*upgradeConfigMessage, error) {
}

var PrecompileUpgrades []params.PrecompileUpgrade

for _, precompileUpgrade := range config.PrecompileUpgrades {
module, ok := modules.GetPrecompileModule(precompileUpgrade.Key)
if !ok {
return nil, ErrUnknowPrecompile
}
preCompile := module.MakeConfig()

version, err := Codec.Unmarshal(precompileUpgrade.Bytes, preCompile)
if version != Version {
return nil, ErrInvalidVersion
Expand All @@ -55,17 +63,23 @@ func ParseUpgradeConfig(bytes []byte) (*upgradeConfigMessage, error) {
PrecompileUpgrades = append(PrecompileUpgrades, params.PrecompileUpgrade{Config: preCompile})
}

config.config = params.UpgradeConfig{
return &params.UpgradeConfig{
OptionalNetworkUpgrades: &params.OptionalNetworkUpgrades{Updates: config.OptionalNetworkUpgrades},
StateUpgrades: config.StateUpgrades,
PrecompileUpgrades: PrecompileUpgrades,
}
config.bytes = bytes

return &config, nil
}, nil
}

func NewUpgradeConfig(config params.UpgradeConfig) (*upgradeConfigMessage, error) {
// Wraps an instance of *params.UpgradeConfig
//
// This function returns the serialized UpgradeConfig, ready to be send over to
// other peers. The struct also includes a hash of the content, ready to be used
// as part of the handshake protocol.
//
// Since params.UpgradeConfig should never change without a node reloading, it
// is safe to call this function once and store its output globally to re-use
// multiple times
func UpgradeConfigToNetworkMessage(config *params.UpgradeConfig) (*UpgradeConfigMessage, error) {
PrecompileUpgrades := make([]rawPrecompileUpgrade, 0)
for _, precompileConfig := range config.PrecompileUpgrades {
bytes, err := Codec.Marshal(Version, precompileConfig.Config)
Expand All @@ -83,33 +97,22 @@ func NewUpgradeConfig(config params.UpgradeConfig) (*upgradeConfigMessage, error
optionalNetworkUpgrades = config.OptionalNetworkUpgrades.Updates
}

wrappedConfig := upgradeConfigMessage{
wrappedConfig := networkUpgradeConfigMessage{
OptionalNetworkUpgrades: optionalNetworkUpgrades,
StateUpgrades: config.StateUpgrades,
PrecompileUpgrades: PrecompileUpgrades,
config: config,
bytes: make([]byte, 0),
}
bytes, err := Codec.Marshal(Version, wrappedConfig)
if err != nil {
return nil, err
}
wrappedConfig.bytes = bytes

return &wrappedConfig, nil
}

func (r *upgradeConfigMessage) Config() params.UpgradeConfig {
return r.config
}

func (r *upgradeConfigMessage) Bytes() []byte {
return r.bytes
}

func (r *upgradeConfigMessage) Hash() [8]byte {
hash := crypto.Keccak256(r.bytes)
hash := crypto.Keccak256(bytes)
var firstBytes [8]byte
copy(firstBytes[:], hash[:8])
return firstBytes

return &UpgradeConfigMessage{
Bytes: bytes,
Hash: hash,
}, nil
}
14 changes: 8 additions & 6 deletions plugin/evm/message/handshake/upgrade_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func TestSerialize(t *testing.T) {
var t0 uint64 = 0
var t1 uint64 = 1
config, err := NewUpgradeConfig(params.UpgradeConfig{
message, err := UpgradeConfigToNetworkMessage(&params.UpgradeConfig{
PrecompileUpgrades: []params.PrecompileUpgrade{
{
Config: nativeminter.NewConfig(&t0, nil, nil, nil, nil), // enable at genesis
Expand All @@ -26,13 +26,15 @@ func TestSerialize(t *testing.T) {
})
require.NoError(t, err)

config2, err := ParseUpgradeConfig(config.Bytes())
config, err := ParseUpgradeConfigMessage(message.Bytes)
require.NoError(t, err)

config3, err := NewUpgradeConfig(config2.Config())
message2, err := UpgradeConfigToNetworkMessage(config)
require.NoError(t, err)

require.Equal(t, config2, config3)
require.Equal(t, config.Hash(), config2.Hash())
require.Equal(t, config.Hash(), config3.Hash())
config3, err := ParseUpgradeConfigMessage(message2.Bytes)
require.NoError(t, err)

require.Equal(t, config, config3)
require.Equal(t, message.Hash, message2.Hash)
}

0 comments on commit 41d7776

Please sign in to comment.