Ejemplo n.º 1
0
        private static void PushDupPop(int times)
        {
            var compiler = new BytecodeCompiler();

            for (int k = 0; k < times; k++)
            {
                compiler.Push(k);
            }

            compiler.Dup(times);

            Machine machine = new Machine();

            machine.Execute(compiler.ToBytes());

            DataWord value = new DataWord(times);

            Assert.AreEqual(DataWord.Zero, machine.Stack.Pop());

            for (int k = 0; k < times; k++)
            {
                value = value.Subtract(DataWord.One);
                Assert.AreEqual(value, machine.Stack.Pop());
            }

            Assert.AreEqual(0, machine.Stack.Size);
        }
Ejemplo n.º 2
0
        public ProgramInvoke(byte[] address, byte[] origin, byte[] caller, long balance,
                             long call_value, long token_value, long token_id, byte[] msg,
                             byte[] last_hash, byte[] coinbase, long timestamp,
                             long number, IDeposit deposit, long vm_start, long vm_should_end, long energy_limit)
        {
            this.address     = new DataWord(address);
            this.origin      = new DataWord(origin);
            this.caller      = new DataWord(caller);
            this.balance     = new DataWord(balance);
            this.call_value  = new DataWord(call_value);
            this.token_value = new DataWord(token_value);
            this.token_id    = new DataWord(token_id);
            if (msg != null && msg.Length > 0)
            {
                Array.Copy(msg, 0, this.msg, 0, msg.Length);
            }

            this.prev_hash = new DataWord(last_hash);
            this.coinbase  = new DataWord(coinbase);
            this.timestamp = new DataWord(timestamp);
            this.number    = new DataWord(number);
            this.deposit   = deposit;

            this.vm_start      = vm_start;
            this.vm_should_end = vm_should_end;
            this.energy_limit  = energy_limit;
        }
Ejemplo n.º 3
0
        public void Not()
        {
            DataWord dw1 = new DataWord(new byte[] { 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0 });
            DataWord dw2 = new DataWord(new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0, 0x0f });

            Assert.AreEqual(dw2, dw1.Not());
        }
Ejemplo n.º 4
0
 public override void OnStackPush(DataWord value)
 {
     if (this.enabled)
     {
         actions.AddStackPush(value);
     }
 }
Ejemplo n.º 5
0
        public ProgramInvoke(DataWord address, DataWord origin, DataWord caller, DataWord balance,
                             DataWord call_value, DataWord token_value, DataWord token_id, byte[] msg,
                             DataWord last_hash, DataWord coinbase, DataWord timestamp, DataWord number,
                             DataWord difficulty,
                             IDeposit deposit, int call_deep, bool is_static_call, bool is_testing_suite,
                             long vm_start, long vm_should_end, long energy_limit)
        {
            this.address     = address;
            this.origin      = origin;
            this.caller      = caller;
            this.balance     = balance;
            this.call_value  = call_value;
            this.token_value = token_value;
            this.token_id    = token_id;
            if (msg != null && msg.Length > 0)
            {
                Array.Copy(msg, 0, this.msg, 0, msg.Length);
            }

            this.prev_hash = last_hash;
            this.coinbase  = coinbase;
            this.timestamp = timestamp;
            this.number    = number;
            this.call_deep = call_deep;

            this.deposit          = deposit;
            this.by_transaction   = false;
            this.is_static_call   = is_static_call;
            this.is_testing_suite = is_testing_suite;
            this.vm_start         = vm_start;
            this.vm_should_end    = vm_should_end;
            this.energy_limit     = energy_limit;
        }
 public ReturnDataCopyIllegalBoundsException(DataWord offset, DataWord size, long return_data_size)
     : base(string.Format("Illegal RETURNDATACOPY arguments: offset (%s) + size (%s) > RETURNDATASIZE (%d)",
                          offset,
                          size,
                          return_data_size))
 {
 }
Ejemplo n.º 7
0
        public void Xor()
        {
            DataWord dw1 = new DataWord(new byte[] { 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0 });
            DataWord dw2 = new DataWord(new byte[] { 0xf1, 0x1f, 0xf1, 0x1f, 0xf1, 0x1f });
            DataWord dw3 = new DataWord(new byte[] { 0xfe, 0xef, 0xfe, 0xef, 0xfe, 0xef });

            Assert.AreEqual(dw3, dw1.Xor(dw2));
        }
Ejemplo n.º 8
0
        public void And()
        {
            DataWord dw1 = new DataWord(new byte[] { 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0 });
            DataWord dw2 = new DataWord(new byte[] { 0xf1, 0x1f, 0xf1, 0x1f, 0xf1, 0x1f });
            DataWord dw3 = new DataWord(new byte[] { 0x01, 0x10, 0x01, 0x10, 0x01, 0x10 });

            Assert.AreEqual(dw3, dw1.And(dw2));
        }
Ejemplo n.º 9
0
        public void DivideSixIntoThree()
        {
            var dw = new DataWord(6);

            var result = dw.Divide(new DataWord(3)).Value;

            Assert.IsNotNull(result);
            Assert.AreEqual(new BigInteger(2), result);
        }
Ejemplo n.º 10
0
        public void NegatePositiveInteger()
        {
            var dw = new DataWord(3);

            var result = dw.Negate();

            Assert.IsNotNull(result);
            Assert.AreEqual(new DataWord(-3), result);
        }
Ejemplo n.º 11
0
        public void GetSmallIntegerAsBigInteger()
        {
            var dw = new DataWord(1);

            var result = dw.Value;

            Assert.IsNotNull(result);
            Assert.AreEqual(BigInteger.One, result);
        }
Ejemplo n.º 12
0
        public void AddOneToOne()
        {
            var dw = new DataWord(1);

            var result = dw.Add(dw).Value;

            Assert.IsNotNull(result);
            Assert.AreEqual(new BigInteger(2), result);
        }
Ejemplo n.º 13
0
        public void MultiplyTwoByThree()
        {
            var dw = new DataWord(2);

            var result = dw.Multiply(new DataWord(3)).Value;

            Assert.IsNotNull(result);
            Assert.AreEqual(new BigInteger(6), result);
        }
Ejemplo n.º 14
0
        public void SubtractOneFromTwo()
        {
            var dw = new DataWord(2);

            var result = dw.Subtract(new DataWord(1)).Value;

            Assert.IsNotNull(result);
            Assert.AreEqual(BigInteger.One, result);
        }
Ejemplo n.º 15
0
        public void SubtractMinusOneFromTwo()
        {
            var dw = new DataWord(2);

            var result = dw.Subtract(new DataWord(-1)).Value;

            Assert.IsNotNull(result);
            Assert.AreEqual(new BigInteger(3), result);
        }
Ejemplo n.º 16
0
        public void AddOneToMinusOne()
        {
            var dw  = new DataWord(1);
            var dw2 = new DataWord(-1);

            var result = dw.Add(dw2).Value;

            Assert.IsNotNull(result);
            Assert.AreEqual(BigInteger.Zero, result);
        }
 public SolidityMessageCall(SolidityOpCodes opCode, DataWord gas,
                            DataWord codeAddress, DataWord endowment, DataWord inDataOffs, DataWord inDataSize)
 {
     OpCode      = opCode;
     Gas         = gas;
     CodeAddress = codeAddress;
     Endowment   = endowment;
     InDataOffs  = inDataOffs;
     InDataSize  = inDataSize;
 }
Ejemplo n.º 18
0
 public void PutStorageValue(byte[] address, DataWord key, DataWord value)
 {
     if (CanListenTrace(address))
     {
         this.program_listener.OnStoragePut(key, value);
     }
     else
     {
         this.deposit.PutStorageValue(address, key, value);
     }
 }
Ejemplo n.º 19
0
 public void Put(DataWord key, DataWord value)
 {
     if (this.row_cache.ContainsKey(key))
     {
         this.row_cache[key].Value = value;
     }
     else
     {
         byte[] row_key = Compose(key.Data, this.address_hash);
         this.row_cache.Add(key, new StorageRowCapsule(row_key, value.Data));
     }
 }
Ejemplo n.º 20
0
        public Op AddOp(byte code, int pc, int deep, DataWord energy, OpActions actions)
        {
            Op op = new Op();

            op.Code    = (OpCode)code;
            op.PC      = pc;
            op.Deep    = deep;
            op.Energy  = energy.ToBigInteger();
            op.Actions = actions;
            this.ops.Add(op);

            return(op);
        }
Ejemplo n.º 21
0
        public IProgramInvoke CreateProgramInvoke(Program program,
                                                  DataWord to_adderess,
                                                  DataWord caller_address,
                                                  DataWord in_value,
                                                  DataWord token_value,
                                                  DataWord token_id,
                                                  long in_balance,
                                                  byte[] in_data,
                                                  IDeposit deposit,
                                                  bool is_static_call,
                                                  bool is_testing_suite,
                                                  long vm_start,
                                                  long vm_should_end,
                                                  long energy_limit)
        {
            DataWord address    = to_adderess;
            DataWord origin     = program.OriginAddress;
            DataWord caller     = caller_address;
            DataWord balance    = new DataWord(in_balance);
            DataWord call_value = in_value;

            byte[] data = null;
            Array.Copy(in_data, 0, data, 0, in_data.Length);

            DataWord last_hash  = program.PrevHash;
            DataWord coinbase   = program.Coinbase;
            DataWord timestamp  = program.Timestamp;
            DataWord number     = program.Number;
            DataWord difficulty = program.Difficulty;

            return(new ProgramInvoke(address,
                                     origin,
                                     caller,
                                     balance,
                                     call_value,
                                     token_value,
                                     token_id,
                                     data,
                                     last_hash,
                                     coinbase,
                                     timestamp,
                                     number,
                                     difficulty,
                                     deposit,
                                     program.CallDeep + 1,
                                     is_static_call,
                                     is_testing_suite,
                                     vm_start,
                                     vm_should_end,
                                     energy_limit));
        }
Ejemplo n.º 22
0
 public override void OnStoragePut(DataWord key, DataWord value)
 {
     if (this.enabled)
     {
         if (value == DataWord.ZERO)
         {
             actions.AddStorageRemove(key);
         }
         else
         {
             actions.AddStoragePut(key, value);
         }
     }
 }
Ejemplo n.º 23
0
        public void CreateDataWordUsingNegativeInteger()
        {
            var dw = new DataWord(-1);

            var result = dw.Bytes;

            Assert.IsNotNull(result);
            Assert.AreEqual(32, result.Length);

            for (int k = 0; k < 32; k++)
            {
                Assert.AreEqual(0xff, result[k]);
            }
        }
Ejemplo n.º 24
0
        public void CreateDataWordUsingBytes()
        {
            var dw = new DataWord(new byte[] { 0x01, 0x02 });

            Assert.AreEqual(new BigInteger(258), dw.Value);

            var result = dw.Bytes;

            for (int k = 0; k < 30; k++)
            {
                Assert.AreEqual(0x00, result[k]);
            }

            Assert.AreEqual(0x01, result[30]);
            Assert.AreEqual(0x02, result[31]);
        }
Ejemplo n.º 25
0
        public void Not()
        {
            DataWord dw1 = new DataWord(new byte[] { 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0 });
            DataWord dw2 = new DataWord(new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0, 0x0f });

            var compiler = new BytecodeCompiler();

            compiler.Push(dw1);
            compiler.Not();

            Machine machine = new Machine();

            machine.Execute(compiler.ToBytes());

            Assert.AreEqual(dw2, machine.Stack.Pop());
        }
Ejemplo n.º 26
0
        public void CreateDataWordUsingPositiveInteger()
        {
            var dw = new DataWord(1);

            var result = dw.Bytes;

            Assert.IsNotNull(result);
            Assert.AreEqual(32, result.Length);

            for (int k = 0; k < 31; k++)
            {
                Assert.AreEqual(0x00, result[k]);
            }

            Assert.AreEqual(0x01, result[31]);
        }
Ejemplo n.º 27
0
        public void CreateDataWordUsingPositiveLongInteger()
        {
            var dw = new DataWord(0x0100000000L);

            var result = dw.Bytes;

            Assert.IsNotNull(result);
            Assert.AreEqual(32, result.Length);

            for (int k = 0; k < 32; k++)
            {
                if (k != 32 - 5)
                {
                    Assert.AreEqual(0x00, result[k]);
                }
            }

            Assert.AreEqual(0x01, result[32 - 5]);
        }
Ejemplo n.º 28
0
        public void Equals()
        {
            var dw1 = new DataWord(1);
            var dw2 = new DataWord(2);
            var dw3 = new DataWord(1);

            Assert.AreEqual(dw1, dw1);
            Assert.AreEqual(dw1, dw3);
            Assert.AreEqual(dw3, dw1);

            Assert.IsFalse(dw1.Equals(null));

            Assert.AreNotEqual(dw1, null);
            Assert.AreNotEqual(dw1, "foo");
            Assert.AreNotEqual(dw1, 42);
            Assert.AreNotEqual(dw1, dw2);

            Assert.AreEqual(dw1.GetHashCode(), dw3.GetHashCode());
        }
Ejemplo n.º 29
0
        public void PutStorageValue(byte[] address, DataWord key, DataWord value)
        {
            address = Wallet.ToAddAddressPrefix(address);
            if (GetAccount(address) != null)
            {
                Key       address_key = new Key(address);
                VMStorage storage     = null;

                if (this.storage_cache.ContainsKey(address_key))
                {
                    storage = this.storage_cache[address_key];
                }
                else
                {
                    storage = GetStorage(address);
                    this.storage_cache.Add(address_key, storage);
                }
                storage.Put(key, value);
            }
        }
Ejemplo n.º 30
0
        public DataWord GetDataValue(DataWord index_data)
        {
            BigInteger temp_index = index_data.ToBigInteger();
            int        index      = temp_index.IntValue;
            int        size       = 32; // maximum datavalue size

            if (msg == null || index >= msg.Length ||
                temp_index.CompareTo(MAX_MSG_DATA) > 0)
            {
                return(new DataWord());
            }
            if (index + size > msg.Length)
            {
                size = msg.Length - index;
            }

            byte[] data = new byte[32];
            Array.Copy(msg, index, data, 0, size);

            return(new DataWord(data));
        }