@@ -16,11 +16,13 @@ use witnet_config::defaults::{
16
16
use witnet_data_structures:: {
17
17
chain:: {
18
18
tapi:: ActiveWips , Block , ChainState , CheckpointBeacon , DataRequestInfo , Epoch , Hash ,
19
- Hashable , NodeStats , PublicKeyHash , SuperBlockVote , SupplyInfo ,
19
+ Hashable , NodeStats , PublicKeyHash , SuperBlockVote , SupplyInfo , ValueTransferOutput ,
20
20
} ,
21
21
error:: { ChainInfoError , TransactionError :: DataRequestNotFound } ,
22
22
staking:: errors:: StakesError ,
23
- transaction:: { DRTransaction , StakeTransaction , Transaction , VTTransaction } ,
23
+ transaction:: {
24
+ DRTransaction , StakeTransaction , Transaction , UnstakeTransaction , VTTransaction ,
25
+ } ,
24
26
transaction_factory:: { self , NodeBalance } ,
25
27
types:: LastBeacon ,
26
28
utxo_pool:: { get_utxo_info, UtxoInfo } ,
@@ -33,8 +35,8 @@ use crate::{
33
35
chain_manager:: { handlers:: BlockBatches :: * , BlockCandidate } ,
34
36
messages:: {
35
37
AddBlocks , AddCandidates , AddCommitReveal , AddSuperBlock , AddSuperBlockVote ,
36
- AddTransaction , Broadcast , BuildDrt , BuildStake , BuildVtt , EpochNotification ,
37
- EstimatePriority , GetBalance , GetBalanceTarget , GetBlocksEpochRange ,
38
+ AddTransaction , Broadcast , BuildDrt , BuildStake , BuildUnstake , BuildVtt ,
39
+ EpochNotification , EstimatePriority , GetBalance , GetBalanceTarget , GetBlocksEpochRange ,
38
40
GetDataRequestInfo , GetHighestCheckpointBeacon , GetMemoryTransaction , GetMempool ,
39
41
GetMempoolResult , GetNodeStats , GetReputation , GetReputationResult , GetSignalingInfo ,
40
42
GetState , GetSuperBlockVotes , GetSupplyInfo , GetUtxoInfo , IsConfirmedBlock ,
@@ -1357,6 +1359,65 @@ impl Handler<BuildStake> for ChainManager {
1357
1359
}
1358
1360
}
1359
1361
1362
+ impl Handler < BuildUnstake > for ChainManager {
1363
+ type Result = ResponseActFuture < Self , <BuildUnstake as Message >:: Result > ;
1364
+
1365
+ fn handle ( & mut self , msg : BuildUnstake , _ctx : & mut Self :: Context ) -> Self :: Result {
1366
+ if !msg. dry_run && self . sm_state != StateMachine :: Synced {
1367
+ return Box :: pin ( actix:: fut:: err (
1368
+ ChainManagerError :: NotSynced {
1369
+ current_state : self . sm_state ,
1370
+ }
1371
+ . into ( ) ,
1372
+ ) ) ;
1373
+ }
1374
+
1375
+ let withdrawal = ValueTransferOutput {
1376
+ time_lock : 0 ,
1377
+ pkh : self . own_pkh . unwrap ( ) ,
1378
+ value : msg. value ,
1379
+ } ;
1380
+ match transaction_factory:: build_ut ( withdrawal, msg. operator ) {
1381
+ Err ( e) => {
1382
+ log:: error!( "Error when building stake transaction: {}" , e) ;
1383
+ Box :: pin ( actix:: fut:: err ( e. into ( ) ) )
1384
+ }
1385
+ Ok ( ut) => {
1386
+ let fut = signature_mngr:: sign_transaction ( & ut, 1 )
1387
+ . into_actor ( self )
1388
+ . then ( move |s, act, _ctx| match s {
1389
+ Ok ( signature) => {
1390
+ let ut =
1391
+ UnstakeTransaction :: new ( ut, signature. first ( ) . unwrap ( ) . clone ( ) ) ;
1392
+
1393
+ if msg. dry_run {
1394
+ Either :: Right ( actix:: fut:: result ( Ok ( ut) ) )
1395
+ } else {
1396
+ let transaction = Transaction :: Unstake ( ut. clone ( ) ) ;
1397
+ Either :: Left (
1398
+ act. add_transaction (
1399
+ AddTransaction {
1400
+ transaction,
1401
+ broadcast_flag : true ,
1402
+ } ,
1403
+ get_timestamp ( ) ,
1404
+ )
1405
+ . map_ok ( move |_, _, _| ut) ,
1406
+ )
1407
+ }
1408
+ }
1409
+ Err ( e) => {
1410
+ log:: error!( "Failed to sign stake transaction: {}" , e) ;
1411
+ Either :: Right ( actix:: fut:: result ( Err ( e) ) )
1412
+ }
1413
+ } ) ;
1414
+
1415
+ Box :: pin ( fut)
1416
+ }
1417
+ }
1418
+ }
1419
+ }
1420
+
1360
1421
impl Handler < QueryStake > for ChainManager {
1361
1422
type Result = <QueryStake as Message >:: Result ;
1362
1423
0 commit comments