forked from TrueCloudLab/neoneo-go
Merge pull request #2290 from nspcc-dev/microopt
Trivial microoptimizations
This commit is contained in:
commit
96339e9f64
7 changed files with 30 additions and 6 deletions
|
@ -54,7 +54,7 @@ func (g *GAS) increaseBalance(_ *interop.Context, _ util.Uint160, si *state.Stor
|
|||
err = errors.New("insufficient funds")
|
||||
}
|
||||
return err
|
||||
} else if sign == -1 && acc.Balance.Cmp(new(big.Int).Neg(amount)) == -1 {
|
||||
} else if sign == -1 && acc.Balance.CmpAbs(amount) == -1 {
|
||||
return errors.New("insufficient funds")
|
||||
}
|
||||
acc.Balance.Add(&acc.Balance, amount)
|
||||
|
|
|
@ -405,7 +405,7 @@ func (n *NEO) increaseBalance(ic *interop.Context, h util.Uint160, si *state.Sto
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if (amount.Sign() == -1 && acc.Balance.Cmp(new(big.Int).Neg(amount)) == -1) ||
|
||||
if (amount.Sign() == -1 && acc.Balance.CmpAbs(amount) == -1) ||
|
||||
(amount.Sign() == 0 && checkBal != nil && acc.Balance.Cmp(checkBal) == -1) {
|
||||
return errors.New("insufficient funds")
|
||||
}
|
||||
|
|
|
@ -262,7 +262,9 @@ func (c *nep17TokenNative) burn(ic *interop.Context, h util.Uint160, amount *big
|
|||
if amount.Sign() == 0 {
|
||||
return
|
||||
}
|
||||
c.addTokens(ic, h, new(big.Int).Neg(amount))
|
||||
amount.Neg(amount)
|
||||
c.addTokens(ic, h, amount)
|
||||
amount.Neg(amount)
|
||||
c.postTransfer(ic, &h, nil, amount, stackitem.Null{}, false)
|
||||
}
|
||||
|
||||
|
|
|
@ -68,6 +68,9 @@ type Transaction struct {
|
|||
// Hash of the transaction (double SHA256).
|
||||
hash util.Uint256
|
||||
|
||||
// Whether hash is correct.
|
||||
hashed bool
|
||||
|
||||
// Trimmed indicates this is a transaction from trimmed
|
||||
// data.
|
||||
Trimmed bool
|
||||
|
@ -78,6 +81,7 @@ type Transaction struct {
|
|||
func NewTrimmedTX(hash util.Uint256) *Transaction {
|
||||
return &Transaction{
|
||||
hash: hash,
|
||||
hashed: true,
|
||||
Trimmed: true,
|
||||
}
|
||||
}
|
||||
|
@ -98,7 +102,7 @@ func New(script []byte, gas int64) *Transaction {
|
|||
|
||||
// Hash returns the hash of the transaction.
|
||||
func (t *Transaction) Hash() util.Uint256 {
|
||||
if t.hash.Equals(util.Uint256{}) {
|
||||
if !t.hashed {
|
||||
if t.createHash() != nil {
|
||||
panic("failed to compute hash!")
|
||||
}
|
||||
|
@ -172,6 +176,7 @@ func (t *Transaction) decodeHashableFields(br *io.BinReader, buf []byte) {
|
|||
if buf != nil {
|
||||
end = len(buf) - br.Len()
|
||||
t.hash = hash.Sha256(buf[start:end])
|
||||
t.hashed = true
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -261,6 +266,7 @@ func (t *Transaction) createHash() error {
|
|||
}
|
||||
|
||||
shaHash.Sum(t.hash[:0])
|
||||
t.hashed = true
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -308,3 +308,17 @@ func TestTransaction_HasSigner(t *testing.T) {
|
|||
require.True(t, tx.HasSigner(u1))
|
||||
require.False(t, tx.HasSigner(util.Uint160{}))
|
||||
}
|
||||
|
||||
func BenchmarkTxHash(b *testing.B) {
|
||||
script := []byte{0x51}
|
||||
tx := New(script, 1)
|
||||
tx.NetworkFee = 123
|
||||
tx.Signers = []Signer{{Account: util.Uint160{1, 2, 3}}}
|
||||
tx.Scripts = []Witness{{InvocationScript: []byte{}, VerificationScript: []byte{}}}
|
||||
|
||||
// Prime cache.
|
||||
tx.Hash()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = tx.Hash()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ func RipeMD160(data []byte) util.Uint160 {
|
|||
hasher := ripemd160.New()
|
||||
_, _ = hasher.Write(data)
|
||||
|
||||
hash, _ = util.Uint160DecodeBytesBE(hasher.Sum(nil))
|
||||
hasher.Sum(hash[:0])
|
||||
return hash
|
||||
}
|
||||
|
||||
|
|
|
@ -317,7 +317,9 @@ func (v *VM) loadScriptWithCallingHash(b []byte, exe *nef.File, caller util.Uint
|
|||
|
||||
v.checkInvocationStackSize()
|
||||
ctx := NewContextWithParams(b, rvcount, offset)
|
||||
if rvcount != -1 || v.estack.Len() != 0 {
|
||||
v.estack = newStack("evaluation", &v.refs)
|
||||
}
|
||||
ctx.estack = v.estack
|
||||
initStack(&ctx.tryStack, "exception", nil)
|
||||
ctx.callFlag = f
|
||||
|
|
Loading…
Reference in a new issue