Ejemplo n.º 1
0
        public coin__UxBody makeUxBody()
        {
            var uxb = new coin__UxBody();
            var s   = new cipher_SecKey();

            makeUxBodyWithSecret(uxb, s);
            return(uxb);
        }
Ejemplo n.º 2
0
        public void makeUxOutWithSecret(coin__UxOut uxOut, cipher_SecKey secKey)
        {
            var uxBody = new coin__UxBody();

            makeUxBodyWithSecret(uxBody, secKey);
            uxOut = new coin__UxOut();
            var uxHead = new coin__UxHead();

            uxHead.Time  = 100;
            uxHead.BkSeq = 2;
            uxOut.Body   = uxBody;
            uxOut.Head   = uxHead;
        }
Ejemplo n.º 3
0
        public void makeUxBodyWithSecret(coin__UxBody uxBody, cipher_SecKey secKey)
        {
            var p = new cipher_PubKey();

            skycoin.skycoin.SKY_cipher_GenerateKeyPair(p, secKey);
            var b = new GoSlice();

            skycoin.skycoin.SKY_cipher_RandByte(128, b);
            var h = new cipher_SHA256();

            skycoin.skycoin.SKY_cipher_SumSHA256(b, h);
            uxBody.SetSrcTransaction(h);
            var a = new cipher__Address();

            skycoin.skycoin.SKY_cipher_AddressFromPubKey(p, a);
            uxBody.Address = a;
            uxBody.Coins   = (ulong)(1e6);
            uxBody.Hours   = 100;
        }
Ejemplo n.º 4
0
        public void TestUxOutSnapshotHash()
        {
            var p   = new cipher_PubKey();
            var s   = new cipher_SecKey();
            var err = SKY_cipher_GenerateKeyPair(p, s);

            Assert.AreEqual(err, SKY_OK);
            var uxb = new coin__UxBody();
            var b   = new GoSlice();

            err = SKY_cipher_RandByte(128, b);
            Assert.AreEqual(err, SKY_OK);
            var h = new cipher_SHA256();

            err = SKY_cipher_SumSHA256(b, h);
            Assert.AreEqual(err, SKY_OK);
            uxb.SetSrcTransaction(h);
            var a = new cipher__Address();

            err = SKY_cipher_AddressFromPubKey(p, a);
            Assert.AreEqual(err, SKY_OK);
            uxb.Address = a;
            uxb.Coins   = (ulong)1e6;
            uxb.Hours   = (ulong)100;
            var uxo = new coin__UxOut();
            var uxh = new coin__UxHead();

            uxh.Time  = 100;
            uxh.BkSeq = 2;
            uxo.Head  = uxh;
            uxo.Body  = uxb;
            var hn = new cipher_SHA256();

            err = SKY_coin_UxOut_SnapshotHash(uxo, hn);
            Assert.AreEqual(err, SKY_OK);
            // snapshot hash should be dependent on every field in body and head
            // Head Time
            var uxo_2 = uxo;

            uxh.Time   = 20;
            uxo_2.Head = uxh;
            var hn_2 = new cipher_SHA256();

            err = SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(hn.isEqual(hn_2), 0);
            // Head BkSeq
            uxo_2      = uxo;
            uxh.BkSeq  = 4;
            uxo_2.Head = uxh;
            hn_2       = new cipher_SHA256();
            err        = SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(hn.isEqual(hn_2), 0);
            // Body SetSrcTransaction
            uxo_2 = uxo;
            uxb   = new coin__UxBody();
            err   = SKY_cipher_RandByte(128, b);
            Assert.AreEqual(err, SKY_OK);
            h   = new cipher_SHA256();
            err = SKY_cipher_SumSHA256(b, h);
            Assert.AreEqual(err, SKY_OK);
            uxb.SetSrcTransaction(h);
            uxo_2.Body = uxb;
            hn_2       = new cipher_SHA256();
            err        = SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(hn.isEqual(hn_2), 0);
            // Body Address
            var p_2 = new cipher_PubKey();
            var s_2 = new cipher_SecKey();

            err = SKY_cipher_GenerateKeyPair(p_2, s_2);
            Assert.AreEqual(err, SKY_OK);
            var a_2 = new cipher__Address();

            err = SKY_cipher_AddressFromPubKey(p_2, a_2);
            Assert.AreEqual(err, SKY_OK);
            uxo_2       = uxo;
            uxb         = new coin__UxBody();
            uxb.Address = a_2;
            uxo_2.Body  = uxb;
            hn_2        = new cipher_SHA256();
            err         = SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(hn.isEqual(hn_2), 0);
            // Body Coins
            uxo_2      = uxo;
            uxb        = new coin__UxBody();
            uxb.Coins  = 2;
            uxo_2.Body = uxb;
            hn_2       = new cipher_SHA256();
            err        = SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(hn.isEqual(hn_2), 0);
            // Body Hours
            uxo_2      = uxo;
            uxb        = new coin__UxBody();
            uxb.Hours  = 2;
            uxo_2.Body = uxb;
            hn_2       = new cipher_SHA256();
            err        = SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(hn.isEqual(hn_2), 0);
        }
Ejemplo n.º 5
0
        public void TestUxOutCoinHours()
        {
            var p = new cipher_PubKey();
            var s = new cipher_SecKey();

            SKY_cipher_GenerateKeyPair(p, s);
            var uxb = new coin__UxBody();
            var b   = new GoSlice();

            SKY_cipher_RandByte(128, b);
            var h   = new cipher_SHA256();
            var err = SKY_cipher_SumSHA256(b, h);

            Assert.AreEqual(err, SKY_OK);
            uxb.SetSrcTransaction(h);
            var a = new cipher__Address();

            SKY_cipher_AddressFromPubKey(p, a);
            uxb.Address = a;
            uxb.Coins   = (ulong)1e6;
            uxb.Hours   = 100;
            var uxo = new coin__UxOut();
            var uxh = new coin__UxHead();

            uxh.Time  = 100;
            uxh.BkSeq = 2;
            uxo.Head  = uxh;
            uxo.Body  = uxb;

            // Less than an hour passed
            var now   = uxh.Time + 100;
            var hours = new_GoUint64p();

            err = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(uxh.Time, GoUint64p_value(hours), "Less than an hour passed");
            // 1 hours passed
            now = uxh.Time + 3600;
            err = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), uxh.Time + (uxb.Coins / 1000000), "1 hours passed");
            // 6 hours passed
            now = uxh.Time + 3600 * 6;
            err = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), uxh.Time + (uxb.Coins / 1000000) * 6, "1 hours passed");
            // Time is backwards (treated as no hours passed)
            now = uxh.Time / 2;
            err = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), uxh.Time);
            // 1 hour has passed, output has 1.5 coins, should gain 1 coinhour
            uxb.Coins = 1500000;
            now       = uxh.Time + 3600;
            err       = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), uxb.Hours + 1);
            // 2 hours have passed, output has 1.5 coins, should gain 3 coin hours
            uxb.Coins = 1500000;
            uxo.Body  = uxb;
            now       = uxh.Time + 3600 * 2;
            err       = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            // 1 second has passed, output has 3600 coins, should gain 1 coin hour
            uxb.Coins = 3600000000;
            uxo.Body  = uxb;
            now       = uxh.Time + 1;
            err       = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), uxb.Hours + 1);
            // 1000000 hours minus 1 second have passed, output has 1 droplet, should gain 0 coin hour
            uxb.Coins = 1;
            uxo.Body  = uxb;
            now       = (ulong)(uxh.Time + (ulong)Convert.ToInt64(1e6 * 3600) - 1);
            err       = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), uxb.Hours);
            // 1000000 hours have passed, output has 1 droplet, should gain 1 coin hour
            uxb.Coins = 1;
            uxo.Body  = uxb;
            now       = uxh.Time + Convert.ToUInt64(10e5 * 3600);
            err       = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), uxb.Hours + 1);
            // No hours passed, using initial coin hours
            uxb.Coins = (ulong)10e8;
            uxb.Hours = 1000 * 1000;
            uxo.Body  = uxb;
            now       = uxh.Time;
            err       = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), uxb.Hours);
            // One hour passed, using initial coin hours
            now = uxh.Time + 3600;
            err = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), uxb.Hours + (10e8 / 10e5));
            // No hours passed and no hours to begin with 0
            uxb.Hours = 0;
            uxo.Body  = uxb;
            now       = uxh.Time;
            err       = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_OK);
            Assert.AreEqual(GoUint64p_value(hours), 0);
            // Centuries have passed, time-based calculation overflows uint64 when calculating the whole coin seconds
            uxb.Coins = (ulong)20e5;
            uxo.Body  = uxb;
            now       = 0xFFFFFFFFFFFFFFFF;
            err       = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_ERROR);
            // Centuries have passed, time-based calculation overflows uint64 when calculating the droplet seconds
            uxb.Coins = (ulong)15e5;
            uxo.Body  = uxb;
            now       = 0xFFFFFFFFFFFFFFFF;
            err       = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_ERROR);
            // Output would overflow if given more hours, has reached its limit
            uxb.Coins = (ulong)36e8;
            uxo.Body  = uxb;
            now       = 0xFFFFFFFFFFFFFFFF;
            err       = SKY_coin_UxOut_CoinHours(uxo, now, hours);
            Assert.AreEqual(err, SKY_ERROR);
        }