1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of OpenEthereum.

// OpenEthereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// OpenEthereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with OpenEthereum.  If not, see <http://www.gnu.org/licenses/>.

//! Smart contract based node filter.

extern crate ethabi;
extern crate ethcore;
extern crate ethcore_network as network;
extern crate ethcore_network_devp2p as devp2p;
extern crate ethereum_types;
extern crate lru_cache;
extern crate parking_lot;

extern crate ethabi_derive;
#[macro_use]
extern crate ethabi_contract;
#[cfg(test)]
extern crate ethcore_io as io;
#[cfg(test)]
extern crate kvdb_memorydb;
#[cfg(test)]
extern crate tempdir;
#[macro_use]
extern crate log;

use std::sync::Weak;

use devp2p::NodeId;
use ethabi::FunctionOutputDecoder;
use ethcore::client::{BlockChainClient, BlockId};
use ethereum_types::{Address, H256};
use network::{ConnectionDirection, ConnectionFilter};

use_contract!(peer_set, "res/peer_set.json");

/// Connection filter that uses a contract to manage permissions.
pub struct NodeFilter {
    client: Weak<dyn BlockChainClient>,
    contract_address: Address,
}

impl NodeFilter {
    /// Create a new instance. Accepts a contract address.
    pub fn new(client: Weak<dyn BlockChainClient>, contract_address: Address) -> NodeFilter {
        NodeFilter {
            client,
            contract_address,
        }
    }
}

impl ConnectionFilter for NodeFilter {
    fn connection_allowed(
        &self,
        own_id: &NodeId,
        connecting_id: &NodeId,
        _direction: ConnectionDirection,
    ) -> bool {
        let client = match self.client.upgrade() {
            Some(client) => client,
            None => return false,
        };

        let address = self.contract_address;
        let own_low = H256::from_slice(&own_id[0..32]);
        let own_high = H256::from_slice(&own_id[32..64]);
        let id_low = H256::from_slice(&connecting_id[0..32]);
        let id_high = H256::from_slice(&connecting_id[32..64]);

        let (data, decoder) =
            peer_set::functions::connection_allowed::call(own_low, own_high, id_low, id_high);
        let allowed = client
            .call_contract(BlockId::Latest, address, data)
            .and_then(|value| decoder.decode(&value).map_err(|e| e.to_string()))
            .unwrap_or_else(|e| {
                debug!("Error callling peer set contract: {:?}", e);
                false
            });

        allowed
    }
}

#[cfg(test)]
mod test {
    use super::NodeFilter;
    use ethcore::{
        client::{BlockChainClient, Client, ClientConfig},
        miner::Miner,
        spec::Spec,
        test_helpers,
    };
    use ethereum_types::Address;
    use io::IoChannel;
    use network::{ConnectionDirection, ConnectionFilter, NodeId};
    use std::{
        str::FromStr,
        sync::{Arc, Weak},
    };
    use tempdir::TempDir;

    /// Contract code: https://gist.github.com/arkpar/467dbcc73cbb85b0997a7a10ffa0695f
    #[test]
    fn node_filter() {
        let contract_addr = Address::from_str("0000000000000000000000000000000000000005").unwrap();
        let data = include_bytes!("../res/node_filter.json");
        let tempdir = TempDir::new("").unwrap();
        let spec = Spec::load(&tempdir.path(), &data[..]).unwrap();
        let client_db = test_helpers::new_db();

        let client = Client::new(
            ClientConfig::default(),
            &spec,
            client_db,
            Arc::new(Miner::new_for_tests(&spec, None)),
            IoChannel::disconnected(),
        )
        .unwrap();
        let filter = NodeFilter::new(
            Arc::downgrade(&client) as Weak<dyn BlockChainClient>,
            contract_addr,
        );
        let self1 = NodeId::from_str("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002").unwrap();
        let self2 = NodeId::from_str("00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003").unwrap();
        let node1 = NodeId::from_str("00000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012").unwrap();
        let node2 = NodeId::from_str("00000000000000000000000000000000000000000000000000000000000000210000000000000000000000000000000000000000000000000000000000000022").unwrap();
        let nodex = NodeId::from_str("77000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();

        assert!(filter.connection_allowed(&self1, &node1, ConnectionDirection::Inbound));
        assert!(filter.connection_allowed(&self1, &nodex, ConnectionDirection::Inbound));
        assert!(filter.connection_allowed(&self2, &node1, ConnectionDirection::Inbound));
        assert!(filter.connection_allowed(&self2, &node2, ConnectionDirection::Inbound));
    }
}