Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to include dynamic control of parameters #25

Merged
merged 1 commit into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"

"github.com/MadBase/MadNet/dynamics"
"github.com/MadBase/MadNet/errorz"
"github.com/MadBase/MadNet/utils"
"github.com/sirupsen/logrus"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/MadBase/MadNet/application/objs/uint256"
"github.com/MadBase/MadNet/application/pendingtx"
"github.com/MadBase/MadNet/application/utxohandler"
"github.com/MadBase/MadNet/application/wrapper"
trie "github.com/MadBase/MadNet/badgerTrie"
"github.com/MadBase/MadNet/consensus/appmock"
consensusdb "github.com/MadBase/MadNet/consensus/db"
Expand All @@ -36,8 +38,9 @@ type Application struct {
}

// Init initializes Application ...
func (a *Application) Init(conDB *consensusdb.Database, memDB *badger.DB, dph *deposit.Handler) error {
func (a *Application) Init(conDB *consensusdb.Database, memDB *badger.DB, dph *deposit.Handler, storageInterface dynamics.StorageGetter) error {
a.logger = logging.GetLogger(constants.LoggerApp)
storage := wrapper.NewStorage(storageInterface)
uHdlr := utxohandler.NewUTXOHandler(conDB.DB())
pHdlr := pendingtx.NewPendingTxHandler(memDB)
pHdlr.UTXOHandler = uHdlr
Expand All @@ -50,6 +53,7 @@ func (a *Application) Init(conDB *consensusdb.Database, memDB *badger.DB, dph *d
dHdlr: dph,
uHdlr: uHdlr,
cdb: conDB,
storage: storage,
}
a.txHandler.dHdlr.IsSpent = a.txHandler.uHdlr.TrieContains
// initialize the application with a random key.
Expand Down Expand Up @@ -151,7 +155,7 @@ func (a *Application) IsValid(txn *badger.Txn, chainID uint32, height uint32, st
utils.DebugTrace(a.logger, err)
return false, err
}
if err := txs.Validate(height, vout); err != nil {
if err := txs.Validate(height, vout, a.txHandler.storage); err != nil {
utils.DebugTrace(a.logger, err)
return false, err
}
Expand Down Expand Up @@ -221,7 +225,11 @@ func (a *Application) SetMiningKey(privKey []byte, curveSpec constants.CurveSpec
switch curveSpec {
case constants.CurveBN256Eth:
signer := &crypto.BNSigner{}
signer.SetPrivk(privKey)
err := signer.SetPrivk(privKey)
if err != nil {
utils.DebugTrace(a.logger, err)
return err
}
pubk, err := signer.Pubkey()
if err != nil {
utils.DebugTrace(a.logger, err)
Expand Down
1 change: 1 addition & 0 deletions application/deposit/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func (dp *Handler) Add(txn *badger.Txn, chainID uint32, utxoID []byte, biValue *
Value: value,
ChainID: chainID,
Owner: vso,
Fee: new(uint256.Uint256).SetZero(),
},
TxHash: n2,
}
Expand Down
7 changes: 5 additions & 2 deletions application/deposit/deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ func newDepositHandler() *Handler {

func testingOwner() *objs.Owner {
signer := &crypto.BNSigner{}
signer.SetPrivk([]byte("secret"))
err := signer.SetPrivk([]byte("secret"))
if err != nil {
panic(err)
}
pubk, _ := signer.Pubkey()
acct := crypto.GetAccount(pubk)
owner := &objs.Owner{}
curveSpec := constants.CurveSecp256k1
err := owner.New(acct, curveSpec)
err = owner.New(acct, curveSpec)
if err != nil {
panic(err)
}
Expand Down
26 changes: 15 additions & 11 deletions application/indexer/expsize.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,34 +94,38 @@ func (esi *ExpSizeIndex) Drop(txn *badger.Txn, utxoID []byte) error {
return utils.DeleteValue(txn, key)
}

func (esi *ExpSizeIndex) GetExpiredObjects(txn *badger.Txn, epoch uint32, maxBytes uint32) ([][]byte, uint32) {
func (esi *ExpSizeIndex) GetExpiredObjects(txn *badger.Txn, epoch uint32, maxBytes uint32, maxObjects int) ([][]byte, uint32) {
result := [][]byte{}
byteCount := uint32(0)
objCount := 0
opts := badger.DefaultIteratorOptions
prefix := esi.prefix()
opts.Prefix = prefix
iter := txn.NewIterator(opts)
defer iter.Close()
for iter.Seek(prefix); iter.ValidForPrefix(prefix); iter.Next() {
if objCount+1 > maxObjects {
break
}
if byteCount+constants.HashLen > maxBytes {
break
}
itm := iter.Item()
key := itm.KeyCopy(nil)
key = key[len(prefix):]
epochBytes := key[:4]
// slice is 4 bytes so no error will be raised
epochObj, _ := utils.UnmarshalUint32(epochBytes)
sizeInvBytes := key[4:8]
// slice is 4 bytes so no error will be raised
sizeObjInv, _ := utils.UnmarshalUint32(sizeInvBytes)
sizeObj := constants.MaxUint32 - sizeObjInv
utxoID := key[8:]
if epochObj <= epoch {
if byteCount+sizeObj <= maxBytes {
byteCount += sizeObj
result = append(result, utxoID)
}
if epochObj > epoch {
break
}
byteCount += constants.HashLen
result = append(result, utxoID)
objCount++
}
return result, byteCount
remainingBytes := maxBytes - byteCount
return result, remainingBytes
}

func (esi *ExpSizeIndex) makeKey(epoch uint32, size uint32, utxoID []byte) *ExpSizeIndexKey {
Expand Down
14 changes: 8 additions & 6 deletions application/indexer/indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,8 @@ func TestExpSizeIndex(t *testing.T) {
if err != nil {
t.Fatal(err)
}
expObjs, expSize := index.GetExpiredObjects(txn, epoch1, 99)
maxBytes := uint32(99)
expObjs, remainingBytes := index.GetExpiredObjects(txn, epoch1, maxBytes, constants.MaxTxVectorLength)
if len(expObjs) != 2 {
t.Fatal("expObjs len wrong", len(expObjs))
}
Expand All @@ -633,22 +634,23 @@ func TestExpSizeIndex(t *testing.T) {
if !bytes.Equal(expObjs[1], txHash1) {
t.Fatal("exp1 wrong")
}
if expSize != size1+size2 {
t.Fatal("exp size wrong")
if maxBytes-remainingBytes != 2*constants.HashLen {
t.Fatal("wrong size for remainingBytes")
}
err = index.Drop(txn, txHash1)
if err != nil {
t.Fatal(err)
}
expObjs, expSize = index.GetExpiredObjects(txn, epoch1, 99)
maxBytes = 99
expObjs, remainingBytes = index.GetExpiredObjects(txn, epoch1, maxBytes, constants.MaxTxVectorLength)
if len(expObjs) != 1 {
t.Fatal("expObjs len wrong", len(expObjs))
}
if !bytes.Equal(expObjs[0], txHash2) {
t.Fatal("exp0 wrong")
}
if expSize != size2 {
t.Fatal("exp size wrong")
if maxBytes-remainingBytes != constants.HashLen {
t.Fatal("wrong size for remainingBytes")
}
return nil
})
Expand Down
169 changes: 0 additions & 169 deletions application/indexer/valueold.go

This file was deleted.

Loading