Skip to content

Commit

Permalink
feat: #369 wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lucanicoladebiasi committed Apr 12, 2024
1 parent 68295e0 commit 33dff68
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 20 deletions.
14 changes: 7 additions & 7 deletions apps/sdk-node-integration/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -939,10 +939,10 @@
dependencies:
"@types/yargs-parser" "*"

"@vechain/sdk-core@1.0.0-beta.2":
"@vechain/sdk-core@1.0.0-beta.3":
version "1.0.0-beta.3"
resolved "https://registry.yarnpkg.com/@vechain/sdk-core/-/sdk-core-1.0.0-beta.2.tgz#5b217f2fef47f2507a369f65e149f4aa6ac2fb65"
integrity sha512-eGJjts+/eJdybSnb7Y1q9d53vkeRuJB0kTsdUZDGf8M7JrkCzNEkdzxiXXWnP5hUItFbqJHPSH7Kl7k8DOknjw==
resolved "https://registry.yarnpkg.com/@vechain/sdk-core/-/sdk-core-1.0.0-beta.3.tgz#e095902dced4bd2a4a8c9b65033f6ac4b0226dc1"
integrity sha512-PgKjCPOwb+9rAXp8FTcrG1UwFnAaV0gzfmbCA/pPiEr8jOIrQd5+1S4Q9vDZ5dVDCnJq73y2OH5zPf4ff84IAA==
dependencies:
"@ethereumjs/rlp" "^5.0.2"
"@types/elliptic" "^6.4.18"
Expand All @@ -951,10 +951,10 @@
elliptic "^6.5.5"
ethers "6.11.1"

"@vechain/sdk-errors@1.0.0-beta.2":
"@vechain/sdk-errors@1.0.0-beta.3":
version "1.0.0-beta.3"
resolved "https://registry.yarnpkg.com/@vechain/sdk-errors/-/sdk-errors-1.0.0-beta.2.tgz#29ee4b4b0a4371a83e823b52170af8bb45e1085d"
integrity sha512-O7CVZ15jzrFZmDEvymeRl5mhLug1pGYuOsJzU8a1V3qk5wzyzIZthMT1eK6xczapJsLArx6F82vMR+5mYIDQOA==
resolved "https://registry.yarnpkg.com/@vechain/sdk-errors/-/sdk-errors-1.0.0-beta.3.tgz#44eb37d4ef0d1b050373b2611256667a9ee8f7b4"
integrity sha512-ojuCG/sA4gGQqhpAqLtC3T096TtJEEd+2WhdifZL5IGxQX34Gn/eJF8UDo7DapybAUhWUDTdOO8pI/0zLEetsQ==

acorn-walk@^8.1.1:
version "8.3.2"
Expand Down Expand Up @@ -1588,7 +1588,7 @@ function-bind@^1.1.2:
integrity sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==

gensync@^1.0.0-beta.2:
version "1.0.0-beta.3"
version "1.0.0-beta.2"
resolved "https://registry.yarnpkg.com/gensync/-/gensync-1.0.0-beta.2.tgz#32a6ee76c3d7f52d46b2b1ae5d93fea8580a25e0"
integrity sha512-3hN7NaskYvMDLQY55gnW3NQ+mesEAepTqlg+VEbj7zzqEMBVNhzcGYYeqFo/TlYz6eQiFcp1HcsCZO+nGgS8zg==

Expand Down
9 changes: 1 addition & 8 deletions packages/core/src/bloom/bloom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,9 @@ class Generator {
this.hashes.set(hash(key), true);
}

/**
* Generates a variable-length Bloom filter based on the bits per key and count of keys.
* The generator will be reset after generation.
*
* @param bitsPerKey - The number of bits per key.
* @param k - The number of hash functions to be used (count of keys).
* @returns A new Filter instance.
*/
/**
* Generates a Bloom filter with the specified number of bits per key and number of hash functions.
* The generator will be reset after generation.
*
* @param {number} bitsPerKey - The desired number of bits per key in the Bloom filter.
* @param {number} k - The number of hash functions to use in the Bloom filter.
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/utils/bloom/bloom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ const isAddressInBloom = (
'Invalid address given as input in Bloom filter. Ensure it is a valid vechain thor address.',
{ addressToCheck }
);

return isInBloom(bloom, k, addressToCheck);
};

Expand Down
70 changes: 68 additions & 2 deletions packages/core/tests/utils/bloom/bloom.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { describe, expect, test } from '@jest/globals';
import { bloomUtils } from '../../../src';
import { addressUtils, bloom, bloomUtils, Hex, Hex0x } from '../../../src';
import {
bloomTestCases,
blooms,
invalidAddressBloomTestCases,
validAddressBloomTestCases,
valueTypeBloomTestCases
valueTypeBloomTestCases,
expandedBlockDetail
} from './fixture';
import {
InvalidBloomError,
InvalidDataTypeError,
InvalidKError
} from '@vechain/sdk-errors';
import {
type Clause,
type ExpandedBlockDetail,
type TransactionsExpandedBlockDetail
} from '@vechain/sdk-network';
import * as utils from '@noble/curves/abstract/utils';

/**
* Bloom utils tests
Expand Down Expand Up @@ -125,4 +132,63 @@ describe('utils/bloom', () => {
}
);
});

test('boolUtils.getAddressesOf', () => {
const addresses = getAddressesOf(expandedBlockDetail).filter(
(address) => {
return addressUtils.isAddress(address);
}
);
const filter = bloomFilterOf(addresses);
console.log(filter);
console.log(filter.length);
addresses.forEach((address) => {
expect(
bloomUtils.isAddressInBloom(filter, 30, Hex0x.canon(address))
).toBeTruthy();
});
});
});

export function bloomFilterOf(addresses: string[]): string {
const keys: Uint8Array[] = [];
addresses.forEach((address) => {
if (addressUtils.isAddress(Hex0x.canon(address))) {
keys.push(utils.hexToBytes(Hex.canon(address)));
}
});
const generator = new bloom.Generator();
keys.forEach((key) => {
generator.add(key);
});
return utils.bytesToHex(generator.generate(20 * 8, 30).bits);
}

function getAddressesOf(block: ExpandedBlockDetail): string[] {
const addresses: string[] = [block.beneficiary, block.signer];
block.transactions.forEach(
(transaction: TransactionsExpandedBlockDetail) => {
transaction.clauses.forEach((clause: Clause) => {
if (typeof clause.to === 'string') {
addresses.push(clause.to);
}
});
addresses.push(transaction.delegator);
addresses.push(transaction.gasPayer);
addresses.push(transaction.origin);
transaction.outputs.forEach((output) => {
if (typeof output.contractAddress === 'string') {
addresses.push(output.contractAddress);
}
output.events.forEach((event) => {
addresses.push(event.address);
});
output.transfers.forEach((transfer) => {
addresses.push(transfer.recipient);
addresses.push(transfer.sender);
});
});
}
);
return addresses;
}
229 changes: 227 additions & 2 deletions packages/core/tests/utils/bloom/fixture.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { InvalidAddressError } from '@vechain/sdk-errors';
import { Hex } from '../../../src';
import { type ExpandedBlockDetail } from '@vechain/sdk-network';

/**
* Blooms filter used to check if "it is a bloom filter or not" verification works
Expand Down Expand Up @@ -124,10 +125,234 @@ const validAddressBloomTestCases = [
}
];

const expandedBlockDetail: ExpandedBlockDetail = {
number: 17229578,
id: '0x0106e70ac5e7a11f0d508a0f5dc5916e7e0bc837e4de4f79c9cfbef6a702ea32',
size: 1255,
parentID:
'0x0106e7096f12cedd83f4922791599ac90fadc054d796e4a47581e8726cb4c369',
timestamp: 1702821970,
gasLimit: 30000000,
beneficiary: '0x1eef8963e1222417af4dac0d98553abddb4a76b5',
gasUsed: 290302,
totalScore: 1680196497,
txsRoot:
'0xcbe8f25d8ef87a2d2664157d9df8872ec9840584423d2e327d7f31b697865164',
txsFeatures: 1,
stateRoot:
'0xbe07b1207976cadcdaff95bf4bca52c7f856e13711be9dcc1f4804001b0ff58b',
receiptsRoot:
'0x2a6f43d81cc422a6daf359b28c9e696c94b7246f9052c3ddbff523bc3ef4cbb7',
com: true,
signer: '0x6298c7a54720febdefd741d0899d287c70954c68',
isTrunk: true,
isFinalized: false,
transactions: [
{
id: '0x9c2964962df2e2f9e6f378726d47df7c7a948dd98112043c838185f74c6a4e2f',
chainTag: '74',
blockRef: '0x0106e7096f12cedd',
expiration: 32,
clauses: [
{
to: '0x576da7124c7bb65a692d95848276367e5a844d95',
value: '0x1043561a8829300000',
data: '0x7ff36ab50000000000000000000000000000000000000000000002394e270e08364000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000a416bdda32b00e218f08ace220bab512c863ff2f00000000000000000000000000000000000000000000000000000000657f0433000000000000000000000000000000000000000000000000000000000000000200000000000000000000000045429a2255e7248e57fce99e7239aed3f84b7a530000000000000000000000005db3c8a942333f6468176a870db36eef120a34dc'
}
],
gasPriceCoef: 255,
gas: 253631,
origin: '0xa416bdda32b00e218f08ace220bab512c863ff2f',
delegator: '',
nonce: '0x2603c69707b46b55',
dependsOn: '',
size: 365,
gasUsed: 223631,
gasPayer: '0xa416bdda32b00e218f08ace220bab512c863ff2f',
paid: '0x3e11f0316b4ec000',
reward: '0x129efb420697a000',
reverted: false,
outputs: [
{
contractAddress: null,
events: [
{
address:
'0x45429a2255e7248e57fce99e7239aed3f84b7a53',
topics: [
'0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c',
'0x000000000000000000000000576da7124c7bb65a692d95848276367e5a844d95'
],
data: '0x00000000000000000000000000000000000000000000001043561a8829300000'
},
{
address:
'0x45429a2255e7248e57fce99e7239aed3f84b7a53',
topics: [
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
'0x000000000000000000000000576da7124c7bb65a692d95848276367e5a844d95',
'0x0000000000000000000000001a8abd6d5627eb26ad71c0c7ae5224cdc640faf3'
],
data: '0x00000000000000000000000000000000000000000000001043561a8829300000'
},
{
address:
'0x5db3c8a942333f6468176a870db36eef120a34dc',
topics: [
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
'0x0000000000000000000000001a8abd6d5627eb26ad71c0c7ae5224cdc640faf3',
'0x000000000000000000000000a416bdda32b00e218f08ace220bab512c863ff2f'
],
data: '0x00000000000000000000000000000000000000000000023b04b1d40b515745aa'
},
{
address:
'0x1a8abd6d5627eb26ad71c0c7ae5224cdc640faf3',
topics: [
'0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1'
],
data: '0x000000000000000000000000000000000000000000002f87e3cb2aaf503b96140000000000000000000000000000000000000000000687a8d57a3a1d0de7f46c'
},
{
address:
'0x1a8abd6d5627eb26ad71c0c7ae5224cdc640faf3',
topics: [
'0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822',
'0x000000000000000000000000576da7124c7bb65a692d95848276367e5a844d95',
'0x000000000000000000000000a416bdda32b00e218f08ace220bab512c863ff2f'
],
data: '0x00000000000000000000000000000000000000000000001043561a88293000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023b04b1d40b515745aa'
}
],
transfers: [
{
sender: '0xa416bdda32b00e218f08ace220bab512c863ff2f',
recipient:
'0x576da7124c7bb65a692d95848276367e5a844d95',
amount: '0x1043561a8829300000'
},
{
sender: '0x576da7124c7bb65a692d95848276367e5a844d95',
recipient:
'0x45429a2255e7248e57fce99e7239aed3f84b7a53',
amount: '0x1043561a8829300000'
}
]
}
]
},
{
id: '0x3e15b509f2109a60c3f1cfdb198b59b46d4792b777333119e819e11b69a190d8',
chainTag: '74',
blockRef: '0x0106e7096f12cedd',
expiration: 720,
clauses: [
{
to: '0x23a46368e4acc7bb2fe0afeb054def51ec56aa74',
value: '0xac576e2a31c35a0000',
data: '0x000000606060'
}
],
gasPriceCoef: 128,
gas: 29000,
origin: '0xbeae4bef0121f11d269aedf6adb227259d4314ad',
delegator: '0xbeae4bef0121f11d269aedf6adb227259d4314ad',
nonce: '0x33f2b93',
dependsOn: '',
size: 200,
gasUsed: 21216,
gasPayer: '0xbeae4bef0121f11d269aedf6adb227259d4314ad',
paid: '0x46c17f1958beae0',
reward: '0x153a0c879a9f9a9',
reverted: false,
outputs: [
{
contractAddress: null,
events: [],
transfers: [
{
sender: '0xbeae4bef0121f11d269aedf6adb227259d4314ad',
recipient:
'0x23a46368e4acc7bb2fe0afeb054def51ec56aa74',
amount: '0xac576e2a31c35a0000'
}
]
}
]
},
{
id: '0x09804e17727ca0bee6521ae50e15653a8770edade8ac0e36bdda646e09af9c13',
chainTag: '74',
blockRef: '0x0106e7096f12cedd',
expiration: 720,
clauses: [
{
to: '0x0000000000000000000000000000456e65726779',
value: '0x0',
data: '0xa9059cbb00000000000000000000000031437d38b38a5cb28dacb0379dbf0b5185e10643000000000000000000000000000000000000000000fbf26224799870e5280000'
}
],
gasPriceCoef: 10,
gas: 31990,
origin: '0x95fe74d1ae072ee45bdb09879a157364e5341565',
delegator: '',
nonce: '0x4b129bf90751676e',
dependsOn: '',
size: 193,
gasUsed: 24455,
gasPayer: '0x95fe74d1ae072ee45bdb09879a157364e5341565',
paid: '0x386e3296e67232f',
reward: '0x10eddbfa11ef0f4',
reverted: true,
outputs: []
},
{
id: '0xab4c2867d90f6ea27e8eac6b180ca43c267be82593ac15aa801bf70de7301354',
chainTag: '74',
blockRef: '0x0106e7096f12cedd',
expiration: 720,
clauses: [
{
to: '0xb7591602c0c9d525bc3a7cf3c729fd91b8bf5bf6',
value: '0xe18cc1b1e0e8c20000',
data: '0x'
}
],
gasPriceCoef: 0,
gas: 21000,
origin: '0x9a107a75cff525b033a3e53cadafe3d193b570ec',
delegator: '',
nonce: '0x7bced3146d8fb8fc',
dependsOn: '',
size: 131,
gasUsed: 21000,
gasPayer: '0x9a107a75cff525b033a3e53cadafe3d193b570ec',
paid: '0x2ea11e32ad50000',
reward: '0xdfd22a8cd98000',
reverted: false,
outputs: [
{
contractAddress: null,
events: [],
transfers: [
{
sender: '0x9a107a75cff525b033a3e53cadafe3d193b570ec',
recipient:
'0xb7591602c0c9d525bc3a7cf3c729fd91b8bf5bf6',
amount: '0xe18cc1b1e0e8c20000'
}
]
}
]
}
]
};

export {
blooms,
bloomTestCases,
valueTypeBloomTestCases,
expandedBlockDetail,
invalidAddressBloomTestCases,
validAddressBloomTestCases
validAddressBloomTestCases,
valueTypeBloomTestCases
};

0 comments on commit 33dff68

Please sign in to comment.