Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation for Tx fee recipient #142

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements/base.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ flower==0.9.5
psycopg2==2.8.6
redis==3.5.3
tblib==1.7.0
thenewboston==0.0.33
thenewboston==0.1.2

# https://github.com/pyca/cryptography/issues/5765
cryptography==3.3.2
6 changes: 3 additions & 3 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ apipkg==1.5 # via execnet
asgiref==3.3.1 # via channels, channels-redis, daphne, django
async-timeout==3.0.1 # via aioredis
attrs==20.3.0 # via automat, flake8-bugbear, pytest, service-identity, twisted
autobahn==21.1.1 # via daphne
autobahn==21.2.1 # via daphne
automat==20.2.0 # via twisted
billiard==3.6.3.0 # via celery
celery==4.4.2 # via -r requirements/base.in, flower
certifi==2020.12.5 # via requests, sentry-sdk
cffi==1.14.4 # via cryptography, pynacl
cffi==1.14.5 # via cryptography, pynacl
channels-redis==2.4.2 # via thenewboston
channels==2.4.0 # via channels-redis, thenewboston
chardet==4.0.0 # via requests
Expand Down Expand Up @@ -90,7 +90,7 @@ sortedcontainers==2.3.0 # via fakeredis
sqlparse==0.4.1 # via django, django-debug-toolbar
tblib==1.7.0 # via -r requirements/base.in
text-unidecode==1.3 # via faker
thenewboston==0.0.33 # via -r requirements/base.in
thenewboston==0.1.2 # via -r requirements/base.in
toml==0.10.2 # via pytest
tornado==6.1 # via flower
twisted[tls]==20.3.0 # via daphne
Expand Down
6 changes: 3 additions & 3 deletions requirements/development.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ apipkg==1.5 # via execnet
asgiref==3.3.1 # via channels, channels-redis, daphne, django
async-timeout==3.0.1 # via aioredis
attrs==20.3.0 # via automat, flake8-bugbear, pytest, service-identity, twisted
autobahn==21.1.1 # via daphne
autobahn==21.2.1 # via daphne
automat==20.2.0 # via twisted
billiard==3.6.3.0 # via celery
celery==4.4.2 # via -r requirements/base.in, flower
certifi==2020.12.5 # via requests, sentry-sdk
cffi==1.14.4 # via cryptography, pynacl
cffi==1.14.5 # via cryptography, pynacl
channels-redis==2.4.2 # via thenewboston
channels==2.4.0 # via channels-redis, thenewboston
chardet==4.0.0 # via requests
Expand Down Expand Up @@ -90,7 +90,7 @@ sortedcontainers==2.3.0 # via fakeredis
sqlparse==0.4.1 # via django, django-debug-toolbar
tblib==1.7.0 # via -r requirements/base.in
text-unidecode==1.3 # via faker
thenewboston==0.0.33 # via -r requirements/base.in
thenewboston==0.1.2 # via -r requirements/base.in
toml==0.10.2 # via pytest
tornado==6.1 # via flower
twisted[tls]==20.3.0 # via daphne
Expand Down
6 changes: 3 additions & 3 deletions requirements/local.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ apipkg==1.5 # via execnet
asgiref==3.3.1 # via channels, channels-redis, daphne, django
async-timeout==3.0.1 # via aioredis
attrs==20.3.0 # via automat, flake8-bugbear, pytest, service-identity, twisted
autobahn==21.1.1 # via daphne
autobahn==21.2.1 # via daphne
automat==20.2.0 # via twisted
babel==2.9.0 # via sphinx
billiard==3.6.3.0 # via celery
celery==4.4.2 # via -r requirements/base.in, flower
certifi==2020.12.5 # via requests, sentry-sdk
cffi==1.14.4 # via cryptography, pynacl
cffi==1.14.5 # via cryptography, pynacl
channels-redis==2.4.2 # via thenewboston
channels==2.4.0 # via channels-redis, thenewboston
chardet==4.0.0 # via requests
Expand Down Expand Up @@ -104,7 +104,7 @@ sphinxcontrib-serializinghtml==1.1.4 # via sphinx
sqlparse==0.4.1 # via django, django-debug-toolbar
tblib==1.7.0 # via -r requirements/base.in
text-unidecode==1.3 # via faker
thenewboston==0.0.33 # via -r requirements/base.in
thenewboston==0.1.2 # via -r requirements/base.in
toml==0.10.2 # via pytest
tornado==6.1 # via flower
twisted[tls]==20.3.0 # via daphne
Expand Down
6 changes: 3 additions & 3 deletions requirements/production.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ apipkg==1.5 # via execnet
asgiref==3.3.1 # via channels, channels-redis, daphne, django
async-timeout==3.0.1 # via aioredis
attrs==20.3.0 # via automat, flake8-bugbear, pytest, service-identity, twisted
autobahn==21.1.1 # via daphne
autobahn==21.2.1 # via daphne
automat==20.2.0 # via twisted
billiard==3.6.3.0 # via celery
celery==4.4.2 # via -r requirements/base.in, flower
certifi==2020.12.5 # via requests, sentry-sdk
cffi==1.14.4 # via cryptography, pynacl
cffi==1.14.5 # via cryptography, pynacl
channels-redis==2.4.2 # via thenewboston
channels==2.4.0 # via channels-redis, thenewboston
chardet==4.0.0 # via requests
Expand Down Expand Up @@ -90,7 +90,7 @@ sortedcontainers==2.3.0 # via fakeredis
sqlparse==0.4.1 # via django, django-debug-toolbar
tblib==1.7.0 # via -r requirements/base.in
text-unidecode==1.3 # via faker
thenewboston==0.0.33 # via -r requirements/base.in
thenewboston==0.1.2 # via -r requirements/base.in
toml==0.10.2 # via pytest
tornado==6.1 # via flower
twisted[tls]==20.3.0 # via daphne
Expand Down
6 changes: 3 additions & 3 deletions requirements/staging.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ apipkg==1.5 # via execnet
asgiref==3.3.1 # via channels, channels-redis, daphne, django
async-timeout==3.0.1 # via aioredis
attrs==20.3.0 # via automat, flake8-bugbear, pytest, service-identity, twisted
autobahn==21.1.1 # via daphne
autobahn==21.2.1 # via daphne
automat==20.2.0 # via twisted
billiard==3.6.3.0 # via celery
celery==4.4.2 # via -r requirements/base.in, flower
certifi==2020.12.5 # via requests, sentry-sdk
cffi==1.14.4 # via cryptography, pynacl
cffi==1.14.5 # via cryptography, pynacl
channels-redis==2.4.2 # via thenewboston
channels==2.4.0 # via channels-redis, thenewboston
chardet==4.0.0 # via requests
Expand Down Expand Up @@ -90,7 +90,7 @@ sortedcontainers==2.3.0 # via fakeredis
sqlparse==0.4.1 # via django, django-debug-toolbar
tblib==1.7.0 # via -r requirements/base.in
text-unidecode==1.3 # via faker
thenewboston==0.0.33 # via -r requirements/base.in
thenewboston==0.1.2 # via -r requirements/base.in
toml==0.10.2 # via pytest
tornado==6.1 # via flower
twisted[tls]==20.3.0 # via daphne
Expand Down
Empty file.
36 changes: 36 additions & 0 deletions v1/bank_blocks/serializers/block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from rest_framework import serializers
from thenewboston.constants.network import PRIMARY_VALIDATOR
from thenewboston.serializers.network_block import NetworkBlockSerializer
from thenewboston.transactions.validation import validate_transaction_exists

from v1.self_configurations.helpers.self_configuration import get_self_configuration


class BlockSerializer(NetworkBlockSerializer):

def create(self, validated_data):
raise RuntimeError('Method unavailable')

def update(self, instance, validated_data):
raise RuntimeError('Method unavailable')

def validate(self, data):
"""Verify that correct payment exist for the Primary Validator"""
data = super(BlockSerializer, self).validate(data)

account_number = data['account_number']
message = data['message']
txs = message['txs']

self_configuration = get_self_configuration(exception_class=RuntimeError)

if account_number != self_configuration.account_number:
validate_transaction_exists(
amount=self_configuration.default_transaction_fee,
fee=PRIMARY_VALIDATOR,
error=serializers.ValidationError,
recipient=self_configuration.account_number,
txs=txs
)

return data
5 changes: 2 additions & 3 deletions v1/bank_blocks/views/bank_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from rest_framework.response import Response
from rest_framework.viewsets import ViewSet
from thenewboston.constants.network import PRIMARY_VALIDATOR
from thenewboston.serializers.network_block import NetworkBlockSerializer

from v1.cache_tools.cache_keys import BLOCK_QUEUE, BLOCK_QUEUE_CACHE_LOCK_KEY
from v1.decorators.nodes import is_signed_bank_block
from v1.self_configurations.helpers.self_configuration import get_self_configuration
from v1.tasks.block_queue import process_block_queue
from ..serializers.block import BlockSerializer

"""
Banks will request a new primary validator if an error status (status code >= 400) is returned
Expand Down Expand Up @@ -41,15 +41,14 @@ def add_block_to_queue(block):
@staticmethod
@is_signed_bank_block
def create(request):

self_configuration = get_self_configuration(exception_class=RuntimeError)

if self_configuration.node_type != PRIMARY_VALIDATOR:
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)

block = request.data.get('block')

serializer = NetworkBlockSerializer(
serializer = BlockSerializer(
data=block,
context={'request': request}
)
Expand Down
18 changes: 12 additions & 6 deletions v1/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pytest_django.migrations import DisableMigrations
from thenewboston.accounts.manage import create_account
from thenewboston.blocks.block import generate_block
from thenewboston.constants.network import PRIMARY_VALIDATOR
from thenewboston.third_party.pytest.client import UserWrapper
from thenewboston.verify_keys.verify_key import encode_verify_key

Expand Down Expand Up @@ -48,6 +49,11 @@ def block_data(account_data, encoded_account_number, random_encoded_account_numb
),
signing_key=signing_key,
transactions=[
{
'amount': 4,
'fee': PRIMARY_VALIDATOR,
'recipient': 'ad1f8845c6a1abb6011a2a434a079a087c460657aad54329a84b406dce8bf314'
},
{
'amount': 1,
'recipient': random_encoded_account_number
Expand Down Expand Up @@ -77,12 +83,6 @@ def enable_db_access_for_all_tests(transactional_db):
pass


@pytest.fixture(scope='session', autouse=True)
def migrations_disabled():
settings.MIGRATION_MODULES = DisableMigrations()
yield None


@pytest.fixture
def encoded_account_number(account_number):
yield encode_verify_key(verify_key=account_number)
Expand All @@ -101,6 +101,12 @@ def load_validator_fixtures(fixtures_dir):
call_command('loaddata', fixture, verbosity=1)


@pytest.fixture(scope='session', autouse=True)
def migrations_disabled():
settings.MIGRATION_MODULES = DisableMigrations()
yield None


@pytest.fixture
def primary_validator(encoded_account_number):
yield ValidatorFactory(
Expand Down
Loading