Commit 62f0c294 authored by luxq's avatar luxq

add persist config.

parent 3d9bcfd1
Pipeline #447 canceled with stages
...@@ -79,6 +79,7 @@ import ( ...@@ -79,6 +79,7 @@ import (
) )
type Bee struct { type Bee struct {
persistCfg *PersistConfig
p2pService io.Closer p2pService io.Closer
p2pHalter p2p.Halter p2pHalter p2p.Halter
p2pCancel context.CancelFunc p2pCancel context.CancelFunc
...@@ -188,6 +189,7 @@ func NewBee(addr string, publicKey *ecdsa.PublicKey, signer crypto.Signer, netwo ...@@ -188,6 +189,7 @@ func NewBee(addr string, publicKey *ecdsa.PublicKey, signer crypto.Signer, netwo
errorLogWriter: logger.WriterLevel(logrus.ErrorLevel), errorLogWriter: logger.WriterLevel(logrus.ErrorLevel),
tracerCloser: tracerCloser, tracerCloser: tracerCloser,
} }
b.initPersistConfig(o.DataDir)
stateStore, err := InitStateStore(logger, o.DataDir) stateStore, err := InitStateStore(logger, o.DataDir)
if err != nil { if err != nil {
...@@ -334,11 +336,17 @@ func NewBee(addr string, publicKey *ecdsa.PublicKey, signer crypto.Signer, netwo ...@@ -334,11 +336,17 @@ func NewBee(addr string, publicKey *ecdsa.PublicKey, signer crypto.Signer, netwo
return nil, fmt.Errorf("invalid transaction hash: %w", err) return nil, fmt.Errorf("invalid transaction hash: %w", err)
} }
if o.BlockHash == "" {
o.BlockHash = b.GetNextBlock(common.Bytes2Hex(txHash))
}
blockHash, err = GetTxNextBlock(p2pCtx, logger, swapBackend, transactionMonitor, pollingInterval, txHash, o.BlockHash) blockHash, err = GetTxNextBlock(p2pCtx, logger, swapBackend, transactionMonitor, pollingInterval, txHash, o.BlockHash)
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid block hash: %w", err) return nil, fmt.Errorf("invalid block hash: %w", err)
} }
b.UpdateTxNextBlock(common.Bytes2Hex(txHash), common.Bytes2Hex(blockHash))
swarmAddress, err := crypto.NewOverlayAddress(*pubKey, networkID, blockHash) swarmAddress, err := crypto.NewOverlayAddress(*pubKey, networkID, blockHash)
err = CheckOverlayWithStore(swarmAddress, stateStore) err = CheckOverlayWithStore(swarmAddress, stateStore)
......
package node
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
const (
persistFile = ".persist.json"
)
type PersistConfig struct {
ChequeBookTx string `json:"cheque_book_tx"`
TxNextBlockHash string `json:"tx_next_block_hash"`
Datadir string
}
func (c *Bee) initPersistConfig(datadir string) (err error) {
cfgfile := filepath.Join(datadir, persistFile)
var cfg PersistConfig
content, err := ioutil.ReadFile(cfgfile)
if err == nil {
json.Unmarshal(content, &cfg)
}
cfg.Datadir = datadir
c.persistCfg = &cfg
return nil
}
func (c *Bee) SavePersistConfig() (err error) {
basepath := c.persistCfg.Datadir
cfgfile := filepath.Join(basepath, persistFile)
bytes, _ := json.Marshal(c.persistCfg)
err = ioutil.WriteFile(cfgfile+".tmp", bytes, 0600)
if err != nil {
return err
}
err = os.Rename(cfgfile+".tmp", cfgfile)
if err != nil {
os.Remove(cfgfile + ".tmp")
return err
}
return nil
}
func (c *Bee) GetNextBlock(txhash string) string {
if c.persistCfg.TxNextBlockHash != "" && strings.Compare(txhash, c.persistCfg.ChequeBookTx) == 0 {
return c.persistCfg.TxNextBlockHash
}
return ""
}
func (c *Bee) UpdateTxNextBlock(txhash string, nextBlock string) {
c.persistCfg.ChequeBookTx = txhash
c.persistCfg.TxNextBlockHash = nextBlock
c.SavePersistConfig()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment