5
5
from node .blockchain .facade import BlockchainFacade
6
6
from node .blockchain .models .block_confirmation import BlockConfirmation as ORMBlockConfirmation
7
7
from node .blockchain .serializers .block_confirmation import BlockConfirmationSerializer
8
+ from node .blockchain .tasks .process_block_confirmations import start_process_block_confirmations_task
9
+ from node .core .utils .misc import apply_on_commit
8
10
9
11
10
12
class BlockConfirmationViewSet (GenericViewSet ):
@@ -16,8 +18,12 @@ def create(self, request, *args, **kwargs):
16
18
block_confirmation = serializer .save ()
17
19
18
20
facade = BlockchainFacade .get_instance ()
19
- if facade .get_next_block_number () == block_confirmation .get_number ():
21
+ next_block_number = facade .get_next_block_number ()
22
+ is_next_block_number = block_confirmation .get_number () == next_block_number
23
+ if is_next_block_number :
20
24
block_confirmation .validate_all (facade )
25
+ else :
26
+ block_confirmation .validate_business_logic ()
21
27
22
28
ORMBlockConfirmation .objects .update_or_create (
23
29
number = block_confirmation .get_number (),
@@ -28,8 +34,9 @@ def create(self, request, *args, **kwargs):
28
34
},
29
35
)
30
36
31
- # TODO(dmu) CRITICAL: https://thenewboston.atlassian.net/browse/BC-272
32
- # if next block number and we have enough confirmations then run a celery task
33
- # to add the block to the blockchain
37
+ if is_next_block_number and (
38
+ ORMBlockConfirmation .objects .filter (number = next_block_number ).count () >= facade .get_minimum_consensus ()
39
+ ):
40
+ apply_on_commit (start_process_block_confirmations_task )
34
41
35
42
return Response (serializer .data , status = status .HTTP_201_CREATED )
0 commit comments