Skip to content
New issue

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

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

Already on GitHub? # to your account

feat: calculate correct gas #14

Merged
merged 26 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions chains/evm/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type EVMConfig struct {
StartBlock uint64 `split_words:"true" required:"true"`
FreshStart bool `default:"false" split_words:"true"`
Latest bool `default:"false" split_words:"true"`
GenericResources []string `default:"0000000000000000000000000000000000000000000000000000000000000005" split_words:"true"`
}

// LoadEVMConfig loads EVM config from the environment and validates the fields
Expand Down
3 changes: 3 additions & 0 deletions chains/evm/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (s *EVMConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad_DefaultValues() {
Latest: false,
FreshStart: false,
StartBlock: 120,
GenericResources: []string{"0000000000000000000000000000000000000000000000000000000000000005"},
})
}

Expand All @@ -91,6 +92,7 @@ func (s *EVMConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad() {
os.Setenv("INCLUSION_PROVER_DOMAINS_1_START_BLOCK", "120")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_FRESH_START", "true")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_LATEST", "true")
os.Setenv("INCLUSION_PROVER_DOMAINS_1_GENERIC_RESOURCES", "1,2")

c, err := config.LoadEVMConfig(1)

Expand All @@ -114,5 +116,6 @@ func (s *EVMConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad() {
Latest: true,
FreshStart: true,
StartBlock: 120,
GenericResources: []string{"1", "2"},
})
}
18 changes: 15 additions & 3 deletions chains/evm/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/sygmaprotocol/sygma-inclusion-prover/chains/evm/util"
)

const TRANSFER_GAS_COST = 200000
const TRANSFER_GAS_COST = 600000

type Batch struct {
proposals []contracts.ExecutorProposal
Expand Down Expand Up @@ -60,7 +60,9 @@ func (e *EVMExecutor) transfer(props []*proposal.Proposal) error {
batchData := props[0].Data.(message.TransferData)
proofBytes, _ := util.ToByteArray(batchData.AccountProof)
for _, batch := range batches {
hash, err := e.contract.ExecuteProposals(batch.proposals, proofBytes, batchData.Slot, transactor.TransactOptions{})
hash, err := e.contract.ExecuteProposals(batch.proposals, proofBytes, batchData.Slot, transactor.TransactOptions{
GasLimit: batch.gasLimit,
})
if err != nil {
log.Err(err).Msgf("Failed executing proposals")
continue
Expand Down Expand Up @@ -89,7 +91,7 @@ func (e *EVMExecutor) proposalBatches(props []*proposal.Proposal) ([]*Batch, err
continue
}

propGasLimit := uint64(TRANSFER_GAS_COST)
propGasLimit := e.proposalGas(prop)
currentBatch.gasLimit += propGasLimit
if currentBatch.gasLimit >= e.transactionMaxGas {
currentBatch = &Batch{
Expand All @@ -113,3 +115,13 @@ func (e *EVMExecutor) proposalBatches(props []*proposal.Proposal) ([]*Batch, err

return batches, nil
}

func (e *EVMExecutor) proposalGas(prop *proposal.Proposal) uint64 {
transferData := prop.Data.(message.TransferData)
if transferData.Type != message.GenericTransfer {
return TRANSFER_GAS_COST
}

genericFee := new(big.Int).SetBytes(transferData.Deposit.Data[:32])
return uint64(TRANSFER_GAS_COST) + genericFee.Uint64()
}
46 changes: 29 additions & 17 deletions chains/evm/message/stateRoot.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/sygmaprotocol/sygma-inclusion-prover/chains/evm/abi"
"github.com/sygmaprotocol/sygma-inclusion-prover/chains/evm/listener/events"
"github.com/sygmaprotocol/sygma-inclusion-prover/chains/evm/util"
"golang.org/x/exp/slices"
)

const (
Expand Down Expand Up @@ -67,15 +68,15 @@ type Client interface {
}

type StateRootHandler struct {
blockFetcher BlockFetcher
blockStorer BlockStorer
client Client
routerAddress common.Address
routerABI ethereumABI.ABI
domainID uint8
slotIndex uint8

msgChan chan []*message.Message
blockFetcher BlockFetcher
blockStorer BlockStorer
client Client
routerAddress common.Address
routerABI ethereumABI.ABI
domainID uint8
slotIndex uint8
genericResources []string
msgChan chan []*message.Message
}

func NewStateRootHandler(
Expand All @@ -86,17 +87,19 @@ func NewStateRootHandler(
msgChan chan []*message.Message,
domainID uint8,
slotIndex uint8,
genericResources []string,
) *StateRootHandler {
routerABI, _ := ethereumABI.JSON(strings.NewReader(abi.RouterABI))
return &StateRootHandler{
blockFetcher: blockFetcher,
blockStorer: blockStorer,
client: client,
routerAddress: routerAddress,
routerABI: routerABI,
domainID: domainID,
slotIndex: slotIndex,
msgChan: msgChan,
blockFetcher: blockFetcher,
blockStorer: blockStorer,
client: client,
routerAddress: routerAddress,
routerABI: routerABI,
domainID: domainID,
slotIndex: slotIndex,
msgChan: msgChan,
genericResources: genericResources,
}
}

Expand Down Expand Up @@ -139,6 +142,7 @@ func (h *StateRootHandler) HandleMessage(m *message.Message) (*proposal.Proposal
Slot: stateRoot.Slot,
AccountProof: accountProof,
StorageProof: storageProof,
Type: h.transferType(d),
}))
}

Expand Down Expand Up @@ -201,6 +205,14 @@ func (h *StateRootHandler) proof(
return resp.AccountProof, resp.StorageProof[0].Proof, nil
}

func (h *StateRootHandler) transferType(d *events.Deposit) TransferType {
if slices.Contains(h.genericResources, hex.EncodeToString(d.ResourceID[:])) {
return GenericTransfer
} else {
return FungibleTransfer
}
}

// slotKey mimics slot key calculation from solidity
// https://github.com/sygmaprotocol/sygma-x-solidity/blob/bd43d1138b38328267f2bfdb65a37817f24e3286/src/contracts/Executor.sol#L235
func (h *StateRootHandler) slotKey(d *events.Deposit) string {
Expand Down
1 change: 1 addition & 0 deletions chains/evm/message/stateRoot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (s *StateRootHandlerTestSuite) SetupTest() {
s.msgChan,
s.sourceDomain,
s.slotIndex,
[]string{"0x0000000000000000000000000000000000000000000000000000000000000500"},
)
}

Expand Down
6 changes: 6 additions & 0 deletions chains/evm/message/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@ import (
"github.com/sygmaprotocol/sygma-inclusion-prover/chains/evm/listener/events"
)

type TransferType string

const (
EVMTransferMessage message.MessageType = "EVMTransferMessage"
EVMTransferProposal proposal.ProposalType = "EVMTransferProposal"

GenericTransfer TransferType = "genericTransfer"
FungibleTransfer TransferType = "fungibleTransfer"
)

type TransferData struct {
Deposit *events.Deposit
Slot *big.Int
AccountProof []string
StorageProof []string
Type TransferType
}

func NewEVMTransferMessage(source uint8, destination uint8, transfer TransferData) *message.Message {
Expand Down
5 changes: 5 additions & 0 deletions e2e/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,10 @@ func (s *EVME2ETestSuite) Test_SuccessfulExecutions() {
s.Nil(err)
s.Equal(e.DepositNonce, uint64(index+1))
s.Equal(e.OriginDomainID, uint8(1))

tx, _, err := s.client.TransactionByHash(context.Background(), execution.TxHash)
s.Nil(err)

s.Equal(tx.Gas(), uint64(2100000))
}
}
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,11 @@ func main() {
time.Duration(config.BlockRetryInterval)*time.Second,
big.NewInt(config.BlockConfirmations),
big.NewInt(config.BlockInterval))

messageHandler := message.NewMessageHandler()
messageHandler.RegisterMessageHandler(evmMessage.EVMStateRootMessage, evmMessage.NewStateRootHandler(beaconProvider, latestBlockStore, client, routerAddress, msgChan, id, config.SlotIndex))
messageHandler.RegisterMessageHandler(
evmMessage.EVMStateRootMessage,
evmMessage.NewStateRootHandler(beaconProvider, latestBlockStore, client, routerAddress, msgChan, id, config.SlotIndex, config.GenericResources))
messageHandler.RegisterMessageHandler(evmMessage.EVMTransferMessage, &evmMessage.TransferHandler{})

startBlock, err := blockStore.GetStartBlock(
Expand Down
Loading