From 04878c5ed9a6c688fe09a11cc342fbfd3a5efd5e Mon Sep 17 00:00:00 2001 From: Ulf Norell Date: Tue, 1 Oct 2019 10:52:09 +0200 Subject: [PATCH 1/2] Add function to compute the byte size of a heap_value Used to compute store gas cost in AEVM --- src/aeb_heap.erl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/aeb_heap.erl b/src/aeb_heap.erl index e180102..14838a4 100644 --- a/src/aeb_heap.erl +++ b/src/aeb_heap.erl @@ -14,6 +14,7 @@ , heap_value_maps/1 , heap_value_offset/1 , heap_value_heap/1 + , heap_value_byte_size/1 , heap_fragment_maps/1 , heap_fragment_offset/1 , heap_fragment_heap/1 @@ -90,6 +91,25 @@ heap_value_offset({_, Heap}) -> Heap#heap.offset. binary() | #{non_neg_integer() => non_neg_integer()}. heap_value_heap({_, Heap}) -> Heap#heap.heap. +%% -- Byte size of a heap value ---------------------------------------------- + +-spec heap_value_byte_size(heap_value()) -> non_neg_integer(). +heap_value_byte_size({_, Heap}) -> + Value = Heap#heap.heap, + Maps = Heap#heap.maps, + ValueSize = + if is_binary(Value) -> byte_size(Value); + true -> 0 end, + MapsSize = + lists:sum([ pmap_size(Map) || Map <- maps:values(Maps#maps.maps) ]), + ValueSize + MapsSize. + +pmap_size(#pmap{data = stored}) -> 0; +pmap_size(#pmap{data = Data}) when is_map(Data) -> + lists:sum([ byte_size(Key) + byte_size(Val) + || {Key, Val} <- maps:to_list(Data), + Val /= tombstone ]). + %% -- Value to binary -------------------------------------------------------- -spec to_binary(aeb_aevm_data:data()) -> aeb_aevm_data:heap(). From 48cfbd03b0763669680b6ae6385180446cf7b922 Mon Sep 17 00:00:00 2001 From: Ulf Norell Date: Tue, 1 Oct 2019 10:52:54 +0200 Subject: [PATCH 2/2] Recalibrate the store map threshold Now that we charge gas for store writes the math is changed. --- src/aeb_fate_maps.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aeb_fate_maps.erl b/src/aeb_fate_maps.erl index 2b62f81..e57831d 100644 --- a/src/aeb_fate_maps.erl +++ b/src/aeb_fate_maps.erl @@ -25,7 +25,7 @@ %% Size in bytes of serialization of a map for which we turn it into a store %% map. It's not worth turning small maps into store maps. %% Under consensus! --define(STORE_MAP_THRESHOLD, 500). +-define(STORE_MAP_THRESHOLD, 100). -type fate_value() :: aeb_fate_data:fate_type(). -type fate_value_or_tombstone() :: fate_value() | ?FATE_MAP_TOMBSTONE.