Skip to content

Commit a482a16

Browse files
committed
add restapi to update gasPrice and gasLimit for nodes
1 parent 67cc676 commit a482a16

6 files changed

Lines changed: 88 additions & 12 deletions

File tree

config.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
2-
"VERSION": "v1.0-beta.23",
2+
"VERSION": "v1.0.0-m",
33
"NodeIP": "#REPLACE-WITH-IP",
44
"NodePort": "9501",
55
"ChainType": "ETH",
6-
"DOSAddressBridgeAddress": "0x72840b59CeE8f0ce40f33219DDf9Ca73B6B29ab2",
6+
"DOSAddressBridgeAddress": "0x98A0E7026778840Aacd28B9c03137D32e06F5ff1",
77
"EthGasLimit": "1500000",
88
"BootStrapIPs": [
99
],
1010
"ChainNodePool": [
11-
"wss://rinkeby.infura.io/ws/v3/#REPLACE-WITH-INFURA-APIKEY"
11+
"wss://mainnet.infura.io/ws/v3/#REPLACE-WITH-INFURA-APIKEY"
1212
]
1313
}

dosnode/dos_restapis.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ func (d *DosNode) startRESTServer() (err error) {
1616
mux := http.NewServeMux()
1717
mux.HandleFunc("/", d.status)
1818
mux.HandleFunc("/balance", d.balance)
19+
mux.HandleFunc("/setGasPrice", d.setGasPrice)
20+
mux.HandleFunc("/setGasLimit", d.setGasLimit)
1921
mux.HandleFunc("/enableAdmin", d.enableAdmin)
2022
mux.HandleFunc("/disableAdmin", d.disableAdmin)
2123
mux.HandleFunc("/enableGuardian", d.enableGuardian)
@@ -51,13 +53,13 @@ func (d *DosNode) status(w http.ResponseWriter, r *http.Request) {
5153
html = html + "TotalQuery : " + strconv.Itoa(d.totalQuery) + "\n|"
5254
html = html + "FulfilledQuery : " + strconv.Itoa(d.fulfilledQuery) + "\n|"
5355
html = html + "Group Number : " + strconv.Itoa(d.numOfworkingGroup) + "\n|"
54-
html = html + "=================================================" + "\n|"
55-
// result := d.dkg.GetGroupNumber()
56+
html = html + "gasPrice : " + strconv.FormatUint(d.chain.GetGasPrice(), 10) + "\n|"
57+
html = html + "gasLimit : " + strconv.FormatUint(d.chain.GetGasLimit(), 10) + "\n|"
5658
balance, err := d.chain.Balance()
5759
if err != nil {
58-
html = html + "Balance : " + err.Error() + "\n|"
60+
html = html + "Balance : " + err.Error() + "\n|"
5961
} else {
60-
html = html + "Balance : " + balance.String() + "\n|"
62+
html = html + "Balance : " + balance.String() + "\n|"
6163
}
6264
workingGroupNum, err := d.chain.GetWorkingGroupSize()
6365
if err != nil {
@@ -83,7 +85,6 @@ func (d *DosNode) status(w http.ResponseWriter, r *http.Request) {
8385
} else {
8486
html = html + "PendingNodeSize : " + strconv.FormatUint(pendingNodeNum, 10) + "\n|"
8587
}
86-
8788
curBlk, err := d.chain.CurrentBlock()
8889
if err != nil {
8990
html = html + "CurrentBlock : " + err.Error() + "\n"
@@ -105,6 +106,39 @@ func (d *DosNode) balance(w http.ResponseWriter, r *http.Request) {
105106
w.Write([]byte(html))
106107
}
107108

109+
func (d *DosNode) setGasLimit(w http.ResponseWriter, r *http.Request) {
110+
gasLimits, ok := r.URL.Query()["gasLimit"]
111+
if !ok || len(gasLimits) == 0 {
112+
return
113+
}
114+
g, ok := new(big.Int).SetString(gasLimits[0], 10)
115+
if !ok {
116+
d.logger.Error(fmt.Errorf("GasLimit SetString error"))
117+
return
118+
}
119+
if g.Cmp(big.NewInt(0)) == 0 {
120+
d.logger.Error(fmt.Errorf("GasLimit cannot be set to 0"))
121+
return
122+
}
123+
d.logger.Info(fmt.Sprintf("Set GasLimit to %v", g))
124+
d.chain.SetGasLimit(g)
125+
}
126+
127+
func (d *DosNode) setGasPrice(w http.ResponseWriter, r *http.Request) {
128+
gasPrices, ok := r.URL.Query()["gasPrice"]
129+
if !ok || len(gasPrices) == 0 {
130+
return
131+
}
132+
g, ok := new(big.Int).SetString(gasPrices[0], 10)
133+
if !ok {
134+
d.logger.Error(fmt.Errorf("GasPrice SetString error"))
135+
return
136+
}
137+
// Set to 0 means using estimate gas price by default
138+
d.logger.Info(fmt.Sprintf("Set GasPrice to %v wei", g))
139+
d.chain.SetGasPrice(g)
140+
}
141+
108142
func (d *DosNode) enableAdmin(w http.ResponseWriter, r *http.Request) {
109143
d.logger.Info("isAdmin")
110144
d.isAdmin = true

onchain/chain_adaptor.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ type ProxyAdapter interface {
4040
StartCommitReveal(startBlock int64, commitDuration int64, revealDuration int64, revealThreshold int64) (err error)
4141
Commit(cid *big.Int, commitment [32]byte) (err error)
4242
Reveal(cid *big.Int, secret *big.Int) (err error)
43+
SetGasPrice(gasPrice *big.Int)
44+
SetGasLimit(gasLimit *big.Int)
4345
//Get functions
46+
GetGasPrice() (result uint64)
47+
GetGasLimit() (result uint64)
4448
GroupToPick() (result uint64, err error)
4549
PendingNonce() (result uint64, err error)
4650
GetExpiredWorkingGroupSize() (r uint64, err error)

onchain/eth_get.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,25 @@ func (e *ethAdaptor) BootstrapStartThreshold() (result uint64, err error) {
565565
}
566566
return
567567
}
568+
569+
func (e *ethAdaptor) GetGasPrice() (result uint64) {
570+
if len(e.proxies) != 0 {
571+
if e.proxies[0].TransactOpts.GasPrice == nil {
572+
result = 0
573+
} else {
574+
result = e.proxies[0].TransactOpts.GasPrice.Uint64()
575+
}
576+
}
577+
return
578+
}
579+
580+
func (e *ethAdaptor) GetGasLimit() (result uint64) {
581+
if len(e.proxies) != 0 {
582+
result = e.proxies[0].TransactOpts.GasLimit
583+
}
584+
return
585+
}
586+
568587
func (e *ethAdaptor) GroupPubKey(gIdx int) (result [4]*big.Int, err error) {
569588
if !e.isConnecting() {
570589
err = errors.New("not connecting to geth")

onchain/eth_proxy.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ type ethAdaptor struct {
4444
httpUrls []string
4545
wsUrls []string
4646
key *keystore.Key
47-
gasPrice int64
48-
gasLimit int64
47+
gasPrice uint64
48+
gasLimit uint64
4949
connTimeout time.Duration
5050
getTimeout time.Duration
5151
setTimeout time.Duration
@@ -76,7 +76,7 @@ func NewEthAdaptor(key *keystore.Key, config *configuration.Config, l logger) (a
7676
adaptor.key = key
7777
adaptor.logger = l
7878
adaptor.reqQueue = make(chan *request)
79-
adaptor.gasLimit = int64(gasLimitInt)
79+
adaptor.gasLimit = uint64(gasLimitInt)
8080
adaptor.connTimeout = 60 * time.Second
8181
adaptor.getTimeout = 60 * time.Second
8282
adaptor.setTimeout = 60 * time.Second
@@ -204,7 +204,7 @@ func (e *ethAdaptor) Connect(urls []string, t time.Time) (err error) {
204204
ctx, cancel := context.WithCancel(e.ctx)
205205
ctx = context.WithValue(ctx, "index", len(e.ctxes))
206206
auth := bind.NewKeyedTransactor(e.key.PrivateKey)
207-
auth.GasLimit = uint64(e.gasLimit)
207+
auth.GasLimit = e.gasLimit
208208
auth.Context = ctx
209209

210210
if bootStrapUrl != "" {

onchain/eth_set.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,25 @@ func (e *ethAdaptor) SignalGroupDissolve() (err error) {
355355
return
356356
}
357357

358+
func (e *ethAdaptor) SetGasLimit(gasLimit *big.Int) {
359+
if len(e.proxies) > 0 && len(e.crs) > 0 {
360+
e.proxies[0].TransactOpts.GasLimit = gasLimit.Uint64()
361+
e.crs[0].TransactOpts.GasLimit = gasLimit.Uint64()
362+
}
363+
}
364+
365+
func (e *ethAdaptor) SetGasPrice(gasPrice *big.Int) {
366+
if len(e.proxies) > 0 && len(e.crs) > 0 {
367+
if gasPrice.Cmp(big.NewInt(0)) == 0 {
368+
e.proxies[0].TransactOpts.GasPrice = nil
369+
e.crs[0].TransactOpts.GasPrice = nil
370+
} else {
371+
e.proxies[0].TransactOpts.GasPrice = gasPrice
372+
e.crs[0].TransactOpts.GasPrice = gasPrice
373+
}
374+
}
375+
}
376+
358377
func (e *ethAdaptor) SignalBootstrap(cid *big.Int) (err error) {
359378
if !e.isConnecting() {
360379
err = errors.New("not connecting to geth")

0 commit comments

Comments
 (0)