On Sunday 29 June 2008 14:52, Daniel Cheng wrote:
> On Sat, Jun 28, 2008 at 10:49 PM,  <toad at freenetproject.org> wrote:
> > Author: toad
> > Date: 2008-06-28 14:49:23 +0000 (Sat, 28 Jun 2008)
> > New Revision: 20845
> >
> > Modified:
> >   
branches/db4o/freenet/src/freenet/support/io/PaddedEphemerallyEncryptedBucket.java
> > Log:
> > Don't store the RNG here. Just store a seed created on startup.
> >
> 
> If we restarted the node twice... The PRNG will be seeded with the
> same seed in the two times...  -- Is this behavior expected?

The PRNG is only instantiated when we need it for padding... what's the 
problem?
> 
> IMO, make Node a static field would make the code much simpler in most
> code -- we can refer to the node object when we need a prng, database,
> etc.... For "many node in a jvm", we can use some ClassLoader
> hacks....

Yay God Object. :)

When we have many node in a jvm, we want to be able to deal with them 
easily... e.g. in the simulation code. Not to have to go through 10 layers of 
indirection because of the hacks we're using.

W.r.t. the database, it was a deliberate decision to pass it around, this 
makes writing relatively non-buggy code easier at this point. It could be 
changed later ... but the single-database-thread 
single-simultaneous-transaction design really doesn't go well with having 
static access to the database: it would be far too easy to just grab the 
database handle and write something, when there might be 10 other threads 
doing the same at the same time. So we'd have to use an embedded 
client-server design, which gives some benefits, but a number of major 
headaches too. Performance-wise I'm not sure it would be a benefit: what we 
gain in parallel reads/writes (which isn't always a gain especially on 
commodity drives), we lose in caching and refreshing.

Having said that, the weak and strong random sources could reasonably be 
static. These are self-contained and can be common between many nodes. That's 
a sensible optimisation both to code quality and to performance.
> 
> > Modified: 
branches/db4o/freenet/src/freenet/support/io/PaddedEphemerallyEncryptedBucket.java
> > ===================================================================
> > --- 
branches/db4o/freenet/src/freenet/support/io/PaddedEphemerallyEncryptedBucket.java
  
2008-06-28 14:48:55 UTC (rev 20844)
> > +++ 
branches/db4o/freenet/src/freenet/support/io/PaddedEphemerallyEncryptedBucket.java
  
2008-06-28 14:49:23 UTC (rev 20845)
> > @@ -18,6 +18,8 @@
> >  import freenet.support.api.Bucket;
> >  import java.util.Random;
> >
> > +import org.spaceroots.mantissa.random.MersenneTwister;
> > +
> >  import com.db4o.ObjectContainer;
> >
> >  /**
> > @@ -29,10 +31,10 @@
> >
> >        private final Bucket bucket;
> >        private final int minPaddedSize;
> > -       private final Random randomSource;
> >        private transient SoftReference /* <Rijndael> */ aesRef;
> >        /** The decryption key. */
> >        private final byte[] key;
> > +       private final byte[] randomSeed;
> >        private long dataLength;
> >        private boolean readOnly;
> >        private int lastOutputStream;
> > @@ -49,10 +51,11 @@
> >         * @throws UnsupportedCipherException
> >         */
> >        public PaddedEphemerallyEncryptedBucket(Bucket bucket, int minSize, 
RandomSource strongPRNG, Random weakPRNG) {
> > -               this.randomSource = weakPRNG;
> >                this.bucket = bucket;
> >                if(bucket.size() != 0) throw new 
IllegalArgumentException("Bucket must be empty");
> >                byte[] tempKey = new byte[32];
> > +               randomSeed = new byte[32];
> > +               weakPRNG.nextBytes(randomSeed);
> >                strongPRNG.nextBytes(tempKey);
> >                this.key = tempKey;
> >                this.minPaddedSize = minSize;
> > @@ -76,9 +79,10 @@
> >                if(bucket.size() < knownSize)
> >                        throw new IOException("Bucket "+bucket+" is too 
small on disk - knownSize="+knownSize+" but bucket.size="+bucket.size()+" 
for "+bucket);
> >                this.dataLength = knownSize;
> > -               this.randomSource = origRandom;
> >                this.bucket = bucket;
> >                if(key.length != 32) throw new 
IllegalArgumentException("Key wrong length: "+key.length);
> > +               randomSeed = new byte[32];
> > +               origRandom.nextBytes(randomSeed);
> >                this.key = key;
> >                this.minPaddedSize = minSize;
> >                readOnly = false;
> > @@ -86,7 +90,6 @@
> >        }
> >
> >        public PaddedEphemerallyEncryptedBucket(SimpleFieldSet fs, 
RandomSource origRandom, PersistentFileTracker f) throws 
CannotCreateFromFieldSetException {
> > -               this.randomSource = origRandom;
> >                String tmp = fs.get("DataLength");
> >                if(tmp == null)
> >                        throw new CannotCreateFromFieldSetException("No 
DataLength");
> > @@ -116,6 +119,8 @@
> >                }
> >                if(dataLength > bucket.size())
> >                        throw new 
CannotCreateFromFieldSetException("Underlying bucket "+bucket+" is too small: 
should be "+dataLength+" actually "+bucket.size());
> > +               randomSeed = new byte[32];
> > +               origRandom.nextBytes(randomSeed);
> >        }
> >
> >        public OutputStream getOutputStream() throws IOException {
> > @@ -184,6 +189,7 @@
> >                                        Logger.normal(this, "Not padding 
out to length because have been superceded: "+getName());
> >                                        return;
> >                                }
> > +                               Random random = new 
MersenneTwister(randomSeed);
> >                                
synchronized(PaddedEphemerallyEncryptedBucket.this) {
> >                                        long finalLength = paddedLength();
> >                                        long padding = finalLength - 
dataLength;
> > @@ -191,7 +197,7 @@
> >                                        long writtenPadding = 0;
> >                                        while(writtenPadding < padding) {
> >                                                int left = (int) 
Math.min((padding - writtenPadding), (long)buf.length);
> > -                                               
randomSource.nextBytes(buf);
> > +                                               random.nextBytes(buf);
> >                                                out.write(buf, 0, left);
> >                                                writtenPadding += left;
> >                                        }
> >
> > _______________________________________________
> > cvs mailing list
> > cvs at freenetproject.org
> > http://emu.freenetproject.org/cgi-bin/mailman/listinfo/cvs
> >
> _______________________________________________
> Devl mailing list
> Devl at freenetproject.org
> http://emu.freenetproject.org/cgi-bin/mailman/listinfo/devl
> 
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: 
<https://emu.freenetproject.org/pipermail/devl/attachments/20080701/47b925cc/attachment.pgp>

Reply via email to