2020-09-28 14:56:16 +00:00
|
|
|
package fee
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/crypto"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Calculate returns network fee for transaction
|
2020-12-11 12:22:49 +00:00
|
|
|
func Calculate(base int64, script []byte) (int64, int) {
|
2020-09-28 14:56:16 +00:00
|
|
|
var (
|
|
|
|
netFee int64
|
|
|
|
size int
|
|
|
|
)
|
|
|
|
if vm.IsSignatureContract(script) {
|
|
|
|
size += 67 + io.GetVarSize(script)
|
2020-12-11 12:22:49 +00:00
|
|
|
netFee += Opcode(base, opcode.PUSHDATA1, opcode.PUSHNULL, opcode.PUSHDATA1) + base*crypto.ECDSAVerifyPrice
|
2020-09-28 14:56:16 +00:00
|
|
|
} else if m, pubs, ok := vm.ParseMultiSigContract(script); ok {
|
|
|
|
n := len(pubs)
|
|
|
|
sizeInv := 66 * m
|
|
|
|
size += io.GetVarSize(sizeInv) + sizeInv + io.GetVarSize(script)
|
2020-12-11 12:22:49 +00:00
|
|
|
netFee += calculateMultisig(base, m) + calculateMultisig(base, n)
|
|
|
|
netFee += Opcode(base, opcode.PUSHNULL) + base*crypto.ECDSAVerifyPrice*int64(n)
|
2020-09-28 14:56:16 +00:00
|
|
|
} else {
|
|
|
|
// We can support more contract types in the future.
|
|
|
|
}
|
|
|
|
return netFee, size
|
|
|
|
}
|
|
|
|
|
2020-12-11 12:22:49 +00:00
|
|
|
func calculateMultisig(base int64, n int) int64 {
|
|
|
|
result := Opcode(base, opcode.PUSHDATA1) * int64(n)
|
2020-09-28 14:56:16 +00:00
|
|
|
bw := io.NewBufBinWriter()
|
|
|
|
emit.Int(bw.BinWriter, int64(n))
|
2020-12-11 12:22:49 +00:00
|
|
|
// it's a hack because coefficients of small PUSH* opcodes are equal
|
|
|
|
result += Opcode(base, opcode.Opcode(bw.Bytes()[0]))
|
2020-09-28 14:56:16 +00:00
|
|
|
return result
|
|
|
|
}
|