Skip to content

Commit

Permalink
Add endpoint for send voting tx data to contract
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaptoss committed Jul 30, 2024
1 parent 759b9ad commit 1c29efa
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 232 deletions.
23 changes: 23 additions & 0 deletions docs/spec/components/schemas/SendTx.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
allOf:
- $ref: '#/components/schemas/SendTxKey'
- type: object
x-go-is-request: true
required:
- attributes
properties:
attributes:
type: object
required:
- tx_data
- destination
properties:
tx_data:
type: string
pattern: "^0x[0-9a-fA-F]+$"
example: "0xdeadbeafdeadbeafdeadbeaf"
description: Serialized transaction data
destination:
type: string
pattern: "^0x[0-9a-fA-F]{40}"
example: "0xdead...beaf"
description: Address of the contract to which the transaction data should be sent
8 changes: 8 additions & 0 deletions docs/spec/components/schemas/SendTxKey.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type: object
required:
- type
properties:
type:
type: string
enum:
- send_transaction
13 changes: 0 additions & 13 deletions docs/spec/components/schemas/Tx.yaml

This file was deleted.

4 changes: 3 additions & 1 deletion docs/spec/components/schemas/TxKey.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ required:
properties:
id:
type: string
example: "0xdead...beaf"
description: Transaction hash
type:
type: string
enum:
- txs
- transaction
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
post:
tags:
- Registration
summary: Registration
operationId: register
- Voting
summary: Voting
operationId: vote
requestBody:
content:
application/json:
application/vnd.api+json:
schema:
type: object
required:
- data
properties:
data:
type: object
required:
- tx_data
properties:
tx_data:
type: string
$ref: '#/components/schemas/SendTx'
responses:
'200':
200:
description: Success
content:
application/json:
Expand All @@ -28,14 +23,14 @@ post:
properties:
data:
type: object
$ref: '#/components/schemas/Tx'
'400':
$ref: '#/components/schemas/TxKey'
400:
description: Bad Request Error
content:
application/json:
schema:
$ref: '#/components/schemas/Errors'
'500':
500:
description: Internal Error
content:
application/json:
Expand Down
43 changes: 8 additions & 35 deletions internal/config/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ import (
"context"
"crypto/ecdsa"
"math/big"
"slices"
"strings"
"sync"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
vaultapi "github.com/hashicorp/vault/api"
Expand All @@ -34,16 +31,11 @@ type ethereum struct {
getter kv.Getter
}

type whitelist []string

type RelayerConfig struct {
RPC *ethclient.Client
RegistrationAddress common.Address
LightweightStateAddress *common.Address
ChainID *big.Int
PrivateKey *ecdsa.PrivateKey
WhiteList whitelist
nonce uint64
RPC *ethclient.Client
ChainID *big.Int
PrivateKey *ecdsa.PrivateKey
nonce uint64

mut *sync.Mutex
}
Expand All @@ -53,13 +45,10 @@ func (e *ethereum) RelayerConfig() *RelayerConfig {
var result RelayerConfig

networkConfig := struct {
RPC *ethclient.Client `fig:"rpc,required"`
RegistrationAddress common.Address `fig:"registration,required"`
LightweightStateAddress *common.Address `fig:"lightweight_state"`
PrivateKey *ecdsa.PrivateKey `fig:"private_key"`
VaultAddress string `fig:"vault_address"`
VaultMountPath string `fig:"vault_mount_path"`
WhiteList []string `fig:"whitelist"`
RPC *ethclient.Client `fig:"rpc,required"`
PrivateKey *ecdsa.PrivateKey `fig:"private_key"`
VaultAddress string `fig:"vault_address"`
VaultMountPath string `fig:"vault_mount_path"`
}{}
err := figure.
Out(&networkConfig).
Expand All @@ -71,8 +60,6 @@ func (e *ethereum) RelayerConfig() *RelayerConfig {
}

result.RPC = networkConfig.RPC
result.RegistrationAddress = networkConfig.RegistrationAddress
result.LightweightStateAddress = networkConfig.LightweightStateAddress

result.ChainID, err = result.RPC.ChainID(context.Background())
if err != nil {
Expand All @@ -89,16 +76,6 @@ func (e *ethereum) RelayerConfig() *RelayerConfig {
panic(errors.Wrap(err, "failed to get nonce"))
}

result.WhiteList = make(whitelist, 0, len(networkConfig.WhiteList))
for _, address := range networkConfig.WhiteList {
address = strings.ToLower(address)
if result.WhiteList.IsPresent(address) {
continue
}

result.WhiteList = append(result.WhiteList, address)
}

result.mut = &sync.Mutex{}
return &result
}).(*RelayerConfig)
Expand Down Expand Up @@ -169,7 +146,3 @@ func extractPrivateKey(vaultAddress, vaultMountPath string) *ecdsa.PrivateKey {

return vaultRelayerConf.PrivateKey
}

func (w whitelist) IsPresent(address string) bool {
return slices.Contains(w, address)
}
48 changes: 0 additions & 48 deletions internal/service/handlers/transit_state.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,31 @@ type txData struct {
gas uint64
}

func Registration(w http.ResponseWriter, r *http.Request) {
req, err := requests.NewRegistrationRequest(r)
func Voting(w http.ResponseWriter, r *http.Request) {
req, err := requests.NewVotingRequest(r)
if err != nil {
Log(r).WithError(err).Error("failed to get request")
ape.RenderErr(w, problems.BadRequest(err)...)
return
}

var (
destination = req.Data.Attributes.Destination
calldata = req.Data.Attributes.TxData
)

log := Log(r).WithFields(logan.F{
"user-agent": r.Header.Get("User-Agent"),
"calldata": req.Data.TxData,
"user-agent": r.Header.Get("User-Agent"),
"calldata": calldata,
"destination": destination,
})
log.Debug("registration request")

// `RelayerConfig(r).RegistrationAddress` is default value for target contract
// if destination not specified this value will be used
// this value is required in config
registrationAddress := RelayerConfig(r).RegistrationAddress
if req.Data.Destination != nil {
if !RelayerConfig(r).WhiteList.IsPresent(*req.Data.Destination) {
ape.RenderErr(w, problems.BadRequest(validation.Errors{
"data/destination": fmt.Errorf("specified contract address not allowed"),
})...)
}
log.Debug("voting request")

// destination is valid hex address because of request validation
registrationAddress = common.HexToAddress(*req.Data.Destination)
}
// destination is valid hex address because of request validation
votingAddress := common.HexToAddress(destination)

var txd txData
txd.dataBytes, err = hexutil.Decode(req.Data.TxData)
txd.dataBytes, err = hexutil.Decode(calldata)
if err != nil {
Log(r).WithError(err).Error("failed to decode data")
ape.RenderErr(w, problems.BadRequest(err)...)
Expand All @@ -67,7 +61,7 @@ func Registration(w http.ResponseWriter, r *http.Request) {
RelayerConfig(r).LockNonce()
defer RelayerConfig(r).UnlockNonce()

err = confGas(r, &txd, &registrationAddress)
err = confGas(r, &txd, &votingAddress)
if err != nil {
Log(r).WithError(err).Error("failed to configure gas and gasPrice")
// `errors.Is` is not working for rpc errors, they passed as a string without additional wrapping
Expand All @@ -83,7 +77,7 @@ func Registration(w http.ResponseWriter, r *http.Request) {
return
}

tx, err := sendTx(r, &txd, &registrationAddress)
tx, err := sendTx(r, &txd, &votingAddress)
if err != nil {
Log(r).WithError(err).Error("failed to send tx")
ape.RenderErr(w, problems.InternalError())
Expand All @@ -92,22 +86,12 @@ func Registration(w http.ResponseWriter, r *http.Request) {

RelayerConfig(r).IncrementNonce()

ape.Render(w, newTxResponse(tx))
}

func newTxResponse(tx *types.Transaction) resources.TxResponse {
return resources.TxResponse{
Data: resources.Tx{

Key: resources.Key{
ID: tx.Hash().String(),
Type: resources.TXS,
},
Attributes: resources.TxAttributes{
TxHash: tx.Hash().String(),
},
ape.Render(w, resources.Relation{
Data: &resources.Key{
ID: tx.Hash().String(),
Type: resources.TRANSACTION,
},
}
})
}

func confGas(r *http.Request, txd *txData, receiver *common.Address) (err error) {
Expand Down
41 changes: 0 additions & 41 deletions internal/service/requests/registration.go

This file was deleted.

Loading

0 comments on commit 1c29efa

Please sign in to comment.