@onekeyfe/kaspa-core-lib 1.0.0-alpha.3 → 1.0.1-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -164,6 +164,8 @@ declare module '@onekeyfe/kaspa-core-lib' {
164
164
  lockUntilBlockHeight(height: number): this;
165
165
 
166
166
  getMassAndSize():{txSize:number, mass:number};
167
+ calcStorageMass():number;
168
+ calcComputeMass():number;
167
169
 
168
170
  hasWitnesses(): boolean;
169
171
  getFee(): number;
package/lib/privatekey.js CHANGED
@@ -379,7 +379,7 @@ PrivateKey.prototype.toPublicKey = function () {
379
379
  if (!this._pubkey) {
380
380
  //this._pubkey = PublicKey.fromPrivateKey(this);
381
381
  let publicKey = secp256k1.getPublicKey(this.toString(), true);
382
- console.log("publicKey", Buffer.from(publicKey).toString('hex'));
382
+ // console.log("publicKey", Buffer.from(publicKey).toString('hex'));
383
383
  this._pubkey = new PublicKey(Buffer.from(publicKey), {
384
384
  network: this.network.name,
385
385
  });
@@ -80,7 +80,7 @@ Output.prototype.invalidSatoshis = function() {
80
80
  if(!BN.fromString(this._satoshis).eq(this._satoshisBN)) {
81
81
  return 'transaction txout satoshis has corrupted value';
82
82
  }
83
- if(BN.fromString(this._satoshis).lt(new BN(0))) {
83
+ if(BN.fromString(this._satoshis).lt(BN.Zero)) {
84
84
  return 'transaction txout negative';
85
85
  }
86
86
  return false;
@@ -106,6 +106,7 @@ Transaction.NLOCKTIME_MAX_VALUE = 4294967295;
106
106
 
107
107
  // Value used for fee estimation (satoshis per kilobyte)
108
108
  Transaction.FEE_PER_KB = 100000;
109
+ Transaction.STORAGE_MASS = new BN(10).pow(new BN(12));
109
110
 
110
111
 
111
112
  // Safe upper bound for change address script size in bytes
@@ -226,7 +227,7 @@ Transaction.prototype.getSerializationError = function(opts) {
226
227
 
227
228
  var unspent = this._getUnspentValue();
228
229
  var unspentError;
229
- if (unspent.lt(0)) {
230
+ if (unspent.lt(BN.Zero)) {
230
231
  if (!opts.disableMoreOutputThanInput) {
231
232
  unspentError = new errors.Transaction.InvalidOutputAmountSum();
232
233
  }
@@ -249,7 +250,7 @@ Transaction.prototype._hasFeeError = function(opts, unspent) {
249
250
 
250
251
  if (!opts.disableLargeFees) {
251
252
  var maximumFee = Math.floor(Transaction.FEE_SECURITY_MARGIN * this._estimateFee());
252
- if (unspent > maximumFee) {
253
+ if (unspent.gt(new BN(maximumFee))) {
253
254
  if (this._missingChange()) {
254
255
  return new errors.Transaction.ChangeAddressMissing(
255
256
  'Fee is too large and no change address was provided'
@@ -262,7 +263,7 @@ Transaction.prototype._hasFeeError = function(opts, unspent) {
262
263
  }
263
264
  if (!opts.disableSmallFees) {
264
265
  var minimumFee = Math.ceil(this._estimateFee() / Transaction.FEE_SECURITY_MARGIN);
265
- if (unspent < minimumFee) {
266
+ if (unspent.lt(new BN(minimumFee))) {
266
267
  return new errors.Transaction.FeeError.TooSmall(
267
268
  'expected more than ' + minimumFee + ' but got ' + unspent
268
269
  );
@@ -381,17 +382,19 @@ Transaction.prototype.getStandaloneMass = function() {
381
382
  }
382
383
  }
383
384
  Transaction.prototype.getMassAndSize = function() {
384
- let {txSize, mass:standaloneMass} = this.getStandaloneMass();
385
- let sigOpsCount = 0;
386
- this.inputs.forEach(input=>{
387
- //console.log("input.script", input.output.script.toASM())
388
- sigOpsCount += input.output.script.getSignatureOperationsCount()
389
- });
385
+ let {txSize} = this.getStandaloneMass();
386
+
387
+ let storageMass = this.calcStorageMass();
388
+ let computeMass = this.calcComputeMass();
389
+
390
+ console.log("storageMass", storageMass)
391
+ console.log("computeMass", computeMass)
390
392
 
391
- //console.log("standaloneMass", standaloneMass)
392
- //console.log("sigOpsCount", sigOpsCount)
393
- //console.log("total MASS ", standaloneMass + sigOpsCount * Transaction.MassPerSigOp)
394
- return {txSize, mass: standaloneMass + sigOpsCount * Transaction.MassPerSigOp};
393
+ // alpha version 11
394
+ // mass = storageMass + computeMass
395
+
396
+ // current version 10
397
+ return {txSize, mass: Math.max(storageMass, computeMass)};
395
398
  }
396
399
  Transaction.prototype.fromBuffer = function(buffer) {
397
400
  var reader = new BufferReader(buffer);
@@ -971,7 +974,7 @@ Transaction.prototype._updateChangeOutput = function() {
971
974
  var available = this._getUnspentValue();
972
975
  var fee = this.getFee();
973
976
  var changeAmount = available.sub(new BN(fee));
974
- if (changeAmount.gt(0)) {
977
+ if (changeAmount.gt(BN.Zero)) {
975
978
  this._changeIndex = this.outputs.length;
976
979
  this._addOutput(new Output({
977
980
  script: this._changeScript,
@@ -1026,7 +1029,7 @@ Transaction.prototype._estimateFee = function() {
1026
1029
  }
1027
1030
  var fee = Math.ceil(getFee(estimatedSize));
1028
1031
  var feeWithChange = Math.ceil(getFee(estimatedSize) + getFee(Transaction.CHANGE_OUTPUT_MAX_SIZE));
1029
- if (!this._changeScript || available.lte(feeWithChange)) {
1032
+ if (!this._changeScript || available.lte(BN.fromNumber(feeWithChange))) {
1030
1033
  return fee;
1031
1034
  }
1032
1035
  return feeWithChange;
@@ -1042,14 +1045,33 @@ Transaction.prototype._clearSignatures = function() {
1042
1045
  });
1043
1046
  };
1044
1047
 
1048
+ // https://github.com/kaspanet/rusty-kaspa/blob/613d082b0c2e51247819d932af7ddb5ebd5aa460/consensus/core/src/mass/mod.rs#L23-L42
1045
1049
  Transaction.prototype._estimateSize = function() {
1046
- var result = Transaction.MAXIMUM_EXTRA_SIZE;
1050
+ var result = 0;
1051
+ result += 2; // Tx version (u16)
1052
+ result += 8; // Number of inputs (u64)
1047
1053
  _.each(this.inputs, function(input) {
1054
+ if(input._scriptBuffer.length === 0){
1055
+ result += 65; // script
1056
+ }
1048
1057
  result += input._estimateSize();
1049
1058
  });
1059
+
1060
+ result += 8; // Number of outputs (u64)
1050
1061
  _.each(this.outputs, function(output) {
1051
- result += output.script.toBuffer().length + 9;
1062
+ result += 8; // value (u64)
1063
+ result += 2; // output.ScriptPublicKey.Version (u16)
1064
+ result += 8; // length of script public key (u64)
1065
+ result += output.script.toBuffer().length;
1052
1066
  });
1067
+
1068
+ result += 8; // lock time (u64)
1069
+ result += 20; // subnetwork id
1070
+ result += 8; // gas (u64)
1071
+ result += 32; // payload hash
1072
+
1073
+ result += 8; // length of the payload (u64)
1074
+ result += 32; // payload hash
1053
1075
  return result;
1054
1076
  };
1055
1077
 
@@ -1380,5 +1402,51 @@ Transaction.prototype.setVersion = function(version) {
1380
1402
  return this;
1381
1403
  };
1382
1404
 
1405
+ function negativeMass(inputs, outputs_num) {
1406
+ const inputs_num = inputs.length;
1407
+
1408
+ if (outputs_num === 1 || inputs_num === 1 || (outputs_num === 2 && inputs_num === 2)) {
1409
+ return inputs.reduce((sum, v) => sum.add(Transaction.STORAGE_MASS.div(BN.fromNumber(v.output.satoshis))), new BN(0));
1410
+ }
1411
+
1412
+ const sumInputs = inputs.reduce((sum, v) => sum.add(BN.fromNumber(v.output.satoshis)), new BN(0));
1413
+ const avgInput = sumInputs.div(new BN(inputs_num));
1414
+ return new BN(inputs_num).mul(Transaction.STORAGE_MASS.div(avgInput));
1415
+ }
1416
+
1417
+ Transaction.prototype.calcStorageMass = function() {
1418
+ const N = negativeMass(this.inputs, this.outputs.length);
1419
+ const P = this.outputs.reduce((sum, o) =>
1420
+ sum.add(Transaction.STORAGE_MASS.div(BN.fromNumber(o.satoshis))
1421
+ ), new BN(0));
1422
+ return BN.max(P.sub(N), new BN(0));
1423
+ }
1424
+
1425
+ Transaction.prototype.calcComputeMass = function() {
1426
+ // 如果是coinbase交易,返回0
1427
+ if (this.isCoinbase()) {
1428
+ return 0;
1429
+ }
1430
+
1431
+ // 计算序列化大小
1432
+ const size = this._estimateSize();
1433
+
1434
+ // 计算序列化交易的mass
1435
+ let massForSize = size * Transaction.MassPerTxByte;
1436
+
1437
+ // 计算所有输出的scriptPublicKey的mass
1438
+ const totalScriptPublicKeySize = this.outputs.reduce((sum, output) => {
1439
+ return sum + 2 + output._scriptBuffer.length;
1440
+ }, 0);
1441
+ const totalScriptPublicKeyMass = totalScriptPublicKeySize * Transaction.MassPerScriptPubKeyByte;
1442
+
1443
+ // 计算所有输入的sigOpCount的mass
1444
+ const totalSigops = this.inputs.reduce((sum, input) => {
1445
+ return sum + input.output.script.getSignatureOperationsCount();
1446
+ }, 0);
1447
+ const totalSigopsMass = totalSigops * Transaction.MassPerSigOp;
1448
+
1449
+ return Math.floor(massForSize + totalScriptPublicKeyMass + totalSigopsMass);
1450
+ };
1383
1451
 
1384
1452
  module.exports = Transaction;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/kaspa-core-lib",
3
- "version": "1.0.0-alpha.3",
3
+ "version": "1.0.1-alpha.1",
4
4
  "main": "index.js",
5
5
  "authors": [
6
6
  {