Skip to content

Instantly share code, notes, and snippets.

@shingonu
Last active September 1, 2018 06:59
Show Gist options
  • Select an option

  • Save shingonu/e7ebea336ac9cd0d8124d329e1dd4ff1 to your computer and use it in GitHub Desktop.

Select an option

Save shingonu/e7ebea336ac9cd0d8124d329e1dd4ff1 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.24;

contract StorageTest {
  // saved at index 0
	uint storedUint256 = 15; // actually stored value: 0x000000000000000000000000000000000000000000000000000000000000000f
                           // get stored value: 0x0f = 15

  // constant variable is not stored in a storage. available only in code
	uint constant constUint256 = 16;

  // index 1
  // 0x1000000020000000000000004000000000000000000000000000000080: because each of items are packed
  uint128 storedUint128 = uint128(128); // 0x00000000000000000000000000000080: 128bit packed
	uint64 storedUint64 = uint64(64); // 0x0000000000000040: 64bit packed
  uint32 storedUint32 = uint32(32); // 0x00000020: 32bit packed
  uint16 storedUint16 = uint16(16); // 0x10: 16bit packed

  // bytes(string) are left-aligned
  // index 2
	bytes16 storedBytes16 = 'bytes16'; // actually stored value: 0x6279746573313600000000000000000000000000000000000000000000000000
                                     // get stored value: 0x62797465733136000000000000000000
  // index 3
	bytes32 storedBytes32 = 'bytes32'; // 0x6279746573333200000000000000000000000000000000000000000000000000

  // index 4
	string storedString = 'lets string something'; // 0x6c65747320737472696e6720736f6d657468696e67000000000000000000002a
                                                 // 2a: length of the stored string
  // index 5
  bytes storedBytes = 'lets bytes something'; // 0x6c65747320627974657320736f6d657468696e67000000000000000000000028
                                              // 28: length of the stored bytes

  // index 6
	mapping (address => uint) storedUintMapping;

	// index 7
	mapping (address => DeviceData) storedStructMapping;

	// index 8
	uint[] stroedUintArray;

	// index 9
	DeviceData[] stroedStructArray;

	// index 10
	// index: 000000000000000000000000000000000000000000000000000000000000000a; not 10
	bytes long_bytes = 'lets bytes somethinglets bytes somethinglets bytes somethinglets bytes somethinglets bytes somethinglets bytes somethinglets bytes somethinglets bytes somethinglets bytes something';

	struct DeviceData {
		string deviceBrand;
		string deviceYear;
		string batteryWearLevel;
	}

	constructor() {
		address address1 = 0xbccc714d56bc0da0fd33d96d2a87b680dd6d0df6;
		address address2 = 0xaee905fdd3ed851e48d22059575b9f4245a82b04;

		// key:   000000000000000000000000bccc714d56bc0da0fd33d96d2a87b680dd6d0df6
		// index: 0000000000000000000000000000000000000000000000000000000000000006
		// newKey = web3.sha3(key + index, {encoding: 'hex'})
		// web3.eth.getStorageAt(storage.address, newKey)
		storedUintMapping[address1] = 88;
		// key:   000000000000000000000000aee905fdd3ed851e48d22059575b9f4245a82b04
		// index: 0000000000000000000000000000000000000000000000000000000000000006
		// newKey = web3.sha3(key + index, {encoding: 'hex'})
		// web3.eth.getStorageAt(storage.address, newKey)
		storedUintMapping[address2] = 99;

		DeviceData memory deviceData1 = DeviceData('deviceBrand', 'deviceYear', 'wearLevel');
		DeviceData memory deviceData2 = DeviceData('deviceBrand2', 'deviceYear2', 'wearLevel2');

		// key:   000000000000000000000000bccc714d56bc0da0fd33d96d2a87b680dd6d0df6
		// index: 0000000000000000000000000000000000000000000000000000000000000007
		// newKey = web3.sha3(key + index, {encoding: 'hex'})
		// web3.eth.getStorageAt(storage.address, newKey)
		storedStructMapping[address1] = deviceData1;
		// key:   000000000000000000000000aee905fdd3ed851e48d22059575b9f4245a82b04
		// index: 0000000000000000000000000000000000000000000000000000000000000007
		storedStructMapping[address2] = deviceData2;

		// index: 0000000000000000000000000000000000000000000000000000000000000008
		// newKey = web3.sha3(index, {encoding: 'hex'})
		stroedUintArray.push(8000);
		stroedUintArray.push(9000);

		// // index: 0000000000000000000000000000000000000000000000000000000000000009
		stroedStructArray.push(deviceData1);
		stroedStructArray.push(deviceData2);
	}
}

py-evm(set_storage):

slot: 0
value: 15

slot: 1
value: 128

slot: 1
value: 21778071482940061661655974875633165533312

slot: 1
value: 200867255532373784464523333025585386976931349098482269945984

slot:
1
value:
431359146875277492247046025856837423803779697737580508796140033933440

slot:
2
value:
130894972304013175724498431486149525504

slot:
3
value:
44541250993661105723971443186088606928234546916071428720225455249805470072832

slot:
4
value:
49029042906922969901734554691301297887570019801897965441898792426797317226538

slot:
5
value:
49029042906915984554512737078615976724949305516480722206860508365224614559784

slot:
10
value:
361


slot:
89717814153306320011181716697424560163256864414616650038987186496166826726056
value:
49029042906915984554512737078615976724949305516514269239347215852646736011379


slot:
89717814153306320011181716697424560163256864414616650038987186496166826726057
value:
50400012737534102478324154351084227948985683662619822954465920641346665018483


slot:
89717814153306320011181716697424560163256864414616650038987186496166826726058
value:
14648000416358483001101598620517239928977449173790888839152161428277576951156


slot:
89717814153306320011181716697424560163256864414616650038987186496166826726059
value:
47226817174849552041714915493183906153384245614527817775140899635286081960308


slot:
89717814153306320011181716697424560163256864414616650038987186496166826726060
value:
45887009087371050849815886630706467834827751969975616337214757157066605424231


slot:
89717814153306320011181716697424560163256864414616650038987186496166826726061
value:
49029042906915984554512737078615976724949305516480722206860508365224614559744


slot:
51585035973360692978684636316493780110208890353207776343162585620461974854853
value:
88


slot:
6254894918754573291960568658852041102015644193293940157536329702859138961221
value:
99


slot:
43982064508480847873351347735937200379899590164103267355902819853076606033409
value:
45410553659200149633113472071629400325855882494866985506843706053079571890198


slot:
43982064508480847873351347735937200379899590164103267355902819853076606033410
value:
45410553659200186511086513177822188662218627412332250148632543772189780869140


slot:
43982064508480847873351347735937200379899590164103267355902819853076606033411
value:
54004353085826349185781162644695375349977207480772039398741334963602702991378


slot:
37691979376221348395802834935613400534714617395574144608542841186711889942432
value:
45410553659200149633113472144704482192401028405051227142657857035876199039000


slot:
37691979376221348395802834935613400534714617395574144608542841186711889942433
value:
45410553659200186511086531885043146497775980419498108917055195368126330961942


slot:
37691979376221348395802834935613400534714617395574144608542841186711889942434
value:
54004353085826349185785951693260581252659577315231884114940143523359526748180


slot:
8
value:
1


slot:
110349606679412691172957834289542550319383271247755660854362242977991410020067
value:
8000


slot:
8
value:
2


slot:
110349606679412691172957834289542550319383271247755660854362242977991410020068
value:
9000


slot:
9
value:
1


slot:
49791959467252497455735130940088646708311117250336157395101362029847983277999
value:
45410553659200149633113472071629400325855882494866985506843706053079571890198


slot:
49791959467252497455735130940088646708311117250336157395101362029847983278000
value:
45410553659200186511086513177822188662218627412332250148632543772189780869140


slot:
49791959467252497455735130940088646708311117250336157395101362029847983278001
value:
54004353085826349185781162644695375349977207480772039398741334963602702991378

반면에 상태변수에 값을 할당하지 않으면 어떻게 될까?

pragma solidity ^0.4.24;

contract StorageTest {
  // saved at index 0
	uint storedUint256;

  // index 1
  // 0x1000000020000000000000004000000000000000000000000000000080: because each of items are packed
  uint128 storedUint128;
	uint64 storedUint64;
  uint32 storedUint32;
  uint16 storedUint16;

  // bytes(string) are left-aligned
  // index 2
	bytes16 storedBytes16;
                                     // get stored value: 0x62797465733136000000000000000000
  // index 3
	bytes32 storedBytes32;

  // index 4
	string storedString;
                                                 // 2a: length of the stored string
  // index 5
  bytes storedBytes;
                                              // 28: length of the stored bytes

  // index 6
	mapping (address => uint) storedUintMapping;

	// index 7
	mapping (address => DeviceData) storedStructMapping;

	// index 8
	uint[] stroedUintArray;

	// index 9
	DeviceData[] stroedStructArray;

	// index 10
	// index: 000000000000000000000000000000000000000000000000000000000000000a; not 10
	bytes long_bytes;

	struct DeviceData {
		string deviceBrand;
		string deviceYear;
		string batteryWearLevel;
	}

	constructor() {
	}
}

set_storage를 아예 하지 않는다. 45410553659200186511086513177822188662218627412332250148632543772189780869140지정하 45410553659200186511086513177822188662218627412332250148632543772189780869140

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment