Skip to content

Commit

Permalink
refactor: enable htlc types via dedicated milestone (#3353)
Browse files Browse the repository at this point in the history
  • Loading branch information
spkjp authored and faustbrian committed Dec 20, 2019
1 parent 68e1c9d commit e246093
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 47 deletions.
4 changes: 4 additions & 0 deletions __tests__/helpers/transaction-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,13 @@ export class TransactionFactory {

if (sign) {
const aip11: boolean = Managers.configManager.getMilestone().aip11;
const htlcEnabled: boolean = Managers.configManager.getMilestone().htlcEnabled;
if (this.builder.data.version === 1 && aip11) {
Managers.configManager.getMilestone().aip11 = false;
Managers.configManager.getMilestone().htlcEnabled = false;
} else if (testnet) {
Managers.configManager.getMilestone().aip11 = true;
Managers.configManager.getMilestone().htlcEnabled = htlcEnabled;
}

this.builder.sign(this.passphrase);
Expand All @@ -380,6 +383,7 @@ export class TransactionFactory {

if (testnet) {
Managers.configManager.getMilestone().aip11 = true;
Managers.configManager.getMilestone().htlcEnabled = true;
}

transactions.push(transaction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ let walletManager: State.IWalletManager;

beforeAll(() => {
Managers.configManager.getMilestone().aip11 = true;
Managers.configManager.getMilestone().htlcEnabled = true;
jest.spyOn(Handlers.Registry, "isKnownWalletAttribute").mockReturnValue(true);
});

Expand Down
8 changes: 8 additions & 0 deletions __tests__/unit/crypto/transactions/deserializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,16 @@ describe("Transaction serializer / Deserializer", () => {
.build();

configManager.getMilestone().aip11 = false;
configManager.getMilestone().htlcEnabled = true;
expect(htlcLock.verify()).toBeFalse();
configManager.getMilestone().aip11 = true;
configManager.getMilestone().htlcEnabled = false;
expect(htlcLock.verify()).toBeFalse();
configManager.getMilestone().aip11 = false;
configManager.getMilestone().htlcEnabled = false;
expect(htlcLock.verify()).toBeFalse();
configManager.getMilestone().aip11 = true;
configManager.getMilestone().htlcEnabled = true;
expect(htlcLock.verify()).toBeTrue();
});
});
Expand Down
11 changes: 7 additions & 4 deletions packages/core-database/src/database-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class DatabaseService implements Database.IDatabaseService {
public async init(): Promise<void> {
if (process.env.CORE_ENV === "test") {
Managers.configManager.getMilestone().aip11 = false;
Managers.configManager.getMilestone().htlcEnabled = false;
}

this.emitter.emit(ApplicationEvents.StateStarting, this);
Expand Down Expand Up @@ -192,7 +193,7 @@ export class DatabaseService implements Database.IDatabaseService {

delegates = cloneDeep(delegates);
for (let i = 0, delCount = delegates.length; i < delCount; i++) {
for (let x = 0; x < 4 && i < delCount; i++, x++) {
for (let x = 0; x < 4 && i < delCount; i++ , x++) {
const newIndex = currentSeed[x] % delCount;
const b = delegates[newIndex];
delegates[newIndex] = delegates[i];
Expand Down Expand Up @@ -241,9 +242,9 @@ export class DatabaseService implements Database.IDatabaseService {
headersOnly || !block.transactions
? undefined
: block.transactions.map(
(transaction: string) =>
Transactions.TransactionFactory.fromBytesUnsafe(Buffer.from(transaction, "hex")).data,
),
(transaction: string) =>
Transactions.TransactionFactory.fromBytesUnsafe(Buffer.from(transaction, "hex")).data,
),
}));
}

Expand Down Expand Up @@ -384,6 +385,7 @@ export class DatabaseService implements Database.IDatabaseService {

if (block.height === 1 && process.env.CORE_ENV === "test") {
Managers.configManager.getMilestone().aip11 = true;
Managers.configManager.getMilestone().htlcEnabled = true;
}

return lastBlock;
Expand Down Expand Up @@ -642,6 +644,7 @@ export class DatabaseService implements Database.IDatabaseService {

if (process.env.CORE_ENV === "test") {
Managers.configManager.getMilestone().aip11 = true;
Managers.configManager.getMilestone().htlcEnabled = true;
}

this.configureState(lastBlock);
Expand Down
7 changes: 4 additions & 3 deletions packages/core-transactions/src/handlers/htlc-claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export class HtlcClaimTransactionHandler extends TransactionHandler {
}

public async isActivated(): Promise<boolean> {
return Managers.configManager.getMilestone().aip11 === true;
const milestone = Managers.configManager.getMilestone();
return milestone.aip11 === true && milestone.htlcEnabled === true;
}

public dynamicFee(context: IDynamicFeeContext): Utils.BigNumber {
Expand Down Expand Up @@ -190,11 +191,11 @@ export class HtlcClaimTransactionHandler extends TransactionHandler {
transaction: Interfaces.ITransaction,
walletManager: State.IWalletManager,
// tslint:disable-next-line: no-empty
): Promise<void> {}
): Promise<void> { }

public async revertForRecipient(
transaction: Interfaces.ITransaction,
walletManager: State.IWalletManager,
// tslint:disable-next-line: no-empty
): Promise<void> {}
): Promise<void> { }
}
7 changes: 4 additions & 3 deletions packages/core-transactions/src/handlers/htlc-lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export class HtlcLockTransactionHandler extends TransactionHandler {
}

public async isActivated(): Promise<boolean> {
return Managers.configManager.getMilestone().aip11 === true;
const milestone = Managers.configManager.getMilestone();
return milestone.aip11 === true && milestone.htlcEnabled === true;
}

public async throwIfCannotBeApplied(
Expand Down Expand Up @@ -138,11 +139,11 @@ export class HtlcLockTransactionHandler extends TransactionHandler {
transaction: Interfaces.ITransaction,
walletManager: State.IWalletManager,
// tslint:disable-next-line: no-empty
): Promise<void> {}
): Promise<void> { }

public async revertForRecipient(
transaction: Interfaces.ITransaction,
walletManager: State.IWalletManager,
// tslint:disable-next-line: no-empty
): Promise<void> {}
): Promise<void> { }
}
7 changes: 4 additions & 3 deletions packages/core-transactions/src/handlers/htlc-refund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export class HtlcRefundTransactionHandler extends TransactionHandler {
}

public async isActivated(): Promise<boolean> {
return Managers.configManager.getMilestone().aip11 === true;
const milestone = Managers.configManager.getMilestone();
return milestone.aip11 === true && milestone.htlcEnabled === true;
}

public dynamicFee(context: IDynamicFeeContext): Utils.BigNumber {
Expand Down Expand Up @@ -176,11 +177,11 @@ export class HtlcRefundTransactionHandler extends TransactionHandler {
transaction: Interfaces.ITransaction,
walletManager: State.IWalletManager,
// tslint:disable-next-line: no-empty
): Promise<void> {}
): Promise<void> { }

public async revertForRecipient(
transaction: Interfaces.ITransaction,
walletManager: State.IWalletManager,
// tslint:disable-next-line: no-empty
): Promise<void> {}
): Promise<void> { }
}
50 changes: 26 additions & 24 deletions packages/core/bin/config/devnet/peers.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
{
"list": [
{
"ip": "167.114.29.51",
"port": 4002
},
{
"ip": "167.114.29.52",
"port": 4002
},
{
"ip": "167.114.29.53",
"port": 4002
},
{
"ip": "167.114.29.54",
"port": 4002
},
{
"ip": "167.114.29.55",
"port": 4002
}
],
"sources": ["https://raw.githubusercontent.com/ARKEcosystem/peers/master/devnet.json"]
}
"list": [
{
"ip": "167.114.29.51",
"port": 4002
},
{
"ip": "167.114.29.52",
"port": 4002
},
{
"ip": "167.114.29.53",
"port": 4002
},
{
"ip": "167.114.29.54",
"port": 4002
},
{
"ip": "167.114.29.55",
"port": 4002
}
],
"sources": [
"https://raw.githubusercontent.com/ARKEcosystem/peers/master/devnet.json"
]
}
11 changes: 8 additions & 3 deletions packages/crypto/src/networks/devnet/milestones.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,19 @@
{
"height": 3963000,
"p2p": {
"minimumVersions": ["^2.6 || ^2.6.0-next.0"]
"minimumVersions": [
"^2.6 || ^2.6.0-next.0"
]
}
},
{
"height": 4006000,
"aip11": true,
"htlcEnabled": true,
"p2p": {
"minimumVersions": ["^2.6 || ^2.6.0-next.5"]
"minimumVersions": [
"^2.6 || ^2.6.0-next.5"
]
}
}
]
]
5 changes: 3 additions & 2 deletions packages/crypto/src/networks/testnet/milestones.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
},
{
"height": 2,
"aip11": true
"aip11": true,
"htlcEnabled": true
},
{
"height": 75600,
Expand All @@ -46,4 +47,4 @@
"idFullSha256": true
}
}
]
]
5 changes: 3 additions & 2 deletions packages/crypto/src/networks/unitnet/milestones.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
},
"vendorFieldLength": 64,
"multiPaymentLimit": 256,
"aip11": true
"aip11": true,
"htlcEnabled": true
},
{
"height": 75600,
Expand All @@ -43,4 +44,4 @@
"idFullSha256": true
}
}
]
]
3 changes: 2 additions & 1 deletion packages/crypto/src/transactions/types/htlc-claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export class HtlcClaimTransaction extends Transaction {
protected static defaultStaticFee: BigNumber = BigNumber.ZERO;

public verify(): boolean {
return configManager.getMilestone().aip11 && super.verify();
const milestone = configManager.getMilestone();
return milestone.aip11 === true && milestone.htlcEnabled === true && super.verify();
}

public serialize(options?: ISerializeOptions): ByteBuffer {
Expand Down
3 changes: 2 additions & 1 deletion packages/crypto/src/transactions/types/htlc-lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export class HtlcLockTransaction extends Transaction {
protected static defaultStaticFee: BigNumber = BigNumber.make("10000000");

public verify(): boolean {
return configManager.getMilestone().aip11 && super.verify();
const milestone = configManager.getMilestone();
return milestone.aip11 === true && milestone.htlcEnabled === true && super.verify();
}

public hasVendorField(): boolean {
Expand Down
3 changes: 2 additions & 1 deletion packages/crypto/src/transactions/types/htlc-refund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export class HtlcRefundTransaction extends Transaction {
protected static defaultStaticFee: BigNumber = BigNumber.ZERO;

public verify(): boolean {
return configManager.getMilestone().aip11 && super.verify();
const milestone = configManager.getMilestone();
return milestone.aip11 === true && milestone.htlcEnabled === true && super.verify();
}

public serialize(options?: ISerializeOptions): ByteBuffer {
Expand Down

0 comments on commit e246093

Please sign in to comment.