github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/core/fee/calculate.go (about)

     1  package fee
     2  
     3  import (
     4  	"github.com/nspcc-dev/neo-go/pkg/io"
     5  	"github.com/nspcc-dev/neo-go/pkg/vm"
     6  	"github.com/nspcc-dev/neo-go/pkg/vm/emit"
     7  	"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
     8  )
     9  
    10  // ECDSAVerifyPrice is a gas price of a single verification.
    11  const ECDSAVerifyPrice = 1 << 15
    12  
    13  // Calculate returns network fee for a transaction.
    14  func Calculate(base int64, script []byte) (int64, int) {
    15  	var (
    16  		netFee int64
    17  		size   int
    18  	)
    19  	if vm.IsSignatureContract(script) {
    20  		size += 67 + io.GetVarSize(script)
    21  		netFee += Opcode(base, opcode.PUSHDATA1, opcode.PUSHDATA1) + base*ECDSAVerifyPrice
    22  	} else if m, pubs, ok := vm.ParseMultiSigContract(script); ok {
    23  		n := len(pubs)
    24  		sizeInv := 66 * m
    25  		size += io.GetVarSize(sizeInv) + sizeInv + io.GetVarSize(script)
    26  		netFee += calculateMultisig(base, m) + calculateMultisig(base, n)
    27  		netFee += base * ECDSAVerifyPrice * int64(n)
    28  	} /*else {
    29  		// We can support more contract types in the future.
    30  	}*/
    31  	return netFee, size
    32  }
    33  
    34  func calculateMultisig(base int64, n int) int64 {
    35  	result := Opcode(base, opcode.PUSHDATA1) * int64(n)
    36  	bw := io.NewBufBinWriter()
    37  	emit.Int(bw.BinWriter, int64(n))
    38  	// it's a hack because coefficients of small PUSH* opcodes are equal
    39  	result += Opcode(base, opcode.Opcode(bw.Bytes()[0]))
    40  	return result
    41  }