2018-04-06 14:52:10 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2018-01-16 17:05:20 +00:00
|
|
|
import
|
2018-05-07 12:41:54 +00:00
|
|
|
sequtils, stint,
|
2018-01-29 17:40:22 +00:00
|
|
|
../constants, ../errors, ../logging, ../validation, ../utils_numeric, ../utils/bytes
|
2018-01-16 17:05:20 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
Memory* = ref object
|
|
|
|
logger*: Logger
|
2018-01-29 17:40:22 +00:00
|
|
|
bytes*: seq[byte]
|
2018-01-16 17:05:20 +00:00
|
|
|
|
|
|
|
proc newMemory*: Memory =
|
|
|
|
new(result)
|
|
|
|
result.bytes = @[]
|
2018-02-07 09:50:15 +00:00
|
|
|
result.logger = logging.getLogger("memory.Memory")
|
2018-01-16 17:05:20 +00:00
|
|
|
|
|
|
|
proc len*(memory: Memory): int =
|
2018-02-06 19:20:06 +00:00
|
|
|
result = memory.bytes.len
|
2018-01-16 17:05:20 +00:00
|
|
|
|
2018-05-07 12:41:54 +00:00
|
|
|
|
|
|
|
# TODO: why is the size passed as a UInt256?
|
2018-02-20 17:27:43 +00:00
|
|
|
proc extend*(memory: var Memory; startPosition: UInt256; size: UInt256) =
|
2018-01-16 17:05:20 +00:00
|
|
|
if size == 0:
|
|
|
|
return
|
|
|
|
var newSize = ceil32(startPosition + size)
|
2018-02-20 17:27:43 +00:00
|
|
|
if newSize <= len(memory).u256:
|
2018-01-16 17:05:20 +00:00
|
|
|
return
|
2018-02-20 17:27:43 +00:00
|
|
|
var sizeToExtend = newSize - len(memory).u256
|
2018-05-07 12:41:54 +00:00
|
|
|
memory.bytes = memory.bytes.concat(repeat(0.byte, sizeToExtend.toInt))
|
2018-01-16 17:05:20 +00:00
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
proc newMemory*(size: UInt256): Memory =
|
2018-01-24 13:31:24 +00:00
|
|
|
result = newMemory()
|
2018-02-20 17:27:43 +00:00
|
|
|
result.extend(0.u256, size)
|
2018-01-24 13:31:24 +00:00
|
|
|
|
2018-05-07 12:41:54 +00:00
|
|
|
# TODO: why is the size passed as a UInt256?
|
2018-02-20 17:27:43 +00:00
|
|
|
proc read*(memory: var Memory, startPosition: UInt256, size: UInt256): seq[byte] =
|
2018-05-07 12:41:54 +00:00
|
|
|
result = memory.bytes[startPosition.toInt ..< (startPosition + size).toInt]
|
2018-01-22 22:23:07 +00:00
|
|
|
|
2018-05-07 12:41:54 +00:00
|
|
|
# TODO: why is the size passed as a UInt256?
|
2018-02-20 17:27:43 +00:00
|
|
|
proc write*(memory: var Memory, startPosition: UInt256, size: UInt256, value: seq[byte]) =
|
2018-02-06 19:20:06 +00:00
|
|
|
if size == 0:
|
|
|
|
return
|
2018-04-06 14:52:10 +00:00
|
|
|
#echo size
|
2018-02-07 16:16:04 +00:00
|
|
|
#echo startPosition
|
2018-02-20 17:27:43 +00:00
|
|
|
#validateGte(startPosition, 0)
|
|
|
|
#validateGte(size, 0)
|
2018-05-07 12:41:54 +00:00
|
|
|
validateLength(value, size.toInt)
|
2018-02-06 19:20:06 +00:00
|
|
|
validateLte(startPosition + size, memory.len)
|
|
|
|
let index = memory.len
|
2018-02-20 17:27:43 +00:00
|
|
|
if memory.len.u256 < startPosition + size:
|
2018-05-07 12:41:54 +00:00
|
|
|
memory.bytes = memory.bytes.concat(repeat(0.byte, memory.len - (startPosition + size).toInt)) # TODO: better logarithmic scaling?
|
2018-02-06 19:20:06 +00:00
|
|
|
|
|
|
|
for z, b in value:
|
2018-05-07 12:41:54 +00:00
|
|
|
memory.bytes[z + startPosition.toInt] = b
|
2018-01-29 17:40:22 +00:00
|
|
|
|
2018-02-20 17:27:43 +00:00
|
|
|
template write*(memory: var Memory, startPosition: UInt256, size: UInt256, value: cstring) =
|
2018-01-29 17:40:22 +00:00
|
|
|
memory.write(startPosition, size, value.toBytes)
|