Skip to content

Commit

Permalink
Merge pull request #4 from rarimo/fix/detached-commit
Browse files Browse the repository at this point in the history
Fix: lost commit
  • Loading branch information
artemskriabin authored May 7, 2024
2 parents d7b1a04 + ad47bc0 commit e94cae3
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
.vscode/
.idea/
config.*.yaml
Expand Down
9 changes: 5 additions & 4 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
network:
rpc: ""
proposer: ""
voting_registry: ""
lightweight_state: ""
rpc: "https://rpc.evm.node1.mainnet-beta.rarimo.com/"
proposer: "0xcB6Cd78e7527F4Ec032598E3aEe4dbd597fEFC97"
voting_registry: "0xcB6Cd78e7527F4Ec032598E3aEe4dbd597fEFC97"
lightweight_state: "0xcB6Cd78e7527F4Ec032598E3aEe4dbd597fEFC97"
vault_address: "http://127.0.0.1:8200"
vault_mount_path: "secret_data"
gas_multiplier: 1.23

log:
level: debug
Expand Down
2 changes: 2 additions & 0 deletions internal/config/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type NetworkConfig struct {
LightweightState common.Address `fig:"lightweight_state,required"`
Address string `fig:"vault_address,required"`
MountPath string `fig:"vault_mount_path,required"`
GasMultiplier float64 `fig:"gas_multiplier,required"`

ChainID *big.Int `fig:"chain_id"`
Token string `dig:"VAULT_TOKEN,clear"`
Expand Down Expand Up @@ -82,6 +83,7 @@ func (e *ethereum) NetworkConfig() *NetworkConfig {
"lightweight_state": result.LightweightState,
"vault_address": result.Address,
"vault_mount_path": result.MountPath,
"gas_multiplier": result.GasMultiplier,
}).Now(); err != nil {
panic(err)
}
Expand Down
35 changes: 35 additions & 0 deletions internal/service/api/handlers/multiplier_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package handlers

import (
"github.com/magiconair/properties/assert"
"math/big"
"testing"
)

var (
gasMultiplier = 1.3
gasPriceZero = big.NewInt(0)
gasPriceAverage = big.NewInt(7709)
gasPriceLow = big.NewInt(150)
gasPriceNegative = big.NewInt(-5543)
)

func TestZeroGasMultiplier(t *testing.T) {
multiplied := multiplyGasPrice(gasPriceZero, gasMultiplier)
assert.Equal(t, multiplied, big.NewInt(0))
}

func TestAverageGasMultiplier(t *testing.T) {
multiplied := multiplyGasPrice(gasPriceAverage, gasMultiplier)
assert.Equal(t, multiplied, big.NewInt(10021))
}

func TestLowGasMultiplier(t *testing.T) {
multiplied := multiplyGasPrice(gasPriceLow, gasMultiplier)
assert.Equal(t, multiplied, big.NewInt(195))
}

func TestNegativeGasMultiplier(t *testing.T) {
multiplied := multiplyGasPrice(gasPriceNegative, gasMultiplier)
assert.Equal(t, multiplied, big.NewInt(-7206))
}
1 change: 1 addition & 0 deletions internal/service/api/handlers/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func Register(w http.ResponseWriter, r *http.Request) {
ape.RenderErr(w, problems.InternalError())
return
}
gasPrice = multiplyGasPrice(gasPrice, NetworkConfig(r).GasMultiplier)

NetworkConfig(r).LockNonce()
defer NetworkConfig(r).UnlockNonce()
Expand Down
1 change: 1 addition & 0 deletions internal/service/api/handlers/transit_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TransitState(w http.ResponseWriter, r *http.Request) {
ape.RenderErr(w, problems.InternalError())
return
}
gasPrice = multiplyGasPrice(gasPrice, NetworkConfig(r).GasMultiplier)

NetworkConfig(r).LockNonce()
defer NetworkConfig(r).UnlockNonce()
Expand Down
11 changes: 11 additions & 0 deletions internal/service/api/handlers/vote.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"math/big"
"net/http"
"strings"

Expand Down Expand Up @@ -54,6 +55,7 @@ func Vote(w http.ResponseWriter, r *http.Request) {
ape.RenderErr(w, problems.InternalError())
return
}
gasPrice = multiplyGasPrice(gasPrice, NetworkConfig(r).GasMultiplier)

NetworkConfig(r).LockNonce()
defer NetworkConfig(r).UnlockNonce()
Expand Down Expand Up @@ -136,3 +138,12 @@ func Vote(w http.ResponseWriter, r *http.Request) {
},
})
}

// ONE - One GWEI
var ONE = 1000000000

func multiplyGasPrice(gasPrice *big.Int, multiplier float64) *big.Int {
mult := big.NewFloat(0).Mul(big.NewFloat(multiplier), big.NewFloat(float64(ONE)))
gas, _ := big.NewFloat(0).Mul(big.NewFloat(0).SetInt(gasPrice), mult).Int(nil)
return big.NewInt(0).Div(gas, big.NewInt(int64(ONE)))
}

0 comments on commit e94cae3

Please sign in to comment.