2020-09-28 14:56:16 +00:00
|
|
|
package fee
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2021-02-05 08:25:22 +00:00
|
|
|
// ECDSAVerifyPrice is a gas price of a single verification.
|
|
|
|
const ECDSAVerifyPrice = 1 << 15
|
|
|
|
|
2020-09-28 14:56:16 +00:00
|
|
|
// 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)
|
2021-03-05 07:18:03 +00:00
|
|
|
netFee += Opcode(base, opcode.PUSHDATA1, opcode.PUSHDATA1) + base*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)
|
2021-03-09 15:11:21 +00:00
|
|
|
netFee += base * ECDSAVerifyPrice * int64(n)
|
2021-05-12 16:10:31 +00:00
|
|
|
} /*else {
|
2020-09-28 14:56:16 +00:00
|
|
|
// We can support more contract types in the future.
|
2021-05-12 16:10:31 +00:00
|
|
|
}*/
|
2020-09-28 14:56:16 +00:00
|
|
|
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
|
|
|
|
}
|