Exemple #1
0
        public static async Task <(IStore store, StoreError err)> Load(int cacheSize, string path, ILogger logger)
        {
            logger = logger.AddNamedContext("LocalDbStore");
            logger.Verbose("Load store");

            var db = new DBreezeEngine(new DBreezeConfiguration
            {
                DBreezeDataFolderName = path
            });

            var store = new LocalDbStore(
                null,
                null,
                db,
                path,
                logger
                );

            using (var tx = store.BeginTx())
            {
                var(participants, err) = await store.DbGetParticipants();

                if (err != null)
                {
                    return(null, err);
                }

                var inmemStore = new InmemStore(participants, cacheSize, logger);

                //read roots from db and put them in InmemStore
                var roots = new Dictionary <string, Root>();
                foreach (var p in participants)
                {
                    Root root;
                    (root, err) = await store.DbGetRoot(p.Key);

                    if (err != null)
                    {
                        return(null, err);
                    }

                    roots[p.Key] = root;
                }



                err = inmemStore.Reset(roots);
                if (err != null)
                {
                    return(null, err);
                }

                store.participants = participants;
                store.InMemStore   = inmemStore;

                tx.Commit();
            }

            return(store, null);
        }
        public ParticipantEventsCache(int size, Dictionary <string, int> participants, ILogger logger, string instanceName = null)
        {
            this.logger = logger.AddNamedContext("ParticipantEventsCache", instanceName);

            Participants = participants;
            Rim          = new RollingIndexMap <string>(size, participants.GetValues());
        }
        public ParticipantEventsCache(int size, Dictionary <string, int> participants, ILogger logger, string instanceName = null)
        {
            var items = new Dictionary <string, RollingIndex <string> >();

            foreach (var k in participants.Keys)
            {
                items.Add(k, new RollingIndex <string>(size));
            }

            Size              = size;
            Participants      = participants;
            this.logger       = logger.AddNamedContext("ParticipantEventsCache");
            ParticipantEvents = items;
        }
Exemple #4
0
        public Node(Config conf, int id, CngKey key, Peer[] participants, IStore store, ITransport trans, IAppProxy proxy, ILogger loggerIn)

        {
            logger = loggerIn.AddNamedContext("Node", id.ToString());

            LocalAddr = trans.LocalAddr;

            var(pmap, _) = store.Participants();

            commitCh        = new AsyncProducerConsumerQueue <Block>(400);
            commitChMonitor = new AsyncMonitor();

            Controller   = new Controller(id, key, pmap, store, commitCh, logger);
            coreLock     = new AsyncLock();
            PeerSelector = new RandomPeerSelector(participants, LocalAddr);

            selectorLock = new AsyncLock();
            Id           = id;
            Store        = store;
            Conf         = conf;



            Trans = trans;

            netCh        = trans.Consumer;
            netChMonitor = new AsyncMonitor();

            Proxy = proxy;

            this.participants = participants;

            submitCh        = proxy.SubmitCh();
            submitChMonitor = new AsyncMonitor();

            controlTimer = ControlTimer.NewRandomControlTimer(conf.HeartbeatTimeout);

            nodeState = new NodeState();

            //Initialize as Babbling
            nodeState.SetStarting(true);
            nodeState.SetState(NodeStateEnum.Babbling);
        }
Exemple #5
0
        public InmemStore(Dictionary <string, int> participants, int cacheSize, ILogger logger)
        {
            var rts = new Dictionary <string, Root>();

            foreach (var p in participants)
            {
                rts.Add(p.Key, Root.NewBaseRoot());
            }

            this.participants      = participants;
            this.cacheSize         = cacheSize;
            this.logger            = logger.AddNamedContext("InmemStore");
            eventCache             = new LruCache <string, Event>(cacheSize, null, logger, "EventCache");
            roundCache             = new LruCache <int, RoundInfo>(cacheSize, null, logger, "RoundCache");
            consensusCache         = new RollingIndex <string>(cacheSize);
            participantEventsCache = new ParticipantEventsCache(cacheSize, participants, logger);
            roots     = rts;
            lastRound = -1;
        }
Exemple #6
0
        public Hashgraph(Dictionary <string, int> participants, IStore store, Channel <Event> commitCh, ILogger logger)
        {
            this.logger = logger.AddNamedContext("HashGraph");
            var reverseParticipants = participants.ToDictionary(p => p.Value, p => p.Key);
            var cacheSize           = store.CacheSize();

            Participants        = participants;
            ReverseParticipants = reverseParticipants;
            Store                   = store;
            CommitChannel           = commitCh;
            AncestorCache           = new LruCache <string, bool>(cacheSize, null, logger, "AncestorCache");
            SelfAncestorCache       = new LruCache <string, bool>(cacheSize, null, logger, "SelfAncestorCache");
            OldestSelfAncestorCache = new LruCache <string, string>(cacheSize, null, logger, "OldestAncestorCache");
            StronglySeeCache        = new LruCache <string, bool>(cacheSize, null, logger, "StronglySeeCache");
            ParentRoundCache        = new LruCache <string, ParentRoundInfo>(cacheSize, null, logger, "ParentRoundCache");
            RoundCache              = new LruCache <string, int>(cacheSize, null, logger, "RoundCache");
            UndeterminedEvents      = new List <string>();
            SuperMajority           = 2 * participants.Count / 3 + 1;
            UndecidedRounds         = new Queue <int>(); //initialize
        }
Exemple #7
0
        //LoadBadgerStore creates a Store from an existing database
        public static async Task <(IStore store, StoreError err)> New(Dictionary <string, int> participants, int cacheSize, string path, ILogger logger)
        {
            logger = logger.AddNamedContext("LocalDbStore");
            logger.Verbose("New store");

            var inmemStore = new InmemStore(participants, cacheSize, logger);
            var db         = new DBreezeEngine(new DBreezeConfiguration
            {
                //Storage =  DBreezeConfiguration.eStorage.MEMORY,
                DBreezeDataFolderName = path
            });

            var store = new LocalDbStore(
                participants,
                inmemStore,
                db,
                path,
                logger
                );

            using (var tx = store.BeginTx())
            {
                var err = await store.DbSetParticipants(participants);

                if (err != null)
                {
                    return(null, err);
                }

                err = await store.DbSetRoots(inmemStore.Roots);

                if (err != null)
                {
                    return(null, err);
                }
                tx.Commit();
            }

            return(store, null);
        }
Exemple #8
0
 // NewLRU constructs an LRU of the given size
 public LruCache(int size, Action <TKey, TValue> evictAction, ILogger logger, string instanceName = null)
 {
     this.size        = size;
     this.evictAction = evictAction;
     this.logger      = logger.AddNamedContext("LruCache", instanceName);
 }
Exemple #9
0
 public InMemAppProxy(int id, ILogger logger)
 {
     submitCh = new AsyncProducerConsumerQueue <byte[]>();
     committedTransactions = new List <byte[]>();
     this.logger           = logger.AddNamedContext(nameof(InMemAppProxy), id.ToString());
 }