Skip to content

Commit

Permalink
add validator resignation
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsobries committed Oct 30, 2024
1 parent df1822b commit b772050
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 7 deletions.
28 changes: 28 additions & 0 deletions src/Enums/AbiFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace ArkEcosystem\Crypto\Enums;

use ArkEcosystem\Crypto\Transactions\Types\Unvote;
use ArkEcosystem\Crypto\Transactions\Types\ValidatorRegistration;
use ArkEcosystem\Crypto\Transactions\Types\ValidatorResignation;
use ArkEcosystem\Crypto\Transactions\Types\Vote;

enum AbiFunction: string
{
case VOTE = 'vote';
case UNVOTE = 'unvote';
case VALIDATOR_REGISTRATION = 'registerValidator';
case VALIDATOR_RESIGNATION = 'resignValidator';

public function transactionClass(): string
{
return match ($this) {
self::VOTE => Vote::class,
self::UNVOTE => Unvote::class,
self::VALIDATOR_REGISTRATION => ValidatorRegistration::class,
self::VALIDATOR_RESIGNATION => ValidatorResignation::class,
};
}
}
13 changes: 10 additions & 3 deletions src/Transactions/Deserializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
namespace ArkEcosystem\Crypto\Transactions;

use ArkEcosystem\Crypto\ByteBuffer\ByteBuffer;
use ArkEcosystem\Crypto\Enums\AbiFunction;
use ArkEcosystem\Crypto\Transactions\Types\AbstractTransaction;
use ArkEcosystem\Crypto\Transactions\Types\EvmCall;
use ArkEcosystem\Crypto\Transactions\Types\Transfer;
use ArkEcosystem\Crypto\Transactions\Types\Unvote;
use ArkEcosystem\Crypto\Transactions\Types\ValidatorRegistration;
use ArkEcosystem\Crypto\Transactions\Types\ValidatorResignation;
use ArkEcosystem\Crypto\Transactions\Types\Vote;
use ArkEcosystem\Crypto\Utils\AbiDecoder;
use ArkEcosystem\Crypto\Utils\Address;
Expand Down Expand Up @@ -74,18 +76,23 @@ private function guessTransactionFromData(array $data): AbstractTransaction

$functionName = $payloadData['functionName'];

if ($functionName === 'vote') {

if ($functionName === AbiFunction::VOTE->value) {
return new Vote($data);
}

if ($functionName === 'unvote') {
if ($functionName === AbiFunction::UNVOTE->value) {
return new Unvote($data);
}

if ($functionName === 'registerValidator') {
if ($functionName === AbiFunction::VALIDATOR_REGISTRATION->value) {
return new ValidatorRegistration($data);
}

if ($functionName === AbiFunction::VALIDATOR_RESIGNATION->value) {
return new ValidatorResignation($data);
}

return new EvmCall();
}

Expand Down
3 changes: 2 additions & 1 deletion src/Transactions/Types/Unvote.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

namespace ArkEcosystem\Crypto\Transactions\Types;

use ArkEcosystem\Crypto\Enums\AbiFunction;
use ArkEcosystem\Crypto\Utils\AbiEncoder;

class Unvote extends AbstractTransaction
{
public function getPayload(): string
{
return (new AbiEncoder())->encodeFunctionCall('unvote');
return (new AbiEncoder())->encodeFunctionCall(AbiFunction::UNVOTE->value);
}
}
3 changes: 2 additions & 1 deletion src/Transactions/Types/ValidatorRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace ArkEcosystem\Crypto\Transactions\Types;

use ArkEcosystem\Crypto\Enums\AbiFunction;
use ArkEcosystem\Crypto\Utils\AbiEncoder;

class ValidatorRegistration extends AbstractTransaction
Expand All @@ -21,6 +22,6 @@ public function __construct(?array $data = [])

public function getPayload(): string
{
return (new AbiEncoder())->encodeFunctionCall('registerValidator', [$this->data['asset']['validatorPublicKey']]);
return (new AbiEncoder())->encodeFunctionCall(AbiFunction::VALIDATOR_REGISTRATION->value, [$this->data['asset']['validatorPublicKey']]);
}
}
5 changes: 4 additions & 1 deletion src/Transactions/Types/ValidatorResignation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

namespace ArkEcosystem\Crypto\Transactions\Types;

use ArkEcosystem\Crypto\Enums\AbiFunction;
use ArkEcosystem\Crypto\Utils\AbiEncoder;

class ValidatorResignation extends AbstractTransaction
{
public function getPayload(): string
{
return '';
return (new AbiEncoder())->encodeFunctionCall(AbiFunction::VALIDATOR_RESIGNATION->value);
}
}
3 changes: 2 additions & 1 deletion src/Transactions/Types/Vote.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace ArkEcosystem\Crypto\Transactions\Types;

use ArkEcosystem\Crypto\Enums\AbiFunction;
use ArkEcosystem\Crypto\Utils\AbiEncoder;

class Vote extends AbstractTransaction
Expand All @@ -21,6 +22,6 @@ public function __construct(?array $data = [])

public function getPayload(): string
{
return (new AbiEncoder())->encodeFunctionCall('vote', [$this->data['asset']['vote']]);
return (new AbiEncoder())->encodeFunctionCall(AbiFunction::VOTE->value, [$this->data['asset']['vote']]);
}
}
11 changes: 11 additions & 0 deletions tests/Unit/Transactions/DeserializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use ArkEcosystem\Crypto\Transactions\Types\Transfer;
use ArkEcosystem\Crypto\Transactions\Types\Unvote;
use ArkEcosystem\Crypto\Transactions\Types\ValidatorRegistration;
use ArkEcosystem\Crypto\Transactions\Types\ValidatorResignation;
use ArkEcosystem\Crypto\Transactions\Types\Vote;
use ArkEcosystem\Tests\Crypto\TestCase;

Expand Down Expand Up @@ -60,6 +61,16 @@ public function it_should_deserialize_a_validator_registration_signed_with_a_pas
expect($transaction)->toBeInstanceOf(ValidatorRegistration::class);
}

/** @test */
public function it_should_deserialize_a_validator_resignation_signed_with_a_passphrase()
{
$fixture = $this->getTransactionFixture('evm_call', 'validator-resignation');

$transaction = $this->assertTransaction($fixture);

expect($transaction)->toBeInstanceOf(ValidatorResignation::class);
}

private function assertTransaction(array $fixture): AbstractTransaction
{
$actual = $this->assertDeserialized($fixture, [
Expand Down
11 changes: 11 additions & 0 deletions tests/Unit/Transactions/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ArkEcosystem\Crypto\Transactions\Types\Transfer;
use ArkEcosystem\Crypto\Transactions\Types\Unvote;
use ArkEcosystem\Crypto\Transactions\Types\ValidatorRegistration;
use ArkEcosystem\Crypto\Transactions\Types\ValidatorResignation;
use ArkEcosystem\Crypto\Transactions\Types\Vote;
use ArkEcosystem\Tests\Crypto\TestCase;

Expand Down Expand Up @@ -54,4 +55,14 @@ public function it_should_serialize_a_validator_registration_transaction()

$this->assertSame($fixture['serialized'], $transaction->serialize()->getHex());
}

/** @test */
public function it_should_serialize_a_validator_resignation_transaction()
{
$fixture = $this->getTransactionFixture('evm_call', 'validator-resignation');

$transaction = new ValidatorResignation($fixture['data']);

$this->assertSame($fixture['serialized'], $transaction->serialize()->getHex());
}
}
22 changes: 22 additions & 0 deletions tests/fixtures/transactions/evm_call/validator-resignation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"data": {
"version": 1,
"network": 30,
"typeGroup": 1,
"type": 10,
"nonce": "0",
"senderPublicKey": "03a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd",
"fee": "5",
"amount": "0",
"recipientId": "0x522B3294E6d06aA25Ad0f1B8891242E335D3B459",
"asset": {
"evmCall": {
"gasLimit": 150000,
"payload": "b85f5da2"
}
},
"signature": "c84e7269d8f45364bd9da8bcbb2ae843bcc3e0f49a044832855e86c024c6ff6617cc090acb67551a21537dda0e78c903fbc7c72a0cbb575e0edfbff388547967",
"id": "a521da35fc32122f3d95c63eb500a8c96a5b8ac3139aa5b94ed2f068cf8ed708"
},
"serialized": "ff011e010000000a00000000000000000003a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000000001522b3294e6d06aa25ad0f1b8891242e335d3b459f049020004000000b85f5da2c84e7269d8f45364bd9da8bcbb2ae843bcc3e0f49a044832855e86c024c6ff6617cc090acb67551a21537dda0e78c903fbc7c72a0cbb575e0edfbff388547967"
}

0 comments on commit b772050

Please sign in to comment.