Merge pull request #418 from XinFinOrg/dev-upgrade

Dev upgrade Merge from Auditing Change
This commit is contained in:
Liam 2024-02-05 20:29:39 +08:00 committed by GitHub
commit d31552b0e2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
161 changed files with 1333 additions and 788 deletions

1
.gitignore vendored
View file

@ -53,3 +53,4 @@ go.sum
cicd/devnet/terraform/.terraform* cicd/devnet/terraform/.terraform*
cicd/devnet/tmp cicd/devnet/tmp
.env .env
cicd/devnet/terraform/node-config.json

View file

@ -171,6 +171,7 @@ jobs:
echo "Force deploy xdc-$i" echo "Force deploy xdc-$i"
aws ecs update-service --region ap-southeast-2 --cluster devnet-xdcnode-cluster --service ecs-service-xdc$i --force-new-deployment --no-cli-pager; aws ecs update-service --region ap-southeast-2 --cluster devnet-xdcnode-cluster --service ecs-service-xdc$i --force-new-deployment --no-cli-pager;
done done
aws ecs update-service --region ap-southeast-1 --cluster devnet-xdcnode-cluster --service ecs-service-rpc1 --force-new-deployment --no-cli-pager;
- stage: (Devnet) Send Deployment Notification - stage: (Devnet) Send Deployment Notification
if: branch = dev-upgrade AND type = push AND tag IS blank if: branch = dev-upgrade AND type = push AND tag IS blank

View file

@ -95,8 +95,14 @@ func NewMongoDBEngine(cfg *Config) *XDCxDAO.MongoDatabase {
} }
func New(cfg *Config) *XDCX { func New(cfg *Config) *XDCX {
tokenDecimalCache, _ := lru.New(defaultCacheLimit) tokenDecimalCache, err := lru.New(defaultCacheLimit)
orderCache, _ := lru.New(tradingstate.OrderCacheLimit) if err != nil {
log.Warn("[XDCx-New] fail to create new lru for token decimal", "error", err)
}
orderCache, err := lru.New(tradingstate.OrderCacheLimit)
if err != nil {
log.Warn("[XDCx-New] fail to create new lru for order", "error", err)
}
XDCX := &XDCX{ XDCX := &XDCX{
orderNonce: make(map[common.Address]*big.Int), orderNonce: make(map[common.Address]*big.Int),
Triegc: prque.New(), Triegc: prque.New(),
@ -121,7 +127,10 @@ func New(cfg *Config) *XDCX {
// Overflow returns an indication if the message queue is full. // Overflow returns an indication if the message queue is full.
func (XDCx *XDCX) Overflow() bool { func (XDCx *XDCX) Overflow() bool {
val, _ := XDCx.settings.Load(overflowIdx) val, ok := XDCx.settings.Load(overflowIdx)
if !ok {
log.Warn("[XDCx-Overflow] fail to load overflow index")
}
return val.(bool) return val.(bool)
} }
@ -198,20 +207,12 @@ func (XDCx *XDCX) ProcessOrderPending(header *types.Header, coinbase common.Addr
S: common.BigToHash(S), S: common.BigToHash(S),
}, },
} }
cancel := false
if order.Status == tradingstate.OrderStatusCancelled {
cancel = true
}
log.Info("Process order pending", "orderPending", order, "BaseToken", order.BaseToken.Hex(), "QuoteToken", order.QuoteToken) log.Info("Process order pending", "orderPending", order, "BaseToken", order.BaseToken.Hex(), "QuoteToken", order.QuoteToken)
originalOrder := &tradingstate.OrderItem{} originalOrder := &tradingstate.OrderItem{}
*originalOrder = *order *originalOrder = *order
originalOrder.Quantity = tradingstate.CloneBigInt(order.Quantity) originalOrder.Quantity = tradingstate.CloneBigInt(order.Quantity)
if cancel {
order.Status = tradingstate.OrderStatusCancelled
}
newTrades, newRejectedOrders, err := XDCx.CommitOrder(header, coinbase, chain, statedb, XDCXstatedb, tradingstate.GetTradingOrderBookHash(order.BaseToken, order.QuoteToken), order) newTrades, newRejectedOrders, err := XDCx.CommitOrder(header, coinbase, chain, statedb, XDCXstatedb, tradingstate.GetTradingOrderBookHash(order.BaseToken, order.QuoteToken), order)
for _, reject := range newRejectedOrders { for _, reject := range newRejectedOrders {
@ -579,17 +580,16 @@ func (XDCX *XDCX) GetEmptyTradingState() (*tradingstate.TradingStateDB, error) {
func (XDCx *XDCX) GetStateCache() tradingstate.Database { func (XDCx *XDCX) GetStateCache() tradingstate.Database {
return XDCx.StateCache return XDCx.StateCache
} }
func (XDCx *XDCX) HasTradingState(block *types.Block, author common.Address) bool { func (XDCx *XDCX) HasTradingState(block *types.Block, author common.Address) bool {
root, err := XDCx.GetTradingStateRoot(block, author) root, err := XDCx.GetTradingStateRoot(block, author)
if err != nil { if err != nil {
return false return false
} }
_, err = XDCx.StateCache.OpenTrie(root) _, err = XDCx.StateCache.OpenTrie(root)
if err != nil { return err == nil
return false
}
return true
} }
func (XDCx *XDCX) GetTriegc() *prque.Prque { func (XDCx *XDCX) GetTriegc() *prque.Prque {
return XDCx.Triegc return XDCx.Triegc
} }
@ -639,7 +639,7 @@ func (XDCx *XDCX) RollbackReorgTxMatch(txhash common.Hash) error {
continue continue
} }
orderCacheAtTxHash := c.(map[common.Hash]tradingstate.OrderHistoryItem) orderCacheAtTxHash := c.(map[common.Hash]tradingstate.OrderHistoryItem)
orderHistoryItem, _ := orderCacheAtTxHash[tradingstate.GetOrderHistoryKey(order.BaseToken, order.QuoteToken, order.Hash)] orderHistoryItem := orderCacheAtTxHash[tradingstate.GetOrderHistoryKey(order.BaseToken, order.QuoteToken, order.Hash)]
if (orderHistoryItem == tradingstate.OrderHistoryItem{}) { if (orderHistoryItem == tradingstate.OrderHistoryItem{}) {
log.Debug("XDCx reorg: remove order due to empty orderHistory", "order", tradingstate.ToJSON(order)) log.Debug("XDCx reorg: remove order due to empty orderHistory", "order", tradingstate.ToJSON(order))
if err := db.DeleteObject(order.Hash, &tradingstate.OrderItem{}); err != nil { if err := db.DeleteObject(order.Hash, &tradingstate.OrderItem{}); err != nil {

View file

@ -2,11 +2,12 @@ package XDCx
import ( import (
"encoding/json" "encoding/json"
"github.com/XinFinOrg/XDPoSChain/core/types"
"math/big" "math/big"
"strconv" "strconv"
"time" "time"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/consensus" "github.com/XinFinOrg/XDPoSChain/consensus"
"fmt" "fmt"
@ -303,7 +304,10 @@ func (XDCx *XDCX) processOrderList(coinbase common.Address, chain consensus.Chai
} }
if tradedQuantity.Sign() > 0 { if tradedQuantity.Sign() > 0 {
quantityToTrade = tradingstate.Sub(quantityToTrade, tradedQuantity) quantityToTrade = tradingstate.Sub(quantityToTrade, tradedQuantity)
tradingStateDB.SubAmountOrderItem(orderBook, orderId, price, tradedQuantity, side) err := tradingStateDB.SubAmountOrderItem(orderBook, orderId, price, tradedQuantity, side)
if err != nil {
log.Warn("processOrderList SubAmountOrderItem", "err", err, "orderBook", orderBook, "orderId", orderId, "price", *price, "tradedQuantity", *tradedQuantity, "side", side)
}
tradingStateDB.SetLastPrice(orderBook, price) tradingStateDB.SetLastPrice(orderBook, price)
log.Debug("Update quantity for orderId", "orderId", orderId.Hex()) log.Debug("Update quantity for orderId", "orderId", orderId.Hex())
log.Debug("TRADE", "orderBook", orderBook, "Taker price", price, "maker price", order.Price, "Amount", tradedQuantity, "orderId", orderId, "side", side) log.Debug("TRADE", "orderBook", orderBook, "Taker price", price, "maker price", order.Price, "Amount", tradedQuantity, "orderId", orderId, "side", side)
@ -589,11 +593,22 @@ func DoSettleBalance(coinbase common.Address, takerOrder, makerOrder *tradingsta
masternodeOwner := statedb.GetOwner(coinbase) masternodeOwner := statedb.GetOwner(coinbase)
statedb.AddBalance(masternodeOwner, matchingFee) statedb.AddBalance(masternodeOwner, matchingFee)
tradingstate.SetTokenBalance(takerOrder.UserAddress, newTakerInTotal, settleBalance.Taker.InToken, statedb) err = tradingstate.SetTokenBalance(takerOrder.UserAddress, newTakerInTotal, settleBalance.Taker.InToken, statedb)
tradingstate.SetTokenBalance(takerOrder.UserAddress, newTakerOutTotal, settleBalance.Taker.OutToken, statedb) if err != nil {
log.Warn("DoSettleBalance SetTokenBalance", "err", err, "takerOder.UserAddress", takerOrder.UserAddress, "newTakerInTotal", *newTakerInTotal, "settleBalance.Taker.InToken", settleBalance.Taker.InToken)
tradingstate.SetTokenBalance(makerOrder.UserAddress, newMakerInTotal, settleBalance.Maker.InToken, statedb) }
tradingstate.SetTokenBalance(makerOrder.UserAddress, newMakerOutTotal, settleBalance.Maker.OutToken, statedb) err = tradingstate.SetTokenBalance(takerOrder.UserAddress, newTakerOutTotal, settleBalance.Taker.OutToken, statedb)
if err != nil {
log.Warn("DoSettleBalance SetTokenBalance", "err", err, "takerOrder.UserAddress", takerOrder.UserAddress, "newTakerOutTotal", *newTakerOutTotal, "settleBalance.Taker.OutToken", settleBalance.Taker.OutToken)
}
err = tradingstate.SetTokenBalance(makerOrder.UserAddress, newMakerInTotal, settleBalance.Maker.InToken, statedb)
if err != nil {
log.Warn("DoSettleBalance SetTokenBalance", "err", err, "makerOrder.UserAddress", makerOrder.UserAddress, "newMakerInTotal", *newMakerInTotal, "settleBalance.Maker.InToken", settleBalance.Maker.InToken)
}
err = tradingstate.SetTokenBalance(makerOrder.UserAddress, newMakerOutTotal, settleBalance.Maker.OutToken, statedb)
if err != nil {
log.Warn("DoSettleBalance SetTokenBalance", "err", err, "makerOrder.UserAddress", makerOrder.UserAddress, "newMakerOutTotal", *newMakerOutTotal, "settleBalance.Maker.OutToken", settleBalance.Maker.OutToken)
}
// add balance for relayers // add balance for relayers
//log.Debug("ApplyXDCXMatchedTransaction settle fee for relayers", //log.Debug("ApplyXDCXMatchedTransaction settle fee for relayers",
@ -602,8 +617,14 @@ func DoSettleBalance(coinbase common.Address, takerOrder, makerOrder *tradingsta
// "makerRelayerOwner", makerExOwner, // "makerRelayerOwner", makerExOwner,
// "makerFeeToken", quoteToken, "makerFee", settleBalanceResult[makerAddr][XDCx.Fee].(*big.Int)) // "makerFeeToken", quoteToken, "makerFee", settleBalanceResult[makerAddr][XDCx.Fee].(*big.Int))
// takerFee // takerFee
tradingstate.SetTokenBalance(takerExOwner, newTakerFee, makerOrder.QuoteToken, statedb) err = tradingstate.SetTokenBalance(takerExOwner, newTakerFee, makerOrder.QuoteToken, statedb)
tradingstate.SetTokenBalance(makerExOwner, newMakerFee, makerOrder.QuoteToken, statedb) if err != nil {
log.Warn("DoSettleBalance SetTokenBalance", "err", err, "takerExOwner", takerExOwner, "newTakerFee", *newTakerFee, "makerOrder.QuoteToken", makerOrder.QuoteToken)
}
err = tradingstate.SetTokenBalance(makerExOwner, newMakerFee, makerOrder.QuoteToken, statedb)
if err != nil {
log.Warn("DoSettleBalance SetTokenBalance", "err", err, "makerExOwner", makerExOwner, "newMakerFee", *newMakerFee, "makerOrder.QuoteToken", makerOrder.QuoteToken)
}
return nil return nil
} }
@ -652,7 +673,10 @@ func (XDCx *XDCX) ProcessCancelOrder(header *types.Header, tradingStateDB *tradi
return err, false return err, false
} }
// relayers pay XDC for masternode // relayers pay XDC for masternode
tradingstate.SubRelayerFee(originOrder.ExchangeAddress, common.RelayerCancelFee, statedb) err = tradingstate.SubRelayerFee(originOrder.ExchangeAddress, common.RelayerCancelFee, statedb)
if err != nil {
log.Warn("ProcessCancelOrder SubRelayerFee", "err", err, "originOrder.ExchangeAddress", originOrder.ExchangeAddress, "common.RelayerCancelFee", *common.RelayerCancelFee)
}
masternodeOwner := statedb.GetOwner(coinbase) masternodeOwner := statedb.GetOwner(coinbase)
// relayers pay XDC for masternode // relayers pay XDC for masternode
statedb.AddBalance(masternodeOwner, common.RelayerCancelFee) statedb.AddBalance(masternodeOwner, common.RelayerCancelFee)
@ -661,12 +685,24 @@ func (XDCx *XDCX) ProcessCancelOrder(header *types.Header, tradingStateDB *tradi
switch originOrder.Side { switch originOrder.Side {
case tradingstate.Ask: case tradingstate.Ask:
// users pay token (which they have) for relayer // users pay token (which they have) for relayer
tradingstate.SubTokenBalance(originOrder.UserAddress, tokenCancelFee, originOrder.BaseToken, statedb) err := tradingstate.SubTokenBalance(originOrder.UserAddress, tokenCancelFee, originOrder.BaseToken, statedb)
tradingstate.AddTokenBalance(relayerOwner, tokenCancelFee, originOrder.BaseToken, statedb) if err != nil {
log.Warn("ProcessCancelOrder SubTokenBalance", "err", err, "originOrder.UserAddress", originOrder.UserAddress, "tokenCancelFee", *tokenCancelFee, "originOrder.BaseToken", originOrder.BaseToken)
}
err = tradingstate.AddTokenBalance(relayerOwner, tokenCancelFee, originOrder.BaseToken, statedb)
if err != nil {
log.Warn("ProcessCancelOrder AddTokenBalance", "err", err, "relayerOwner", relayerOwner, "tokenCancelFee", *tokenCancelFee, "originOrder.BaseToken", originOrder.BaseToken)
}
case tradingstate.Bid: case tradingstate.Bid:
// users pay token (which they have) for relayer // users pay token (which they have) for relayer
tradingstate.SubTokenBalance(originOrder.UserAddress, tokenCancelFee, originOrder.QuoteToken, statedb) err := tradingstate.SubTokenBalance(originOrder.UserAddress, tokenCancelFee, originOrder.QuoteToken, statedb)
tradingstate.AddTokenBalance(relayerOwner, tokenCancelFee, originOrder.QuoteToken, statedb) if err != nil {
log.Warn("ProcessCancelOrder SubTokenBalance", "err", err, "originOrder.UserAddress", originOrder.UserAddress, "tokenCancelFee", *tokenCancelFee, "originOrder.QuoteToken", originOrder.QuoteToken)
}
err = tradingstate.AddTokenBalance(relayerOwner, tokenCancelFee, originOrder.QuoteToken, statedb)
if err != nil {
log.Warn("ProcessCancelOrder AddTokenBalance", "err", err, "relayerOwner", relayerOwner, "tokenCancelFee", *tokenCancelFee, "originOrder.QuoteToken", originOrder.QuoteToken)
}
default: default:
} }
// update cancel fee // update cancel fee

View file

@ -20,6 +20,7 @@ import (
"math/big" "math/big"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/log"
) )
type journalEntry interface { type journalEntry interface {
@ -78,13 +79,19 @@ type (
) )
func (ch insertOrder) undo(s *TradingStateDB) { func (ch insertOrder) undo(s *TradingStateDB) {
s.CancelOrder(ch.orderBook, ch.order) err := s.CancelOrder(ch.orderBook, ch.order)
if err != nil {
log.Warn("undo CancelOrder", "err", err, "ch.orderBook", ch.orderBook, "ch.order", ch.order)
}
} }
func (ch cancelOrder) undo(s *TradingStateDB) { func (ch cancelOrder) undo(s *TradingStateDB) {
s.InsertOrderItem(ch.orderBook, ch.orderId, ch.order) s.InsertOrderItem(ch.orderBook, ch.orderId, ch.order)
} }
func (ch insertLiquidationPrice) undo(s *TradingStateDB) { func (ch insertLiquidationPrice) undo(s *TradingStateDB) {
s.RemoveLiquidationPrice(ch.orderBook, ch.price, ch.lendingBook, ch.tradeId) err := s.RemoveLiquidationPrice(ch.orderBook, ch.price, ch.lendingBook, ch.tradeId)
if err != nil {
log.Warn("undo RemoveLiquidationPrice", "err", err, "ch.orderBook", ch.orderBook, "ch.price", ch.price, "ch.lendingBook", ch.lendingBook, "ch.tradeId", ch.tradeId)
}
} }
func (ch removeLiquidationPrice) undo(s *TradingStateDB) { func (ch removeLiquidationPrice) undo(s *TradingStateDB) {
s.InsertLiquidationPrice(ch.orderBook, ch.price, ch.lendingBook, ch.tradeId) s.InsertLiquidationPrice(ch.orderBook, ch.price, ch.lendingBook, ch.tradeId)

View file

@ -41,10 +41,7 @@ func IsResignedRelayer(relayer common.Address, statedb *state.StateDB) bool {
slot := RelayerMappingSlot["RESIGN_REQUESTS"] slot := RelayerMappingSlot["RESIGN_REQUESTS"]
locBig := GetLocMappingAtKey(relayer.Hash(), slot) locBig := GetLocMappingAtKey(relayer.Hash(), slot)
locHash := common.BigToHash(locBig) locHash := common.BigToHash(locBig)
if statedb.GetState(common.HexToAddress(common.RelayerRegistrationSMC), locHash) != (common.Hash{}) { return statedb.GetState(common.HexToAddress(common.RelayerRegistrationSMC), locHash) != (common.Hash{})
return true
}
return false
} }
func GetBaseTokenLength(relayer common.Address, statedb *state.StateDB) uint64 { func GetBaseTokenLength(relayer common.Address, statedb *state.StateDB) uint64 {

View file

@ -118,7 +118,11 @@ func (self *liquidationPriceState) updateTrie(db Database) Trie {
self.setError(tr.TryDelete(lendingId[:])) self.setError(tr.TryDelete(lendingId[:]))
continue continue
} }
stateObject.updateRoot(db) err := stateObject.updateRoot(db)
if err != nil {
log.Warn("updateTrie updateRoot", "err", err)
}
// Encoding []byte cannot fail, ok to ignore the error. // Encoding []byte cannot fail, ok to ignore the error.
v, _ := rlp.EncodeToBytes(stateObject) v, _ := rlp.EncodeToBytes(stateObject)
self.setError(tr.TryUpdate(lendingId[:], v)) self.setError(tr.TryUpdate(lendingId[:], v))

View file

@ -213,7 +213,10 @@ func (self *tradingExchanges) updateAsksTrie(db Database) Trie {
self.setError(tr.TryDelete(price[:])) self.setError(tr.TryDelete(price[:]))
continue continue
} }
orderList.updateRoot(db) err := orderList.updateRoot(db)
if err != nil {
log.Warn("updateAsksTrie updateRoot", "err", err, "price", price, "orderList", *orderList)
}
// Encoding []byte cannot fail, ok to ignore the error. // Encoding []byte cannot fail, ok to ignore the error.
v, _ := rlp.EncodeToBytes(orderList) v, _ := rlp.EncodeToBytes(orderList)
self.setError(tr.TryUpdate(price[:], v)) self.setError(tr.TryUpdate(price[:], v))
@ -279,7 +282,10 @@ func (self *tradingExchanges) updateBidsTrie(db Database) Trie {
self.setError(tr.TryDelete(price[:])) self.setError(tr.TryDelete(price[:]))
continue continue
} }
orderList.updateRoot(db) err := orderList.updateRoot(db)
if err != nil {
log.Warn("updateBidsTrie updateRoot", "err", err, "price", price, "orderList", *orderList)
}
// Encoding []byte cannot fail, ok to ignore the error. // Encoding []byte cannot fail, ok to ignore the error.
v, _ := rlp.EncodeToBytes(orderList) v, _ := rlp.EncodeToBytes(orderList)
self.setError(tr.TryUpdate(price[:], v)) self.setError(tr.TryUpdate(price[:], v))
@ -753,7 +759,10 @@ func (self *tradingExchanges) updateLiquidationPriceTrie(db Database) Trie {
self.setError(tr.TryDelete(price[:])) self.setError(tr.TryDelete(price[:]))
continue continue
} }
stateObject.updateRoot(db) err := stateObject.updateRoot(db)
if err != nil {
log.Warn("updateLiquidationPriceTrie updateRoot", "err", err, "price", price, "stateObject", *stateObject)
}
// Encoding []byte cannot fail, ok to ignore the error. // Encoding []byte cannot fail, ok to ignore the error.
v, _ := rlp.EncodeToBytes(stateObject) v, _ := rlp.EncodeToBytes(stateObject)
self.setError(tr.TryUpdate(price[:], v)) self.setError(tr.TryUpdate(price[:], v))

View file

@ -539,7 +539,10 @@ func (s *TradingStateDB) Finalise() {
for addr, stateObject := range s.stateExhangeObjects { for addr, stateObject := range s.stateExhangeObjects {
if _, isDirty := s.stateExhangeObjectsDirty[addr]; isDirty { if _, isDirty := s.stateExhangeObjectsDirty[addr]; isDirty {
// Write any storage changes in the state object to its storage trie. // Write any storage changes in the state object to its storage trie.
stateObject.updateAsksRoot(s.db) err := stateObject.updateAsksRoot(s.db)
if err != nil {
log.Warn("Finalise updateAsksRoot", "err", err, "addr", addr, "stateObject", *stateObject)
}
stateObject.updateBidsRoot(s.db) stateObject.updateBidsRoot(s.db)
stateObject.updateOrdersRoot(s.db) stateObject.updateOrdersRoot(s.db)
stateObject.updateLiquidationPriceRoot(s.db) stateObject.updateLiquidationPriceRoot(s.db)
@ -713,7 +716,10 @@ func (self *TradingStateDB) RemoveLiquidationPrice(orderBook common.Hash, price
lendingBookState.subVolume(One) lendingBookState.subVolume(One)
liquidationPriceState.subVolume(One) liquidationPriceState.subVolume(One)
if liquidationPriceState.Volume().Sign() == 0 { if liquidationPriceState.Volume().Sign() == 0 {
orderbookState.getLiquidationPriceTrie(self.db).TryDelete(priceHash[:]) err := orderbookState.getLiquidationPriceTrie(self.db).TryDelete(priceHash[:])
if err != nil {
log.Warn("RemoveLiquidationPrice getLiquidationPriceTrie.TryDelete", "err", err, "priceHash", priceHash[:])
}
} }
orderbookState.subLendingCount(One) orderbookState.subLendingCount(One)
self.journal = append(self.journal, removeLiquidationPrice{ self.journal = append(self.journal, removeLiquidationPrice{

View file

@ -4,9 +4,10 @@ import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"errors" "errors"
"sync"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core/rawdb" "github.com/XinFinOrg/XDPoSChain/core/rawdb"
"sync"
"github.com/XinFinOrg/XDPoSChain/ethdb" "github.com/XinFinOrg/XDPoSChain/ethdb"
"github.com/XinFinOrg/XDPoSChain/log" "github.com/XinFinOrg/XDPoSChain/log"
@ -57,7 +58,7 @@ func NewBatchDatabaseWithEncode(datadir string, cacheLimit int) *BatchDatabase {
} }
func (db *BatchDatabase) IsEmptyKey(key []byte) bool { func (db *BatchDatabase) IsEmptyKey(key []byte) bool {
return key == nil || len(key) == 0 || bytes.Equal(key, db.emptyKey) return len(key) == 0 || bytes.Equal(key, db.emptyKey)
} }
func (db *BatchDatabase) getCacheKey(key []byte) string { func (db *BatchDatabase) getCacheKey(key []byte) string {
@ -171,13 +172,13 @@ func (db *BatchDatabase) Sync() error {
} }
func (db *BatchDatabase) NewIterator(prefix []byte, start []byte) ethdb.Iterator { func (db *BatchDatabase) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
return db.NewIterator(prefix, start) panic("NewIterator from XDCxDAO leveldb is not supported")
} }
func (db *BatchDatabase) Stat(property string) (string, error) { func (db *BatchDatabase) Stat(property string) (string, error) {
return db.Stat(property) return "", errNotSupported
} }
func (db *BatchDatabase) Compact(start []byte, limit []byte) error { func (db *BatchDatabase) Compact(start []byte, limit []byte) error {
return db.Compact(start, limit) return errNotSupported
} }

View file

@ -4,6 +4,9 @@ import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"strings"
"time"
"github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate" "github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate"
"github.com/XinFinOrg/XDPoSChain/XDCxlending/lendingstate" "github.com/XinFinOrg/XDPoSChain/XDCxlending/lendingstate"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
@ -12,8 +15,6 @@ import (
"github.com/globalsign/mgo" "github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson" "github.com/globalsign/mgo/bson"
lru "github.com/hashicorp/golang-lru" lru "github.com/hashicorp/golang-lru"
"strings"
"time"
) )
const ( const (
@ -77,7 +78,7 @@ func NewMongoDatabase(session *mgo.Session, dbName string, mongoURL string, repl
} }
func (db *MongoDatabase) IsEmptyKey(key []byte) bool { func (db *MongoDatabase) IsEmptyKey(key []byte) bool {
return key == nil || len(key) == 0 || bytes.Equal(key, db.emptyKey) return len(key) == 0 || bytes.Equal(key, db.emptyKey)
} }
func (db *MongoDatabase) getCacheKey(key []byte) string { func (db *MongoDatabase) getCacheKey(key []byte) string {
@ -873,15 +874,15 @@ func (db *MongoDatabase) Sync() error {
} }
func (db *MongoDatabase) NewIterator(prefix []byte, start []byte) ethdb.Iterator { func (db *MongoDatabase) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
return db.NewIterator(prefix, start) panic("NewIterator from XDCxDAO mongodb is not supported")
} }
func (db *MongoDatabase) Stat(property string) (string, error) { func (db *MongoDatabase) Stat(property string) (string, error) {
return db.Stat(property) return "", errNotSupported
} }
func (db *MongoDatabase) Compact(start []byte, limit []byte) error { func (db *MongoDatabase) Compact(start []byte, limit []byte) error {
return db.Compact(start, limit) return errNotSupported
} }
func (db *MongoDatabase) NewBatch() ethdb.Batch { func (db *MongoDatabase) NewBatch() ethdb.Batch {

View file

@ -144,20 +144,12 @@ func (l *Lending) ProcessOrderPending(header *types.Header, coinbase common.Addr
S: common.BigToHash(S), S: common.BigToHash(S),
}, },
} }
cancel := false
if order.Status == lendingstate.LendingStatusCancelled {
cancel = true
}
log.Info("Process order pending", "orderPending", order, "LendingToken", order.LendingToken.Hex(), "CollateralToken", order.CollateralToken) log.Info("Process order pending", "orderPending", order, "LendingToken", order.LendingToken.Hex(), "CollateralToken", order.CollateralToken)
originalOrder := &lendingstate.LendingItem{} originalOrder := &lendingstate.LendingItem{}
*originalOrder = *order *originalOrder = *order
originalOrder.Quantity = lendingstate.CloneBigInt(order.Quantity) originalOrder.Quantity = lendingstate.CloneBigInt(order.Quantity)
if cancel {
order.Status = lendingstate.LendingStatusCancelled
}
newTrades, newRejectedOrders, err := l.CommitOrder(header, coinbase, chain, statedb, lendingStatedb, tradingStateDb, lendingstate.GetLendingOrderBookHash(order.LendingToken, order.Term), order) newTrades, newRejectedOrders, err := l.CommitOrder(header, coinbase, chain, statedb, lendingStatedb, tradingStateDb, lendingstate.GetLendingOrderBookHash(order.LendingToken, order.Term), order)
for _, reject := range newRejectedOrders { for _, reject := range newRejectedOrders {
log.Debug("Reject order", "reject", *reject) log.Debug("Reject order", "reject", *reject)
@ -764,7 +756,7 @@ func (l *Lending) RollbackLendingData(txhash common.Hash) error {
continue continue
} }
cacheAtTxHash := c.(map[common.Hash]lendingstate.LendingItemHistoryItem) cacheAtTxHash := c.(map[common.Hash]lendingstate.LendingItemHistoryItem)
lendingItemHistory, _ := cacheAtTxHash[lendingstate.GetLendingItemHistoryKey(item.LendingToken, item.CollateralToken, item.Hash)] lendingItemHistory := cacheAtTxHash[lendingstate.GetLendingItemHistoryKey(item.LendingToken, item.CollateralToken, item.Hash)]
if (lendingItemHistory == lendingstate.LendingItemHistoryItem{}) { if (lendingItemHistory == lendingstate.LendingItemHistoryItem{}) {
log.Debug("XDCxlending reorg: remove item due to empty lendingItemHistory", "item", lendingstate.ToJSON(item)) log.Debug("XDCxlending reorg: remove item due to empty lendingItemHistory", "item", lendingstate.ToJSON(item))
if err := db.DeleteObject(item.Hash, &lendingstate.LendingItem{}); err != nil { if err := db.DeleteObject(item.Hash, &lendingstate.LendingItem{}); err != nil {
@ -797,7 +789,7 @@ func (l *Lending) RollbackLendingData(txhash common.Hash) error {
continue continue
} }
cacheAtTxHash := c.(map[common.Hash]lendingstate.LendingTradeHistoryItem) cacheAtTxHash := c.(map[common.Hash]lendingstate.LendingTradeHistoryItem)
lendingTradeHistoryItem, _ := cacheAtTxHash[trade.Hash] lendingTradeHistoryItem := cacheAtTxHash[trade.Hash]
if (lendingTradeHistoryItem == lendingstate.LendingTradeHistoryItem{}) { if (lendingTradeHistoryItem == lendingstate.LendingTradeHistoryItem{}) {
log.Debug("XDCxlending reorg: remove trade due to empty LendingTradeHistory", "trade", lendingstate.ToJSON(trade)) log.Debug("XDCxlending reorg: remove trade due to empty LendingTradeHistory", "trade", lendingstate.ToJSON(trade))
if err := db.DeleteObject(trade.Hash, &lendingstate.LendingTrade{}); err != nil { if err := db.DeleteObject(trade.Hash, &lendingstate.LendingTrade{}); err != nil {

View file

@ -17,8 +17,10 @@
package lendingstate package lendingstate
import ( import (
"github.com/XinFinOrg/XDPoSChain/common"
"math/big" "math/big"
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/log"
) )
type journalEntry interface { type journalEntry interface {
@ -76,7 +78,10 @@ type (
) )
func (ch insertOrder) undo(s *LendingStateDB) { func (ch insertOrder) undo(s *LendingStateDB) {
s.CancelLendingOrder(ch.orderBook, ch.order) err := s.CancelLendingOrder(ch.orderBook, ch.order)
if err != nil {
log.Warn("undo CancelLendingOrder", "err", err, "ch.orderBook", ch.orderBook, "ch.order", *ch.order)
}
} }
func (ch cancelOrder) undo(s *LendingStateDB) { func (ch cancelOrder) undo(s *LendingStateDB) {
s.InsertLendingItem(ch.orderBook, ch.orderId, ch.order) s.InsertLendingItem(ch.orderBook, ch.orderId, ch.order)

View file

@ -2,12 +2,13 @@ package lendingstate
import ( import (
"fmt" "fmt"
"math/big"
"github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate" "github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core/state" "github.com/XinFinOrg/XDPoSChain/core/state"
"github.com/XinFinOrg/XDPoSChain/crypto" "github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/log" "github.com/XinFinOrg/XDPoSChain/log"
"math/big"
) )
var ( var (
@ -142,12 +143,11 @@ func IsValidPair(statedb *state.StateDB, coinbase common.Address, baseToken comm
// @param baseToken: address of baseToken // @param baseToken: address of baseToken
// @param terms: term // @param terms: term
// @return: // @return:
// - collaterals []common.Address : list of addresses of collateral // - collaterals []common.Address : list of addresses of collateral
// - isSpecialCollateral : TRUE if collateral is a token which is NOT available for trading in XDCX, otherwise FALSE func GetCollaterals(statedb *state.StateDB, coinbase common.Address, baseToken common.Address, term uint64) (collaterals []common.Address) {
func GetCollaterals(statedb *state.StateDB, coinbase common.Address, baseToken common.Address, term uint64) (collaterals []common.Address, isSpecialCollateral bool) {
validPair, _ := IsValidPair(statedb, coinbase, baseToken, term) validPair, _ := IsValidPair(statedb, coinbase, baseToken, term)
if !validPair { if !validPair {
return []common.Address{}, false return []common.Address{}
} }
//TODO: ILO Collateral is not supported in release 2.2.0 //TODO: ILO Collateral is not supported in release 2.2.0
@ -171,7 +171,7 @@ func GetCollaterals(statedb *state.StateDB, coinbase common.Address, baseToken c
collaterals = append(collaterals, addr) collaterals = append(collaterals, addr)
} }
} }
return collaterals, false return collaterals
} }
// @function GetCollateralDetail // @function GetCollateralDetail

View file

@ -27,6 +27,13 @@ const (
LendingStatusCancelled = "CANCELLED" LendingStatusCancelled = "CANCELLED"
Market = "MO" Market = "MO"
Limit = "LO" Limit = "LO"
/*
Based on all structs that were used to encode into extraData, we can see the liquidationData is likely be the one with max length in payload.
A assumptions was made that each numeric value (RecallAmount, LiquidationAmount, CollateralPrice) is up to 30 digits long and the Reason field is 20 characters long, the estimated maximum size of the ExtraData JSON string in the ProcessLiquidationData function would be approximately 185 bytes.
Hence the value of 200 has been chosen to safeguard the block/tx in XDC in terms of sizes.
*/
MaxLendingExtraDataSize = 200
) )
var ValidInputLendingStatus = map[string]bool{ var ValidInputLendingStatus = map[string]bool{
@ -233,6 +240,9 @@ func (l *LendingItem) VerifyLendingItem(state *state.StateDB) error {
if err := l.VerifyLendingSignature(); err != nil { if err := l.VerifyLendingSignature(); err != nil {
return err return err
} }
if err := l.VerifyLendingExtraData(); err != nil {
return err
}
return nil return nil
} }
@ -248,7 +258,7 @@ func (l *LendingItem) VerifyCollateral(state *state.StateDB) error {
return fmt.Errorf("invalid collateral %s", l.CollateralToken.Hex()) return fmt.Errorf("invalid collateral %s", l.CollateralToken.Hex())
} }
validCollateral := false validCollateral := false
collateralList, _ := GetCollaterals(state, l.Relayer, l.LendingToken, l.Term) collateralList := GetCollaterals(state, l.Relayer, l.LendingToken, l.Term)
for _, collateral := range collateralList { for _, collateral := range collateralList {
if l.CollateralToken.String() == collateral.String() { if l.CollateralToken.String() == collateral.String() {
validCollateral = true validCollateral = true
@ -282,6 +292,13 @@ func (l *LendingItem) VerifyLendingType() error {
return nil return nil
} }
func (l *LendingItem) VerifyLendingExtraData() error {
if len(l.ExtraData) > MaxLendingExtraDataSize {
return fmt.Errorf("VerifyLendingExtraData: invalid lending extraData size. Size: %v", len(l.ExtraData))
}
return nil
}
func (l *LendingItem) VerifyLendingStatus() error { func (l *LendingItem) VerifyLendingStatus() error {
if valid, ok := ValidInputLendingStatus[l.Status]; !ok && !valid { if valid, ok := ValidInputLendingStatus[l.Status]; !ok && !valid {
return fmt.Errorf("VerifyLendingStatus: invalid lending status. Status: %s", l.Status) return fmt.Errorf("VerifyLendingStatus: invalid lending status. Status: %s", l.Status)

View file

@ -2,17 +2,18 @@ package lendingstate
import ( import (
"fmt" "fmt"
"math/big"
"math/rand"
"os"
"testing"
"time"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core/rawdb" "github.com/XinFinOrg/XDPoSChain/core/rawdb"
"github.com/XinFinOrg/XDPoSChain/core/state" "github.com/XinFinOrg/XDPoSChain/core/state"
"github.com/XinFinOrg/XDPoSChain/crypto" "github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/crypto/sha3" "github.com/XinFinOrg/XDPoSChain/crypto/sha3"
"github.com/XinFinOrg/XDPoSChain/rpc" "github.com/XinFinOrg/XDPoSChain/rpc"
"math/big"
"math/rand"
"os"
"testing"
"time"
) )
func TestLendingItem_VerifyLendingSide(t *testing.T) { func TestLendingItem_VerifyLendingSide(t *testing.T) {
@ -108,6 +109,27 @@ func TestLendingItem_VerifyLendingType(t *testing.T) {
} }
} }
func TestLendingItem_VerifyExtraData(t *testing.T) {
tests := []struct {
name string
fields *LendingItem
wantErr bool
}{
{"within the limit", &LendingItem{ExtraData: "123"}, false},
{"within the limit", &LendingItem{ExtraData: "This is a string specifically designed to exceed 201 bytes in length. It contains enough characters, including spaces and punctuation, to ensure that its total size goes beyond the specified limit for demonstration purposes."}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &LendingItem{
ExtraData: tt.fields.ExtraData,
}
if err := l.VerifyLendingExtraData(); (err != nil) != tt.wantErr {
t.Errorf("VerifyLendingExtraData() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestLendingItem_VerifyLendingStatus(t *testing.T) { func TestLendingItem_VerifyLendingStatus(t *testing.T) {
tests := []struct { tests := []struct {
name string name string

View file

@ -18,11 +18,12 @@ package lendingstate
import ( import (
"fmt" "fmt"
"io"
"math/big"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/log" "github.com/XinFinOrg/XDPoSChain/log"
"github.com/XinFinOrg/XDPoSChain/rlp" "github.com/XinFinOrg/XDPoSChain/rlp"
"io"
"math/big"
) )
type lendingExchangeState struct { type lendingExchangeState struct {
@ -181,8 +182,10 @@ func (self *lendingExchangeState) getLiquidationTimeTrie(db Database) Trie {
return self.liquidationTimeTrie return self.liquidationTimeTrie
} }
/** /*
Get State *
Get State
*/ */
func (self *lendingExchangeState) getBorrowingOrderList(db Database, rate common.Hash) (stateOrderList *itemListState) { func (self *lendingExchangeState) getBorrowingOrderList(db Database, rate common.Hash) (stateOrderList *itemListState) {
// Prefer 'live' objects. // Prefer 'live' objects.
@ -299,8 +302,10 @@ func (self *lendingExchangeState) getLendingTrade(db Database, tradeId common.Ha
return obj return obj
} }
/** /*
Update Trie *
Update Trie
*/ */
func (self *lendingExchangeState) updateLendingTimeTrie(db Database) Trie { func (self *lendingExchangeState) updateLendingTimeTrie(db Database) Trie {
tr := self.getLendingItemTrie(db) tr := self.getLendingItemTrie(db)
@ -344,7 +349,10 @@ func (self *lendingExchangeState) updateBorrowingTrie(db Database) Trie {
self.setError(tr.TryDelete(rate[:])) self.setError(tr.TryDelete(rate[:]))
continue continue
} }
orderList.updateRoot(db) err := orderList.updateRoot(db)
if err != nil {
log.Warn("updateBorrowingTrie updateRoot", "err", err, "rate", rate, "orderList", *orderList)
}
// Encoding []byte cannot fail, ok to ignore the error. // Encoding []byte cannot fail, ok to ignore the error.
v, _ := rlp.EncodeToBytes(orderList) v, _ := rlp.EncodeToBytes(orderList)
self.setError(tr.TryUpdate(rate[:], v)) self.setError(tr.TryUpdate(rate[:], v))
@ -362,7 +370,10 @@ func (self *lendingExchangeState) updateInvestingTrie(db Database) Trie {
self.setError(tr.TryDelete(rate[:])) self.setError(tr.TryDelete(rate[:]))
continue continue
} }
orderList.updateRoot(db) err := orderList.updateRoot(db)
if err != nil {
log.Warn("updateInvestingTrie updateRoot", "err", err, "rate", rate, "orderList", *orderList)
}
// Encoding []byte cannot fail, ok to ignore the error. // Encoding []byte cannot fail, ok to ignore the error.
v, _ := rlp.EncodeToBytes(orderList) v, _ := rlp.EncodeToBytes(orderList)
self.setError(tr.TryUpdate(rate[:], v)) self.setError(tr.TryUpdate(rate[:], v))
@ -380,7 +391,10 @@ func (self *lendingExchangeState) updateLiquidationTimeTrie(db Database) Trie {
self.setError(tr.TryDelete(time[:])) self.setError(tr.TryDelete(time[:]))
continue continue
} }
itemList.updateRoot(db) err := itemList.updateRoot(db)
if err != nil {
log.Warn("updateLiquidationTimeTrie updateRoot", "err", err, "time", time, "itemList", *itemList)
}
// Encoding []byte cannot fail, ok to ignore the error. // Encoding []byte cannot fail, ok to ignore the error.
v, _ := rlp.EncodeToBytes(itemList) v, _ := rlp.EncodeToBytes(itemList)
self.setError(tr.TryUpdate(time[:], v)) self.setError(tr.TryUpdate(time[:], v))
@ -513,8 +527,10 @@ func (self *lendingExchangeState) CommitLiquidationTimeTrie(db Database) error {
return err return err
} }
/** /*
Get Trie Data *
Get Trie Data
*/ */
func (self *lendingExchangeState) getBestInvestingInterest(db Database) common.Hash { func (self *lendingExchangeState) getBestInvestingInterest(db Database) common.Hash {
trie := self.getInvestingTrie(db) trie := self.getInvestingTrie(db)

View file

@ -524,7 +524,10 @@ func (s *LendingStateDB) Finalise() {
for addr, stateObject := range s.lendingExchangeStates { for addr, stateObject := range s.lendingExchangeStates {
if _, isDirty := s.lendingExchangeStatesDirty[addr]; isDirty { if _, isDirty := s.lendingExchangeStatesDirty[addr]; isDirty {
// Write any storage changes in the state object to its storage trie. // Write any storage changes in the state object to its storage trie.
stateObject.updateInvestingRoot(s.db) err := stateObject.updateInvestingRoot(s.db)
if err != nil {
log.Warn("Finalise updateInvestingRoot", "err", err, "addr", addr, "stateObject", *stateObject)
}
stateObject.updateBorrowingRoot(s.db) stateObject.updateBorrowingRoot(s.db)
stateObject.updateOrderRoot(s.db) stateObject.updateOrderRoot(s.db)
stateObject.updateLendingTradeRoot(s.db) stateObject.updateLendingTradeRoot(s.db)
@ -630,7 +633,10 @@ func (self *LendingStateDB) RemoveLiquidationTime(lendingBook common.Hash, trade
liquidationTime.removeTradeId(self.db, tradeIdHash) liquidationTime.removeTradeId(self.db, tradeIdHash)
liquidationTime.subVolume(One) liquidationTime.subVolume(One)
if liquidationTime.Volume().Sign() == 0 { if liquidationTime.Volume().Sign() == 0 {
lendingExchangeState.getLiquidationTimeTrie(self.db).TryDelete(timeHash[:]) err := lendingExchangeState.getLiquidationTimeTrie(self.db).TryDelete(timeHash[:])
if err != nil {
log.Warn("RemoveLiquidationTime getLiquidationTimeTrie.TryDelete", "err", err, "timeHash[:]", timeHash[:])
}
} }
return nil return nil
} }

View file

@ -339,7 +339,10 @@ func (l *Lending) processOrderList(header *types.Header, coinbase common.Address
} }
if tradedQuantity.Sign() > 0 { if tradedQuantity.Sign() > 0 {
quantityToTrade = lendingstate.Sub(quantityToTrade, tradedQuantity) quantityToTrade = lendingstate.Sub(quantityToTrade, tradedQuantity)
lendingStateDB.SubAmountLendingItem(lendingOrderBook, orderId, Interest, tradedQuantity, side) err := lendingStateDB.SubAmountLendingItem(lendingOrderBook, orderId, Interest, tradedQuantity, side)
if err != nil {
log.Warn("processOrderList SubAmountLendingItem", "err", err, "lendingOrderBook", lendingOrderBook, "orderId", orderId, "Interest", *Interest, "tradedQuantity", *tradedQuantity, "side", side)
}
log.Debug("Update quantity for orderId", "orderId", orderId.Hex()) log.Debug("Update quantity for orderId", "orderId", orderId.Hex())
log.Debug("LEND", "lendingOrderBook", lendingOrderBook.Hex(), "Taker Interest", Interest, "maker Interest", order.Interest, "Amount", tradedQuantity, "orderId", orderId, "side", side) log.Debug("LEND", "lendingOrderBook", lendingOrderBook.Hex(), "Taker Interest", Interest, "maker Interest", order.Interest, "Amount", tradedQuantity, "orderId", orderId, "side", side)
tradingId := lendingStateDB.GetTradeNonce(lendingOrderBook) + 1 tradingId := lendingStateDB.GetTradeNonce(lendingOrderBook) + 1
@ -669,7 +672,10 @@ func DoSettleBalance(coinbase common.Address, takerOrder, makerOrder *lendingsta
statedb.AddBalance(masternodeOwner, matchingFee) statedb.AddBalance(masternodeOwner, matchingFee)
for token, balances := range mapBalances { for token, balances := range mapBalances {
for adrr, value := range balances { for adrr, value := range balances {
lendingstate.SetTokenBalance(adrr, value, token, statedb) err := lendingstate.SetTokenBalance(adrr, value, token, statedb)
if err != nil {
log.Warn("DoSettleBalance SetTokenBalance", "err", err, "addr", adrr, "value", *value, "token", token)
}
} }
} }
return nil return nil
@ -744,12 +750,24 @@ func (l *Lending) ProcessCancelOrder(header *types.Header, lendingStateDB *lendi
switch originOrder.Side { switch originOrder.Side {
case lendingstate.Investing: case lendingstate.Investing:
// users pay token for relayer // users pay token for relayer
lendingstate.SubTokenBalance(originOrder.UserAddress, tokenCancelFee, originOrder.LendingToken, statedb) err := lendingstate.SubTokenBalance(originOrder.UserAddress, tokenCancelFee, originOrder.LendingToken, statedb)
lendingstate.AddTokenBalance(relayerOwner, tokenCancelFee, originOrder.LendingToken, statedb) if err != nil {
log.Warn("ProcessCancelOrder SubTokenBalance", "err", err, "originOrder.UserAddress", originOrder.UserAddress, "tokenCancelFee", *tokenCancelFee, "originOrder.LendingToken", originOrder.LendingToken)
}
err = lendingstate.AddTokenBalance(relayerOwner, tokenCancelFee, originOrder.LendingToken, statedb)
if err != nil {
log.Warn("ProcessCancelOrder AddTokenBalance", "err", err, "relayerOwner", relayerOwner, "tokenCancelFee", *tokenCancelFee, "originOrder.LendingToken", originOrder.LendingToken)
}
case lendingstate.Borrowing: case lendingstate.Borrowing:
// users pay token for relayer // users pay token for relayer
lendingstate.SubTokenBalance(originOrder.UserAddress, tokenCancelFee, originOrder.CollateralToken, statedb) err := lendingstate.SubTokenBalance(originOrder.UserAddress, tokenCancelFee, originOrder.CollateralToken, statedb)
lendingstate.AddTokenBalance(relayerOwner, tokenCancelFee, originOrder.CollateralToken, statedb) if err != nil {
log.Warn("ProcessCancelOrder SubTokenBalance", "err", err, "originOrder.UserAddress", originOrder.UserAddress, "tokenCancelFee", *tokenCancelFee, "originOrder.CollateralToken", originOrder.CollateralToken)
}
err = lendingstate.AddTokenBalance(relayerOwner, tokenCancelFee, originOrder.CollateralToken, statedb)
if err != nil {
log.Warn("ProcessCancelOrder AddTokenBalance", "err", err, "relayerOwner", relayerOwner, "tokenCancelFee", *tokenCancelFee, "originOrder.CollateralToken", originOrder.CollateralToken)
}
default: default:
} }
extraData, _ := json.Marshal(struct { extraData, _ := json.Marshal(struct {
@ -829,12 +847,21 @@ func (l *Lending) LiquidationExpiredTrade(header *types.Header, chain consensus.
recallAmount := common.Big0 recallAmount := common.Big0
if repayAmount.Cmp(lendingTrade.CollateralLockedAmount) < 0 { if repayAmount.Cmp(lendingTrade.CollateralLockedAmount) < 0 {
recallAmount = new(big.Int).Sub(lendingTrade.CollateralLockedAmount, repayAmount) recallAmount = new(big.Int).Sub(lendingTrade.CollateralLockedAmount, repayAmount)
lendingstate.AddTokenBalance(lendingTrade.Borrower, recallAmount, lendingTrade.CollateralToken, statedb) err := lendingstate.AddTokenBalance(lendingTrade.Borrower, recallAmount, lendingTrade.CollateralToken, statedb)
if err != nil {
log.Warn("LiquidationExpiredTrade AddTokenBalance", "err", err, "lendingTrade.Borrower", lendingTrade.Borrower, "recallAmount", *recallAmount, "lendingTrade.CollateralToken", lendingTrade.CollateralToken)
}
} else { } else {
repayAmount = lendingTrade.CollateralLockedAmount repayAmount = lendingTrade.CollateralLockedAmount
} }
lendingstate.SubTokenBalance(common.HexToAddress(common.LendingLockAddress), lendingTrade.CollateralLockedAmount, lendingTrade.CollateralToken, statedb) err = lendingstate.SubTokenBalance(common.HexToAddress(common.LendingLockAddress), lendingTrade.CollateralLockedAmount, lendingTrade.CollateralToken, statedb)
lendingstate.AddTokenBalance(lendingTrade.Investor, repayAmount, lendingTrade.CollateralToken, statedb) if err != nil {
log.Warn("LiquidationExpiredTrade SubTokenBalance", "err", err, "LendingLockAddress", common.HexToAddress(common.LendingLockAddress), "lendingTrade.CollateralLockedAmount", *lendingTrade.CollateralLockedAmount, "lendingTrade.CollateralToken", lendingTrade.CollateralToken)
}
err = lendingstate.AddTokenBalance(lendingTrade.Investor, repayAmount, lendingTrade.CollateralToken, statedb)
if err != nil {
log.Warn("LiquidationExpiredTrade AddTokenBalance", "err", err, "lendingTrade.Investor", lendingTrade.Investor, "repayAmount", repayAmount, "lendingTrade.CollateralToken", lendingTrade.CollateralToken)
}
err = lendingStateDB.RemoveLiquidationTime(lendingBook, lendingTradeId, lendingTrade.LiquidationTime) err = lendingStateDB.RemoveLiquidationTime(lendingBook, lendingTradeId, lendingTrade.LiquidationTime)
if err != nil { if err != nil {
@ -870,10 +897,15 @@ func (l *Lending) LiquidationTrade(lendingStateDB *lendingstate.LendingStateDB,
if lendingTrade.TradeId != lendingTradeId { if lendingTrade.TradeId != lendingTradeId {
return nil, fmt.Errorf("Lending Trade Id not found : %d ", lendingTradeId) return nil, fmt.Errorf("Lending Trade Id not found : %d ", lendingTradeId)
} }
lendingstate.SubTokenBalance(common.HexToAddress(common.LendingLockAddress), lendingTrade.CollateralLockedAmount, lendingTrade.CollateralToken, statedb) err := lendingstate.SubTokenBalance(common.HexToAddress(common.LendingLockAddress), lendingTrade.CollateralLockedAmount, lendingTrade.CollateralToken, statedb)
lendingstate.AddTokenBalance(lendingTrade.Investor, lendingTrade.CollateralLockedAmount, lendingTrade.CollateralToken, statedb) if err != nil {
log.Warn("LiquidationTrade SubTokenBalance", "err", err, "LendingLockAddress", common.HexToAddress(common.LendingLockAddress), "lendingTrade.CollateralLockedAmount", *lendingTrade.CollateralLockedAmount, "lendingTrade.CollateralToken", lendingTrade.CollateralToken)
err := lendingStateDB.RemoveLiquidationTime(lendingBook, lendingTradeId, lendingTrade.LiquidationTime) }
err = lendingstate.AddTokenBalance(lendingTrade.Investor, lendingTrade.CollateralLockedAmount, lendingTrade.CollateralToken, statedb)
if err != nil {
log.Warn("LiquidationTrade AddTokenBalance", "err", err, "lendingTrade.Investor", lendingTrade.Investor, "lendingTrade.CollateralLockedAmount", *lendingTrade.CollateralLockedAmount, "lendingTrade.CollateralToken", lendingTrade.CollateralToken)
}
err = lendingStateDB.RemoveLiquidationTime(lendingBook, lendingTradeId, lendingTrade.LiquidationTime)
if err != nil { if err != nil {
log.Debug("LiquidationTrade RemoveLiquidationTime", "err", err) log.Debug("LiquidationTrade RemoveLiquidationTime", "err", err)
return nil, err return nil, err
@ -1097,8 +1129,14 @@ func (l *Lending) ProcessTopUpLendingTrade(lendingStateDB *lendingstate.LendingS
if err != nil { if err != nil {
return err, true, nil return err, true, nil
} }
lendingstate.SubTokenBalance(lendingTrade.Borrower, quantity, lendingTrade.CollateralToken, statedb) err = lendingstate.SubTokenBalance(lendingTrade.Borrower, quantity, lendingTrade.CollateralToken, statedb)
lendingstate.AddTokenBalance(common.HexToAddress(common.LendingLockAddress), quantity, lendingTrade.CollateralToken, statedb) if err != nil {
log.Warn("ProcessTopUpLendingTrade SubTokenBalance", "err", err, "lendingTrade.Borrower", lendingTrade.Borrower, "quantity", *quantity, "lendingTrade.CollateralToken", lendingTrade.CollateralToken)
}
err = lendingstate.AddTokenBalance(common.HexToAddress(common.LendingLockAddress), quantity, lendingTrade.CollateralToken, statedb)
if err != nil {
log.Warn("ProcessTopUpLendingTrade AddTokenBalance", "err", err, "LendingLockAddress", common.HexToAddress(common.LendingLockAddress), "quantity", *quantity, "lendingTrade.CollateralToken", lendingTrade.CollateralToken)
}
oldLockedAmount := lendingTrade.CollateralLockedAmount oldLockedAmount := lendingTrade.CollateralLockedAmount
newLockedAmount := new(big.Int).Add(quantity, oldLockedAmount) newLockedAmount := new(big.Int).Add(quantity, oldLockedAmount)
newLiquidationPrice := new(big.Int).Mul(lendingTrade.LiquidationPrice, oldLockedAmount) newLiquidationPrice := new(big.Int).Mul(lendingTrade.LiquidationPrice, oldLockedAmount)
@ -1153,11 +1191,22 @@ func (l *Lending) ProcessRepayLendingTrade(header *types.Header, chain consensus
} }
return newLendingTrade, err return newLendingTrade, err
} else { } else {
lendingstate.SubTokenBalance(lendingTrade.Borrower, paymentBalance, lendingTrade.LendingToken, statedb) err := lendingstate.SubTokenBalance(lendingTrade.Borrower, paymentBalance, lendingTrade.LendingToken, statedb)
lendingstate.AddTokenBalance(lendingTrade.Investor, paymentBalance, lendingTrade.LendingToken, statedb) if err != nil {
log.Warn("ProcessRepayLendingTrade SubTokenBalance", "err", err, "lendingTrade.Borrower", lendingTrade.Borrower, "paymentBalance", *paymentBalance, "lendingTrade.LendingToken", lendingTrade.LendingToken)
lendingstate.SubTokenBalance(common.HexToAddress(common.LendingLockAddress), lendingTrade.CollateralLockedAmount, lendingTrade.CollateralToken, statedb) }
lendingstate.AddTokenBalance(lendingTrade.Borrower, lendingTrade.CollateralLockedAmount, lendingTrade.CollateralToken, statedb) err = lendingstate.AddTokenBalance(lendingTrade.Investor, paymentBalance, lendingTrade.LendingToken, statedb)
if err != nil {
log.Warn("ProcessRepayLendingTrade AddTokenBalance", "err", err, "lendingTrade.Investor", lendingTrade.Investor, "paymentBalance", *paymentBalance, "lendingTrade.LendingToken", lendingTrade.LendingToken)
}
err = lendingstate.SubTokenBalance(common.HexToAddress(common.LendingLockAddress), lendingTrade.CollateralLockedAmount, lendingTrade.CollateralToken, statedb)
if err != nil {
log.Warn("ProcessRepayLendingTrade SubTokenBalance", "err", err, "LendingLockAddress", common.HexToAddress(common.LendingLockAddress), "lendingTrade.CollateralLockedAmount", *lendingTrade.CollateralLockedAmount, "lendingTrade.CollateralToken", lendingTrade.CollateralToken)
}
err = lendingstate.AddTokenBalance(lendingTrade.Borrower, lendingTrade.CollateralLockedAmount, lendingTrade.CollateralToken, statedb)
if err != nil {
log.Warn("ProcessRepayLendingTrade AddTokenBalance", "err", err, "lendingTrade.Borrower", lendingTrade.Borrower, "lendingTrade.CollateralLockedAmount", *lendingTrade.CollateralLockedAmount, "lendingTrade.CollateralToken", lendingTrade.CollateralToken)
}
err = lendingStateDB.RemoveLiquidationTime(lendingBook, lendingTradeId, lendingTrade.LiquidationTime) err = lendingStateDB.RemoveLiquidationTime(lendingBook, lendingTradeId, lendingTrade.LiquidationTime)
if err != nil { if err != nil {
@ -1202,8 +1251,14 @@ func (l *Lending) ProcessRecallLendingTrade(lendingStateDB *lendingstate.Lending
if err != nil { if err != nil {
return err, true, nil return err, true, nil
} }
lendingstate.AddTokenBalance(lendingTrade.Borrower, recallAmount, lendingTrade.CollateralToken, statedb) err = lendingstate.AddTokenBalance(lendingTrade.Borrower, recallAmount, lendingTrade.CollateralToken, statedb)
lendingstate.SubTokenBalance(common.HexToAddress(common.LendingLockAddress), recallAmount, lendingTrade.CollateralToken, statedb) if err != nil {
log.Warn("ProcessRecallLendingTrade AddTokenBalance", "err", err, "lendingTrade.Borrower", lendingTrade.Borrower, "recallAmount", *recallAmount, "lendingTrade.CollateralToken", lendingTrade.CollateralToken)
}
err = lendingstate.SubTokenBalance(common.HexToAddress(common.LendingLockAddress), recallAmount, lendingTrade.CollateralToken, statedb)
if err != nil {
log.Warn("ProcessRecallLendingTrade SubTokenBalance", "err", err, "LendingLockAddress", common.HexToAddress(common.LendingLockAddress), "recallAmount", *recallAmount, "lendingTrade.CollateralToken", lendingTrade.CollateralToken)
}
lendingStateDB.UpdateLiquidationPrice(lendingBook, lendingTrade.TradeId, newLiquidationPrice) lendingStateDB.UpdateLiquidationPrice(lendingBook, lendingTrade.TradeId, newLiquidationPrice)
lendingStateDB.UpdateCollateralLockedAmount(lendingBook, lendingTrade.TradeId, newLockedAmount) lendingStateDB.UpdateCollateralLockedAmount(lendingBook, lendingTrade.TradeId, newLockedAmount)

View file

@ -20,7 +20,6 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"errors" "errors"
"io" "io"
"io/ioutil"
"github.com/XinFinOrg/XDPoSChain/accounts/keystore" "github.com/XinFinOrg/XDPoSChain/accounts/keystore"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
@ -31,7 +30,7 @@ import (
// NewTransactor is a utility method to easily create a transaction signer from // NewTransactor is a utility method to easily create a transaction signer from
// an encrypted json key stream and the associated passphrase. // an encrypted json key stream and the associated passphrase.
func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) { func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) {
json, err := ioutil.ReadAll(keyin) json, err := io.ReadAll(keyin)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -20,7 +20,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"math/big" "math/big"
"os" "os"
"sync" "sync"
@ -76,7 +75,7 @@ type SimulatedBackend struct {
func SimulateWalletAddressAndSignFn() (common.Address, func(account accounts.Account, hash []byte) ([]byte, error), error) { func SimulateWalletAddressAndSignFn() (common.Address, func(account accounts.Account, hash []byte) ([]byte, error), error) {
veryLightScryptN := 2 veryLightScryptN := 2
veryLightScryptP := 1 veryLightScryptP := 1
dir, _ := ioutil.TempDir("", "eth-SimulateWalletAddressAndSignFn-test") dir, _ := os.MkdirTemp("", "eth-SimulateWalletAddressAndSignFn-test")
new := func(kd string) *keystore.KeyStore { new := func(kd string) *keystore.KeyStore {
return keystore.NewKeyStore(kd, veryLightScryptN, veryLightScryptP) return keystore.NewKeyStore(kd, veryLightScryptN, veryLightScryptP)

View file

@ -18,7 +18,6 @@ package bind
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -831,7 +830,7 @@ func TestBindings(t *testing.T) {
t.Skip("symlinked environment doesn't support bind (https://github.com/golang/go/issues/14845)") t.Skip("symlinked environment doesn't support bind (https://github.com/golang/go/issues/14845)")
} }
// Create a temporary workspace for the test suite // Create a temporary workspace for the test suite
ws, err := ioutil.TempDir("", "") ws, err := os.MkdirTemp("", "")
if err != nil { if err != nil {
t.Fatalf("failed to create temporary workspace: %v", err) t.Fatalf("failed to create temporary workspace: %v", err)
} }
@ -848,7 +847,7 @@ func TestBindings(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("test %d: failed to generate binding: %v", i, err) t.Fatalf("test %d: failed to generate binding: %v", i, err)
} }
if err = ioutil.WriteFile(filepath.Join(pkg, strings.ToLower(tt.name)+".go"), []byte(bind), 0600); err != nil { if err = os.WriteFile(filepath.Join(pkg, strings.ToLower(tt.name)+".go"), []byte(bind), 0600); err != nil {
t.Fatalf("test %d: failed to write binding: %v", i, err) t.Fatalf("test %d: failed to write binding: %v", i, err)
} }
// Generate the test file with the injected test code // Generate the test file with the injected test code
@ -857,7 +856,7 @@ func TestBindings(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("test %d: failed to generate tests: %v", i, err) t.Fatalf("test %d: failed to generate tests: %v", i, err)
} }
if err := ioutil.WriteFile(filepath.Join(pkg, strings.ToLower(tt.name)+"_test.go"), blob, 0600); err != nil { if err := os.WriteFile(filepath.Join(pkg, strings.ToLower(tt.name)+"_test.go"), blob, 0600); err != nil {
t.Fatalf("test %d: failed to write tests: %v", i, err) t.Fatalf("test %d: failed to write tests: %v", i, err)
} }
} }

View file

@ -103,7 +103,11 @@ func NewType(t string) (typ Type, err error) {
return typ, err return typ, err
} }
// parse the type and size of the abi-type. // parse the type and size of the abi-type.
parsedType := typeRegex.FindAllStringSubmatch(t, -1)[0] matches := typeRegex.FindAllStringSubmatch(t, -1)
if len(matches) == 0 {
return Type{}, fmt.Errorf("invalid type '%v'", t)
}
parsedType := matches[0]
// varSize is the size of the variable // varSize is the size of the variable
var varSize int var varSize int
if len(parsedType[3]) > 0 { if len(parsedType[3]) > 0 {

View file

@ -18,7 +18,6 @@ package keystore
import ( import (
"fmt" "fmt"
"io/ioutil"
"math/rand" "math/rand"
"os" "os"
"path/filepath" "path/filepath"
@ -381,11 +380,11 @@ func TestUpdatedKeyfileContents(t *testing.T) {
return return
} }
// needed so that modTime of `file` is different to its current value after ioutil.WriteFile // needed so that modTime of `file` is different to its current value after os.WriteFile
time.Sleep(1000 * time.Millisecond) time.Sleep(1000 * time.Millisecond)
// Now replace file contents with crap // Now replace file contents with crap
if err := ioutil.WriteFile(file, []byte("foo"), 0644); err != nil { if err := os.WriteFile(file, []byte("foo"), 0644); err != nil {
t.Fatal(err) t.Fatal(err)
return return
} }
@ -398,9 +397,9 @@ func TestUpdatedKeyfileContents(t *testing.T) {
// forceCopyFile is like cp.CopyFile, but doesn't complain if the destination exists. // forceCopyFile is like cp.CopyFile, but doesn't complain if the destination exists.
func forceCopyFile(dst, src string) error { func forceCopyFile(dst, src string) error {
data, err := ioutil.ReadFile(src) data, err := os.ReadFile(src)
if err != nil { if err != nil {
return err return err
} }
return ioutil.WriteFile(dst, data, 0644) return os.WriteFile(dst, data, 0644)
} }

View file

@ -17,7 +17,6 @@
package keystore package keystore
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -41,7 +40,7 @@ func (fc *fileCache) scan(keyDir string) (mapset.Set, mapset.Set, mapset.Set, er
t0 := time.Now() t0 := time.Now()
// List all the failes from the keystore folder // List all the failes from the keystore folder
files, err := ioutil.ReadDir(keyDir) files, err := os.ReadDir(keyDir)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }
@ -58,14 +57,18 @@ func (fc *fileCache) scan(keyDir string) (mapset.Set, mapset.Set, mapset.Set, er
for _, fi := range files { for _, fi := range files {
// Skip any non-key files from the folder // Skip any non-key files from the folder
path := filepath.Join(keyDir, fi.Name()) path := filepath.Join(keyDir, fi.Name())
if skipKeyFile(fi) { fiInfo, err := fi.Info()
if err != nil {
log.Warn("scan get FileInfo", "err", err, "path", path)
}
if skipKeyFile(fiInfo) {
log.Trace("Ignoring file on account scan", "path", path) log.Trace("Ignoring file on account scan", "path", path)
continue continue
} }
// Gather the set of all and fresly modified files // Gather the set of all and fresly modified files
all.Add(path) all.Add(path)
modified := fi.ModTime() modified := fiInfo.ModTime()
if modified.After(fc.lastMod) { if modified.After(fc.lastMod) {
mods.Add(path) mods.Add(path)
} }

View file

@ -23,7 +23,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -188,7 +187,7 @@ func writeKeyFile(file string, content []byte) error {
} }
// Atomic write: create a temporary hidden file first // Atomic write: create a temporary hidden file first
// then move it into place. TempFile assigns mode 0600. // then move it into place. TempFile assigns mode 0600.
f, err := ioutil.TempFile(filepath.Dir(file), "."+filepath.Base(file)+".tmp") f, err := os.CreateTemp(filepath.Dir(file), "."+filepath.Base(file)+".tmp")
if err != nil { if err != nil {
return err return err
} }

View file

@ -198,11 +198,19 @@ func (ks *KeyStore) Subscribe(sink chan<- accounts.WalletEvent) event.Subscripti
// forces a manual refresh (only triggers for systems where the filesystem notifier // forces a manual refresh (only triggers for systems where the filesystem notifier
// is not running). // is not running).
func (ks *KeyStore) updater() { func (ks *KeyStore) updater() {
// Create a timer for the wallet refresh cycle
timer := time.NewTimer(walletRefreshCycle)
defer timer.Stop()
for { for {
// Wait for an account update or a refresh timeout // Wait for an account update or a refresh timeout
select { select {
case <-ks.changes: case <-ks.changes:
case <-time.After(walletRefreshCycle): // Stop the timer if we receive an account update before the timer fires
if !timer.Stop() {
<-timer.C
}
case <-timer.C:
} }
// Run the wallet refresher // Run the wallet refresher
ks.refreshWallets() ks.refreshWallets()
@ -215,6 +223,9 @@ func (ks *KeyStore) updater() {
return return
} }
ks.mu.Unlock() ks.mu.Unlock()
// Reset the timer for the next cycle
timer.Reset(walletRefreshCycle)
} }
} }

View file

@ -33,7 +33,7 @@ import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
@ -76,7 +76,7 @@ type keyStorePassphrase struct {
func (ks keyStorePassphrase) GetKey(addr common.Address, filename, auth string) (*Key, error) { func (ks keyStorePassphrase) GetKey(addr common.Address, filename, auth string) (*Key, error) {
// Load the key from the keystore and decrypt its contents // Load the key from the keystore and decrypt its contents
keyjson, err := ioutil.ReadFile(filename) keyjson, err := os.ReadFile(filename)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -17,7 +17,7 @@
package keystore package keystore
import ( import (
"io/ioutil" "os"
"testing" "testing"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
@ -30,7 +30,7 @@ const (
// Tests that a json key file can be decrypted and encrypted in multiple rounds. // Tests that a json key file can be decrypted and encrypted in multiple rounds.
func TestKeyEncryptDecrypt(t *testing.T) { func TestKeyEncryptDecrypt(t *testing.T) {
keyjson, err := ioutil.ReadFile("testdata/very-light-scrypt.json") keyjson, err := os.ReadFile("testdata/very-light-scrypt.json")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -20,7 +20,6 @@ import (
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -32,7 +31,7 @@ import (
) )
func tmpKeyStoreIface(t *testing.T, encrypted bool) (dir string, ks keyStore) { func tmpKeyStoreIface(t *testing.T, encrypted bool) (dir string, ks keyStore) {
d, err := ioutil.TempDir("", "geth-keystore-test") d, err := os.MkdirTemp("", "geth-keystore-test")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -17,7 +17,6 @@
package keystore package keystore
import ( import (
"io/ioutil"
"math/rand" "math/rand"
"os" "os"
"runtime" "runtime"
@ -375,7 +374,7 @@ func checkEvents(t *testing.T, want []walletEvent, have []walletEvent) {
} }
func tmpKeyStore(t *testing.T, encrypted bool) (string, *KeyStore) { func tmpKeyStore(t *testing.T, encrypted bool) (string, *KeyStore) {
d, err := ioutil.TempDir("", "eth-keystore-test") d, err := os.MkdirTemp("", "eth-keystore-test")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -39,7 +39,6 @@ import (
"fmt" "fmt"
"go/parser" "go/parser"
"go/token" "go/token"
"io/ioutil"
"log" "log"
"os" "os"
"os/exec" "os/exec"
@ -149,7 +148,7 @@ func doInstall(cmdline []string) {
goinstall.Args = append(goinstall.Args, packages...) goinstall.Args = append(goinstall.Args, packages...)
build.MustRun(goinstall) build.MustRun(goinstall)
if cmds, err := ioutil.ReadDir("cmd"); err == nil { if cmds, err := os.ReadDir("cmd"); err == nil {
for _, cmd := range cmds { for _, cmd := range cmds {
pkgs, err := parser.ParseDir(token.NewFileSet(), filepath.Join(".", "cmd", cmd.Name()), nil, parser.PackageClauseOnly) pkgs, err := parser.ParseDir(token.NewFileSet(), filepath.Join(".", "cmd", cmd.Name()), nil, parser.PackageClauseOnly)
if err != nil { if err != nil {

View file

@ -10,4 +10,4 @@ eu_west_1_end=72
# Sydney # Sydney
ap_southeast_2_start=73 ap_southeast_2_start=73
ap_southeast_2_end=110 ap_southeast_2_end=108

View file

@ -23,7 +23,6 @@ module "us-east-2" {
devnetNodeKeys = local.devnetNodeKeys["us-east-2"] devnetNodeKeys = local.devnetNodeKeys["us-east-2"]
logLevel = local.logLevel logLevel = local.logLevel
devnet_xdc_ecs_tasks_execution_role_arn = aws_iam_role.devnet_xdc_ecs_tasks_execution_role.arn devnet_xdc_ecs_tasks_execution_role_arn = aws_iam_role.devnet_xdc_ecs_tasks_execution_role.arn
providers = { providers = {
aws = aws.us-east-2 aws = aws.us-east-2
} }
@ -40,7 +39,6 @@ module "eu-west-1" {
devnetNodeKeys = local.devnetNodeKeys["eu-west-1"] devnetNodeKeys = local.devnetNodeKeys["eu-west-1"]
logLevel = local.logLevel logLevel = local.logLevel
devnet_xdc_ecs_tasks_execution_role_arn = aws_iam_role.devnet_xdc_ecs_tasks_execution_role.arn devnet_xdc_ecs_tasks_execution_role_arn = aws_iam_role.devnet_xdc_ecs_tasks_execution_role.arn
providers = { providers = {
aws = aws.eu-west-1 aws = aws.eu-west-1
} }
@ -57,8 +55,27 @@ module "ap-southeast-2" {
devnetNodeKeys = local.devnetNodeKeys["ap-southeast-2"] devnetNodeKeys = local.devnetNodeKeys["ap-southeast-2"]
logLevel = local.logLevel logLevel = local.logLevel
devnet_xdc_ecs_tasks_execution_role_arn = aws_iam_role.devnet_xdc_ecs_tasks_execution_role.arn devnet_xdc_ecs_tasks_execution_role_arn = aws_iam_role.devnet_xdc_ecs_tasks_execution_role.arn
providers = { providers = {
aws = aws.ap-southeast-2 aws = aws.ap-southeast-2
} }
} }
# WARNING: APSE-1 will only be used to host rpc node
# Workaround to avoid conflicts with existing ecs cluster in existing regions
provider "aws" {
alias = "ap-southeast-1"
region = "ap-southeast-1"
}
module "ap-southeast-1-rpc" {
source = "./module/region"
region = "ap-southeast-1"
devnetNodeKeys = local.rpcNodeKeys
enableFixedIp = true
logLevel = local.logLevel
devnet_xdc_ecs_tasks_execution_role_arn = aws_iam_role.devnet_xdc_ecs_tasks_execution_role.arn
providers = {
aws = aws.ap-southeast-1
}
}

View file

@ -56,15 +56,17 @@ data "aws_ecs_task_definition" "devnet_ecs_task_definition" {
task_definition = aws_ecs_task_definition.devnet_task_definition_group[each.key].family task_definition = aws_ecs_task_definition.devnet_task_definition_group[each.key].family
} }
# ECS cluster
resource "aws_ecs_cluster" "devnet_ecs_cluster" { resource "aws_ecs_cluster" "devnet_ecs_cluster" {
name = "devnet-xdcnode-cluster" name = "devnet-xdcnode-cluster"
tags = { tags = {
Name = "TfDevnetEcsCluster" Name = "TfDevnetEcsCluster"
} }
} }
resource "aws_ecs_service" "devnet_ecs_service" { resource "aws_ecs_service" "devnet_ecs_service" {
for_each = var.devnetNodeKeys for_each = var.enableFixedIp ? {} : var.devnetNodeKeys
name = "ecs-service-${each.key}" name = "ecs-service-${each.key}"
cluster = aws_ecs_cluster.devnet_ecs_cluster.id cluster = aws_ecs_cluster.devnet_ecs_cluster.id
task_definition = "${aws_ecs_task_definition.devnet_task_definition_group[each.key].family}:${max(aws_ecs_task_definition.devnet_task_definition_group[each.key].revision, data.aws_ecs_task_definition.devnet_ecs_task_definition[each.key].revision)}" task_definition = "${aws_ecs_task_definition.devnet_task_definition_group[each.key].family}:${max(aws_ecs_task_definition.devnet_task_definition_group[each.key].revision, data.aws_ecs_task_definition.devnet_ecs_task_definition[each.key].revision)}"

View file

@ -72,6 +72,14 @@ resource "aws_default_security_group" "devnet_xdcnode_security_group" {
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
ingress {
description = "rpc port"
from_port = 8545
to_port = 8545
protocol = "tcp"
cidr_blocks = ["10.0.0.0/16"]
}
egress { egress {
from_port = 0 from_port = 0
to_port = 0 to_port = 0

View file

@ -0,0 +1,104 @@
# Allocate an Elastic IP for the NLB
resource "aws_eip" "nlb_eip" {
domain = "vpc"
}
# Create a Network Load Balancer
resource "aws_lb" "rpc_node_nlb" {
count = var.enableFixedIp ? 1 : 0
name = "rpc-node-nlb"
load_balancer_type = "network"
enable_deletion_protection = false
subnet_mapping {
subnet_id = aws_subnet.devnet_subnet.id
allocation_id = aws_eip.nlb_eip.id
}
}
# Listener and Target Group for the rpc node container
resource "aws_lb_target_group" "rpc_node_tg_8545" {
count = var.enableFixedIp ? 1 : 0
name = "rpc-node-tg"
port = 8545
protocol = "TCP"
vpc_id = aws_vpc.devnet_vpc.id
target_type = "ip"
}
resource "aws_lb_listener" "rpc_node_listener_8545" {
count = var.enableFixedIp ? 1 : 0
load_balancer_arn = aws_lb.rpc_node_nlb[0].arn
port = 8545
protocol = "TCP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.rpc_node_tg_8545[0].arn
}
}
resource "aws_ecs_service" "devnet_rpc_node_ecs_service" {
for_each = var.enableFixedIp ? var.devnetNodeKeys : {}
name = "ecs-service-${each.key}"
cluster = aws_ecs_cluster.devnet_ecs_cluster.id
task_definition = "${aws_ecs_task_definition.devnet_task_definition_group[each.key].family}:${max(aws_ecs_task_definition.devnet_task_definition_group[each.key].revision, data.aws_ecs_task_definition.devnet_ecs_task_definition[each.key].revision)}"
launch_type = "FARGATE"
scheduling_strategy = "REPLICA"
desired_count = 1
force_new_deployment = true
deployment_minimum_healthy_percent = 0
deployment_maximum_percent = 100
network_configuration {
subnets = [aws_subnet.devnet_subnet.id]
assign_public_ip = true
security_groups = [
aws_default_security_group.devnet_xdcnode_security_group.id
]
}
deployment_circuit_breaker {
enable = true
rollback = false
}
load_balancer {
target_group_arn = aws_lb_target_group.rpc_node_tg_8545[0].arn
container_name = "tfXdcNode"
container_port = 8545
}
depends_on = [
aws_lb_listener.rpc_node_listener_8545
]
tags = {
Name = "TfDevnetRpcNodeEcsService-${each.key}"
}
}
# Target Group for port 30303
resource "aws_lb_target_group" "rpc_node_tg_30303" {
count = var.enableFixedIp ? 1 : 0
name = "rpc-node-tg-30303"
port = 30303
protocol = "TCP"
vpc_id = aws_vpc.devnet_vpc.id
target_type = "ip"
}
# Listener for port 30303
resource "aws_lb_listener" "rpc_node_listener_30303" {
count = var.enableFixedIp ? 1 : 0
load_balancer_arn = aws_lb.rpc_node_nlb[0].arn
port = 30303
protocol = "TCP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.rpc_node_tg_30303[0].arn
}
}

View file

@ -17,3 +17,9 @@ variable "devnet_xdc_ecs_tasks_execution_role_arn" {
description = "aws iam role resource arn" description = "aws iam role resource arn"
type = string type = string
} }
variable "enableFixedIp" {
description = "a flag to indicate whether fixed ip should be associated to the nodes. This is used for RPC node"
type = bool
default = false
}

View file

@ -40,5 +40,7 @@ locals {
r.name => { for i in local.keyNames[r.name]: i => local.predefinedNodesConfig[i] } r.name => { for i in local.keyNames[r.name]: i => local.predefinedNodesConfig[i] }
} }
rpcNodeKeys = { "rpc1": local.predefinedNodesConfig["rpc1"]} // we hardcode the rpc to a single node for now
s3BucketName = "tf-devnet-bucket" s3BucketName = "tf-devnet-bucket"
} }

View file

@ -18,7 +18,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "os"
"github.com/XinFinOrg/XDPoSChain/accounts" "github.com/XinFinOrg/XDPoSChain/accounts"
"github.com/XinFinOrg/XDPoSChain/accounts/keystore" "github.com/XinFinOrg/XDPoSChain/accounts/keystore"
@ -340,7 +340,7 @@ func importWallet(ctx *cli.Context) error {
if len(keyfile) == 0 { if len(keyfile) == 0 {
utils.Fatalf("keyfile must be given as argument") utils.Fatalf("keyfile must be given as argument")
} }
keyJson, err := ioutil.ReadFile(keyfile) keyJson, err := os.ReadFile(keyfile)
if err != nil { if err != nil {
utils.Fatalf("Could not read wallet file: %v", err) utils.Fatalf("Could not read wallet file: %v", err)
} }

View file

@ -17,7 +17,7 @@
package main package main
import ( import (
"io/ioutil" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
@ -115,7 +115,7 @@ Passphrase: {{.InputLine "foo"}}
Address: {xdcd4584b5f6229b7be90727b0fc8c6b91bb427821f} Address: {xdcd4584b5f6229b7be90727b0fc8c6b91bb427821f}
`) `)
files, err := ioutil.ReadDir(filepath.Join(XDC.Datadir, "keystore")) files, err := os.ReadDir(filepath.Join(XDC.Datadir, "keystore"))
if len(files) != 1 { if len(files) != 1 {
t.Errorf("expected one key file in keystore directory, found %d files (error: %v)", len(files), err) t.Errorf("expected one key file in keystore directory, found %d files (error: %v)", len(files), err)
} }

View file

@ -20,8 +20,8 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/url" "net/url"
"os"
"os/exec" "os/exec"
"runtime" "runtime"
"strings" "strings"
@ -74,7 +74,7 @@ func printOSDetails(w io.Writer) {
case "openbsd", "netbsd", "freebsd", "dragonfly": case "openbsd", "netbsd", "freebsd", "dragonfly":
printCmdOut(w, "uname -v: ", "uname", "-v") printCmdOut(w, "uname -v: ", "uname", "-v")
case "solaris": case "solaris":
out, err := ioutil.ReadFile("/etc/release") out, err := os.ReadFile("/etc/release")
if err == nil { if err == nil {
fmt.Fprintf(w, "/etc/release: %s\n", out) fmt.Fprintf(w, "/etc/release: %s\n", out)
} else { } else {

View file

@ -17,13 +17,13 @@
package main package main
import ( import (
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
"io/ioutil"
"math/big" "math/big"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core" "github.com/XinFinOrg/XDPoSChain/core"
) )
@ -108,7 +108,7 @@ func testDAOForkBlockNewChain(t *testing.T, test int, genesis string, expectBloc
// Start a XDC instance with the requested flags set and immediately terminate // Start a XDC instance with the requested flags set and immediately terminate
if genesis != "" { if genesis != "" {
json := filepath.Join(datadir, "genesis.json") json := filepath.Join(datadir, "genesis.json")
if err := ioutil.WriteFile(json, []byte(genesis), 0600); err != nil { if err := os.WriteFile(json, []byte(genesis), 0600); err != nil {
t.Fatalf("test %d: failed to write genesis file: %v", test, err) t.Fatalf("test %d: failed to write genesis file: %v", test, err)
} }
runXDC(t, "--datadir", datadir, "init", json).WaitExit() runXDC(t, "--datadir", datadir, "init", json).WaitExit()

View file

@ -18,7 +18,6 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"testing" "testing"
@ -27,7 +26,7 @@ import (
) )
func tmpdir(t *testing.T) string { func tmpdir(t *testing.T) string {
dir, err := ioutil.TempDir("", "XDC-test") dir, err := os.MkdirTemp("", "XDC-test")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -20,7 +20,6 @@ import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"strings" "strings"
@ -100,7 +99,7 @@ func main() {
} }
} else { } else {
// Otherwise load up the ABI, optional bytecode and type name from the parameters // Otherwise load up the ABI, optional bytecode and type name from the parameters
abi, err := ioutil.ReadFile(*abiFlag) abi, err := os.ReadFile(*abiFlag)
if err != nil { if err != nil {
fmt.Printf("Failed to read input ABI: %v\n", err) fmt.Printf("Failed to read input ABI: %v\n", err)
os.Exit(-1) os.Exit(-1)
@ -109,7 +108,7 @@ func main() {
bin := []byte{} bin := []byte{}
if *binFlag != "" { if *binFlag != "" {
if bin, err = ioutil.ReadFile(*binFlag); err != nil { if bin, err = os.ReadFile(*binFlag); err != nil {
fmt.Printf("Failed to read input bytecode: %v\n", err) fmt.Printf("Failed to read input bytecode: %v\n", err)
os.Exit(-1) os.Exit(-1)
} }
@ -133,7 +132,7 @@ func main() {
fmt.Printf("%s\n", code) fmt.Printf("%s\n", code)
return return
} }
if err := ioutil.WriteFile(*outFlag, []byte(code), 0600); err != nil { if err := os.WriteFile(*outFlag, []byte(code), 0600); err != nil {
fmt.Printf("Failed to write ABI binding: %v\n", err) fmt.Printf("Failed to write ABI binding: %v\n", err)
os.Exit(-1) os.Exit(-1)
} }

View file

@ -19,7 +19,6 @@ package main
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -100,7 +99,7 @@ If you want to encrypt an existing private key, it can be specified by setting
if err := os.MkdirAll(filepath.Dir(keyfilepath), 0700); err != nil { if err := os.MkdirAll(filepath.Dir(keyfilepath), 0700); err != nil {
utils.Fatalf("Could not create directory %s", filepath.Dir(keyfilepath)) utils.Fatalf("Could not create directory %s", filepath.Dir(keyfilepath))
} }
if err := ioutil.WriteFile(keyfilepath, keyjson, 0600); err != nil { if err := os.WriteFile(keyfilepath, keyjson, 0600); err != nil {
utils.Fatalf("Failed to write keyfile to %s: %v", keyfilepath, err) utils.Fatalf("Failed to write keyfile to %s: %v", keyfilepath, err)
} }

View file

@ -19,7 +19,7 @@ package main
import ( import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io/ioutil" "os"
"github.com/XinFinOrg/XDPoSChain/accounts/keystore" "github.com/XinFinOrg/XDPoSChain/accounts/keystore"
"github.com/XinFinOrg/XDPoSChain/cmd/utils" "github.com/XinFinOrg/XDPoSChain/cmd/utils"
@ -54,7 +54,7 @@ make sure to use this feature with great caution!`,
keyfilepath := ctx.Args().First() keyfilepath := ctx.Args().First()
// Read key from file. // Read key from file.
keyjson, err := ioutil.ReadFile(keyfilepath) keyjson, err := os.ReadFile(keyfilepath)
if err != nil { if err != nil {
utils.Fatalf("Failed to read the keyfile at '%s': %v", keyfilepath, err) utils.Fatalf("Failed to read the keyfile at '%s': %v", keyfilepath, err)
} }

View file

@ -19,7 +19,7 @@ package main
import ( import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io/ioutil" "os"
"github.com/XinFinOrg/XDPoSChain/accounts/keystore" "github.com/XinFinOrg/XDPoSChain/accounts/keystore"
"github.com/XinFinOrg/XDPoSChain/cmd/utils" "github.com/XinFinOrg/XDPoSChain/cmd/utils"
@ -56,7 +56,7 @@ To sign a message contained in a file, use the --msgfile flag.
// Load the keyfile. // Load the keyfile.
keyfilepath := ctx.Args().First() keyfilepath := ctx.Args().First()
keyjson, err := ioutil.ReadFile(keyfilepath) keyjson, err := os.ReadFile(keyfilepath)
if err != nil { if err != nil {
utils.Fatalf("Failed to read the keyfile at '%s': %v", keyfilepath, err) utils.Fatalf("Failed to read the keyfile at '%s': %v", keyfilepath, err)
} }
@ -146,7 +146,7 @@ func getMessage(ctx *cli.Context, msgarg int) []byte {
if len(ctx.Args()) > msgarg { if len(ctx.Args()) > msgarg {
utils.Fatalf("Can't use --msgfile and message argument at the same time.") utils.Fatalf("Can't use --msgfile and message argument at the same time.")
} }
msg, err := ioutil.ReadFile(file) msg, err := os.ReadFile(file)
if err != nil { if err != nil {
utils.Fatalf("Can't read message file: %v", err) utils.Fatalf("Can't read message file: %v", err)
} }

View file

@ -17,14 +17,13 @@
package main package main
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
) )
func TestMessageSignVerify(t *testing.T) { func TestMessageSignVerify(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "ethkey-test") tmpdir, err := os.MkdirTemp("", "ethkey-test")
if err != nil { if err != nil {
t.Fatal("Can't create temporary directory:", err) t.Fatal("Can't create temporary directory:", err)
} }

View file

@ -19,7 +19,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "os"
"strings" "strings"
"github.com/XinFinOrg/XDPoSChain/cmd/utils" "github.com/XinFinOrg/XDPoSChain/cmd/utils"
@ -35,7 +35,7 @@ func getPassPhrase(ctx *cli.Context, confirmation bool) string {
// Look for the --passphrase flag. // Look for the --passphrase flag.
passphraseFile := ctx.String(passphraseFlag.Name) passphraseFile := ctx.String(passphraseFlag.Name)
if passphraseFile != "" { if passphraseFile != "" {
content, err := ioutil.ReadFile(passphraseFile) content, err := os.ReadFile(passphraseFile)
if err != nil { if err != nil {
utils.Fatalf("Failed to read passphrase file '%s': %v", utils.Fatalf("Failed to read passphrase file '%s': %v",
passphraseFile, err) passphraseFile, err)
@ -64,7 +64,8 @@ func getPassPhrase(ctx *cli.Context, confirmation bool) string {
// that can be safely used to calculate a signature from. // that can be safely used to calculate a signature from.
// //
// The hash is calulcated as // The hash is calulcated as
// keccak256("\x19Ethereum Signed Message:\n"${message length}${message}). //
// keccak256("\x19Ethereum Signed Message:\n"${message length}${message}).
// //
// This gives context to the signed message and prevents signing of transactions. // This gives context to the signed message and prevents signing of transactions.
func signHash(data []byte) []byte { func signHash(data []byte) []byte {

View file

@ -19,7 +19,7 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "os"
"github.com/XinFinOrg/XDPoSChain/cmd/evm/internal/compiler" "github.com/XinFinOrg/XDPoSChain/cmd/evm/internal/compiler"
@ -41,7 +41,7 @@ func compileCmd(ctx *cli.Context) error {
} }
fn := ctx.Args().First() fn := ctx.Args().First()
src, err := ioutil.ReadFile(fn) src, err := os.ReadFile(fn)
if err != nil { if err != nil {
return err return err
} }

View file

@ -19,7 +19,7 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "os"
"strings" "strings"
"github.com/XinFinOrg/XDPoSChain/core/asm" "github.com/XinFinOrg/XDPoSChain/core/asm"
@ -39,7 +39,7 @@ func disasmCmd(ctx *cli.Context) error {
} }
fn := ctx.Args().First() fn := ctx.Args().First()
in, err := ioutil.ReadFile(fn) in, err := os.ReadFile(fn)
if err != nil { if err != nil {
return err return err
} }

View file

@ -20,12 +20,13 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/XinFinOrg/XDPoSChain/core/rawdb" "io"
"io/ioutil"
"os" "os"
"runtime/pprof" "runtime/pprof"
"time" "time"
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
goruntime "runtime" goruntime "runtime"
"github.com/XinFinOrg/XDPoSChain/cmd/evm/internal/compiler" "github.com/XinFinOrg/XDPoSChain/cmd/evm/internal/compiler"
@ -125,13 +126,13 @@ func runCmd(ctx *cli.Context) error {
// If - is specified, it means that code comes from stdin // If - is specified, it means that code comes from stdin
if ctx.GlobalString(CodeFileFlag.Name) == "-" { if ctx.GlobalString(CodeFileFlag.Name) == "-" {
//Try reading from stdin //Try reading from stdin
if hexcode, err = ioutil.ReadAll(os.Stdin); err != nil { if hexcode, err = io.ReadAll(os.Stdin); err != nil {
fmt.Printf("Could not load code from stdin: %v\n", err) fmt.Printf("Could not load code from stdin: %v\n", err)
os.Exit(1) os.Exit(1)
} }
} else { } else {
// Codefile with hex assembly // Codefile with hex assembly
if hexcode, err = ioutil.ReadFile(ctx.GlobalString(CodeFileFlag.Name)); err != nil { if hexcode, err = os.ReadFile(ctx.GlobalString(CodeFileFlag.Name)); err != nil {
fmt.Printf("Could not load code from file: %v\n", err) fmt.Printf("Could not load code from file: %v\n", err)
os.Exit(1) os.Exit(1)
} }
@ -142,7 +143,7 @@ func runCmd(ctx *cli.Context) error {
code = common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name)) code = common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name))
} else if fn := ctx.Args().First(); len(fn) > 0 { } else if fn := ctx.Args().First(); len(fn) > 0 {
// EASM-file to compile // EASM-file to compile
src, err := ioutil.ReadFile(fn) src, err := os.ReadFile(fn)
if err != nil { if err != nil {
return err return err
} }

View file

@ -20,7 +20,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"github.com/XinFinOrg/XDPoSChain/core/state" "github.com/XinFinOrg/XDPoSChain/core/state"
@ -76,7 +75,7 @@ func stateTestCmd(ctx *cli.Context) error {
debugger = vm.NewStructLogger(config) debugger = vm.NewStructLogger(config)
} }
// Load the test content from the input file // Load the test content from the input file
src, err := ioutil.ReadFile(ctx.Args().First()) src, err := os.ReadFile(ctx.Args().First())
if err != nil { if err != nil {
return err return err
} }

View file

@ -28,7 +28,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"html/template" "html/template"
"io/ioutil" "io"
"math" "math"
"math/big" "math/big"
"net/http" "net/http"
@ -139,7 +139,7 @@ func main() {
log.Crit("Failed to render the faucet template", "err", err) log.Crit("Failed to render the faucet template", "err", err)
} }
// Load and parse the genesis block requested by the user // Load and parse the genesis block requested by the user
blob, err := ioutil.ReadFile(*genesisFlag) blob, err := os.ReadFile(*genesisFlag)
if err != nil { if err != nil {
log.Crit("Failed to read genesis block contents", "genesis", *genesisFlag, "err", err) log.Crit("Failed to read genesis block contents", "genesis", *genesisFlag, "err", err)
} }
@ -157,13 +157,13 @@ func main() {
} }
} }
// Load up the account key and decrypt its password // Load up the account key and decrypt its password
if blob, err = ioutil.ReadFile(*accPassFlag); err != nil { if blob, err = os.ReadFile(*accPassFlag); err != nil {
log.Crit("Failed to read account password contents", "file", *accPassFlag, "err", err) log.Crit("Failed to read account password contents", "file", *accPassFlag, "err", err)
} }
pass := string(blob) pass := string(blob)
ks := keystore.NewKeyStore(filepath.Join(os.Getenv("HOME"), ".faucet", "keys"), keystore.StandardScryptN, keystore.StandardScryptP) ks := keystore.NewKeyStore(filepath.Join(os.Getenv("HOME"), ".faucet", "keys"), keystore.StandardScryptN, keystore.StandardScryptP)
if blob, err = ioutil.ReadFile(*accJSONFlag); err != nil { if blob, err = os.ReadFile(*accJSONFlag); err != nil {
log.Crit("Failed to read account key contents", "file", *accJSONFlag, "err", err) log.Crit("Failed to read account key contents", "file", *accJSONFlag, "err", err)
} }
acc, err := ks.Import(blob, pass, pass) acc, err := ks.Import(blob, pass, pass)
@ -729,7 +729,7 @@ func authTwitter(url string) (string, string, common.Address, error) {
} }
username := parts[len(parts)-3] username := parts[len(parts)-3]
body, err := ioutil.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
return "", "", common.Address{}, err return "", "", common.Address{}, err
} }
@ -763,7 +763,7 @@ func authGooglePlus(url string) (string, string, common.Address, error) {
} }
defer res.Body.Close() defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
return "", "", common.Address{}, err return "", "", common.Address{}, err
} }
@ -797,7 +797,7 @@ func authFacebook(url string) (string, string, common.Address, error) {
} }
defer res.Body.Close() defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
return "", "", common.Address{}, err return "", "", common.Address{}, err
} }

View file

@ -1,15 +1,15 @@
// Code generated by go-bindata. DO NOT EDIT. // Code generated by go-bindata. DO NOT EDIT.
// sources: // sources:
// faucet.html // faucet.html (11.726kB)
package main package main
import ( import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"crypto/sha256"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -19,7 +19,7 @@ import (
func bindataRead(data []byte, name string) ([]byte, error) { func bindataRead(data []byte, name string) ([]byte, error) {
gz, err := gzip.NewReader(bytes.NewBuffer(data)) gz, err := gzip.NewReader(bytes.NewBuffer(data))
if err != nil { if err != nil {
return nil, fmt.Errorf("Read %q: %v", name, err) return nil, fmt.Errorf("read %q: %w", name, err)
} }
var buf bytes.Buffer var buf bytes.Buffer
@ -27,7 +27,7 @@ func bindataRead(data []byte, name string) ([]byte, error) {
clErr := gz.Close() clErr := gz.Close()
if err != nil { if err != nil {
return nil, fmt.Errorf("Read %q: %v", name, err) return nil, fmt.Errorf("read %q: %w", name, err)
} }
if clErr != nil { if clErr != nil {
return nil, err return nil, err
@ -37,8 +37,9 @@ func bindataRead(data []byte, name string) ([]byte, error) {
} }
type asset struct { type asset struct {
bytes []byte bytes []byte
info os.FileInfo info os.FileInfo
digest [sha256.Size]byte
} }
type bindataFileInfo struct { type bindataFileInfo struct {
@ -83,7 +84,7 @@ func faucetHtml() (*asset, error) {
} }
info := bindataFileInfo{name: "faucet.html", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)} info := bindataFileInfo{name: "faucet.html", size: 0, mode: os.FileMode(0), modTime: time.Unix(0, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info, digest: [32]uint8{0x9b, 0x9e, 0x78, 0xa7, 0x98, 0x53, 0xe5, 0xcc, 0x18, 0xfc, 0x7e, 0x9a, 0xb2, 0xa6, 0xa5, 0x82, 0xbc, 0x1e, 0xb5, 0x94, 0xa5, 0x5d, 0xfe, 0x42, 0x78, 0x75, 0x6d, 0x98, 0x6d, 0x26, 0x43, 0x8d}}
return a, nil return a, nil
} }
@ -102,6 +103,12 @@ func Asset(name string) ([]byte, error) {
return nil, fmt.Errorf("Asset %s not found", name) return nil, fmt.Errorf("Asset %s not found", name)
} }
// AssetString returns the asset contents as a string (instead of a []byte).
func AssetString(name string) (string, error) {
data, err := Asset(name)
return string(data), err
}
// MustAsset is like Asset but panics when Asset would return an error. // MustAsset is like Asset but panics when Asset would return an error.
// It simplifies safe initialization of global variables. // It simplifies safe initialization of global variables.
func MustAsset(name string) []byte { func MustAsset(name string) []byte {
@ -113,6 +120,12 @@ func MustAsset(name string) []byte {
return a return a
} }
// MustAssetString is like AssetString but panics when Asset would return an
// error. It simplifies safe initialization of global variables.
func MustAssetString(name string) string {
return string(MustAsset(name))
}
// AssetInfo loads and returns the asset info for the given name. // AssetInfo loads and returns the asset info for the given name.
// It returns an error if the asset could not be found or // It returns an error if the asset could not be found or
// could not be loaded. // could not be loaded.
@ -128,6 +141,33 @@ func AssetInfo(name string) (os.FileInfo, error) {
return nil, fmt.Errorf("AssetInfo %s not found", name) return nil, fmt.Errorf("AssetInfo %s not found", name)
} }
// AssetDigest returns the digest of the file with the given name. It returns an
// error if the asset could not be found or the digest could not be loaded.
func AssetDigest(name string) ([sha256.Size]byte, error) {
canonicalName := strings.Replace(name, "\\", "/", -1)
if f, ok := _bindata[canonicalName]; ok {
a, err := f()
if err != nil {
return [sha256.Size]byte{}, fmt.Errorf("AssetDigest %s can't read by error: %v", name, err)
}
return a.digest, nil
}
return [sha256.Size]byte{}, fmt.Errorf("AssetDigest %s not found", name)
}
// Digests returns a map of all known files and their checksums.
func Digests() (map[string][sha256.Size]byte, error) {
mp := make(map[string][sha256.Size]byte, len(_bindata))
for name := range _bindata {
a, err := _bindata[name]()
if err != nil {
return nil, err
}
mp[name] = a.digest
}
return mp, nil
}
// AssetNames returns the names of the assets. // AssetNames returns the names of the assets.
func AssetNames() []string { func AssetNames() []string {
names := make([]string, 0, len(_bindata)) names := make([]string, 0, len(_bindata))
@ -142,18 +182,23 @@ var _bindata = map[string]func() (*asset, error){
"faucet.html": faucetHtml, "faucet.html": faucetHtml,
} }
// AssetDebug is true if the assets were built with the debug flag enabled.
const AssetDebug = false
// AssetDir returns the file names below a certain // AssetDir returns the file names below a certain
// directory embedded in the file by go-bindata. // directory embedded in the file by go-bindata.
// For example if you run go-bindata on data/... and data contains the // For example if you run go-bindata on data/... and data contains the
// following hierarchy: // following hierarchy:
// data/ //
// foo.txt // data/
// img/ // foo.txt
// a.png // img/
// b.png // a.png
// then AssetDir("data") would return []string{"foo.txt", "img"} // b.png
// AssetDir("data/img") would return []string{"a.png", "b.png"} //
// AssetDir("foo.txt") and AssetDir("notexist") would return an error // then AssetDir("data") would return []string{"foo.txt", "img"},
// AssetDir("data/img") would return []string{"a.png", "b.png"},
// AssetDir("foo.txt") and AssetDir("notexist") would return an error, and
// AssetDir("") will return []string{"data"}. // AssetDir("") will return []string{"data"}.
func AssetDir(name string) ([]string, error) { func AssetDir(name string) ([]string, error) {
node := _bintree node := _bintree
@ -186,7 +231,7 @@ var _bintree = &bintree{nil, map[string]*bintree{
"faucet.html": {faucetHtml, map[string]*bintree{}}, "faucet.html": {faucetHtml, map[string]*bintree{}},
}} }}
// RestoreAsset restores an asset under the given directory // RestoreAsset restores an asset under the given directory.
func RestoreAsset(dir, name string) error { func RestoreAsset(dir, name string) error {
data, err := Asset(name) data, err := Asset(name)
if err != nil { if err != nil {
@ -200,14 +245,14 @@ func RestoreAsset(dir, name string) error {
if err != nil { if err != nil {
return err return err
} }
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode()) err = os.WriteFile(_filePath(dir, name), data, info.Mode())
if err != nil { if err != nil {
return err return err
} }
return os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime()) return os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime())
} }
// RestoreAssets restores an asset under the given directory recursively // RestoreAssets restores an asset under the given directory recursively.
func RestoreAssets(dir, name string) error { func RestoreAssets(dir, name string) error {
children, err := AssetDir(name) children, err := AssetDir(name)
// File // File

View file

@ -21,7 +21,6 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"net" "net"
"os" "os"
"os/user" "os/user"
@ -72,7 +71,7 @@ func dial(server string, pubkey []byte) (*sshClient, error) {
var auths []ssh.AuthMethod var auths []ssh.AuthMethod
path := filepath.Join(user.HomeDir, ".ssh", "id_rsa") path := filepath.Join(user.HomeDir, ".ssh", "id_rsa")
if buf, err := ioutil.ReadFile(path); err != nil { if buf, err := os.ReadFile(path); err != nil {
log.Warn("No SSH key, falling back to passwords", "path", path, "err", err) log.Warn("No SSH key, falling back to passwords", "path", path, "err", err)
} else { } else {
key, err := ssh.ParsePrivateKey(buf) key, err := ssh.ParsePrivateKey(buf)

View file

@ -20,7 +20,6 @@ import (
"bufio" "bufio"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"math/big" "math/big"
"net" "net"
"os" "os"
@ -63,7 +62,7 @@ func (c config) flush() {
os.MkdirAll(filepath.Dir(c.path), 0755) os.MkdirAll(filepath.Dir(c.path), 0755)
out, _ := json.MarshalIndent(c.Genesis, "", " ") out, _ := json.MarshalIndent(c.Genesis, "", " ")
if err := ioutil.WriteFile(c.path, out, 0644); err != nil { if err := os.WriteFile(c.path, out, 0644); err != nil {
log.Warn("Failed to save puppeth configs", "file", c.path, "err", err) log.Warn("Failed to save puppeth configs", "file", c.path, "err", err)
} }
} }

View file

@ -20,8 +20,8 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"math/rand" "math/rand"
"os"
"time" "time"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
@ -419,7 +419,7 @@ func (w *wizard) manageGenesis() {
fmt.Println() fmt.Println()
fmt.Printf("Which file to save the genesis into? (default = %s.json)\n", w.network) fmt.Printf("Which file to save the genesis into? (default = %s.json)\n", w.network)
out, _ := json.MarshalIndent(w.conf.Genesis, "", " ") out, _ := json.MarshalIndent(w.conf.Genesis, "", " ")
if err := ioutil.WriteFile(w.readDefaultString(fmt.Sprintf("%s.json", w.network)), out, 0644); err != nil { if err := os.WriteFile(w.readDefaultString(fmt.Sprintf("%s.json", w.network)), out, 0644); err != nil {
log.Error("Failed to save genesis file", "err", err) log.Error("Failed to save genesis file", "err", err)
} }
log.Info("Exported existing genesis block") log.Info("Exported existing genesis block")

View file

@ -20,7 +20,6 @@ import (
"bufio" "bufio"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -76,7 +75,7 @@ func (w *wizard) run() {
// Load initial configurations and connect to all live servers // Load initial configurations and connect to all live servers
w.conf.path = filepath.Join(os.Getenv("HOME"), ".puppeth", w.network) w.conf.path = filepath.Join(os.Getenv("HOME"), ".puppeth", w.network)
blob, err := ioutil.ReadFile(w.conf.path) blob, err := os.ReadFile(w.conf.path)
if err != nil { if err != nil {
log.Warn("No previous configurations found", "path", w.conf.path) log.Warn("No previous configurations found", "path", w.conf.path)
} else if err := json.Unmarshal(blob, &w.conf); err != nil { } else if err := json.Unmarshal(blob, &w.conf); err != nil {

View file

@ -19,7 +19,6 @@ package main
import ( import (
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"testing" "testing"
@ -67,7 +66,7 @@ func TestFailsNoBzzAccount(t *testing.T) {
} }
func TestCmdLineOverrides(t *testing.T) { func TestCmdLineOverrides(t *testing.T) {
dir, err := ioutil.TempDir("", "bzztest") dir, err := os.MkdirTemp("", "bzztest")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -161,7 +160,7 @@ func TestFileOverrides(t *testing.T) {
t.Fatalf("Error creating TOML file in TestFileOverride: %v", err) t.Fatalf("Error creating TOML file in TestFileOverride: %v", err)
} }
//create file //create file
f, err := ioutil.TempFile("", "testconfig.toml") f, err := os.CreateTemp("", "testconfig.toml")
if err != nil { if err != nil {
t.Fatalf("Error writing TOML file in TestFileOverride: %v", err) t.Fatalf("Error writing TOML file in TestFileOverride: %v", err)
} }
@ -172,7 +171,7 @@ func TestFileOverrides(t *testing.T) {
} }
f.Sync() f.Sync()
dir, err := ioutil.TempDir("", "bzztest") dir, err := os.MkdirTemp("", "bzztest")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -259,7 +258,7 @@ func TestEnvVars(t *testing.T) {
envVars = append(envVars, fmt.Sprintf("%s=%s", CorsStringFlag.EnvVar, "*")) envVars = append(envVars, fmt.Sprintf("%s=%s", CorsStringFlag.EnvVar, "*"))
envVars = append(envVars, fmt.Sprintf("%s=%s", SwarmSyncEnabledFlag.EnvVar, "true")) envVars = append(envVars, fmt.Sprintf("%s=%s", SwarmSyncEnabledFlag.EnvVar, "true"))
dir, err := ioutil.TempDir("", "bzztest") dir, err := os.MkdirTemp("", "bzztest")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -368,7 +367,7 @@ func TestCmdLineOverridesFile(t *testing.T) {
t.Fatalf("Error creating TOML file in TestFileOverride: %v", err) t.Fatalf("Error creating TOML file in TestFileOverride: %v", err)
} }
//write file //write file
f, err := ioutil.TempFile("", "testconfig.toml") f, err := os.CreateTemp("", "testconfig.toml")
if err != nil { if err != nil {
t.Fatalf("Error writing TOML file in TestFileOverride: %v", err) t.Fatalf("Error writing TOML file in TestFileOverride: %v", err)
} }
@ -379,7 +378,7 @@ func TestCmdLineOverridesFile(t *testing.T) {
} }
f.Sync() f.Sync()
dir, err := ioutil.TempDir("", "bzztest") dir, err := os.MkdirTemp("", "bzztest")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -33,7 +33,7 @@ func hash(ctx *cli.Context) {
} }
f, err := os.Open(args[0]) f, err := os.Open(args[0])
if err != nil { if err != nil {
utils.Fatalf("Error opening file " + args[1]) utils.Fatalf("Error opening file " + args[0])
} }
defer f.Close() defer f.Close()

View file

@ -19,7 +19,6 @@ package main
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/signal" "os/signal"
"runtime" "runtime"
@ -153,7 +152,7 @@ var (
} }
) )
//declare a few constant error messages, useful for later error check comparisons in test // declare a few constant error messages, useful for later error check comparisons in test
var ( var (
SWARM_ERR_NO_BZZACCOUNT = "bzzaccount option is required but not set; check your config file, command line or environment variables" SWARM_ERR_NO_BZZACCOUNT = "bzzaccount option is required but not set; check your config file, command line or environment variables"
SWARM_ERR_SWAP_SET_NO_API = "SWAP is enabled but --swap-api is not set" SWARM_ERR_SWAP_SET_NO_API = "SWAP is enabled but --swap-api is not set"
@ -504,7 +503,7 @@ func decryptStoreAccount(ks *keystore.KeyStore, account string, passwords []stri
if err != nil { if err != nil {
utils.Fatalf("Can't find swarm account key: %v - Is the provided bzzaccount(%s) from the right datadir/Path?", err, account) utils.Fatalf("Can't find swarm account key: %v - Is the provided bzzaccount(%s) from the right datadir/Path?", err, account)
} }
keyjson, err := ioutil.ReadFile(a.URL.Path) keyjson, err := os.ReadFile(a.URL.Path)
if err != nil { if err != nil {
utils.Fatalf("Can't load swarm account key: %v", err) utils.Fatalf("Can't load swarm account key: %v", err)
} }

View file

@ -18,7 +18,6 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
@ -89,7 +88,7 @@ func newTestCluster(t *testing.T, size int) *testCluster {
} }
}() }()
tmpdir, err := ioutil.TempDir("", "swarm-test") tmpdir, err := os.MkdirTemp("", "swarm-test")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -21,7 +21,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"mime" "mime"
"net/http" "net/http"
"os" "os"
@ -51,7 +50,7 @@ func upload(ctx *cli.Context) {
if len(args) != 1 { if len(args) != 1 {
if fromStdin { if fromStdin {
tmp, err := ioutil.TempFile("", "swarm-stdin") tmp, err := os.CreateTemp("", "swarm-stdin")
if err != nil { if err != nil {
utils.Fatalf("error create tempfile: %s", err) utils.Fatalf("error create tempfile: %s", err)
} }

View file

@ -18,7 +18,6 @@ package main
import ( import (
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"testing" "testing"
@ -33,7 +32,7 @@ func TestCLISwarmUp(t *testing.T) {
defer cluster.Shutdown() defer cluster.Shutdown()
// create a tmp file // create a tmp file
tmp, err := ioutil.TempFile("", "swarm-test") tmp, err := os.CreateTemp("", "swarm-test")
assertNil(t, err) assertNil(t, err)
defer tmp.Close() defer tmp.Close()
defer os.Remove(tmp.Name()) defer os.Remove(tmp.Name())
@ -68,7 +67,7 @@ func assertHTTPResponse(t *testing.T, res *http.Response, expectedStatus int, ex
if res.StatusCode != expectedStatus { if res.StatusCode != expectedStatus {
t.Fatalf("expected HTTP status %d, got %s", expectedStatus, res.Status) t.Fatalf("expected HTTP status %d, got %s", expectedStatus, res.Status)
} }
data, err := ioutil.ReadAll(res.Body) data, err := io.ReadAll(res.Body)
assertNil(t, err) assertNil(t, err)
if string(data) != expectedBody { if string(data) != expectedBody {
t.Fatalf("expected HTTP body %q, got %q", expectedBody, data) t.Fatalf("expected HTTP body %q, got %q", expectedBody, data)

View file

@ -20,7 +20,6 @@ package utils
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"fmt" "fmt"
"io/ioutil"
"math/big" "math/big"
"os" "os"
"path/filepath" "path/filepath"
@ -848,7 +847,7 @@ func MakePasswordList(ctx *cli.Context) []string {
if path == "" { if path == "" {
return nil return nil
} }
text, err := ioutil.ReadFile(path) text, err := os.ReadFile(path)
if err != nil { if err != nil {
Fatalf("Failed to read password file: %v", err) Fatalf("Failed to read password file: %v", err)
} }

View file

@ -1,7 +1,6 @@
package utils package utils
import ( import (
"io/ioutil"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
@ -18,13 +17,13 @@ func TestWalkMatch(t *testing.T) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
test1Dir, _ := ioutil.TempDir(dir, "test1") test1Dir, _ := os.MkdirTemp(dir, "test1")
test2Dir, _ := ioutil.TempDir(dir, "test2") test2Dir, _ := os.MkdirTemp(dir, "test2")
err = ioutil.WriteFile(filepath.Join(test1Dir, "test1.ldb"), []byte("hello"), os.ModePerm) err = os.WriteFile(filepath.Join(test1Dir, "test1.ldb"), []byte("hello"), os.ModePerm)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
err = ioutil.WriteFile(filepath.Join(test2Dir, "test2.abc"), []byte("hello"), os.ModePerm) err = os.WriteFile(filepath.Join(test2Dir, "test2.abc"), []byte("hello"), os.ModePerm)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View file

@ -28,7 +28,6 @@ import (
"encoding/hex" "encoding/hex"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -483,7 +482,7 @@ func sendFilesLoop() {
fmt.Println("Quit command received") fmt.Println("Quit command received")
return return
} }
b, err := ioutil.ReadFile(s) b, err := os.ReadFile(s)
if err != nil { if err != nil {
fmt.Printf(">>> Error: %s \n", err) fmt.Printf(">>> Error: %s \n", err)
} else { } else {
@ -513,7 +512,7 @@ func fileReaderLoop() {
fmt.Println("Quit command received") fmt.Println("Quit command received")
return return
} }
raw, err := ioutil.ReadFile(s) raw, err := os.ReadFile(s)
if err != nil { if err != nil {
fmt.Printf(">>> Error: %s \n", err) fmt.Printf(">>> Error: %s \n", err)
} else { } else {
@ -670,7 +669,7 @@ func writeMessageToFile(dir string, msg *whisper.ReceivedMessage, show bool) {
//} //}
fullpath := filepath.Join(dir, name) fullpath := filepath.Join(dir, name)
err := ioutil.WriteFile(fullpath, env.Data, 0644) err := os.WriteFile(fullpath, env.Data, 0644)
if err != nil { if err != nil {
fmt.Printf("\n%s {%x}: message received but not saved: %s\n", timestamp, address, err) fmt.Printf("\n%s {%x}: message received but not saved: %s\n", timestamp, address, err)
} else if show { } else if show {

View file

@ -22,7 +22,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "os"
"os/exec" "os/exec"
"regexp" "regexp"
"strconv" "strconv"
@ -184,7 +184,7 @@ func (s *Solidity) run(cmd *exec.Cmd, source string) (map[string]*Contract, erro
func slurpFiles(files []string) (string, error) { func slurpFiles(files []string) (string, error) {
var concat bytes.Buffer var concat bytes.Buffer
for _, file := range files { for _, file := range files {
content, err := ioutil.ReadFile(file) content, err := os.ReadFile(file)
if err != nil { if err != nil {
return "", err return "", err
} }

View file

@ -19,12 +19,12 @@ package common
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "os"
) )
// LoadJSON reads the given file and unmarshals its content. // LoadJSON reads the given file and unmarshals its content.
func LoadJSON(file string, val interface{}) error { func LoadJSON(file string, val interface{}) error {
content, err := ioutil.ReadFile(file) content, err := os.ReadFile(file)
if err != nil { if err != nil {
return err return err
} }

View file

@ -5,9 +5,9 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"math/big" "math/big"
"math/rand" "math/rand"
"os"
"path/filepath" "path/filepath"
"sync" "sync"
"time" "time"
@ -849,7 +849,7 @@ func (x *XDPoS_v1) Finalize(chain consensus.ChainReader, header *types.Header, s
if len(common.StoreRewardFolder) > 0 { if len(common.StoreRewardFolder) > 0 {
data, err := json.Marshal(rewards) data, err := json.Marshal(rewards)
if err == nil { if err == nil {
err = ioutil.WriteFile(filepath.Join(common.StoreRewardFolder, header.Number.String()+"."+header.Hash().Hex()), data, 0644) err = os.WriteFile(filepath.Join(common.StoreRewardFolder, header.Number.String()+"."+header.Hash().Hex()), data, 0644)
} }
if err != nil { if err != nil {
log.Error("Error when save reward info ", "number", header.Number, "hash", header.Hash().Hex(), "err", err) log.Error("Error when save reward info ", "number", header.Number, "hash", header.Hash().Hex(), "err", err)

View file

@ -3,8 +3,8 @@ package engine_v2
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"math/big" "math/big"
"os"
"path/filepath" "path/filepath"
"sync" "sync"
"time" "time"
@ -385,7 +385,7 @@ func (x *XDPoS_v2) Finalize(chain consensus.ChainReader, header *types.Header, s
if len(common.StoreRewardFolder) > 0 { if len(common.StoreRewardFolder) > 0 {
data, err := json.Marshal(rewards) data, err := json.Marshal(rewards)
if err == nil { if err == nil {
err = ioutil.WriteFile(filepath.Join(common.StoreRewardFolder, header.Number.String()+"."+header.Hash().Hex()), data, 0644) err = os.WriteFile(filepath.Join(common.StoreRewardFolder, header.Number.String()+"."+header.Hash().Hex()), data, 0644)
} }
if err != nil { if err != nil {
log.Error("Error when save reward info ", "number", header.Number, "hash", header.Hash().Hex(), "err", err) log.Error("Error when save reward info ", "number", header.Number, "hash", header.Hash().Hex(), "err", err)
@ -1088,8 +1088,10 @@ func (x *XDPoS_v2) allowedToSend(chain consensus.ChainReader, blockHeader *types
// Periodlly execution(Attached to engine initialisation during "new"). Used for pool cleaning etc // Periodlly execution(Attached to engine initialisation during "new"). Used for pool cleaning etc
func (x *XDPoS_v2) periodicJob() { func (x *XDPoS_v2) periodicJob() {
go func() { go func() {
ticker := time.NewTicker(utils.PeriodicJobPeriod * time.Second)
defer ticker.Stop()
for { for {
<-time.After(utils.PeriodicJobPeriod * time.Second) <-ticker.C
x.hygieneVotePool() x.hygieneVotePool()
x.hygieneTimeoutPool() x.hygieneTimeoutPool()
} }

View file

@ -3,7 +3,6 @@ package engine_v2
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"fmt" "fmt"
"io/ioutil"
"math/big" "math/big"
"math/rand" "math/rand"
"os" "os"
@ -48,7 +47,7 @@ func RandStringBytes(n int) string {
func getSignerAndSignFn(pk *ecdsa.PrivateKey) (common.Address, func(account accounts.Account, hash []byte) ([]byte, error), error) { func getSignerAndSignFn(pk *ecdsa.PrivateKey) (common.Address, func(account accounts.Account, hash []byte) ([]byte, error), error) {
veryLightScryptN := 2 veryLightScryptN := 2
veryLightScryptP := 1 veryLightScryptP := 1
dir, _ := ioutil.TempDir("", fmt.Sprintf("eth-getSignerAndSignFn-test-%v", RandStringBytes(5))) dir, _ := os.MkdirTemp("", fmt.Sprintf("eth-getSignerAndSignFn-test-%v", RandStringBytes(5)))
new := func(kd string) *keystore.KeyStore { new := func(kd string) *keystore.KeyStore {
return keystore.NewKeyStore(kd, veryLightScryptN, veryLightScryptP) return keystore.NewKeyStore(kd, veryLightScryptN, veryLightScryptP)

View file

@ -2,7 +2,7 @@ package engine_v2
import ( import (
"fmt" "fmt"
"io/ioutil" "os"
"testing" "testing"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
@ -24,7 +24,7 @@ func TestGetMasterNodes(t *testing.T) {
func TestStoreLoadSnapshot(t *testing.T) { func TestStoreLoadSnapshot(t *testing.T) {
snap := newSnapshot(1, common.Hash{0x1}, nil) snap := newSnapshot(1, common.Hash{0x1}, nil)
dir, err := ioutil.TempDir("", "snapshot-test") dir, err := os.MkdirTemp("", "snapshot-test")
if err != nil { if err != nil {
panic(fmt.Sprintf("can't create temporary directory: %v", err)) panic(fmt.Sprintf("can't create temporary directory: %v", err))
} }

View file

@ -156,12 +156,16 @@ func generateCache(dest []uint32, epoch uint64, seed []byte) {
defer close(done) defer close(done)
go func() { go func() {
waitDuration := 3 * time.Second
timer := time.NewTimer(waitDuration)
defer timer.Stop()
for { for {
select { select {
case <-done: case <-done:
return return
case <-time.After(3 * time.Second): case <-timer.C:
logger.Info("Generating ethash verification cache", "percentage", atomic.LoadUint32(&progress)*100/uint32(rows)/4, "elapsed", common.PrettyDuration(time.Since(start))) logger.Info("Generating ethash verification cache", "percentage", atomic.LoadUint32(&progress)*100/uint32(rows)/4, "elapsed", common.PrettyDuration(time.Since(start)))
timer.Reset(waitDuration)
} }
} }
}() }()

View file

@ -18,7 +18,6 @@ package ethash
import ( import (
"bytes" "bytes"
"io/ioutil"
"math/big" "math/big"
"os" "os"
"reflect" "reflect"
@ -688,7 +687,7 @@ func TestHashimoto(t *testing.T) {
// Tests that caches generated on disk may be done concurrently. // Tests that caches generated on disk may be done concurrently.
func TestConcurrentDiskCacheGeneration(t *testing.T) { func TestConcurrentDiskCacheGeneration(t *testing.T) {
// Create a temp folder to generate the caches into // Create a temp folder to generate the caches into
cachedir, err := ioutil.TempDir("", "") cachedir, err := os.MkdirTemp("", "")
if err != nil { if err != nil {
t.Fatalf("Failed to create temporary cache dir: %v", err) t.Fatalf("Failed to create temporary cache dir: %v", err)
} }

View file

@ -17,7 +17,6 @@
package ethash package ethash
import ( import (
"io/ioutil"
"math/big" "math/big"
"math/rand" "math/rand"
"os" "os"
@ -46,7 +45,7 @@ func TestTestMode(t *testing.T) {
// This test checks that cache lru logic doesn't crash under load. // This test checks that cache lru logic doesn't crash under load.
// It reproduces https://github.com/XinFinOrg/XDPoSChain/issues/14943 // It reproduces https://github.com/XinFinOrg/XDPoSChain/issues/14943
func TestCacheFileEvict(t *testing.T) { func TestCacheFileEvict(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "ethash-test") tmpdir, err := os.MkdirTemp("", "ethash-test")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -6,7 +6,6 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io/ioutil"
"math/big" "math/big"
"math/rand" "math/rand"
"os" "os"
@ -77,7 +76,7 @@ func RandStringBytes(n int) string {
func getSignerAndSignFn(pk *ecdsa.PrivateKey) (common.Address, func(account accounts.Account, hash []byte) ([]byte, error), error) { func getSignerAndSignFn(pk *ecdsa.PrivateKey) (common.Address, func(account accounts.Account, hash []byte) ([]byte, error), error) {
veryLightScryptN := 2 veryLightScryptN := 2
veryLightScryptP := 1 veryLightScryptP := 1
dir, _ := ioutil.TempDir("", fmt.Sprintf("eth-getSignerAndSignFn-test-%v", RandStringBytes(5))) dir, _ := os.MkdirTemp("", fmt.Sprintf("eth-getSignerAndSignFn-test-%v", RandStringBytes(5)))
new := func(kd string) *keystore.KeyStore { new := func(kd string) *keystore.KeyStore {
return keystore.NewKeyStore(kd, veryLightScryptN, veryLightScryptP) return keystore.NewKeyStore(kd, veryLightScryptN, veryLightScryptP)

View file

@ -19,7 +19,6 @@ package console
import ( import (
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
@ -28,11 +27,11 @@ import (
"strings" "strings"
"syscall" "syscall"
"github.com/dop251/goja"
"github.com/XinFinOrg/XDPoSChain/internal/jsre" "github.com/XinFinOrg/XDPoSChain/internal/jsre"
"github.com/XinFinOrg/XDPoSChain/internal/jsre/deps" "github.com/XinFinOrg/XDPoSChain/internal/jsre/deps"
"github.com/XinFinOrg/XDPoSChain/internal/web3ext" "github.com/XinFinOrg/XDPoSChain/internal/web3ext"
"github.com/XinFinOrg/XDPoSChain/rpc" "github.com/XinFinOrg/XDPoSChain/rpc"
"github.com/dop251/goja"
"github.com/mattn/go-colorable" "github.com/mattn/go-colorable"
"github.com/peterh/liner" "github.com/peterh/liner"
) )
@ -137,7 +136,7 @@ func (c *Console) init(preload []string) error {
// Configure the input prompter for history and tab completion. // Configure the input prompter for history and tab completion.
if c.prompter != nil { if c.prompter != nil {
if content, err := ioutil.ReadFile(c.histPath); err != nil { if content, err := os.ReadFile(c.histPath); err != nil {
c.prompter.SetHistory(nil) c.prompter.SetHistory(nil)
} else { } else {
c.history = strings.Split(string(content), "\n") c.history = strings.Split(string(content), "\n")
@ -452,7 +451,7 @@ func (c *Console) Execute(path string) error {
// Stop cleans up the console and terminates the runtime environment. // Stop cleans up the console and terminates the runtime environment.
func (c *Console) Stop(graceful bool) error { func (c *Console) Stop(graceful bool) error {
if err := ioutil.WriteFile(c.histPath, []byte(strings.Join(c.history, "\n")), 0600); err != nil { if err := os.WriteFile(c.histPath, []byte(strings.Join(c.history, "\n")), 0600); err != nil {
return err return err
} }
if err := os.Chmod(c.histPath, 0600); err != nil { // Force 0600, even if it was different previously if err := os.Chmod(c.histPath, 0600); err != nil { // Force 0600, even if it was different previously

View file

@ -19,14 +19,14 @@ package console
import ( import (
"bytes" "bytes"
"errors" "errors"
"github.com/XinFinOrg/XDPoSChain/XDCx"
"github.com/XinFinOrg/XDPoSChain/XDCxlending"
"io/ioutil"
"os" "os"
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/XinFinOrg/XDPoSChain/XDCx"
"github.com/XinFinOrg/XDPoSChain/XDCxlending"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/consensus/ethash" "github.com/XinFinOrg/XDPoSChain/consensus/ethash"
"github.com/XinFinOrg/XDPoSChain/core" "github.com/XinFinOrg/XDPoSChain/core"
@ -86,7 +86,7 @@ type tester struct {
// Please ensure you call Close() on the returned tester to avoid leaks. // Please ensure you call Close() on the returned tester to avoid leaks.
func newTester(t *testing.T, confOverride func(*eth.Config)) *tester { func newTester(t *testing.T, confOverride func(*eth.Config)) *tester {
// Create a temporary storage for the node keys and initialize it // Create a temporary storage for the node keys and initialize it
workspace, err := ioutil.TempDir("", "console-tester-") workspace, err := os.MkdirTemp("", "console-tester-")
if err != nil { if err != nil {
t.Fatalf("failed to create temporary keystore: %v", err) t.Fatalf("failed to create temporary keystore: %v", err)
} }

View file

@ -30,7 +30,6 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"math/big" "math/big"
"os" "os"
"sync" "sync"
@ -160,7 +159,7 @@ func (self *Chequebook) setBalanceFromBlockChain() {
// LoadChequebook loads a chequebook from disk (file path). // LoadChequebook loads a chequebook from disk (file path).
func LoadChequebook(path string, prvKey *ecdsa.PrivateKey, backend Backend, checkBalance bool) (self *Chequebook, err error) { func LoadChequebook(path string, prvKey *ecdsa.PrivateKey, backend Backend, checkBalance bool) (self *Chequebook, err error) {
var data []byte var data []byte
data, err = ioutil.ReadFile(path) data, err = os.ReadFile(path)
if err != nil { if err != nil {
return return
} }
@ -230,7 +229,7 @@ func (self *Chequebook) Save() (err error) {
} }
self.log.Trace("Saving chequebook to disk", self.path) self.log.Trace("Saving chequebook to disk", self.path)
return ioutil.WriteFile(self.path, data, os.ModePerm) return os.WriteFile(self.path, data, os.ModePerm)
} }
// Stop quits the autodeposit go routine to terminate // Stop quits the autodeposit go routine to terminate

View file

@ -14,6 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License // You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
//go:build none
// +build none // +build none
// This program generates contract/code.go, which contains the chequebook code // This program generates contract/code.go, which contains the chequebook code
@ -22,8 +23,8 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"math/big" "math/big"
"os"
"github.com/XinFinOrg/XDPoSChain/accounts/abi/bind" "github.com/XinFinOrg/XDPoSChain/accounts/abi/bind"
"github.com/XinFinOrg/XDPoSChain/accounts/abi/bind/backends" "github.com/XinFinOrg/XDPoSChain/accounts/abi/bind/backends"
@ -64,7 +65,7 @@ func main() {
// updated when the contract code is changed. // updated when the contract code is changed.
const ContractDeployedCode = "%#x" const ContractDeployedCode = "%#x"
`, code) `, code)
if err := ioutil.WriteFile("contract/code.go", []byte(content), 0644); err != nil { if err := os.WriteFile("contract/code.go", []byte(content), 0644); err != nil {
panic(err) panic(err)
} }
} }

View file

@ -18,12 +18,12 @@ package core
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
"io/ioutil"
"math/big" "math/big"
"os" "os"
"testing" "testing"
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/common/math" "github.com/XinFinOrg/XDPoSChain/common/math"
"github.com/XinFinOrg/XDPoSChain/consensus/ethash" "github.com/XinFinOrg/XDPoSChain/consensus/ethash"
@ -151,7 +151,7 @@ func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) {
if !disk { if !disk {
db = rawdb.NewMemoryDatabase() db = rawdb.NewMemoryDatabase()
} else { } else {
dir, err := ioutil.TempDir("", "eth-core-bench") dir, err := os.MkdirTemp("", "eth-core-bench")
if err != nil { if err != nil {
b.Fatalf("cannot create temporary directory: %v", err) b.Fatalf("cannot create temporary directory: %v", err)
} }
@ -248,7 +248,7 @@ func makeChainForBench(db ethdb.Database, full bool, count uint64) {
func benchWriteChain(b *testing.B, full bool, count uint64) { func benchWriteChain(b *testing.B, full bool, count uint64) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
dir, err := ioutil.TempDir("", "eth-chain-bench") dir, err := os.MkdirTemp("", "eth-chain-bench")
if err != nil { if err != nil {
b.Fatalf("cannot create temporary directory: %v", err) b.Fatalf("cannot create temporary directory: %v", err)
} }
@ -263,7 +263,7 @@ func benchWriteChain(b *testing.B, full bool, count uint64) {
} }
func benchReadChain(b *testing.B, full bool, count uint64) { func benchReadChain(b *testing.B, full bool, count uint64) {
dir, err := ioutil.TempDir("", "eth-chain-bench") dir, err := os.MkdirTemp("", "eth-chain-bench")
if err != nil { if err != nil {
b.Fatalf("cannot create temporary directory: %v", err) b.Fatalf("cannot create temporary directory: %v", err)
} }

View file

@ -2346,7 +2346,11 @@ func (bc *BlockChain) reportBlock(block *types.Block, receipts types.Receipts, e
var roundNumber = types.Round(0) var roundNumber = types.Round(0)
engine, ok := bc.Engine().(*XDPoS.XDPoS) engine, ok := bc.Engine().(*XDPoS.XDPoS)
if ok { if ok {
var err error
roundNumber, err = engine.EngineV2.GetRoundNumber(block.Header()) roundNumber, err = engine.EngineV2.GetRoundNumber(block.Header())
if err != nil {
log.Error("reportBlock", "GetRoundNumber", err)
}
} }
var receiptString string var receiptString string

View file

@ -606,6 +606,9 @@ func (s *MatcherSession) DeliverSections(bit uint, sections []uint64, bitsets []
// of the session, any request in-flight need to be responded to! Empty responses // of the session, any request in-flight need to be responded to! Empty responses
// are fine though in that case. // are fine though in that case.
func (s *MatcherSession) Multiplex(batch int, wait time.Duration, mux chan chan *Retrieval) { func (s *MatcherSession) Multiplex(batch int, wait time.Duration, mux chan chan *Retrieval) {
ticker := time.NewTicker(wait)
defer ticker.Stop()
for { for {
// Allocate a new bloom bit index to retrieve data for, stopping when done // Allocate a new bloom bit index to retrieve data for, stopping when done
bit, ok := s.AllocateRetrieval() bit, ok := s.AllocateRetrieval()
@ -621,7 +624,7 @@ func (s *MatcherSession) Multiplex(batch int, wait time.Duration, mux chan chan
s.DeliverSections(bit, []uint64{}, [][]byte{}) s.DeliverSections(bit, []uint64{}, [][]byte{})
return return
case <-time.After(wait): case <-ticker.C:
// Throttling up, fetch whatever's available // Throttling up, fetch whatever's available
} }
} }

View file

@ -435,7 +435,7 @@ func (pool *LendingPool) validateNewLending(cloneStateDb *state.StateDB, cloneLe
return ErrInvalidLendingCollateral return ErrInvalidLendingCollateral
} }
validCollateral := false validCollateral := false
collateralList, _ := lendingstate.GetCollaterals(cloneStateDb, tx.RelayerAddress(), tx.LendingToken(), tx.Term()) collateralList := lendingstate.GetCollaterals(cloneStateDb, tx.RelayerAddress(), tx.LendingToken(), tx.Term())
for _, collateral := range collateralList { for _, collateral := range collateralList {
if tx.CollateralToken().String() == collateral.String() { if tx.CollateralToken().String() == collateral.String() {
validCollateral = true validCollateral = true

View file

@ -19,15 +19,15 @@ package core
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"fmt" "fmt"
"github.com/XinFinOrg/XDPoSChain/consensus"
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
"io/ioutil"
"math/big" "math/big"
"math/rand" "math/rand"
"os" "os"
"testing" "testing"
"time" "time"
"github.com/XinFinOrg/XDPoSChain/consensus"
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core/state" "github.com/XinFinOrg/XDPoSChain/core/state"
"github.com/XinFinOrg/XDPoSChain/core/types" "github.com/XinFinOrg/XDPoSChain/core/types"
@ -1543,7 +1543,7 @@ func testTransactionJournaling(t *testing.T, nolocals bool) {
t.Parallel() t.Parallel()
// Create a temporary file for the journal // Create a temporary file for the journal
file, err := ioutil.TempFile("", "") file, err := os.CreateTemp("", "")
if err != nil { if err != nil {
t.Fatalf("failed to create temporary journal: %v", err) t.Fatalf("failed to create temporary journal: %v", err)
} }

View file

@ -32,7 +32,6 @@ import (
var ( var (
// ErrInvalidLengdingSig invalidate signer // ErrInvalidLengdingSig invalidate signer
ErrInvalidLengdingSig = errors.New("invalid transaction v, r, s values") ErrInvalidLengdingSig = errors.New("invalid transaction v, r, s values")
errNoSignerLengding = errors.New("missing signing methods")
) )
const ( const (
@ -86,50 +85,32 @@ type lendingtxdata struct {
// IsCreatedLending check if tx is cancelled transaction // IsCreatedLending check if tx is cancelled transaction
func (tx *LendingTransaction) IsCreatedLending() bool { func (tx *LendingTransaction) IsCreatedLending() bool {
if (tx.IsLoTypeLending() || tx.IsMoTypeLending()) && tx.Status() == LendingStatusNew { return (tx.IsLoTypeLending() || tx.IsMoTypeLending()) && tx.Status() == LendingStatusNew
return true
}
return false
} }
// IsCancelledLending check if tx is cancelled transaction // IsCancelledLending check if tx is cancelled transaction
func (tx *LendingTransaction) IsCancelledLending() bool { func (tx *LendingTransaction) IsCancelledLending() bool {
if tx.Status() == LendingStatusCancelled { return tx.Status() == LendingStatusCancelled
return true
}
return false
} }
// IsRepayLending check if tx is repay lending transaction // IsRepayLending check if tx is repay lending transaction
func (tx *LendingTransaction) IsRepayLending() bool { func (tx *LendingTransaction) IsRepayLending() bool {
if tx.Type() == LendingRePay { return tx.Type() == LendingRePay
return true
}
return false
} }
// IsTopupLending check if tx is repay lending transaction // IsTopupLending check if tx is repay lending transaction
func (tx *LendingTransaction) IsTopupLending() bool { func (tx *LendingTransaction) IsTopupLending() bool {
if tx.Type() == LendingTopup { return tx.Type() == LendingTopup
return true
}
return false
} }
// IsMoTypeLending check if tx type is MO lending // IsMoTypeLending check if tx type is MO lending
func (tx *LendingTransaction) IsMoTypeLending() bool { func (tx *LendingTransaction) IsMoTypeLending() bool {
if tx.Type() == LendingTypeMo { return tx.Type() == LendingTypeMo
return true
}
return false
} }
// IsLoTypeLending check if tx type is LO lending // IsLoTypeLending check if tx type is LO lending
func (tx *LendingTransaction) IsLoTypeLending() bool { func (tx *LendingTransaction) IsLoTypeLending() bool {
if tx.Type() == LendingTypeLo { return tx.Type() == LendingTypeLo
return true
}
return false
} }
// EncodeRLP implements rlp.Encoder // EncodeRLP implements rlp.Encoder
@ -363,7 +344,7 @@ func (s *LendingTxByNonce) Pop() interface{} {
return x return x
} }
//LendingTransactionByNonce sort transaction by nonce // LendingTransactionByNonce sort transaction by nonce
type LendingTransactionByNonce struct { type LendingTransactionByNonce struct {
txs map[common.Address]LendingTransactions txs map[common.Address]LendingTransactions
heads LendingTxByNonce heads LendingTxByNonce

View file

@ -32,7 +32,6 @@ import (
var ( var (
// ErrInvalidOrderSig invalidate signer // ErrInvalidOrderSig invalidate signer
ErrInvalidOrderSig = errors.New("invalid transaction v, r, s values") ErrInvalidOrderSig = errors.New("invalid transaction v, r, s values")
errNoSignerOrder = errors.New("missing signing methods")
) )
const ( const (
@ -77,26 +76,17 @@ type ordertxdata struct {
// IsCancelledOrder check if tx is cancelled transaction // IsCancelledOrder check if tx is cancelled transaction
func (tx *OrderTransaction) IsCancelledOrder() bool { func (tx *OrderTransaction) IsCancelledOrder() bool {
if tx.Status() == OrderStatusCancelled { return tx.Status() == OrderStatusCancelled
return true
}
return false
} }
// IsMoTypeOrder check if tx type is MO Order // IsMoTypeOrder check if tx type is MO Order
func (tx *OrderTransaction) IsMoTypeOrder() bool { func (tx *OrderTransaction) IsMoTypeOrder() bool {
if tx.Type() == OrderTypeMo { return tx.Type() == OrderTypeMo
return true
}
return false
} }
// IsLoTypeOrder check if tx type is LO Order // IsLoTypeOrder check if tx type is LO Order
func (tx *OrderTransaction) IsLoTypeOrder() bool { func (tx *OrderTransaction) IsLoTypeOrder() bool {
if tx.Type() == OrderTypeLo { return tx.Type() == OrderTypeLo
return true
}
return false
} }
// EncodeRLP implements rlp.Encoder // EncodeRLP implements rlp.Encoder
@ -166,7 +156,6 @@ func (tx *OrderTransaction) WithSignature(signer OrderSigner, sig []byte) (*Orde
// ImportSignature make order tx with specific signature // ImportSignature make order tx with specific signature
func (tx *OrderTransaction) ImportSignature(V, R, S *big.Int) *OrderTransaction { func (tx *OrderTransaction) ImportSignature(V, R, S *big.Int) *OrderTransaction {
if V != nil { if V != nil {
tx.data.V = V tx.data.V = V
} }
@ -304,6 +293,10 @@ func NewOrderTransactionByNonce(signer OrderSigner, txs map[common.Address]Order
// Initialize a price based heap with the head transactions // Initialize a price based heap with the head transactions
heads := make(OrderTxByNonce, 0, len(txs)) heads := make(OrderTxByNonce, 0, len(txs))
for from, accTxs := range txs { for from, accTxs := range txs {
if len(accTxs) == 0 {
delete(txs, from)
continue
}
heads = append(heads, accTxs[0]) heads = append(heads, accTxs[0])
// Ensure the sender address is from the signer // Ensure the sender address is from the signer
acc, _ := OrderSender(signer, accTxs[0]) acc, _ := OrderSender(signer, accTxs[0])

View file

@ -0,0 +1,31 @@
package types
import (
"crypto/ecdsa"
"math/big"
"testing"
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/crypto"
)
func TestNewOrderTransactionByNonce(t *testing.T) {
// Generate a batch of accounts to start with
keys := make([]*ecdsa.PrivateKey, 1)
for i := 0; i < len(keys); i++ {
keys[i], _ = crypto.GenerateKey()
}
groups := map[common.Address]OrderTransactions{}
for start, key := range keys {
addr := crypto.PubkeyToAddress(key.PublicKey)
for i := 0; i < 1; i++ {
//tx, _ := SignTx(NewTransaction(uint64(start+i), common.Address{}, big.NewInt(100), 100, big.NewInt(int64(start+i)), nil), signer, key)
orderTx := NewOrderTransaction(uint64(start+i), big.NewInt(1), big.NewInt(2), common.Address{}, common.Address{}, common.Address{}, common.Address{}, "new", "BID", "test", common.Hash{}, 1001)
groups[addr] = append(groups[addr], orderTx)
}
}
tx := NewOrderTransactionByNonce(OrderTxSigner{}, groups)
t.Log(tx)
}

View file

@ -20,9 +20,9 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"math/big" "math/big"
"os"
"testing" "testing"
"github.com/XinFinOrg/XDPoSChain/params" "github.com/XinFinOrg/XDPoSChain/params"
@ -245,7 +245,7 @@ func TestWriteExpectedValues(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
_ = ioutil.WriteFile(fmt.Sprintf("testdata/testcases_%v.json", name), data, 0644) _ = os.WriteFile(fmt.Sprintf("testdata/testcases_%v.json", name), data, 0644)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -255,7 +255,7 @@ func TestWriteExpectedValues(t *testing.T) {
// TestJsonTestcases runs through all the testcases defined as json-files // TestJsonTestcases runs through all the testcases defined as json-files
func TestJsonTestcases(t *testing.T) { func TestJsonTestcases(t *testing.T) {
for name := range twoOpMethods { for name := range twoOpMethods {
data, err := ioutil.ReadFile(fmt.Sprintf("testdata/testcases_%v.json", name)) data, err := os.ReadFile(fmt.Sprintf("testdata/testcases_%v.json", name))
if err != nil { if err != nil {
t.Fatal("Failed to read file", err) t.Fatal("Failed to read file", err)
} }

View file

@ -274,7 +274,9 @@ func GenerateNewParams(G, H []ECPoint, x *big.Int, L, R, P ECPoint) ([]ECPoint,
return Gprime, Hprime, Pprime return Gprime, Hprime, Pprime
} }
/* Inner Product Argument /*
Inner Product Argument
Proves that <a,b>=c Proves that <a,b>=c
This is a building block for BulletProofs This is a building block for BulletProofs
*/ */
@ -323,7 +325,7 @@ func InnerProductProveSub(proof InnerProdArg, G, H []ECPoint, a []*big.Int, b []
return InnerProductProveSub(proof, Gprime, Hprime, aprime, bprime, u, Pprime) return InnerProductProveSub(proof, Gprime, Hprime, aprime, bprime, u, Pprime)
} }
//rpresult.IPP = InnerProductProve(left, right, that, P, EC.U, EC.BPG, HPrime) // rpresult.IPP = InnerProductProve(left, right, that, P, EC.U, EC.BPG, HPrime)
func InnerProductProve(a []*big.Int, b []*big.Int, c *big.Int, P, U ECPoint, G, H []ECPoint) InnerProdArg { func InnerProductProve(a []*big.Int, b []*big.Int, c *big.Int, P, U ECPoint, G, H []ECPoint) InnerProdArg {
loglen := int(math.Log2(float64(len(a)))) loglen := int(math.Log2(float64(len(a))))
@ -353,7 +355,9 @@ func InnerProductProve(a []*big.Int, b []*big.Int, c *big.Int, P, U ECPoint, G,
return InnerProductProveSub(runningProof, G, H, a, b, ux, Pprime) return InnerProductProveSub(runningProof, G, H, a, b, ux, Pprime)
} }
/* Inner Product Verify /*
Inner Product Verify
Given a inner product proof, verifies the correctness of the proof Given a inner product proof, verifies the correctness of the proof
Since we're using the Fiat-Shamir transform, we need to verify all x hash computations, Since we're using the Fiat-Shamir transform, we need to verify all x hash computations,
all g' and h' computations all g' and h' computations
@ -416,11 +420,7 @@ func InnerProductVerify(c *big.Int, P, U ECPoint, G, H []ECPoint, ipp InnerProdA
Pcalc3 := ux.Mult(ccalc) Pcalc3 := ux.Mult(ccalc)
Pcalc := Pcalc1.Add(Pcalc2).Add(Pcalc3) Pcalc := Pcalc1.Add(Pcalc2).Add(Pcalc3)
if !Pprime.Equal(Pcalc) { return Pprime.Equal(Pcalc)
return false
}
return true
} }
/* Inner Product Verify Fast /* Inner Product Verify Fast
@ -961,11 +961,14 @@ MultiRangeProof Prove
Takes in a list of values and provides an aggregate Takes in a list of values and provides an aggregate
range proof for all the values. range proof for all the values.
changes: changes:
all values are concatenated
r(x) is computed differently all values are concatenated
tau_x calculation is different r(x) is computed differently
delta calculation is different tau_x calculation is different
delta calculation is different
{(g, h \in G, \textbf{V} \in G^m ; \textbf{v, \gamma} \in Z_p^m) : {(g, h \in G, \textbf{V} \in G^m ; \textbf{v, \gamma} \in Z_p^m) :
V_j = h^{\gamma_j}g^{v_j} \wedge v_j \in [0, 2^n - 1] \forall j \in [1, m]} V_j = h^{\gamma_j}g^{v_j} \wedge v_j \in [0, 2^n - 1] \forall j \in [1, m]}
*/ */
var bitsPerValue = 64 var bitsPerValue = 64

View file

@ -4,11 +4,12 @@ import (
"crypto/rand" "crypto/rand"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/stretchr/testify/assert" "io"
"io/ioutil"
"math/big" "math/big"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func TestInnerProductProveLen1(t *testing.T) { func TestInnerProductProveLen1(t *testing.T) {
@ -409,7 +410,7 @@ func parseTestData(filePath string) MultiRangeProof {
defer jsonFile.Close() defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile) byteValue, _ := io.ReadAll(jsonFile)
// we initialize our Users array // we initialize our Users array
// var result map[string]interface{} // var result map[string]interface{}
@ -448,7 +449,8 @@ func parseTestData(filePath string) MultiRangeProof {
return proof return proof
} }
/** /*
*
Utils for parsing data from json Utils for parsing data from json
*/ */
func MapBigI(list []string, f func(string) *big.Int) []*big.Int { func MapBigI(list []string, f func(string) *big.Int) []*big.Int {

View file

@ -8,9 +8,10 @@ import (
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt" "fmt"
"github.com/XinFinOrg/XDPoSChain/common"
"math/big" "math/big"
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/crypto" "github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/log" "github.com/XinFinOrg/XDPoSChain/log"
) )
@ -28,18 +29,18 @@ const (
pubkeyHybrid byte = 0x6 // y_bit + x coord + y coord pubkeyHybrid byte = 0x6 // y_bit + x coord + y coord
) )
//The proof contains pretty much stuffs // The proof contains pretty much stuffs
//The proof contains pretty much stuffs // The proof contains pretty much stuffs
//Ring size rs: 1 byte => proof[0] // Ring size rs: 1 byte => proof[0]
//num input: number of real inputs: 1 byte => proof[1] // num input: number of real inputs: 1 byte => proof[1]
//List of inputs/UTXO index typed uint64 => total size = rs * numInput * 8 = proof[0]*proof[1]*8 // List of inputs/UTXO index typed uint64 => total size = rs * numInput * 8 = proof[0]*proof[1]*8
//List of key images: total size = numInput * 33 = proof[1] * 33 // List of key images: total size = numInput * 33 = proof[1] * 33
//number of output n: 1 byte // number of output n: 1 byte
//List of output => n * 130 bytes // List of output => n * 130 bytes
//transaction fee: uint256 => 32 byte // transaction fee: uint256 => 32 byte
//ringCT proof size ctSize: uint16 => 2 byte // ringCT proof size ctSize: uint16 => 2 byte
//ringCT proof: ctSize bytes // ringCT proof: ctSize bytes
//bulletproofs: bp // bulletproofs: bp
type PrivateSendVerifier struct { type PrivateSendVerifier struct {
proof []byte proof []byte
//ringCT RingCT //ringCT RingCT
@ -120,14 +121,15 @@ func (r Ring) Bytes() (b []byte) {
return return
} }
func PadTo32Bytes(in []byte) (out []byte) { func PadTo32Bytes(in []byte) []byte {
out = append(out, in...) padded := make([]byte, 32)
for { if len(in) >= 32 {
if len(out) == 32 { copy(padded, in)
return } else {
} copy(padded[32-len(in):], in)
out = append([]byte{0}, out...)
} }
return padded
} }
// converts the signature to a byte array // converts the signature to a byte array
@ -174,6 +176,20 @@ func (r *RingSignature) Serialize() ([]byte, error) {
} }
func computeSignatureSize(numRing int, ringSize int) int { func computeSignatureSize(numRing int, ringSize int) int {
const MaxInt = int(^uint(0) >> 1)
if numRing < 0 || ringSize < 0 {
return -1
}
// Calculate term and check for overflow
term := numRing * ringSize * 65
if term < 0 || term < numRing || term < ringSize {
return -1
}
return 8 + 8 + 32 + 32 + numRing*ringSize*32 + numRing*ringSize*33 + numRing*33 return 8 + 8 + 32 + 32 + numRing*ringSize*32 + numRing*ringSize*33 + numRing*33
} }
@ -198,7 +214,7 @@ func Deserialize(r []byte) (*RingSignature, error) {
sig.NumRing = size_int sig.NumRing = size_int
if len(r) != computeSignatureSize(sig.NumRing, sig.Size) { if len(r) != computeSignatureSize(sig.NumRing, sig.Size) {
return nil, errors.New("incorrect ring size") return nil, fmt.Errorf("incorrect ring size, len r: %d, sig.NumRing: %d sig.Size: %d", len(r), sig.NumRing, sig.Size)
} }
m := r[offset : offset+32] m := r[offset : offset+32]
@ -272,7 +288,7 @@ func GenNewKeyRing(size int, privkey *ecdsa.PrivateKey, s int) ([]*ecdsa.PublicK
ring := make([]*ecdsa.PublicKey, size) ring := make([]*ecdsa.PublicKey, size)
pubkey := privkey.Public().(*ecdsa.PublicKey) pubkey := privkey.Public().(*ecdsa.PublicKey)
if s > len(ring) { if s >= len(ring) {
return nil, errors.New("index s out of bounds") return nil, errors.New("index s out of bounds")
} }
@ -544,7 +560,7 @@ func Link(sig_a *RingSignature, sig_b *RingSignature) bool {
return false return false
} }
//function returns(mutiple rings, private keys, message, error) // function returns(mutiple rings, private keys, message, error)
func GenerateMultiRingParams(numRing int, ringSize int, s int) (rings []Ring, privkeys []*ecdsa.PrivateKey, m [32]byte, err error) { func GenerateMultiRingParams(numRing int, ringSize int, s int) (rings []Ring, privkeys []*ecdsa.PrivateKey, m [32]byte, err error) {
for i := 0; i < numRing; i++ { for i := 0; i < numRing; i++ {
privkey, err := crypto.GenerateKey() privkey, err := crypto.GenerateKey()
@ -566,35 +582,3 @@ func GenerateMultiRingParams(numRing int, ringSize int, s int) (rings []Ring, pr
} }
return rings, privkeys, m, nil return rings, privkeys, m, nil
} }
func TestRingSignature() (bool, []byte) {
/*for i := 14; i < 15; i++ {
for j := 14; j < 15; j++ {
for k := 0; k <= j; k++ {*/
numRing := 1
ringSize := 10
s := 9
rings, privkeys, m, err := GenerateMultiRingParams(numRing, ringSize, s)
ringSignature, err := Sign(m, rings, privkeys, s)
if err != nil {
log.Error("Failed to create Ring signature")
return false, []byte{}
}
sig, err := ringSignature.Serialize()
if err != nil {
return false, []byte{}
}
deserializedSig, err := Deserialize(sig)
if err != nil {
return false, []byte{}
}
verified := Verify(deserializedSig, false)
if !verified {
log.Error("Failed to verify Ring signature")
return false, []byte{}
}
return true, []byte{}
}

View file

@ -1,9 +1,13 @@
package privacy package privacy
import ( import (
"bytes"
"encoding/binary"
"fmt" "fmt"
"testing" "testing"
)
"github.com/stretchr/testify/assert"
)
func TestSign(t *testing.T) { func TestSign(t *testing.T) {
/*for i := 14; i < 15; i++ { /*for i := 14; i < 15; i++ {
@ -44,3 +48,82 @@ func TestSign(t *testing.T) {
} }
} }
func TestDeserialize(t *testing.T) {
numRing := 5
ringSize := 10
s := 5
rings, privkeys, m, err := GenerateMultiRingParams(numRing, ringSize, s)
ringSignature, err := Sign(m, rings, privkeys, s)
if err != nil {
t.Error("Failed to create Ring signature")
}
// A normal signature.
sig, err := ringSignature.Serialize()
if err != nil {
t.Error("Failed to Serialize input Ring signature")
}
// Modify the serialized signature s.t.
// the new signature passes the length check
// but triggers buffer overflow in Deserialize().
// ringSize: 10 -> 56759212534490939
// len(sig): 3495 -> 3804
// 80 + 5 * (56759212534490939*65 + 33) = 18446744073709551616 + 3804
bs := make([]byte, 8)
binary.BigEndian.PutUint64(bs, 56759212534490939)
for i := 0; i < 8; i++ {
sig[i+8] = bs[i]
}
tail := make([]byte, 3804-len(sig))
sig = append(sig, tail...)
_, err = Deserialize(sig)
assert.EqualError(t, err, "incorrect ring size, len r: 3804, sig.NumRing: 5 sig.Size: 56759212534490939")
}
func TestPadTo32Bytes(t *testing.T) {
arr := [44]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34}
// test input slice is longer than 32 bytes
assert.True(t, bytes.Equal(PadTo32Bytes(arr[0:]), arr[0:32]), "Test PadTo32Bytes longer than 32 bytes #1")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[1:]), arr[1:33]), "Test PadTo32Bytes longer than 32 bytes #2")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[2:]), arr[2:34]), "Test PadTo32Bytes longer than 32 bytes #3")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[3:]), arr[3:35]), "Test PadTo32Bytes longer than 32 bytes #4")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[4:]), arr[4:36]), "Test PadTo32Bytes longer than 32 bytes #5")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[5:]), arr[5:37]), "Test PadTo32Bytes longer than 32 bytes #6")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[6:]), arr[6:38]), "Test PadTo32Bytes longer than 32 bytes #7")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[7:]), arr[7:39]), "Test PadTo32Bytes longer than 32 bytes #8")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[8:]), arr[8:40]), "Test PadTo32Bytes longer than 32 bytes #9")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[9:]), arr[9:41]), "Test PadTo32Bytes longer than 32 bytes #10")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[10:]), arr[10:42]), "Test PadTo32Bytes longer than 32 bytes #11")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[11:]), arr[11:43]), "Test PadTo32Bytes longer than 32 bytes #12")
// test input slice is equal 32 bytes
assert.True(t, bytes.Equal(PadTo32Bytes(arr[0:32]), arr[0:32]), "Test PadTo32Bytes equal 32 bytes #1")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[1:33]), arr[1:33]), "Test PadTo32Bytes equal 32 bytes #2")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[2:34]), arr[2:34]), "Test PadTo32Bytes equal 32 bytes #3")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[3:35]), arr[3:35]), "Test PadTo32Bytes equal 32 bytes #4")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[4:36]), arr[4:36]), "Test PadTo32Bytes equal 32 bytes #5")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[5:37]), arr[5:37]), "Test PadTo32Bytes equal 32 bytes #6")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[6:38]), arr[6:38]), "Test PadTo32Bytes equal 32 bytes #7")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[7:39]), arr[7:39]), "Test PadTo32Bytes equal 32 bytes #8")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[8:40]), arr[8:40]), "Test PadTo32Bytes equal 32 bytes #9")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[9:41]), arr[9:41]), "Test PadTo32Bytes equal 32 bytes #10")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[10:42]), arr[10:42]), "Test PadTo32Bytes equal 32 bytes #11")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[11:43]), arr[11:43]), "Test PadTo32Bytes equal 32 bytes #12")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[12:44]), arr[12:44]), "Test PadTo32Bytes equal 32 bytes #13")
// test input slice is shorter than 32 bytes
assert.True(t, bytes.Equal(PadTo32Bytes(arr[10:32]), arr[0:32]), "Test PadTo32Bytes shorter than 32 bytes #1")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[10:33]), arr[1:33]), "Test PadTo32Bytes shorter than 32 bytes #2")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[10:34]), arr[2:34]), "Test PadTo32Bytes shorter than 32 bytes #3")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[10:35]), arr[3:35]), "Test PadTo32Bytes shorter than 32 bytes #4")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[10:36]), arr[4:36]), "Test PadTo32Bytes shorter than 32 bytes #5")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[10:37]), arr[5:37]), "Test PadTo32Bytes shorter than 32 bytes #6")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[10:38]), arr[6:38]), "Test PadTo32Bytes shorter than 32 bytes #7")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[10:39]), arr[7:39]), "Test PadTo32Bytes shorter than 32 bytes #8")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[10:40]), arr[8:40]), "Test PadTo32Bytes shorter than 32 bytes #9")
assert.True(t, bytes.Equal(PadTo32Bytes(arr[10:41]), arr[9:41]), "Test PadTo32Bytes shorter than 32 bytes #10")
}

View file

@ -20,42 +20,52 @@ package bn256
import ( import (
"bytes" "bytes"
"fmt"
"io"
"math/big" "math/big"
cloudflare "github.com/XinFinOrg/XDPoSChain/crypto/bn256/cloudflare" cloudflare "github.com/XinFinOrg/XDPoSChain/crypto/bn256/cloudflare"
google "github.com/XinFinOrg/XDPoSChain/crypto/bn256/google" google "github.com/XinFinOrg/XDPoSChain/crypto/bn256/google"
) )
func getG1Points(input io.Reader) (*cloudflare.G1, *google.G1) {
_, xc, err := cloudflare.RandomG1(input)
if err != nil {
// insufficient input
return nil, nil
}
xg := new(google.G1)
if _, err := xg.Unmarshal(xc.Marshal()); err != nil {
panic(fmt.Sprintf("Could not marshal cloudflare -> google:", err))
}
return xc, xg
}
func getG2Points(input io.Reader) (*cloudflare.G2, *google.G2) {
_, xc, err := cloudflare.RandomG2(input)
if err != nil {
// insufficient input
return nil, nil
}
xg := new(google.G2)
if _, err := xg.Unmarshal(xc.Marshal()); err != nil {
panic(fmt.Sprintf("Could not marshal cloudflare -> google:", err))
}
return xc, xg
}
// FuzzAdd fuzzez bn256 addition between the Google and Cloudflare libraries. // FuzzAdd fuzzez bn256 addition between the Google and Cloudflare libraries.
func FuzzAdd(data []byte) int { func FuzzAdd(data []byte) int {
// Ensure we have enough data in the first place input := bytes.NewReader(data)
if len(data) != 128 { xc, xg := getG1Points(input)
if xc == nil {
return 0 return 0
} }
// Ensure both libs can parse the first curve point yc, yg := getG1Points(input)
xc := new(cloudflare.G1) if yc == nil {
_, errc := xc.Unmarshal(data[:64])
xg := new(google.G1)
_, errg := xg.Unmarshal(data[:64])
if (errc == nil) != (errg == nil) {
panic("parse mismatch")
} else if errc != nil {
return 0 return 0
} }
// Ensure both libs can parse the second curve point // Ensure both libs can parse the second curve point
yc := new(cloudflare.G1)
_, errc = yc.Unmarshal(data[64:])
yg := new(google.G1)
_, errg = yg.Unmarshal(data[64:])
if (errc == nil) != (errg == nil) {
panic("parse mismatch")
} else if errc != nil {
return 0
}
// Add the two points and ensure they result in the same output // Add the two points and ensure they result in the same output
rc := new(cloudflare.G1) rc := new(cloudflare.G1)
rc.Add(xc, yc) rc.Add(xc, yc)
@ -66,73 +76,50 @@ func FuzzAdd(data []byte) int {
if !bytes.Equal(rc.Marshal(), rg.Marshal()) { if !bytes.Equal(rc.Marshal(), rg.Marshal()) {
panic("add mismatch") panic("add mismatch")
} }
return 0 return 1
} }
// FuzzMul fuzzez bn256 scalar multiplication between the Google and Cloudflare // FuzzMul fuzzez bn256 scalar multiplication between the Google and Cloudflare
// libraries. // libraries.
func FuzzMul(data []byte) int { func FuzzMul(data []byte) int {
// Ensure we have enough data in the first place input := bytes.NewReader(data)
if len(data) != 96 { pc, pg := getG1Points(input)
return 0 if pc == nil {
}
// Ensure both libs can parse the curve point
pc := new(cloudflare.G1)
_, errc := pc.Unmarshal(data[:64])
pg := new(google.G1)
_, errg := pg.Unmarshal(data[:64])
if (errc == nil) != (errg == nil) {
panic("parse mismatch")
} else if errc != nil {
return 0 return 0
} }
// Add the two points and ensure they result in the same output // Add the two points and ensure they result in the same output
remaining := input.Len()
if remaining == 0 {
return 0
}
buf := make([]byte, remaining)
input.Read(buf)
rc := new(cloudflare.G1) rc := new(cloudflare.G1)
rc.ScalarMult(pc, new(big.Int).SetBytes(data[64:])) rc.ScalarMult(pc, new(big.Int).SetBytes(buf))
rg := new(google.G1) rg := new(google.G1)
rg.ScalarMult(pg, new(big.Int).SetBytes(data[64:])) rg.ScalarMult(pg, new(big.Int).SetBytes(buf))
if !bytes.Equal(rc.Marshal(), rg.Marshal()) { if !bytes.Equal(rc.Marshal(), rg.Marshal()) {
panic("scalar mul mismatch") panic("scalar mul mismatch")
} }
return 0 return 1
} }
func FuzzPair(data []byte) int { func FuzzPair(data []byte) int {
// Ensure we have enough data in the first place input := bytes.NewReader(data)
if len(data) != 192 { pc, pg := getG1Points(input)
if pc == nil {
return 0 return 0
} }
// Ensure both libs can parse the curve point tc, tg := getG2Points(input)
pc := new(cloudflare.G1) if tc == nil {
_, errc := pc.Unmarshal(data[:64])
pg := new(google.G1)
_, errg := pg.Unmarshal(data[:64])
if (errc == nil) != (errg == nil) {
panic("parse mismatch")
} else if errc != nil {
return 0
}
// Ensure both libs can parse the twist point
tc := new(cloudflare.G2)
_, errc = tc.Unmarshal(data[64:])
tg := new(google.G2)
_, errg = tg.Unmarshal(data[64:])
if (errc == nil) != (errg == nil) {
panic("parse mismatch")
} else if errc != nil {
return 0 return 0
} }
// Pair the two points and ensure thet result in the same output // Pair the two points and ensure thet result in the same output
if cloudflare.PairingCheck([]*cloudflare.G1{pc}, []*cloudflare.G2{tc}) != google.PairingCheck([]*google.G1{pg}, []*google.G2{tg}) { if cloudflare.PairingCheck([]*cloudflare.G1{pc}, []*cloudflare.G2{tc}) != google.PairingCheck([]*google.G1{pg}, []*google.G2{tg}) {
panic("pair mismatch") panic("pair mismatch")
} }
return 0 return 1
} }

View file

@ -23,7 +23,7 @@ import (
func randomK(r io.Reader) (k *big.Int, err error) { func randomK(r io.Reader) (k *big.Int, err error) {
for { for {
k, err = rand.Int(r, Order) k, err = rand.Int(r, Order)
if k.Sign() > 0 || err != nil { if err != nil || k.Sign() > 0 {
return return
} }
} }
@ -100,6 +100,10 @@ func (e *G1) Marshal() []byte {
// Each value is a 256-bit number. // Each value is a 256-bit number.
const numBytes = 256 / 8 const numBytes = 256 / 8
if e.p == nil {
e.p = &curvePoint{}
}
e.p.MakeAffine() e.p.MakeAffine()
ret := make([]byte, numBytes*2) ret := make([]byte, numBytes*2)
if e.p.IsInfinity() { if e.p.IsInfinity() {
@ -382,6 +386,11 @@ func (e *GT) Marshal() []byte {
// Each value is a 256-bit number. // Each value is a 256-bit number.
const numBytes = 256 / 8 const numBytes = 256 / 8
if e.p == nil {
e.p = &gfP12{}
e.p.SetOne()
}
ret := make([]byte, numBytes*12) ret := make([]byte, numBytes*12)
temp := &gfP{} temp := &gfP{}

View file

@ -92,6 +92,19 @@ func TestTripartiteDiffieHellman(t *testing.T) {
} }
} }
func TestG2SelfAddition(t *testing.T) {
s, _ := rand.Int(rand.Reader, Order)
p := new(G2).ScalarBaseMult(s)
if !p.p.IsOnCurve() {
t.Fatal("p isn't on curve")
}
m := p.Add(p, p).Marshal()
if _, err := p.Unmarshal(m); err != nil {
t.Fatalf("p.Add(p, p) ∉ G₂: %v", err)
}
}
func BenchmarkG1(b *testing.B) { func BenchmarkG1(b *testing.B) {
x, _ := rand.Int(rand.Reader, Order) x, _ := rand.Int(rand.Reader, Order)
b.ResetTimer() b.ResetTimer()

View file

@ -171,15 +171,15 @@ func (c *curvePoint) Double(a *curvePoint) {
gfpAdd(t, d, d) gfpAdd(t, d, d)
gfpSub(&c.x, f, t) gfpSub(&c.x, f, t)
gfpMul(&c.z, &a.y, &a.z)
gfpAdd(&c.z, &c.z, &c.z)
gfpAdd(t, C, C) gfpAdd(t, C, C)
gfpAdd(t2, t, t) gfpAdd(t2, t, t)
gfpAdd(t, t2, t2) gfpAdd(t, t2, t2)
gfpSub(&c.y, d, &c.x) gfpSub(&c.y, d, &c.x)
gfpMul(t2, e, &c.y) gfpMul(t2, e, &c.y)
gfpSub(&c.y, t2, t) gfpSub(&c.y, t2, t)
gfpMul(t, &a.y, &a.z)
gfpAdd(&c.z, t, t)
} }
func (c *curvePoint) Mul(a *curvePoint, scalar *big.Int) { func (c *curvePoint) Mul(a *curvePoint, scalar *big.Int) {

Some files were not shown because too many files have changed in this diff Show more