Skip to content

Commit

Permalink
Provide abi encoded exit event in hex format from `bridge_generateExi…
Browse files Browse the repository at this point in the history
…tProof` JSON RPC (#1929)

* Provide abi encoded exit event in hex format

* Simplify EventAbi.Encode function in bindings
  • Loading branch information
Stefan-Ethernal authored Sep 27, 2023
1 parent 19687bc commit e700006
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 72 deletions.
39 changes: 16 additions & 23 deletions command/bridge/exit/exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package exit

import (
"bytes"
"encoding/json"
"encoding/hex"
"errors"
"fmt"
"math/big"
Expand All @@ -16,7 +16,6 @@ import (
"github.com/0xPolygon/polygon-edge/command/bridge/common"
cmdHelper "github.com/0xPolygon/polygon-edge/command/helper"
"github.com/0xPolygon/polygon-edge/command/rootchain/helper"
"github.com/0xPolygon/polygon-edge/consensus/polybft"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
Expand Down Expand Up @@ -168,37 +167,31 @@ func run(cmd *cobra.Command, _ []string) {
}

// createExitTxn encodes parameters for exit function on root chain ExitHelper contract
func createExitTxn(sender ethgo.Address, proof types.Proof) (*ethgo.Transaction, *polybft.ExitEvent, error) {
exitEventMap, ok := proof.Metadata["ExitEvent"].(map[string]interface{})
func createExitTxn(sender ethgo.Address, proof types.Proof) (*ethgo.Transaction,
*contractsapi.L2StateSyncedEvent, error) {
leafIndex, ok := proof.Metadata["LeafIndex"].(float64)
if !ok {
return nil, nil, errors.New("could not get exit event from proof")
return nil, nil, errors.New("failed to convert proof leaf index")
}

raw, err := json.Marshal(exitEventMap)
if err != nil {
return nil, nil, fmt.Errorf("failed to marshal exit event map to JSON. Error: %w", err)
checkpointBlock, ok := proof.Metadata["CheckpointBlock"].(float64)
if !ok {
return nil, nil, errors.New("failed to convert proof checkpoint block")
}

var exitEvent *polybft.ExitEvent
if err = json.Unmarshal(raw, &exitEvent); err != nil {
return nil, nil, fmt.Errorf("failed to unmarshal exit event from JSON. Error: %w", err)
exitEventHex, ok := proof.Metadata["ExitEvent"].(string)
if !ok {
return nil, nil, errors.New("failed to convert exit event")
}

var exitEventAPI contractsapi.L2StateSyncedEvent

exitEventEncoded, err := exitEventAPI.Encode(exitEvent.L2StateSyncedEvent)
exitEventEncoded, err := hex.DecodeString(exitEventHex)
if err != nil {
return nil, nil, fmt.Errorf("failed to encode exit event: %w", err)
return nil, nil, fmt.Errorf("failed to decode hex-encoded exit event '%s': %w", exitEventHex, err)
}

leafIndex, ok := proof.Metadata["LeafIndex"].(float64)
if !ok {
return nil, nil, errors.New("failed to convert proof leaf index")
}

checkpointBlock, ok := proof.Metadata["CheckpointBlock"].(float64)
if !ok {
return nil, nil, errors.New("failed to convert proof checkpoint block")
exitEvent := new(contractsapi.L2StateSyncedEvent)
if err := exitEvent.Decode(exitEventEncoded); err != nil {
return nil, nil, fmt.Errorf("failed to decode exit event: %w", err)
}

exitFn := &contractsapi.ExitExitHelperFn{
Expand Down
15 changes: 7 additions & 8 deletions consensus/polybft/checkpoint_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,7 @@ func (c *checkpointManager) GenerateExitProof(exitID uint64) (types.Proof, error
return types.Proof{}, fmt.Errorf("checkpoint block not found for exit ID %d", exitID)
}

var exitEventAPI contractsapi.L2StateSyncedEvent

e, err := exitEventAPI.Encode(exitEvent.L2StateSyncedEvent)
exitEventEncoded, err := exitEvent.L2StateSyncedEvent.Encode()
if err != nil {
return types.Proof{}, err
}
Expand All @@ -422,23 +420,25 @@ func (c *checkpointManager) GenerateExitProof(exitID uint64) (types.Proof, error
return types.Proof{}, err
}

leafIndex, err := tree.LeafIndex(e)
leafIndex, err := tree.LeafIndex(exitEventEncoded)
if err != nil {
return types.Proof{}, err
}

proof, err := tree.GenerateProof(e)
proof, err := tree.GenerateProof(exitEventEncoded)
if err != nil {
return types.Proof{}, err
}

c.logger.Debug("Generated proof for exit", "exitID", exitID, "leafIndex", leafIndex, "proofLen", len(proof))

exitEventHex := hex.EncodeToString(exitEventEncoded)

return types.Proof{
Data: proof,
Metadata: map[string]interface{}{
"LeafIndex": leafIndex,
"ExitEvent": exitEvent,
"ExitEvent": exitEventHex,
"CheckpointBlock": checkpointBlock,
},
}, nil
Expand All @@ -449,9 +449,8 @@ func createExitTree(exitEvents []*ExitEvent) (*merkle.MerkleTree, error) {
numOfEvents := len(exitEvents)
data := make([][]byte, numOfEvents)

var exitEventAPI contractsapi.L2StateSyncedEvent
for i := 0; i < numOfEvents; i++ {
b, err := exitEventAPI.Encode(exitEvents[i].L2StateSyncedEvent)
b, err := exitEvents[i].L2StateSyncedEvent.Encode()
if err != nil {
return nil, err
}
Expand Down
4 changes: 1 addition & 3 deletions consensus/polybft/consensus_runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/0xPolygon/go-ibft/messages/proto"
"github.com/0xPolygon/polygon-edge/consensus"
"github.com/0xPolygon/polygon-edge/consensus/polybft/bitmap"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
bls "github.com/0xPolygon/polygon-edge/consensus/polybft/signer"
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
"github.com/0xPolygon/polygon-edge/consensus/polybft/wallet"
Expand Down Expand Up @@ -1078,9 +1077,8 @@ func encodeExitEvents(t *testing.T, exitEvents []*ExitEvent) [][]byte {

encodedEvents := make([][]byte, len(exitEvents))

var exitEventAPI contractsapi.L2StateSyncedEvent
for i, e := range exitEvents {
encodedEvent, err := exitEventAPI.Encode(e.L2StateSyncedEvent)
encodedEvent, err := e.L2StateSyncedEvent.Encode()
require.NoError(t, err)

encodedEvents[i] = encodedEvent
Expand Down
11 changes: 8 additions & 3 deletions consensus/polybft/contractsapi/bindings-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,8 @@ func (*{{.TName}}) Sig() ethgo.Hash {
return {{.ContractName}}.Abi.Events["{{.Name}}"].ID()
}
func (*{{.TName}}) Encode(inputs interface{}) ([]byte, error) {
return {{.ContractName}}.Abi.Events["{{.Name}}"].Inputs.Encode(inputs)
func ({{.Sig}} *{{.TName}}) Encode() ([]byte, error) {
return {{.ContractName}}.Abi.Events["{{.Name}}"].Inputs.Encode({{.Sig}})
}
func ({{.Sig}} *{{.TName}}) ParseLog(log *ethgo.Log) (bool, error) {
Expand All @@ -659,7 +659,12 @@ func ({{.Sig}} *{{.TName}}) ParseLog(log *ethgo.Log) (bool, error) {
}
return true, decodeEvent({{.ContractName}}.Abi.Events["{{.Name}}"], log, {{.Sig}})
}`
}
func ({{.Sig}} *{{.TName}}) Decode(input []byte) error {
return {{.ContractName}}.Abi.Events["{{.Name}}"].Inputs.DecodeStruct(input, &{{.Sig}})
}
`

inputs := map[string]interface{}{
"Structs": res,
Expand Down
Loading

0 comments on commit e700006

Please sign in to comment.