forked from TrueCloudLab/neoneo-go
Merge pull request #2136 from nspcc-dev/micro-reduce-allocs
Drop some useless allocations
This commit is contained in:
commit
37c92f5c2f
4 changed files with 45 additions and 58 deletions
|
@ -107,9 +107,11 @@ func New(stateRootEnabled bool) *Block {
|
|||
// Notice that only the hashes of the transactions are stored.
|
||||
func (b *Block) Trim() ([]byte, error) {
|
||||
buf := io.NewBufBinWriter()
|
||||
numTx := len(b.Transactions)
|
||||
buf.Grow(b.GetExpectedBlockSizeWithoutTransactions(numTx) + util.Uint256Size*numTx)
|
||||
b.Header.EncodeBinary(buf.BinWriter)
|
||||
|
||||
buf.WriteVarUint(uint64(len(b.Transactions)))
|
||||
buf.WriteVarUint(uint64(numTx))
|
||||
for _, tx := range b.Transactions {
|
||||
h := tx.Hash()
|
||||
h.EncodeBinary(buf.BinWriter)
|
||||
|
|
|
@ -27,7 +27,6 @@ import (
|
|||
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
||||
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
||||
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
||||
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
||||
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
||||
|
@ -1038,49 +1037,35 @@ func (bc *Blockchain) handleNotification(note *state.NotificationEvent, d dao.DA
|
|||
if !ok || len(arr) != 3 {
|
||||
return
|
||||
}
|
||||
var from []byte
|
||||
fromValue := arr[0].Value()
|
||||
// we don't have `from` set when we are minting tokens
|
||||
if fromValue != nil {
|
||||
from, ok = fromValue.([]byte)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
from, err := parseUint160(arr[0])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var to []byte
|
||||
toValue := arr[1].Value()
|
||||
// we don't have `to` set when we are burning tokens
|
||||
if toValue != nil {
|
||||
to, ok = toValue.([]byte)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
to, err := parseUint160(arr[1])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
amount, ok := arr[2].Value().(*big.Int)
|
||||
if !ok {
|
||||
bs, ok := arr[2].Value().([]byte)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if len(bs) > bigint.MaxBytesLen {
|
||||
return // Not a proper number.
|
||||
}
|
||||
amount = bigint.FromBytes(bs)
|
||||
amount, err := arr[2].TryInteger()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
bc.processNEP17Transfer(d, transCache, h, b, note.ScriptHash, from, to, amount)
|
||||
}
|
||||
|
||||
func parseUint160(addr []byte) util.Uint160 {
|
||||
if u, err := util.Uint160DecodeBytesBE(addr); err == nil {
|
||||
return u
|
||||
func parseUint160(itm stackitem.Item) (util.Uint160, error) {
|
||||
_, ok := itm.(stackitem.Null) // Minting or burning.
|
||||
if ok {
|
||||
return util.Uint160{}, nil
|
||||
}
|
||||
return util.Uint160{}
|
||||
bytes, err := itm.TryBytes()
|
||||
if err != nil {
|
||||
return util.Uint160{}, err
|
||||
}
|
||||
return util.Uint160DecodeBytesBE(bytes)
|
||||
}
|
||||
|
||||
func (bc *Blockchain) processNEP17Transfer(cache dao.DAO, transCache map[util.Uint160]transferData,
|
||||
h util.Uint256, b *block.Block, sc util.Uint160, from, to []byte, amount *big.Int) {
|
||||
toAddr := parseUint160(to)
|
||||
fromAddr := parseUint160(from)
|
||||
h util.Uint256, b *block.Block, sc util.Uint160, from util.Uint160, to util.Uint160, amount *big.Int) {
|
||||
var id int32
|
||||
nativeContract := bc.contracts.ByHash(sc)
|
||||
if nativeContract != nil {
|
||||
|
@ -1094,21 +1079,21 @@ func (bc *Blockchain) processNEP17Transfer(cache dao.DAO, transCache map[util.Ui
|
|||
}
|
||||
transfer := &state.NEP17Transfer{
|
||||
Asset: id,
|
||||
From: fromAddr,
|
||||
To: toAddr,
|
||||
From: from,
|
||||
To: to,
|
||||
Block: b.Index,
|
||||
Timestamp: b.Timestamp,
|
||||
Tx: h,
|
||||
}
|
||||
if !fromAddr.Equals(util.Uint160{}) {
|
||||
if !from.Equals(util.Uint160{}) {
|
||||
_ = transfer.Amount.Neg(amount) // We already have the Int.
|
||||
if appendNEP17Transfer(cache, transCache, fromAddr, transfer) != nil {
|
||||
if appendNEP17Transfer(cache, transCache, from, transfer) != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
if !toAddr.Equals(util.Uint160{}) {
|
||||
_ = transfer.Amount.Set(amount) // We already have the Int.
|
||||
_ = appendNEP17Transfer(cache, transCache, toAddr, transfer) // Nothing useful we can do.
|
||||
if !to.Equals(util.Uint160{}) {
|
||||
_ = transfer.Amount.Set(amount) // We already have the Int.
|
||||
_ = appendNEP17Transfer(cache, transCache, to, transfer) // Nothing useful we can do.
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -57,23 +57,19 @@ func NewContext(trigger trigger.Type, bc blockchainer.Blockchainer, d dao.DAO,
|
|||
block *block.Block, tx *transaction.Transaction, log *zap.Logger) *Context {
|
||||
baseExecFee := int64(DefaultBaseExecFee)
|
||||
dao := d.GetWrapped()
|
||||
nes := make([]state.NotificationEvent, 0)
|
||||
|
||||
if bc != nil && (block == nil || block.Index != 0) {
|
||||
baseExecFee = bc.GetPolicer().GetBaseExecFee()
|
||||
}
|
||||
return &Context{
|
||||
Chain: bc,
|
||||
Network: uint32(bc.GetConfig().Magic),
|
||||
Natives: natives,
|
||||
Trigger: trigger,
|
||||
Block: block,
|
||||
Tx: tx,
|
||||
DAO: dao,
|
||||
Notifications: nes,
|
||||
Log: log,
|
||||
// Functions is a slice of interops sorted by ID.
|
||||
Functions: []Function{},
|
||||
Chain: bc,
|
||||
Network: uint32(bc.GetConfig().Magic),
|
||||
Natives: natives,
|
||||
Trigger: trigger,
|
||||
Block: block,
|
||||
Tx: tx,
|
||||
DAO: dao,
|
||||
Log: log,
|
||||
getContract: getContract,
|
||||
baseExecFee: baseExecFee,
|
||||
}
|
||||
|
|
|
@ -5,18 +5,22 @@ import (
|
|||
"errors"
|
||||
)
|
||||
|
||||
// ErrDrained is returned on an attempt to use already drained write buffer.
|
||||
var ErrDrained = errors.New("buffer already drained")
|
||||
|
||||
// BufBinWriter is an additional layer on top of BinWriter that
|
||||
// automatically creates buffer to write into that you can get after all
|
||||
// writes via Bytes().
|
||||
type BufBinWriter struct {
|
||||
*BinWriter
|
||||
buf *bytes.Buffer
|
||||
buf bytes.Buffer
|
||||
}
|
||||
|
||||
// NewBufBinWriter makes a BufBinWriter with an empty byte buffer.
|
||||
func NewBufBinWriter() *BufBinWriter {
|
||||
b := new(bytes.Buffer)
|
||||
return &BufBinWriter{BinWriter: NewBinWriterFromIO(b), buf: b}
|
||||
b := new(BufBinWriter)
|
||||
b.BinWriter = NewBinWriterFromIO(&b.buf)
|
||||
return b
|
||||
}
|
||||
|
||||
// Len returns the number of bytes of the unread portion of the buffer.
|
||||
|
@ -29,7 +33,7 @@ func (bw *BufBinWriter) Bytes() []byte {
|
|||
if bw.Err != nil {
|
||||
return nil
|
||||
}
|
||||
bw.Err = errors.New("buffer already drained")
|
||||
bw.Err = ErrDrained
|
||||
return bw.buf.Bytes()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue