Пример #1
0
        public static bool InitGenesisBlock(byte[] rawHeader, byte[] pubKeyList)
        {
            if (IsGenesised() != 0)
            {
                return(false);
            }
            Header header = deserializHeader(rawHeader);

            Runtime.Notify("header deserialize");
            if (pubKeyList.Length % MCCHAIN_PUBKEY_LEN != 0)
            {
                Runtime.Notify("Length of pubKeyList is illegal");
                return(false);
            }
            BookKeeper bookKeeper = verifyPubkey(pubKeyList);

            Runtime.Notify("header deserialize");
            if (header.nextBookKeeper != bookKeeper.nextBookKeeper)
            {
                Runtime.Notify("NextBookers illegal");
            }
            Storage.Put(latestHeightPrefix, header.height);
            Storage.Put(latestBookKeeperHeightPrefix, header.height);
            Storage.Put("IsInitGenesisBlock", 1);
            Storage.Put(mCBlockHeadersPrefix.Concat(header.height.AsByteArray()), rawHeader);
            Map <BigInteger, BigInteger> MCKeeperHeight = new Map <BigInteger, BigInteger>();

            MCKeeperHeight[0] = header.height;
            Storage.Put(mCKeeperHeightPrefix, MCKeeperHeight.Serialize());
            Storage.Put(mCKeeperPubKeysPrefix.Concat(header.height.AsByteArray()), bookKeeper.keepers.Serialize());
            InitGenesisBlockEvent(header.height, rawHeader);
            return(true);
        }
Пример #2
0
        public static bool ChangeBookKeeper(byte[] rawHeader, byte[] pubKeyList, byte[] signList)
        {
            Header header = deserializHeader(rawHeader);

            if (header.height == 0)
            {
                return(InitGenesisBlock(rawHeader, pubKeyList));
            }
            BigInteger latestHeight = Storage.Get(latestHeightPrefix).Concat(new byte[] { 0x00 }).AsBigInteger();

            if (latestHeight > header.height)
            {
                Runtime.Notify("The height of header illegal");
                return(false);
            }
            if (header.nextBookKeeper.Length != 20)
            {
                Runtime.Notify("The nextBookKeeper of header is illegal");
                return(false);
            }

            BigInteger latestBookKeeperHeight = Storage.Get(latestBookKeeperHeightPrefix).Concat(new byte[] { 0x00 }).AsBigInteger();

            byte[][] keepers = (byte[][])Storage.Get(mCKeeperPubKeysPrefix.Concat(latestBookKeeperHeight.AsByteArray())).Deserialize();
            int      n       = keepers.Length;
            int      m       = n - (n - 1) / 3;

            if (!verifySig(rawHeader, signList, keepers, m))
            {
                Runtime.Notify("Verify signature failed");
                return(false);
            }
            BookKeeper bookKeeper = verifyPubkey(pubKeyList);

            if (header.nextBookKeeper != bookKeeper.nextBookKeeper)
            {
                Runtime.Notify("NextBookers illegal");
                return(false);
            }
            Storage.Put(latestHeightPrefix, header.height);
            Storage.Put(latestBookKeeperHeightPrefix, header.height);
            Storage.Put(mCBlockHeadersPrefix.Concat(header.height.AsByteArray()), rawHeader);
            Map <BigInteger, BigInteger> MCKeeperHeight = (Map <BigInteger, BigInteger>)Storage.Get(mCKeeperHeightPrefix).Deserialize();

            MCKeeperHeight[MCKeeperHeight.Keys.Length] = header.height;
            MCKeeperHeight = RemoveOutdateHeight(MCKeeperHeight);
            Storage.Put(mCKeeperHeightPrefix, MCKeeperHeight.Serialize());
            Storage.Put(mCKeeperPubKeysPrefix.Concat(header.height.AsByteArray()), bookKeeper.keepers.Serialize());
            ChangeBookKeeperEvent(header.height, rawHeader);
            return(true);
        }
Пример #3
0
        public static bool InitGenesisBlock(byte[] rawHeader, byte[] pubKeyList)
        {
            if (IsGenesised() != 0)
            {
                return(false);
            }
            Header header = deserializHeader(rawHeader);

            Runtime.Notify("header deserialize");
            if (pubKeyList.Length % MCCHAIN_PUBKEY_LEN != 0)
            {
                Runtime.Notify("Length of pubKeyList is illegal");
                return(false);
            }
            BookKeeper bookKeeper = verifyPubkey(pubKeyList);

            Runtime.Notify("header deserialize");
            if (header.nextBookKeeper != bookKeeper.nextBookKeeper)
            {
                Runtime.Notify("NextBookers illegal");
            }
            //更新最新区块高度
            Storage.Put(latestHeightPrefix, header.height);
            //更新共识公钥高度
            Storage.Put(latestBookKeeperHeightPrefix, header.height);
            //更新创世块创建状态 TODO:上线可切换为0
            Storage.Put("IsInitGenesisBlock", 1);
            //存放完整区块头
            Map <BigInteger, Header> MCBlockHeader = new Map <BigInteger, Header>();

            MCBlockHeader[header.height] = header;
            Storage.Put(mCBlockHeadersPrefix, MCBlockHeader.Serialize());
            //存放关键区块头高度
            Map <BigInteger, BigInteger> MCKeeperHeight = new Map <BigInteger, BigInteger>();

            MCKeeperHeight[0] = header.height;
            Storage.Put(mCKeeperHeightPrefix, MCKeeperHeight.Serialize());
            //存放关键区块头公钥
            Map <BigInteger, object[]> MCKeeperPubKeys = new Map <BigInteger, object[]>();

            MCKeeperPubKeys[header.height] = bookKeeper.keepers;
            Storage.Put(mCKeeperPubKeysPrefix, MCKeeperPubKeys.Serialize());
            //触发创世区块事件
            InitGenesisBlockEvent(header.height, rawHeader);
            return(true);
        }
Пример #4
0
        private static BookKeeper getBookKeeper(int keyLength, int m, byte[] pubKeyList)
        {
            byte[] buff = new byte[] { };
            buff = WriteUint16(keyLength, buff);

            byte[][] keepers = new byte[keyLength][];

            for (int i = 0; i < keyLength; i++)
            {
                buff = WriteVarBytes(buff, compressMCPubKey(pubKeyList.Range(i * MCCHAIN_PUBKEY_LEN, MCCHAIN_PUBKEY_LEN)));
                byte[] hash = bytesToBytes32(SmartContract.Sha256((pubKeyList.Range(i * MCCHAIN_PUBKEY_LEN, MCCHAIN_PUBKEY_LEN).Range(3, 64))));
                keepers[i] = hash;
            }
            BookKeeper bookKeeper = new BookKeeper();

            buff = WriteUint16(m, buff);
            bookKeeper.nextBookKeeper = bytesToBytes20(Hash160(buff));
            bookKeeper.keepers        = keepers;
            return(bookKeeper);
        }
Пример #5
0
        public static bool ChangeBookKeeper(byte[] rawHeader, byte[] pubKeyList, byte[] signList)
        {
            Header header = deserializHeader(rawHeader);

            if (header.height == 0)
            {
                return(InitGenesisBlock(rawHeader, pubKeyList));
            }
            BigInteger latestHeight = Storage.Get(latestHeightPrefix).Concat(new byte[] { 0x00 }).AsBigInteger();

            if (latestHeight > header.height)
            {
                Runtime.Notify("The height of header illegal");
                return(false);
            }
            if (header.nextBookKeeper.Length != 20)
            {
                Runtime.Notify("The nextBookKeeper of header is illegal");
                return(false);
            }

            Map <BigInteger, object[]> MCKeeperPubKeys = (Map <BigInteger, object[]>)Storage.Get(mCKeeperPubKeysPrefix).Deserialize();
            BigInteger latestBookKeeperHeight          = Storage.Get(latestBookKeeperHeightPrefix).AsBigInteger();

            object[] McPubKeys = MCKeeperPubKeys[latestBookKeeperHeight];
            int      n         = McPubKeys.Length;
            int      m         = n - (n - 1) / 3;

            if (!verifySig(rawHeader, signList, McPubKeys, m))
            {
                Runtime.Notify("Verify signature failed");
                return(false);
            }
            BookKeeper bookKeeper = verifyPubkey(pubKeyList);

            if (header.nextBookKeeper != bookKeeper.nextBookKeeper)
            {
                Runtime.Notify("NextBookers illegal");
                return(false);
            }
            //更新最新区块高度
            Storage.Put(latestHeightPrefix, header.height);
            //更新共识公钥高度
            Storage.Put(latestBookKeeperHeightPrefix, header.height);
            //存放完整区块头
            Map <BigInteger, Header> MCBlockHeader = (Map <BigInteger, Header>)Storage.Get(mCBlockHeadersPrefix).Deserialize();

            MCBlockHeader[header.height] = header;
            Storage.Put(mCBlockHeadersPrefix, MCBlockHeader.Serialize());
            //更新关键区块头高度
            Map <BigInteger, BigInteger> MCKeeperHeight = (Map <BigInteger, BigInteger>)Storage.Get(mCKeeperHeightPrefix).Deserialize();

            MCKeeperHeight[MCKeeperHeight.Keys.Length] = header.height;
            Storage.Put(mCKeeperHeightPrefix, MCKeeperHeight.Serialize());
            //更新关键区块头公钥
            MCKeeperPubKeys[header.height] = bookKeeper.keepers;
            Storage.Put(mCKeeperPubKeysPrefix, MCKeeperPubKeys.Serialize());
            //触发关键区块头事件
            ChangeBookKeeperEvent(header.height, rawHeader);
            return(true);
        }