From 79265d79c3d79c95d527d9832bd6ce576ccf229b Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 14 Aug 2024 11:07:03 +0200 Subject: [PATCH 001/157] actually add protobuf --- protobuf/substate.proto | 119 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 protobuf/substate.proto diff --git a/protobuf/substate.proto b/protobuf/substate.proto new file mode 100644 index 0000000..54efaa5 --- /dev/null +++ b/protobuf/substate.proto @@ -0,0 +1,119 @@ +yntax = "proto2"; +package research; + +// For a single optional bytes, use `optional BytesValue` +// For an optional list of bytes, use `repeated bytes` +import "google/protobuf/wrappers.proto"; + +option go_package = "../research"; + +message Substate { + + message Account { + required uint64 nonce = 1; + required bytes balance = 2; + + message StorageEntry { + required bytes key = 1; + required bytes value = 2; + } + repeated StorageEntry storage = 3; + + oneof contract { + bytes code = 4; + bytes code_hash = 5; + } + } + message AllocEntry { + required bytes address = 1; + required Account account = 2; + } + message Alloc { + repeated AllocEntry alloc = 1; + } + required Alloc input_alloc = 1; + required Alloc output_alloc = 2; + + message BlockEnv { + required bytes coinbase = 1; + required bytes difficulty = 2; + required uint64 gas_limit = 3; + required uint64 number = 4; + required uint64 timestamp = 5; + + message BlockHashEntry { + required uint64 key = 1; + required bytes value = 2; + } + repeated BlockHashEntry block_hashes = 6; + // London hard fork introduced BASEFEE instruction + optional google.protobuf.BytesValue base_fee = 7; + // The Merge hard fork replaced DIFFICULTY with PREVRANDAO + optional google.protobuf.BytesValue random = 8; + // Cancun hard fork introduced BLOBBASEFEE instruction + optional google.protobuf.BytesValue blob_base_fee = 9; + } + required BlockEnv block_env = 3; + + message TxMessage { + required uint64 nonce = 1; + required bytes gas_price = 2; + required uint64 gas = 3; + + required bytes from = 4; + // TxMessage.to is nil for contract creation + optional google.protobuf.BytesValue to = 5; + required bytes value = 6; + + oneof input { + bytes data = 7; + bytes init_code_hash = 8; + } + + enum TxType { + TXTYPE_LEGACY = 0; + // Berlin hard fork introduced optional access list + TXTYPE_ACCESSLIST = 1; + // London hard fork introduced optional dynamic fee market + TXTYPE_DYNAMICFEE = 2; + // Cancun hard fork introduced optional tx blob + TXTYPE_BLOB = 3; + } + required TxType tx_type = 9; + + // AccessList from TXTYPE_ACCESSLIST + // nil for tx types prior to TXTYPE_ACCESSLIST + message AccessListEntry { + required bytes address = 1; + repeated bytes storage_keys = 2; + } + repeated AccessListEntry access_list = 10; + + // GasFeeCap, GasTipCap from TXTYPE_DYNAMICFEE + // nil for tx types prior to TXTYPE_DYNAMICFEE + optional google.protobuf.BytesValue gas_fee_cap = 11; + optional google.protobuf.BytesValue gas_tip_cap = 12; + + // BlobFeeCap, BlobHashes, optional Sidecar from TXTYPE_BLOB + // nil for tx types prior to TXTYPE_BLOB + optional google.protobuf.BytesValue blob_gas_fee_cap = 13; + repeated bytes blob_hashes = 14; + } + required TxMessage tx_message = 4; + + message Result { + required uint64 status = 1; + required bytes bloom = 2; + + message Log { + required bytes address = 1; + repeated bytes topics = 2; + required bytes data = 3; + } + repeated Log logs = 3; + + required uint64 gas_used = 4; + } + required Result result = 5; + +} From 8838baa7a9f3d614af3880a879302415a5a7baa2 Mon Sep 17 00:00:00 2001 From: Petr Hanzl <84449820+petr-hanzl@users.noreply.github.com> Date: Fri, 23 Aug 2024 10:34:26 +0200 Subject: [PATCH 002/157] Fix incorrect message transition (#82) --- rlp/london.go | 1 + rlp/rlp_message.go | 7 +++++++ rlp/rlp_message_test.go | 17 +++++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 rlp/rlp_message_test.go diff --git a/rlp/london.go b/rlp/london.go index 79a6195..d24e978 100644 --- a/rlp/london.go +++ b/rlp/london.go @@ -146,6 +146,7 @@ func (m londonMessage) toMessage() *Message { Nonce: m.Nonce, CheckNonce: m.CheckNonce, GasPrice: m.GasPrice, + Gas: m.Gas, From: m.From, To: m.To, Value: m.Value, diff --git a/rlp/rlp_message.go b/rlp/rlp_message.go index 88fa96a..5190c1b 100644 --- a/rlp/rlp_message.go +++ b/rlp/rlp_message.go @@ -26,6 +26,13 @@ func NewMessage(sm *substate.Message) *Message { BlobHashes: sm.BlobHashes, } + if mess.To == nil { + // put contract creation init code into codeDB + dataHash := sm.DataHash() + mess.InitCodeHash = &dataHash + mess.Data = nil + } + return mess } diff --git a/rlp/rlp_message_test.go b/rlp/rlp_message_test.go new file mode 100644 index 0000000..47ec81d --- /dev/null +++ b/rlp/rlp_message_test.go @@ -0,0 +1,17 @@ +package rlp + +import ( + "math/big" + "testing" + + "github.com/Fantom-foundation/Substate/substate" + "github.com/Fantom-foundation/Substate/types/hash" +) + +func TestNewMessage_InitCodeHashIsCreated_WhenToIsNil(t *testing.T) { + data := []byte{0x1} + m := NewMessage(&substate.Message{Data: data, Value: big.NewInt(1), To: nil}) + if got, want := *m.InitCodeHash, hash.Keccak256Hash(data); got != want { + t.Fatalf("unexpected code hash\ngot: %s\nwant: %s", got, want) + } +} From c4231f23658c255c302530e349cf0ecb872c0ae8 Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 2 Sep 2024 09:01:24 +0200 Subject: [PATCH 003/157] added protobuf --- db/substate_iterator.go | 7 +- protobuf/.decode.go.swp | Bin 0 -> 12288 bytes protobuf/decode.go | 90 +++ protobuf/protoc-substate.sh | 5 + protobuf/substate.pb.go | 1339 +++++++++++++++++++++++++++++++++++ protobuf/substate.proto | 6 +- 6 files changed, 1442 insertions(+), 5 deletions(-) create mode 100644 protobuf/.decode.go.swp create mode 100644 protobuf/decode.go create mode 100644 protobuf/protoc-substate.sh create mode 100644 protobuf/substate.pb.go diff --git a/db/substate_iterator.go b/db/substate_iterator.go index 99a145c..36ad813 100644 --- a/db/substate_iterator.go +++ b/db/substate_iterator.go @@ -4,8 +4,10 @@ import ( "fmt" "github.com/syndtr/goleveldb/leveldb/util" + "github.com/golang/protobuf/proto" "github.com/Fantom-foundation/Substate/rlp" + pb "github.com/Fantom-foundation/Substate/protobuf" "github.com/Fantom-foundation/Substate/substate" ) @@ -33,8 +35,9 @@ func (i *substateIterator) decode(data rawEntry) (*substate.Substate, error) { return nil, fmt.Errorf("invalid substate key: %v; %w", key, err) } - rlpSubstate, err := rlp.Decode(value) - if err != nil { + //rlpSubstate, err := rlp.Decode(value) + pbSubstate := &pb.Substate{} + if err := proto.Unmarshal(value, pbSubstate); err != nil { return nil, err } diff --git a/protobuf/.decode.go.swp b/protobuf/.decode.go.swp new file mode 100644 index 0000000000000000000000000000000000000000..3adaa33d68cd70f7b81cb81280a126ece24938e2 GIT binary patch literal 12288 zcmeI2&x;&I6vu0osBJbTn46GLJ3^RQXM2(jhz>;9WOw6|{UK(vl8d3Ir)IX9?yjc0 zda~=X3S#skdh;eC-t;cwNx`6k{sCSDC0-LyuWRys{bOc2?#7w9NT3=%4E0{s>we#^ zSGCPnRdVW}y{GSCQkv*OnDHv$Llk+zH+{(}1<6 z=+-XBjYwHa)Phhd*@#=P+OjFkw*1dHx`S3gD=?x0v1-k{aOBF_*)vI@K3kgN$Dh73 zVi4V3E1(t73TOqi0$KsBfL1^&pcQzS6;QzhdkYCXk|l99`<=M&H*4x2t$eN`?fXoc^vS}p6xZRvMKs3M-9fSVCM zoq%id`yzA4SCU&!)0(lR7ldL{_>t3a1y7@6vl=A}W4B7l^645gjPtH+ylh3ABC2y- zjb5{wIWJn#q7WCXpq@B;P4c6$?gWXC2DkHO=F7)rzO+fcIolRdwCqGGxvAA_lCxWdqHpg~lkDrVZ$x(`$+#FwZ@}C_lgvnEz{*c~QQL$q zbG;Hec76w%Cm3*HQ`ntV>1X!L+BeVxreP8rhSa9BnRaRg*;{R~lT9ext2m+7x3V7l z&#BBg?xgBPKX_hccNlSErR5>~ZZR87F}t(iclS>Qj&T#o10S3Rf{&O3Gf+zgW~Acv za~y>qZkw2{GWHeOo6V5yYC8~-d45}oXkE_1h!m_*N_{WFf0{dkScm+7EvzWi7;uBdPlYJYkA+d=kn6xAs1e6&rIbe z{H_y9pVn(~uf-~eRlb@oll)h zmed^zkvw=}#oLU`i$bNzl`CexLkt})Pqq-QSHR;$9Mq@~HLs>|f@aDV;w2g protobuf.Substate.Alloc + 4, // 1: protobuf.Substate.output_alloc:type_name -> protobuf.Substate.Alloc + 5, // 2: protobuf.Substate.block_env:type_name -> protobuf.Substate.BlockEnv + 6, // 3: protobuf.Substate.tx_message:type_name -> protobuf.Substate.TxMessage + 7, // 4: protobuf.Substate.result:type_name -> protobuf.Substate.Result + 8, // 5: protobuf.Substate.Account.storage:type_name -> protobuf.Substate.Account.StorageEntry + 2, // 6: protobuf.Substate.AllocEntry.account:type_name -> protobuf.Substate.Account + 3, // 7: protobuf.Substate.Alloc.alloc:type_name -> protobuf.Substate.AllocEntry + 9, // 8: protobuf.Substate.BlockEnv.block_hashes:type_name -> protobuf.Substate.BlockEnv.BlockHashEntry + 12, // 9: protobuf.Substate.BlockEnv.base_fee:type_name -> google.protobuf.BytesValue + 12, // 10: protobuf.Substate.BlockEnv.random:type_name -> google.protobuf.BytesValue + 12, // 11: protobuf.Substate.BlockEnv.blob_base_fee:type_name -> google.protobuf.BytesValue + 12, // 12: protobuf.Substate.TxMessage.to:type_name -> google.protobuf.BytesValue + 0, // 13: protobuf.Substate.TxMessage.tx_type:type_name -> protobuf.Substate.TxMessage.TxType + 10, // 14: protobuf.Substate.TxMessage.access_list:type_name -> protobuf.Substate.TxMessage.AccessListEntry + 12, // 15: protobuf.Substate.TxMessage.gas_fee_cap:type_name -> google.protobuf.BytesValue + 12, // 16: protobuf.Substate.TxMessage.gas_tip_cap:type_name -> google.protobuf.BytesValue + 12, // 17: protobuf.Substate.TxMessage.blob_gas_fee_cap:type_name -> google.protobuf.BytesValue + 11, // 18: protobuf.Substate.Result.logs:type_name -> protobuf.Substate.Result.Log + 19, // [19:19] is the sub-list for method output_type + 19, // [19:19] is the sub-list for method input_type + 19, // [19:19] is the sub-list for extension type_name + 19, // [19:19] is the sub-list for extension extendee + 0, // [0:19] is the sub-list for field type_name +} + +func init() { file_substate_proto_init() } +func file_substate_proto_init() { + if File_substate_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_substate_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Substate); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_substate_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Substate_Account); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_substate_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Substate_AllocEntry); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_substate_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Substate_Alloc); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_substate_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Substate_BlockEnv); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_substate_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Substate_TxMessage); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_substate_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Substate_Result); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_substate_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Substate_Account_StorageEntry); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_substate_proto_msgTypes[8].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Substate_BlockEnv_BlockHashEntry); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_substate_proto_msgTypes[9].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Substate_TxMessage_AccessListEntry); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_substate_proto_msgTypes[10].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Substate_Result_Log); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + file_substate_proto_msgTypes[1].OneofWrappers = []interface{}{ + (*Substate_Account_Code)(nil), + (*Substate_Account_CodeHash)(nil), + } + file_substate_proto_msgTypes[5].OneofWrappers = []interface{}{ + (*Substate_TxMessage_Data)(nil), + (*Substate_TxMessage_InitCodeHash)(nil), + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_substate_proto_rawDesc, + NumEnums: 1, + NumMessages: 11, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_substate_proto_goTypes, + DependencyIndexes: file_substate_proto_depIdxs, + EnumInfos: file_substate_proto_enumTypes, + MessageInfos: file_substate_proto_msgTypes, + }.Build() + File_substate_proto = out.File + file_substate_proto_rawDesc = nil + file_substate_proto_goTypes = nil + file_substate_proto_depIdxs = nil +} diff --git a/protobuf/substate.proto b/protobuf/substate.proto index 54efaa5..7453a5e 100644 --- a/protobuf/substate.proto +++ b/protobuf/substate.proto @@ -1,11 +1,11 @@ -yntax = "proto2"; -package research; +syntax = "proto2"; +package protobuf; // For a single optional bytes, use `optional BytesValue` // For an optional list of bytes, use `repeated bytes` import "google/protobuf/wrappers.proto"; -option go_package = "../research"; +option go_package = "../protobuf"; message Substate { From 676b0a6f4f4f7d5a6ab1b688368e7de3237f4c0d Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 3 Sep 2024 09:05:38 +0200 Subject: [PATCH 004/157] more decode --- protobuf/decode.go | 58 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 044dafb..d9b2bd6 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -4,6 +4,7 @@ import ( "github.com/Fantom-foundation/Substate/types" "github.com/Fantom-foundation/Substate/substate" pb "github.com/Fantom-foundation/Substate/protobuf" + wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" ) // Decode converts protobuf-encoded Substate into aida-comprehensible substate @@ -46,25 +47,51 @@ func (s *pb.Substate) Decode() (*substate.Substate, error) { // decode converts protobuf-encoded Substate_Alloc into aida-comprehensible WorldState func (alloc *pb.Substate_Alloc) decode() (*substate.WorldState, error) { - world := make(substate.WorldState) - - for _, entry := range Substate_AllocEntry { - addr := types.BytesToAddress(entry.Address) - acct := entry.Account + world := make(substate.WorldState, len(alloc.GetAlloc())) + for _, entry := range alloc.GetAlloc() { + addr, acct, err := entry.decode() } return nil, fmt.Errorf("Not Implemented") } +func (entry *pb.Substate_AllocEntry) decode() ([]byte, *pb.Substate_Account, error) { + return entry.GetAddress(), entry.GetAccount(), nil +} + // decode converts protobuf-encoded Substate_BlockEnv into aida-comprehensible Env func (env *pb.Substate_BlockEnv) decode() (*substate.Env, error) { - return nil, fmt.Errorf("Not Implemented") + blockHashes := make(map[uint64]types.Hash, len(env.GetBlockHashes())) + for _, entry := range env.GetBlockHashes() { + key, value, err := entry.decode() + if err != nil { + return nil, err + } + blockHashes[key] := types.BytesToHash(value) + } + + return &substate.Env{ + Coinbase: types.BytesToAddress(env.GetCoinbase()), + Difficulty: new(big.Int).SetBytes(env.GetDifficulty()), + GasLimit: env.GetGasLimit(), + Number: env.GetNumber(), + Timestamp: env.GetTimestamp(), + BlockHashes: blockHashes, + BaseFee := new(big.Int).SetBytes(env.GetBaseFee().GetValue()), + //Random := env.GetRandom(), // does not exist + BlobBaseFee := new(big.Int).SetBytes(env.GetBlobBaseFee().GetValue()), + }, nil +} + +func (entry *pb.Substate_BlockEnv_BlockHashEntry) decode() (uint64, []byte, error) { + return entry.GetKey(), entry.GetValue(), nil } + // decode converts protobuf-encoded Substate_TxMessage into aida-comprehensible Message func (msg *pb.Substate_TxMessage) decode() (*substate.Message, error) { - /*return &Message{ + /*return &substate.Message{ Nonce: &msg.Nonce, CheckNonce: nil, GasPrice: nil, @@ -85,6 +112,21 @@ func (msg *pb.Substate_TxMessage) decode() (*substate.Message, error) { // decode converts protobuf-encoded Substate_Result into aida-comprehensible Result func (res *pb.Substate_Result) decode() (*substate.Result, error) { - return nil, fmt.Errorf("Not Implemented") + logs := make([]types.Log, 0, len(res.Logs)) + for i, log := res.Logs { + logs[i] := log.decode() + } + + return &substate.Result{ + Status: &res.Status, + Bloom: types.BytesToBloom(res.Bloom), + Logs: logs, + ContractAddress: nil, + GasUsed: &res.GasUsed, + }, nil } +// decode converts protobuf-encoded Substate_Result_Log into aida-comprehensible ResultLogs +func (log *pb.Substate_Result_Log) decode() (*types.Log, error) { + return nil, fmt.Errorf("Not Implemented") +} From b2f90177c7230089f9258361fe7f3003fc7413da Mon Sep 17 00:00:00 2001 From: RT Date: Tue, 3 Sep 2024 07:54:03 +0000 Subject: [PATCH 005/157] remove swp --- protobuf/.decode.go.swp | Bin 12288 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 protobuf/.decode.go.swp diff --git a/protobuf/.decode.go.swp b/protobuf/.decode.go.swp deleted file mode 100644 index 3adaa33d68cd70f7b81cb81280a126ece24938e2..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2&x;&I6vu0osBJbTn46GLJ3^RQXM2(jhz>;9WOw6|{UK(vl8d3Ir)IX9?yjc0 zda~=X3S#skdh;eC-t;cwNx`6k{sCSDC0-LyuWRys{bOc2?#7w9NT3=%4E0{s>we#^ zSGCPnRdVW}y{GSCQkv*OnDHv$Llk+zH+{(}1<6 z=+-XBjYwHa)Phhd*@#=P+OjFkw*1dHx`S3gD=?x0v1-k{aOBF_*)vI@K3kgN$Dh73 zVi4V3E1(t73TOqi0$KsBfL1^&pcQzS6;QzhdkYCXk|l99`<=M&H*4x2t$eN`?fXoc^vS}p6xZRvMKs3M-9fSVCM zoq%id`yzA4SCU&!)0(lR7ldL{_>t3a1y7@6vl=A}W4B7l^645gjPtH+ylh3ABC2y- zjb5{wIWJn#q7WCXpq@B;P4c6$?gWXC2DkHO=F7)rzO+fcIolRdwCqGGxvAA_lCxWdqHpg~lkDrVZ$x(`$+#FwZ@}C_lgvnEz{*c~QQL$q zbG;Hec76w%Cm3*HQ`ntV>1X!L+BeVxreP8rhSa9BnRaRg*;{R~lT9ext2m+7x3V7l z&#BBg?xgBPKX_hccNlSErR5>~ZZR87F}t(iclS>Qj&T#o10S3Rf{&O3Gf+zgW~Acv za~y>qZkw2{GWHeOo6V5yYC8~-d45}oXkE_1h!m_*N_{WFf0{dkScm+7EvzWi7;uBdPlYJYkA+d=kn6xAs1e6&rIbe z{H_y9pVn(~uf-~eRlb@oll)h zmed^zkvw=}#oLU`i$bNzl`CexLkt})Pqq-QSHR;$9Mq@~HLs>|f@aDV;w2g Date: Tue, 3 Sep 2024 09:19:36 +0000 Subject: [PATCH 006/157] alloc done --- protobuf/decode.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index d9b2bd6..53d6f64 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -14,7 +14,7 @@ func (s *pb.Substate) Decode() (*substate.Substate, error) { return nil, err } - output, err := s.GetInputAlloc().decode() + output, err := s.GetOutputAlloc().decode() if err != nil { return nil, err } @@ -51,15 +51,33 @@ func (alloc *pb.Substate_Alloc) decode() (*substate.WorldState, error) { for _, entry := range alloc.GetAlloc() { addr, acct, err := entry.decode() + if err != nil { + return nil, fmt.Errorf("Error decoding alloc entry; %w", err) + } + + address := types.BytesToAddress(addr) + nonce, balance, codehash, err := acct.decode() + if err != nil { + return nil, fmt.Errorf("Error decoding entry account; %w", err) + } + + world = world.Add(address, nonce, balance, codehash) } - return nil, fmt.Errorf("Not Implemented") + return world, nil } func (entry *pb.Substate_AllocEntry) decode() ([]byte, *pb.Substate_Account, error) { return entry.GetAddress(), entry.GetAccount(), nil } +func (acct *pb.Substate_Account) decode() (uint64, *big.Int, []byte, error) { + return acct.GetNonce(), + new(big.Int).SetBytes(acct.GetBalance()), + acct.GetCodeHash(), + nil +} + // decode converts protobuf-encoded Substate_BlockEnv into aida-comprehensible Env func (env *pb.Substate_BlockEnv) decode() (*substate.Env, error) { blockHashes := make(map[uint64]types.Hash, len(env.GetBlockHashes())) From 56b43b6ba72a9153c784992eaae1e5b48699c90c Mon Sep 17 00:00:00 2001 From: RT Date: Wed, 4 Sep 2024 06:53:00 +0000 Subject: [PATCH 007/157] finish result --- protobuf/decode.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 53d6f64..566b8c9 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -97,7 +97,7 @@ func (env *pb.Substate_BlockEnv) decode() (*substate.Env, error) { Timestamp: env.GetTimestamp(), BlockHashes: blockHashes, BaseFee := new(big.Int).SetBytes(env.GetBaseFee().GetValue()), - //Random := env.GetRandom(), // does not exist + //Random := env.GetRandom(), // does not exist in substate.Env BlobBaseFee := new(big.Int).SetBytes(env.GetBlobBaseFee().GetValue()), }, nil } @@ -130,7 +130,7 @@ func (msg *pb.Substate_TxMessage) decode() (*substate.Message, error) { // decode converts protobuf-encoded Substate_Result into aida-comprehensible Result func (res *pb.Substate_Result) decode() (*substate.Result, error) { - logs := make([]types.Log, 0, len(res.Logs)) + logs := make([]types.Log, len(res.Logs)) for i, log := res.Logs { logs[i] := log.decode() } @@ -139,12 +139,20 @@ func (res *pb.Substate_Result) decode() (*substate.Result, error) { Status: &res.Status, Bloom: types.BytesToBloom(res.Bloom), Logs: logs, - ContractAddress: nil, + ContractAddress: nil, // to be processed downstream GasUsed: &res.GasUsed, }, nil } -// decode converts protobuf-encoded Substate_Result_Log into aida-comprehensible ResultLogs func (log *pb.Substate_Result_Log) decode() (*types.Log, error) { - return nil, fmt.Errorf("Not Implemented") + topics := make([]types.Hash, len(log.GetTopics())) + for i, topic := range log.GetTopics() { + topics[i] := types.BytesToHash(topic) + } + + return &types.Log{ + Address: types.BytesToAddress(log.GetAddress()), + Topics: topics, + Data: log.GetData(), + }, nil } From 0dbde6106c32c3a3da29e00c9d30229e8045fb46 Mon Sep 17 00:00:00 2001 From: RT Date: Wed, 4 Sep 2024 07:26:47 +0000 Subject: [PATCH 008/157] finish message --- protobuf/decode.go | 84 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 71 insertions(+), 13 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 566b8c9..60fac4c 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -106,28 +106,86 @@ func (entry *pb.Substate_BlockEnv_BlockHashEntry) decode() (uint64, []byte, erro return entry.GetKey(), entry.GetValue(), nil } - // decode converts protobuf-encoded Substate_TxMessage into aida-comprehensible Message func (msg *pb.Substate_TxMessage) decode() (*substate.Message, error) { - /*return &substate.Message{ - Nonce: &msg.Nonce, - CheckNonce: nil, - GasPrice: nil, - Gas: &msg.Gas, - From: from, - To: to, - Value: value, - Data: data, - dataHash: dataHash, + // to=nil means contract creation + var toAddr *types.Address = nil + to := msg.GetTo() + if to != nil { + toAddr = &types.BytesToAddress(to.GetValue()) + } + + // Berlin hard fork, EIP-2930: Optional access lists + var accessList types.AccessList = nil // nil if EIP-2930 is not activated + if msg.GetAccessList() != nil { + accessList = make([]AccessTuple, len(msg.GetAccessList())) + for i, entry := range msg.GetAccessList() { + addr, keys, err := entry.decode() + if err != nil { + return nil, err + } + + address := types.BytesToAddress(addr) + storageKeys := make([]types.Hash, len(keys)) + for j, key := range keys { + storageKeys[j] := types.BytesToHash(key) + } + + accessList[i] := &AccessTuple{ + Address: address, + StorageKeys: storageKeys, + } + } + } + + // London hard fork, EIP-1559: Fee market + var gasFeeCap *big.Int = nil + gfc := msg.GetGasFeeCap() + if gfc != nil { + gasFeeCap = &new(big.Int).SetBytes(gfc.GetValue()) + } + + var gasTipCap *big.Int = nil + gtc := msg.GetGasTipCap() + if gtc := nil { + gasTipCap = &new(big.Int).SetBytes(gtc.GetValue()) + } + + // Cancun hard fork, EIP-4844 + var blobGasFeeCap *big.Int = nil + bgfc := msg.GetBlobGasFeeCap() + if bgfc := nil { + blobGasFeeCap = &new(big.Int).SetBytes(bgfc.GetValue()) + } + + blobHashes := make([]types.Hash, len(msg.GetBlobHashes())) + for i, hash := msg.GetBlobHashes() { + blobHashes[i] := types.BytesToHash(hash) + } + + return &substate.Message{ + Nonce: msg.GetNonce(), + CheckNonce: true, //always true + GasPrice: new(big.Int).SetBytes(msg.GetGasPrice()), + Gas: msg.GetGas(), + From: types.BytesToAddress(msg.GetFrom()), + To: toAddr, + Value: new(big.Int).SetBytes(msg.GetValue()), + Data: msg.GetData(), + dataHash: msg.GetInitCodeHash(), AccessList: accessList, GasFeeCap: gasFeeCap, GasTipCap: gasTipCap, BlobGasFeeCap: blobGasFeeCap, BlobHashes: blobHashes, - }, nil */ - return nil, fmt.Errorf("Not Implemented") + }, nil +} + +func (entry *Substate_TxMessage_AccessListEntry) decode() ([]byte, [][]byte, error) { + return entry.GetAddress(), entry.GetStorageKeys(), nil } + // decode converts protobuf-encoded Substate_Result into aida-comprehensible Result func (res *pb.Substate_Result) decode() (*substate.Result, error) { logs := make([]types.Log, len(res.Logs)) From 1f627cb2832b29f20c5bac7b88bc9845f4ee11b2 Mon Sep 17 00:00:00 2001 From: RT Date: Wed, 4 Sep 2024 07:48:37 +0000 Subject: [PATCH 009/157] substate iterator now uses pb decode --- db/substate_iterator.go | 5 +++-- protobuf/decode.go | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/db/substate_iterator.go b/db/substate_iterator.go index 36ad813..f96c9f4 100644 --- a/db/substate_iterator.go +++ b/db/substate_iterator.go @@ -36,12 +36,13 @@ func (i *substateIterator) decode(data rawEntry) (*substate.Substate, error) { } //rlpSubstate, err := rlp.Decode(value) + //return rlpSubstate.ToSubstate(i.db.GetCode, block, tx) + pbSubstate := &pb.Substate{} if err := proto.Unmarshal(value, pbSubstate); err != nil { return nil, err } - - return rlpSubstate.ToSubstate(i.db.GetCode, block, tx) + return pbSubstate.Decode(block, tx) } func (i *substateIterator) start(numWorkers int) { diff --git a/protobuf/decode.go b/protobuf/decode.go index 60fac4c..e4c6447 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -8,7 +8,7 @@ import ( ) // Decode converts protobuf-encoded Substate into aida-comprehensible substate -func (s *pb.Substate) Decode() (*substate.Substate, error) { +func (s *pb.Substate) Decode(block uint64, tx int) (*substate.Substate, error) { input, err := s.GetInputAlloc().decode() if err != nil { return nil, err From 700223c841fd323d56118c3b74c24b8e107a33c2 Mon Sep 17 00:00:00 2001 From: RT Date: Wed, 4 Sep 2024 13:45:31 +0000 Subject: [PATCH 010/157] remove cyclical import in decode --- db/substate_iterator.go | 4 +-- protobuf/decode.go | 76 +++++++++++++++++++++++------------------ 2 files changed, 44 insertions(+), 36 deletions(-) diff --git a/db/substate_iterator.go b/db/substate_iterator.go index f96c9f4..9482b06 100644 --- a/db/substate_iterator.go +++ b/db/substate_iterator.go @@ -3,11 +3,11 @@ package db import ( "fmt" - "github.com/syndtr/goleveldb/leveldb/util" "github.com/golang/protobuf/proto" + "github.com/syndtr/goleveldb/leveldb/util" - "github.com/Fantom-foundation/Substate/rlp" pb "github.com/Fantom-foundation/Substate/protobuf" + "github.com/Fantom-foundation/Substate/rlp" "github.com/Fantom-foundation/Substate/substate" ) diff --git a/protobuf/decode.go b/protobuf/decode.go index e4c6447..d98b4ec 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -1,14 +1,13 @@ package protobuf import ( - "github.com/Fantom-foundation/Substate/types" "github.com/Fantom-foundation/Substate/substate" - pb "github.com/Fantom-foundation/Substate/protobuf" + "github.com/Fantom-foundation/Substate/types" wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" ) // Decode converts protobuf-encoded Substate into aida-comprehensible substate -func (s *pb.Substate) Decode(block uint64, tx int) (*substate.Substate, error) { +func (s *Substate) Decode(block uint64, tx int) (*substate.Substate, error) { input, err := s.GetInputAlloc().decode() if err != nil { return nil, err @@ -46,7 +45,7 @@ func (s *pb.Substate) Decode(block uint64, tx int) (*substate.Substate, error) { } // decode converts protobuf-encoded Substate_Alloc into aida-comprehensible WorldState -func (alloc *pb.Substate_Alloc) decode() (*substate.WorldState, error) { +func (alloc *Substate_Alloc) decode() (*substate.WorldState, error) { world := make(substate.WorldState, len(alloc.GetAlloc())) for _, entry := range alloc.GetAlloc() { @@ -67,19 +66,19 @@ func (alloc *pb.Substate_Alloc) decode() (*substate.WorldState, error) { return world, nil } -func (entry *pb.Substate_AllocEntry) decode() ([]byte, *pb.Substate_Account, error) { +func (entry *Substate_AllocEntry) decode() ([]byte, *pb.Substate_Account, error) { return entry.GetAddress(), entry.GetAccount(), nil } -func (acct *pb.Substate_Account) decode() (uint64, *big.Int, []byte, error) { - return acct.GetNonce(), +func (acct *Substate_Account) decode() (uint64, *big.Int, []byte, error) { + return acct.GetNonce(), new(big.Int).SetBytes(acct.GetBalance()), acct.GetCodeHash(), nil } // decode converts protobuf-encoded Substate_BlockEnv into aida-comprehensible Env -func (env *pb.Substate_BlockEnv) decode() (*substate.Env, error) { +func (env *Substate_BlockEnv) decode() (*substate.Env, error) { blockHashes := make(map[uint64]types.Hash, len(env.GetBlockHashes())) for _, entry := range env.GetBlockHashes() { key, value, err := entry.decode() @@ -89,27 +88,37 @@ func (env *pb.Substate_BlockEnv) decode() (*substate.Env, error) { blockHashes[key] := types.BytesToHash(value) } + var baseFee *big.Int = nil + if env.GetBaseFee() != nil { + baseFee = &new(big.Int).SetBytes(env.GetBaseFee().GetValue()) + } + + var blobBaseFee *big.Int = nil + if env.GetBlobBaseFee() != nil { + blobBaseFee = &new(big.Int).SetBytes(env.GetBlobBaseFee().GetValue()) + } + return &substate.Env{ - Coinbase: types.BytesToAddress(env.GetCoinbase()), - Difficulty: new(big.Int).SetBytes(env.GetDifficulty()), - GasLimit: env.GetGasLimit(), - Number: env.GetNumber(), - Timestamp: env.GetTimestamp(), + Coinbase: types.BytesToAddress(env.GetCoinbase()), + Difficulty: new(big.Int).SetBytes(env.GetDifficulty()), + GasLimit: env.GetGasLimit(), + Number: env.GetNumber(), + Timestamp: env.GetTimestamp(), BlockHashes: blockHashes, - BaseFee := new(big.Int).SetBytes(env.GetBaseFee().GetValue()), - //Random := env.GetRandom(), // does not exist in substate.Env - BlobBaseFee := new(big.Int).SetBytes(env.GetBlobBaseFee().GetValue()), + BaseFee: baseFee, + //Random: env.GetRandom(), // does not exist in substate.Env + BlobBaseFee: blobBaseFee, }, nil } -func (entry *pb.Substate_BlockEnv_BlockHashEntry) decode() (uint64, []byte, error) { +func (entry *Substate_BlockEnv_BlockHashEntry) decode() (uint64, []byte, error) { return entry.GetKey(), entry.GetValue(), nil } // decode converts protobuf-encoded Substate_TxMessage into aida-comprehensible Message -func (msg *pb.Substate_TxMessage) decode() (*substate.Message, error) { +func (msg *Substate_TxMessage) decode() (*substate.Message, error) { // to=nil means contract creation - var toAddr *types.Address = nil + var toAddr *types.Address = nil to := msg.GetTo() if to != nil { toAddr = &types.BytesToAddress(to.GetValue()) @@ -132,7 +141,7 @@ func (msg *pb.Substate_TxMessage) decode() (*substate.Message, error) { } accessList[i] := &AccessTuple{ - Address: address, + Address: address, StorageKeys: storageKeys, } } @@ -147,19 +156,19 @@ func (msg *pb.Substate_TxMessage) decode() (*substate.Message, error) { var gasTipCap *big.Int = nil gtc := msg.GetGasTipCap() - if gtc := nil { + if gtc != nil { gasTipCap = &new(big.Int).SetBytes(gtc.GetValue()) } // Cancun hard fork, EIP-4844 var blobGasFeeCap *big.Int = nil bgfc := msg.GetBlobGasFeeCap() - if bgfc := nil { + if bgfc != nil { blobGasFeeCap = &new(big.Int).SetBytes(bgfc.GetValue()) } blobHashes := make([]types.Hash, len(msg.GetBlobHashes())) - for i, hash := msg.GetBlobHashes() { + for i, hash := range msg.GetBlobHashes() { blobHashes[i] := types.BytesToHash(hash) } @@ -178,31 +187,30 @@ func (msg *pb.Substate_TxMessage) decode() (*substate.Message, error) { GasTipCap: gasTipCap, BlobGasFeeCap: blobGasFeeCap, BlobHashes: blobHashes, - }, nil + }, nil } func (entry *Substate_TxMessage_AccessListEntry) decode() ([]byte, [][]byte, error) { return entry.GetAddress(), entry.GetStorageKeys(), nil } - // decode converts protobuf-encoded Substate_Result into aida-comprehensible Result -func (res *pb.Substate_Result) decode() (*substate.Result, error) { - logs := make([]types.Log, len(res.Logs)) - for i, log := res.Logs { +func (res *Substate_Result) decode() (*substate.Result, error) { + logs := make([]types.Log, len(res.GetLogs)) + for i, log := range res.GetLogs() { logs[i] := log.decode() } return &substate.Result{ - Status: &res.Status, - Bloom: types.BytesToBloom(res.Bloom), - Logs: logs, + Status: &res.Status, + Bloom: types.BytesToBloom(res.Bloom), + Logs: logs, ContractAddress: nil, // to be processed downstream - GasUsed: &res.GasUsed, + GasUsed: &res.GasUsed, }, nil } -func (log *pb.Substate_Result_Log) decode() (*types.Log, error) { +func (log *Substate_Result_Log) decode() (*types.Log, error) { topics := make([]types.Hash, len(log.GetTopics())) for i, topic := range log.GetTopics() { topics[i] := types.BytesToHash(topic) @@ -211,6 +219,6 @@ func (log *pb.Substate_Result_Log) decode() (*types.Log, error) { return &types.Log{ Address: types.BytesToAddress(log.GetAddress()), Topics: topics, - Data: log.GetData(), + Data: log.GetData(), }, nil } From a0b6e8352e11a2d3a9614e1f90812e59dddab7f3 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 15:53:03 +0200 Subject: [PATCH 011/157] decode bugfix --- protobuf/decode.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index d98b4ec..50ef2e4 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -1,6 +1,9 @@ package protobuf import ( + "fmt" + "math/big" + "github.com/Fantom-foundation/Substate/substate" "github.com/Fantom-foundation/Substate/types" wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" @@ -34,8 +37,8 @@ func (s *Substate) Decode(block uint64, tx int) (*substate.Substate, error) { } return &substate.Substate{ - InputSubstate: input, - OutputSubstate: output, + InputSubstate: &input, + OutputSubstate: &output, Env: environment, Message: message, Result: result, @@ -63,10 +66,10 @@ func (alloc *Substate_Alloc) decode() (*substate.WorldState, error) { world = world.Add(address, nonce, balance, codehash) } - return world, nil + return &world, nil } -func (entry *Substate_AllocEntry) decode() ([]byte, *pb.Substate_Account, error) { +func (entry *Substate_AllocEntry) decode() ([]byte, *Substate_Account, error) { return entry.GetAddress(), entry.GetAccount(), nil } @@ -85,7 +88,7 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { if err != nil { return nil, err } - blockHashes[key] := types.BytesToHash(value) + blockHashes[key] = types.BytesToHash(value) } var baseFee *big.Int = nil From 9aad26536cd591d3f206e3dfdecfeb10c3a111d7 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 16:00:51 +0200 Subject: [PATCH 012/157] fix big int assignment --- protobuf/decode.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 50ef2e4..2f2ef08 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -37,8 +37,8 @@ func (s *Substate) Decode(block uint64, tx int) (*substate.Substate, error) { } return &substate.Substate{ - InputSubstate: &input, - OutputSubstate: &output, + InputSubstate: *input, + OutputSubstate: *output, Env: environment, Message: message, Result: result, @@ -130,7 +130,7 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { // Berlin hard fork, EIP-2930: Optional access lists var accessList types.AccessList = nil // nil if EIP-2930 is not activated if msg.GetAccessList() != nil { - accessList = make([]AccessTuple, len(msg.GetAccessList())) + accessList = make([]types.AccessTuple, len(msg.GetAccessList())) for i, entry := range msg.GetAccessList() { addr, keys, err := entry.decode() if err != nil { @@ -140,10 +140,10 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { address := types.BytesToAddress(addr) storageKeys := make([]types.Hash, len(keys)) for j, key := range keys { - storageKeys[j] := types.BytesToHash(key) + storageKeys[j] = types.BytesToHash(key) } - accessList[i] := &AccessTuple{ + accessList[i] = &types.AccessTuple{ Address: address, StorageKeys: storageKeys, } From 5339c05c15e0d3aa3122847dad82bb8868c8cb48 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 16:08:46 +0200 Subject: [PATCH 013/157] using newMessage instead of constructor --- protobuf/decode.go | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 2f2ef08..1a4d11f 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -154,43 +154,44 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { var gasFeeCap *big.Int = nil gfc := msg.GetGasFeeCap() if gfc != nil { - gasFeeCap = &new(big.Int).SetBytes(gfc.GetValue()) + gasFeeCap = *new(big.Int).SetBytes(gfc.GetValue()) } var gasTipCap *big.Int = nil gtc := msg.GetGasTipCap() if gtc != nil { - gasTipCap = &new(big.Int).SetBytes(gtc.GetValue()) + gasTipCap = *new(big.Int).SetBytes(gtc.GetValue()) } // Cancun hard fork, EIP-4844 var blobGasFeeCap *big.Int = nil bgfc := msg.GetBlobGasFeeCap() if bgfc != nil { - blobGasFeeCap = &new(big.Int).SetBytes(bgfc.GetValue()) + blobGasFeeCap = *new(big.Int).SetBytes(bgfc.GetValue()) } blobHashes := make([]types.Hash, len(msg.GetBlobHashes())) for i, hash := range msg.GetBlobHashes() { - blobHashes[i] := types.BytesToHash(hash) - } - - return &substate.Message{ - Nonce: msg.GetNonce(), - CheckNonce: true, //always true - GasPrice: new(big.Int).SetBytes(msg.GetGasPrice()), - Gas: msg.GetGas(), - From: types.BytesToAddress(msg.GetFrom()), - To: toAddr, - Value: new(big.Int).SetBytes(msg.GetValue()), - Data: msg.GetData(), - dataHash: msg.GetInitCodeHash(), - AccessList: accessList, - GasFeeCap: gasFeeCap, - GasTipCap: gasTipCap, - BlobGasFeeCap: blobGasFeeCap, - BlobHashes: blobHashes, - }, nil + blobHashes[i] = types.BytesToHash(hash) + } + + // dataHash is not exposed, so we must create Message using constructor + return substate.NewMessage( + msg.GetNonce(), // nonce + true, // CheckNonce + new(big.Int).SetBytes(msg.GetGasPrice()), //GasPrice + msg.GetGas(), // Gas + types.BytesToAddress(msg.GetFrom()), // From + toAddr, // To + new(big.Int).SetBytes(msg.GetValue()), // Value + msg.GetData(), // Data + msg.GetInitCodeHash(), // dataHash + accessList, // AccessList + gasFeeCap, // GasFeeCap + gasTipCap, // GasTipCap + blobGasFeeCap, // BlobGasFeeCap + blobHashes, // BlobHashes + ), nil } func (entry *Substate_TxMessage_AccessListEntry) decode() ([]byte, [][]byte, error) { From 6a551900a81e78cd4c60be517ae97e669810153a Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 16:13:09 +0200 Subject: [PATCH 014/157] *big.Int now directly assigned --- protobuf/decode.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 1a4d11f..7dab45f 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -91,19 +91,24 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { blockHashes[key] = types.BytesToHash(value) } + var diff *big.Int = nil + if env.GetDifficulty() != nil { + diff.SetBytes(env.GetDifficulty()) + } + var baseFee *big.Int = nil if env.GetBaseFee() != nil { - baseFee = &new(big.Int).SetBytes(env.GetBaseFee().GetValue()) + baseFee.SetBytes(env.GetBaseFee().GetValue()) } var blobBaseFee *big.Int = nil if env.GetBlobBaseFee() != nil { - blobBaseFee = &new(big.Int).SetBytes(env.GetBlobBaseFee().GetValue()) + blobBaseFee.SetBytes(env.GetBlobBaseFee().GetValue()) } return &substate.Env{ Coinbase: types.BytesToAddress(env.GetCoinbase()), - Difficulty: new(big.Int).SetBytes(env.GetDifficulty()), + Difficulty: diff, GasLimit: env.GetGasLimit(), Number: env.GetNumber(), Timestamp: env.GetTimestamp(), From 5992162d74c55fdf9dd64090011232f92c6d3b47 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 16:20:58 +0200 Subject: [PATCH 015/157] decode bugfix --- protobuf/decode.go | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 7dab45f..4e4d6be 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -126,10 +126,10 @@ func (entry *Substate_BlockEnv_BlockHashEntry) decode() (uint64, []byte, error) // decode converts protobuf-encoded Substate_TxMessage into aida-comprehensible Message func (msg *Substate_TxMessage) decode() (*substate.Message, error) { // to=nil means contract creation - var toAddr *types.Address = nil + var pTo *types.Address = nil to := msg.GetTo() if to != nil { - toAddr = &types.BytesToAddress(to.GetValue()) + pTo = types.BytesToAddress(to.GetValue()) } // Berlin hard fork, EIP-2930: Optional access lists @@ -148,7 +148,7 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { storageKeys[j] = types.BytesToHash(key) } - accessList[i] = &types.AccessTuple{ + accessList[i] = types.AccessTuple{ Address: address, StorageKeys: storageKeys, } @@ -159,20 +159,20 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { var gasFeeCap *big.Int = nil gfc := msg.GetGasFeeCap() if gfc != nil { - gasFeeCap = *new(big.Int).SetBytes(gfc.GetValue()) + gasFeeCap.SetBytes(gfc.GetValue()) } var gasTipCap *big.Int = nil gtc := msg.GetGasTipCap() if gtc != nil { - gasTipCap = *new(big.Int).SetBytes(gtc.GetValue()) + gasTipCap.SetBytes(gtc.GetValue()) } // Cancun hard fork, EIP-4844 var blobGasFeeCap *big.Int = nil bgfc := msg.GetBlobGasFeeCap() if bgfc != nil { - blobGasFeeCap = *new(big.Int).SetBytes(bgfc.GetValue()) + blobGasFeeCap.SetBytes(bgfc.GetValue()) } blobHashes := make([]types.Hash, len(msg.GetBlobHashes())) @@ -184,13 +184,13 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { return substate.NewMessage( msg.GetNonce(), // nonce true, // CheckNonce - new(big.Int).SetBytes(msg.GetGasPrice()), //GasPrice + new(big.Int).SetBytes(msg.GetGasPrice()), // GasPrice msg.GetGas(), // Gas types.BytesToAddress(msg.GetFrom()), // From toAddr, // To new(big.Int).SetBytes(msg.GetValue()), // Value msg.GetData(), // Data - msg.GetInitCodeHash(), // dataHash + types.BytesToHash(msg.GetInitCodeHash()), // dataHash accessList, // AccessList gasFeeCap, // GasFeeCap gasTipCap, // GasTipCap @@ -205,17 +205,20 @@ func (entry *Substate_TxMessage_AccessListEntry) decode() ([]byte, [][]byte, err // decode converts protobuf-encoded Substate_Result into aida-comprehensible Result func (res *Substate_Result) decode() (*substate.Result, error) { - logs := make([]types.Log, len(res.GetLogs)) + logs := make([]types.Log, len(res.GetLogs())) for i, log := range res.GetLogs() { - logs[i] := log.decode() + logs[i], err := log.decode() + if err != nil { + return nil, fmt.Errorf("Error decoding result; %w", err) + } } return &substate.Result{ - Status: &res.Status, + Status: res.GetStatus(), Bloom: types.BytesToBloom(res.Bloom), Logs: logs, ContractAddress: nil, // to be processed downstream - GasUsed: &res.GasUsed, + GasUsed: res.GetGasUsed(), }, nil } From 0c53db7a13eb518b178af272a2496928ca4a50bd Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 16:29:09 +0200 Subject: [PATCH 016/157] pTo replaces toAddr --- protobuf/decode.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 4e4d6be..4478814 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -6,7 +6,6 @@ import ( "github.com/Fantom-foundation/Substate/substate" "github.com/Fantom-foundation/Substate/types" - wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" ) // Decode converts protobuf-encoded Substate into aida-comprehensible substate @@ -129,7 +128,7 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { var pTo *types.Address = nil to := msg.GetTo() if to != nil { - pTo = types.BytesToAddress(to.GetValue()) + pTo = *types.BytesToAddress(to.GetValue()) } // Berlin hard fork, EIP-2930: Optional access lists @@ -155,6 +154,12 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { } } + var dataHash *types.Hash = nil + dh := msg.GetInitCodeHash() + if dh != nil { + dataHash = *types.BytesToHash(dh) + } + // London hard fork, EIP-1559: Fee market var gasFeeCap *big.Int = nil gfc := msg.GetGasFeeCap() @@ -187,10 +192,10 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { new(big.Int).SetBytes(msg.GetGasPrice()), // GasPrice msg.GetGas(), // Gas types.BytesToAddress(msg.GetFrom()), // From - toAddr, // To + pTo, // To new(big.Int).SetBytes(msg.GetValue()), // Value msg.GetData(), // Data - types.BytesToHash(msg.GetInitCodeHash()), // dataHash + dataHash, // dataHash accessList, // AccessList gasFeeCap, // GasFeeCap gasTipCap, // GasTipCap @@ -207,7 +212,7 @@ func (entry *Substate_TxMessage_AccessListEntry) decode() ([]byte, [][]byte, err func (res *Substate_Result) decode() (*substate.Result, error) { logs := make([]types.Log, len(res.GetLogs())) for i, log := range res.GetLogs() { - logs[i], err := log.decode() + logs[i], err = log.decode() if err != nil { return nil, fmt.Errorf("Error decoding result; %w", err) } @@ -216,7 +221,7 @@ func (res *Substate_Result) decode() (*substate.Result, error) { return &substate.Result{ Status: res.GetStatus(), Bloom: types.BytesToBloom(res.Bloom), - Logs: logs, + Logs: *logs, ContractAddress: nil, // to be processed downstream GasUsed: res.GetGasUsed(), }, nil @@ -225,7 +230,7 @@ func (res *Substate_Result) decode() (*substate.Result, error) { func (log *Substate_Result_Log) decode() (*types.Log, error) { topics := make([]types.Hash, len(log.GetTopics())) for i, topic := range log.GetTopics() { - topics[i] := types.BytesToHash(topic) + topics[i] = types.BytesToHash(topic) } return &types.Log{ From 6aaa19406ece2e1a42b5f2308146366aea203b29 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 16:36:23 +0200 Subject: [PATCH 017/157] predefine err --- protobuf/decode.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 4478814..73ae2c0 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -128,7 +128,7 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { var pTo *types.Address = nil to := msg.GetTo() if to != nil { - pTo = *types.BytesToAddress(to.GetValue()) + pTo = types.BytesToAddress(to.GetValue()) } // Berlin hard fork, EIP-2930: Optional access lists @@ -157,7 +157,7 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { var dataHash *types.Hash = nil dh := msg.GetInitCodeHash() if dh != nil { - dataHash = *types.BytesToHash(dh) + dataHash = types.BytesToHash(dh) } // London hard fork, EIP-1559: Fee market @@ -210,6 +210,7 @@ func (entry *Substate_TxMessage_AccessListEntry) decode() ([]byte, [][]byte, err // decode converts protobuf-encoded Substate_Result into aida-comprehensible Result func (res *Substate_Result) decode() (*substate.Result, error) { + var err error = nil logs := make([]types.Log, len(res.GetLogs())) for i, log := range res.GetLogs() { logs[i], err = log.decode() @@ -221,7 +222,7 @@ func (res *Substate_Result) decode() (*substate.Result, error) { return &substate.Result{ Status: res.GetStatus(), Bloom: types.BytesToBloom(res.Bloom), - Logs: *logs, + Logs: logs, ContractAddress: nil, // to be processed downstream GasUsed: res.GetGasUsed(), }, nil From da95437024ae4308c08ed2c239aaddced37d72b0 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 16:40:05 +0200 Subject: [PATCH 018/157] try using empty bytes as address --- protobuf/decode.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 73ae2c0..fa0980e 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -211,7 +211,7 @@ func (entry *Substate_TxMessage_AccessListEntry) decode() ([]byte, [][]byte, err // decode converts protobuf-encoded Substate_Result into aida-comprehensible Result func (res *Substate_Result) decode() (*substate.Result, error) { var err error = nil - logs := make([]types.Log, len(res.GetLogs())) + logs := make([]*types.Log, len(res.GetLogs())) for i, log := range res.GetLogs() { logs[i], err = log.decode() if err != nil { @@ -223,7 +223,7 @@ func (res *Substate_Result) decode() (*substate.Result, error) { Status: res.GetStatus(), Bloom: types.BytesToBloom(res.Bloom), Logs: logs, - ContractAddress: nil, // to be processed downstream + ContractAddress: []bytes{}, // to be processed downstream GasUsed: res.GetGasUsed(), }, nil } From 3c4190a4be1c9deb07eea4622efa6d517fcb4f24 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 16:44:09 +0200 Subject: [PATCH 019/157] bytes->byte --- protobuf/decode.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index fa0980e..9ce0135 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -128,7 +128,7 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { var pTo *types.Address = nil to := msg.GetTo() if to != nil { - pTo = types.BytesToAddress(to.GetValue()) + pTo = *types.BytesToAddress(to.GetValue()) } // Berlin hard fork, EIP-2930: Optional access lists @@ -157,7 +157,7 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { var dataHash *types.Hash = nil dh := msg.GetInitCodeHash() if dh != nil { - dataHash = types.BytesToHash(dh) + dataHash = &types.BytesToHash(dh) } // London hard fork, EIP-1559: Fee market @@ -223,7 +223,7 @@ func (res *Substate_Result) decode() (*substate.Result, error) { Status: res.GetStatus(), Bloom: types.BytesToBloom(res.Bloom), Logs: logs, - ContractAddress: []bytes{}, // to be processed downstream + ContractAddress: []byte{}, // to be processed downstream GasUsed: res.GetGasUsed(), }, nil } From 5adb6b05f447f046a80f34769152f8d7e847556c Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 16:55:58 +0200 Subject: [PATCH 020/157] try using NewResult instead --- protobuf/decode.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 9ce0135..4604ba0 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -154,10 +154,10 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { } } - var dataHash *types.Hash = nil + var dataHash types.Hash = nil dh := msg.GetInitCodeHash() if dh != nil { - dataHash = &types.BytesToHash(dh) + dataHash = types.BytesToHash(dh) } // London hard fork, EIP-1559: Fee market @@ -195,7 +195,7 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { pTo, // To new(big.Int).SetBytes(msg.GetValue()), // Value msg.GetData(), // Data - dataHash, // dataHash + *dataHash, // dataHash accessList, // AccessList gasFeeCap, // GasFeeCap gasTipCap, // GasTipCap @@ -219,12 +219,12 @@ func (res *Substate_Result) decode() (*substate.Result, error) { } } - return &substate.Result{ - Status: res.GetStatus(), - Bloom: types.BytesToBloom(res.Bloom), - Logs: logs, - ContractAddress: []byte{}, // to be processed downstream - GasUsed: res.GetGasUsed(), + return substate.NewResult{ + res.GetStatus(), // Status + types.BytesToBloom(res.Bloom), // Bloom + logs, // Logs + nil, // ContractAddress, to be processed downstream + res.GetGasUsed(), // GasUsed }, nil } From a61ff96edd19c68108e1f25776624ffc08ee59ac Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 16:59:49 +0200 Subject: [PATCH 021/157] {} -> () --- protobuf/decode.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 4604ba0..a9e91c9 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -154,7 +154,7 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { } } - var dataHash types.Hash = nil + var dataHash types.Hash dh := msg.GetInitCodeHash() if dh != nil { dataHash = types.BytesToHash(dh) @@ -195,7 +195,7 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { pTo, // To new(big.Int).SetBytes(msg.GetValue()), // Value msg.GetData(), // Data - *dataHash, // dataHash + &dataHash, // dataHash accessList, // AccessList gasFeeCap, // GasFeeCap gasTipCap, // GasTipCap @@ -219,13 +219,13 @@ func (res *Substate_Result) decode() (*substate.Result, error) { } } - return substate.NewResult{ + return substate.NewResult( res.GetStatus(), // Status types.BytesToBloom(res.Bloom), // Bloom logs, // Logs nil, // ContractAddress, to be processed downstream res.GetGasUsed(), // GasUsed - }, nil + ), nil } func (log *Substate_Result_Log) decode() (*types.Log, error) { From dc183a8f8a3779dba34fdaeb0c16a2d2b7383a33 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 17:05:18 +0200 Subject: [PATCH 022/157] try using all [20]bytes{} --- protobuf/decode.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index a9e91c9..6e9336c 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -220,11 +220,11 @@ func (res *Substate_Result) decode() (*substate.Result, error) { } return substate.NewResult( - res.GetStatus(), // Status - types.BytesToBloom(res.Bloom), // Bloom - logs, // Logs - nil, // ContractAddress, to be processed downstream - res.GetGasUsed(), // GasUsed + res.GetStatus(), // Status + types.BytesToBloom(res.Bloom), // Bloom + logs, // Logs + types.BytesToAddress([20]byte{}), // ContractAddress, to be processed downstream + res.GetGasUsed(), // GasUsed ), nil } From c9366d6b756c23351b60f1c04c1022ad9a1b4b0f Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 17:06:53 +0200 Subject: [PATCH 023/157] using var instead --- protobuf/decode.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 6e9336c..ad4316a 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -219,12 +219,14 @@ func (res *Substate_Result) decode() (*substate.Result, error) { } } + var nilAddr types.Address + return substate.NewResult( - res.GetStatus(), // Status - types.BytesToBloom(res.Bloom), // Bloom - logs, // Logs - types.BytesToAddress([20]byte{}), // ContractAddress, to be processed downstream - res.GetGasUsed(), // GasUsed + res.GetStatus(), // Status + types.BytesToBloom(res.Bloom), // Bloom + logs, // Logs + nilAddr, // ContractAddress, to be processed downstream + res.GetGasUsed(), // GasUsed ), nil } From 7ba04ea0de4dff3bbf41b39d6588dcd592e03830 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 17:17:16 +0200 Subject: [PATCH 024/157] add addr as intermediate value --- protobuf/decode.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index ad4316a..2b020b9 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -128,7 +128,8 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { var pTo *types.Address = nil to := msg.GetTo() if to != nil { - pTo = *types.BytesToAddress(to.GetValue()) + address := types.BytesToAddress(to.GetValue()) + pTo = &address } // Berlin hard fork, EIP-2930: Optional access lists From 6ed87a7da05a0b21780a1096d9881ee320c39165 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 17:27:50 +0200 Subject: [PATCH 025/157] for now remove rlp --- db/substate_iterator.go | 1 - 1 file changed, 1 deletion(-) diff --git a/db/substate_iterator.go b/db/substate_iterator.go index 9482b06..77ba06e 100644 --- a/db/substate_iterator.go +++ b/db/substate_iterator.go @@ -7,7 +7,6 @@ import ( "github.com/syndtr/goleveldb/leveldb/util" pb "github.com/Fantom-foundation/Substate/protobuf" - "github.com/Fantom-foundation/Substate/rlp" "github.com/Fantom-foundation/Substate/substate" ) From 1fec2a49307be5874ee11b26bee4f5811300d24f Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 17:48:41 +0200 Subject: [PATCH 026/157] GetSubstate now uses protobuf --- db/substate_db.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/db/substate_db.go b/db/substate_db.go index 3f91598..d9217d0 100644 --- a/db/substate_db.go +++ b/db/substate_db.go @@ -108,12 +108,19 @@ func (db *substateDB) GetSubstate(block uint64, tx int) (*substate.Substate, err return nil, fmt.Errorf("cannot get substate block: %v, tx: %v from db; %w", block, tx, err) } - rlpSubstate, err := rlp.Decode(val) - if err != nil { - return nil, fmt.Errorf("cannot decode data into rlp block: %v, tx %v; %w", block, tx, err) + //rlpSubstate, err := rlp.Decode(val) + //if err != nil { + // return nil, fmt.Errorf("cannot decode data into rlp block: %v, tx %v; %w", block, tx, err) + //} + + //return rlpSubstate.ToSubstate(db.GetCode, block, tx) + + pbSubstate := &pb.Substate{} + if err := proto.Unmarshal(value, pbSubstate); err != nil { + return nil, err } - return rlpSubstate.ToSubstate(db.GetCode, block, tx) + return pbSubstate.Decode(block, tx) } // GetBlockSubstates returns substates for given block if exists within DB. From e525473e243301a5815052dfa012416584e30edd Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 17:52:26 +0200 Subject: [PATCH 027/157] substate_db bugfix --- db/substate_db.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/db/substate_db.go b/db/substate_db.go index d9217d0..ff299ba 100644 --- a/db/substate_db.go +++ b/db/substate_db.go @@ -4,9 +4,11 @@ import ( "encoding/binary" "fmt" + pb "github.com/Fantom-foundation/Substate/protobuf" "github.com/Fantom-foundation/Substate/rlp" "github.com/Fantom-foundation/Substate/substate" trlp "github.com/Fantom-foundation/Substate/types/rlp" + "github.com/golang/protobuf/proto" "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/opt" "github.com/syndtr/goleveldb/leveldb/util" @@ -116,7 +118,7 @@ func (db *substateDB) GetSubstate(block uint64, tx int) (*substate.Substate, err //return rlpSubstate.ToSubstate(db.GetCode, block, tx) pbSubstate := &pb.Substate{} - if err := proto.Unmarshal(value, pbSubstate); err != nil { + if err := proto.Unmarshal(val, pbSubstate); err != nil { return nil, err } From 25aa7aa0ab285796de635e6b3a47c6ee34860ed9 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 18:11:46 +0200 Subject: [PATCH 028/157] added contract address logic --- protobuf/decode.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 2b020b9..c068061 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -4,6 +4,7 @@ import ( "fmt" "math/big" + "github.com/ethereum/go-ethereum/crypto" "github.com/Fantom-foundation/Substate/substate" "github.com/Fantom-foundation/Substate/types" ) @@ -30,7 +31,8 @@ func (s *Substate) Decode(block uint64, tx int) (*substate.Substate, error) { return nil, err } - result, err := s.GetResult().decode() + contractAddress := s.GetTxMessage().GetContractAddress() + result, err := s.GetResult().decode(contractAddress) if err != nil { return nil, err } @@ -92,6 +94,7 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { var diff *big.Int = nil if env.GetDifficulty() != nil { + fmt.Println(env.GetDifficulty()) diff.SetBytes(env.GetDifficulty()) } @@ -209,8 +212,23 @@ func (entry *Substate_TxMessage_AccessListEntry) decode() ([]byte, [][]byte, err return entry.GetAddress(), entry.GetStorageKeys(), nil } +// getContractAddress returns the address of the newly created contract if any. +// returns nil otherwise. +func (msg *Substate_TxMessage) getContractAddress() *Type.address { + var contractAddress types.Address + + // *to==nil means contract creation and thus address of newly created contract + to := msg.GetTo() + if to == nil { + fromAddr := types.BytesToAddress(msg.GetFrom()) + contractAddress = crypto.CreateAddress(fromAddr, msg.GetNonce()) + } + + return contractAddress +} + // decode converts protobuf-encoded Substate_Result into aida-comprehensible Result -func (res *Substate_Result) decode() (*substate.Result, error) { +func (res *Substate_Result) decode(contractAddress *types.Address) (*substate.Result, error) { var err error = nil logs := make([]*types.Log, len(res.GetLogs())) for i, log := range res.GetLogs() { @@ -220,13 +238,12 @@ func (res *Substate_Result) decode() (*substate.Result, error) { } } - var nilAddr types.Address return substate.NewResult( res.GetStatus(), // Status types.BytesToBloom(res.Bloom), // Bloom logs, // Logs - nilAddr, // ContractAddress, to be processed downstream + *contractAddress, // ContractAddress res.GetGasUsed(), // GasUsed ), nil } From 6d4e857da7ff1ec33dc1a4e78e1c5428b6483f31 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 18:17:51 +0200 Subject: [PATCH 029/157] common.Address instead of types.Address --- protobuf/decode.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index c068061..182fb95 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -4,9 +4,10 @@ import ( "fmt" "math/big" - "github.com/ethereum/go-ethereum/crypto" "github.com/Fantom-foundation/Substate/substate" "github.com/Fantom-foundation/Substate/types" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" ) // Decode converts protobuf-encoded Substate into aida-comprehensible substate @@ -31,7 +32,7 @@ func (s *Substate) Decode(block uint64, tx int) (*substate.Substate, error) { return nil, err } - contractAddress := s.GetTxMessage().GetContractAddress() + contractAddress := s.GetTxMessage().getContractAddress() result, err := s.GetResult().decode(contractAddress) if err != nil { return nil, err @@ -214,13 +215,13 @@ func (entry *Substate_TxMessage_AccessListEntry) decode() ([]byte, [][]byte, err // getContractAddress returns the address of the newly created contract if any. // returns nil otherwise. -func (msg *Substate_TxMessage) getContractAddress() *Type.address { - var contractAddress types.Address - +func (msg *Substate_TxMessage) getContractAddress() *common.Address { + var contractAddress common.Address + // *to==nil means contract creation and thus address of newly created contract to := msg.GetTo() if to == nil { - fromAddr := types.BytesToAddress(msg.GetFrom()) + fromAddr := common.BytesToAddress(msg.GetFrom()) contractAddress = crypto.CreateAddress(fromAddr, msg.GetNonce()) } @@ -238,7 +239,6 @@ func (res *Substate_Result) decode(contractAddress *types.Address) (*substate.Re } } - return substate.NewResult( res.GetStatus(), // Status types.BytesToBloom(res.Bloom), // Bloom From 64858e7d2290790b6368fb064655db7e6e07fe0c Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 18:21:01 +0200 Subject: [PATCH 030/157] types.Address->common.Address --- protobuf/decode.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 182fb95..1a70f97 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -215,7 +215,7 @@ func (entry *Substate_TxMessage_AccessListEntry) decode() ([]byte, [][]byte, err // getContractAddress returns the address of the newly created contract if any. // returns nil otherwise. -func (msg *Substate_TxMessage) getContractAddress() *common.Address { +func (msg *Substate_TxMessage) getContractAddress() common.Address { var contractAddress common.Address // *to==nil means contract creation and thus address of newly created contract @@ -229,7 +229,7 @@ func (msg *Substate_TxMessage) getContractAddress() *common.Address { } // decode converts protobuf-encoded Substate_Result into aida-comprehensible Result -func (res *Substate_Result) decode(contractAddress *types.Address) (*substate.Result, error) { +func (res *Substate_Result) decode(contractAddress *common.Address) (*substate.Result, error) { var err error = nil logs := make([]*types.Log, len(res.GetLogs())) for i, log := range res.GetLogs() { From 929cc25699ffe8160bb21ae99ea8a1e1110738dc Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 18:26:49 +0200 Subject: [PATCH 031/157] another injection of contract address --- protobuf/decode.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 1a70f97..e2c2eaf 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -229,7 +229,7 @@ func (msg *Substate_TxMessage) getContractAddress() common.Address { } // decode converts protobuf-encoded Substate_Result into aida-comprehensible Result -func (res *Substate_Result) decode(contractAddress *common.Address) (*substate.Result, error) { +func (res *Substate_Result) decode(contractAddress common.Address) (*substate.Result, error) { var err error = nil logs := make([]*types.Log, len(res.GetLogs())) for i, log := range res.GetLogs() { @@ -243,8 +243,8 @@ func (res *Substate_Result) decode(contractAddress *common.Address) (*substate.R res.GetStatus(), // Status types.BytesToBloom(res.Bloom), // Bloom logs, // Logs - *contractAddress, // ContractAddress - res.GetGasUsed(), // GasUsed + types.BytesToAddress(contractAddress.Bytes()), // ContractAddress + res.GetGasUsed(), // GasUsed ), nil } From 83610e3e0501cd8df0539d735100f9e563bb903d Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 18:38:49 +0200 Subject: [PATCH 032/157] add printer to substate_db.go --- db/substate_db.go | 5 +++++ protobuf/decode.go | 1 - 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/db/substate_db.go b/db/substate_db.go index ff299ba..64cc898 100644 --- a/db/substate_db.go +++ b/db/substate_db.go @@ -4,6 +4,8 @@ import ( "encoding/binary" "fmt" + "google.golang.org/protobuf/encoding/protojson" + pb "github.com/Fantom-foundation/Substate/protobuf" "github.com/Fantom-foundation/Substate/rlp" "github.com/Fantom-foundation/Substate/substate" @@ -122,6 +124,9 @@ func (db *substateDB) GetSubstate(block uint64, tx int) (*substate.Substate, err return nil, err } + jsonString := protojson.Format(val) + fmt.Println(jsonString) + return pbSubstate.Decode(block, tx) } diff --git a/protobuf/decode.go b/protobuf/decode.go index e2c2eaf..e832543 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -95,7 +95,6 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { var diff *big.Int = nil if env.GetDifficulty() != nil { - fmt.Println(env.GetDifficulty()) diff.SetBytes(env.GetDifficulty()) } From f1c75e9f9a599fe28c187b060090c6dc23858d0b Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 18:42:09 +0200 Subject: [PATCH 033/157] print using generic go --- db/substate_db.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/db/substate_db.go b/db/substate_db.go index 64cc898..33281a1 100644 --- a/db/substate_db.go +++ b/db/substate_db.go @@ -4,8 +4,6 @@ import ( "encoding/binary" "fmt" - "google.golang.org/protobuf/encoding/protojson" - pb "github.com/Fantom-foundation/Substate/protobuf" "github.com/Fantom-foundation/Substate/rlp" "github.com/Fantom-foundation/Substate/substate" @@ -124,8 +122,7 @@ func (db *substateDB) GetSubstate(block uint64, tx int) (*substate.Substate, err return nil, err } - jsonString := protojson.Format(val) - fmt.Println(jsonString) + fmt.Printf("%+v\n", pbSubstate) return pbSubstate.Decode(block, tx) } From cb0a261673e5de707ab4c5fcc6945c3f673a221b Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 18:45:25 +0200 Subject: [PATCH 034/157] more printer --- db/substate_db.go | 2 +- db/substate_iterator.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/db/substate_db.go b/db/substate_db.go index 33281a1..723ae84 100644 --- a/db/substate_db.go +++ b/db/substate_db.go @@ -122,7 +122,7 @@ func (db *substateDB) GetSubstate(block uint64, tx int) (*substate.Substate, err return nil, err } - fmt.Printf("%+v\n", pbSubstate) + fmt.Printf("GetSubstate: %+v\n", pbSubstate) return pbSubstate.Decode(block, tx) } diff --git a/db/substate_iterator.go b/db/substate_iterator.go index 77ba06e..1d6bc73 100644 --- a/db/substate_iterator.go +++ b/db/substate_iterator.go @@ -41,6 +41,9 @@ func (i *substateIterator) decode(data rawEntry) (*substate.Substate, error) { if err := proto.Unmarshal(value, pbSubstate); err != nil { return nil, err } + + fmt.Printf("iterator: %+v\n", pbSubstate) + return pbSubstate.Decode(block, tx) } From daa7cfaf7655c150082f4cd2bba2dccd30172299 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 18:54:42 +0200 Subject: [PATCH 035/157] give up on single line pointer --- protobuf/decode.go | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index e832543..87e5dc4 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -22,16 +22,22 @@ func (s *Substate) Decode(block uint64, tx int) (*substate.Substate, error) { return nil, err } + fmt.Printf("decode blockenv: %+v\n", s.GetBlockEnv()) + environment, err := s.GetBlockEnv().decode() if err != nil { return nil, err } + fmt.Printf("decode txmsg: %+v\n", s.GetTxMessage()) + message, err := s.GetTxMessage().decode() if err != nil { return nil, err } + fmt.Printf("decode result: %+v\n", s.GetResult()) + contractAddress := s.GetTxMessage().getContractAddress() result, err := s.GetResult().decode(contractAddress) if err != nil { @@ -95,17 +101,20 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { var diff *big.Int = nil if env.GetDifficulty() != nil { - diff.SetBytes(env.GetDifficulty()) + d := new(big.Int).SetBytes(env.GetDifficulty()) + diff = &d } var baseFee *big.Int = nil if env.GetBaseFee() != nil { - baseFee.SetBytes(env.GetBaseFee().GetValue()) + bf := new(big.Int).SetBytes(env.GetBaseFee().GetValue()) + baseFee = &bf } var blobBaseFee *big.Int = nil if env.GetBlobBaseFee() != nil { - blobBaseFee.SetBytes(env.GetBlobBaseFee().GetValue()) + bfb := new(big.Int).SetBytes(env.GetBlobBaseFee().GetValue()) + blobBaseFee = &bfb } return &substate.Env{ @@ -158,6 +167,7 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { } } + // dataHash defaults to nil var dataHash types.Hash dh := msg.GetInitCodeHash() if dh != nil { @@ -166,22 +176,23 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { // London hard fork, EIP-1559: Fee market var gasFeeCap *big.Int = nil - gfc := msg.GetGasFeeCap() - if gfc != nil { - gasFeeCap.SetBytes(gfc.GetValue()) + if msg.GetGasFeeCap() != nil { + gfc := new(big.Int).SetBytes(msg.GetGasFeeCap().GetValue()) + gasFeeCap = &gfc } var gasTipCap *big.Int = nil - gtc := msg.GetGasTipCap() - if gtc != nil { - gasTipCap.SetBytes(gtc.GetValue()) + if msg.GetGasTipCap() != nil { + gtc := new(big.Int).SetBytes(msg.GetGasTipCap().GetValue()) + gasTipCap = >c } // Cancun hard fork, EIP-4844 var blobGasFeeCap *big.Int = nil bgfc := msg.GetBlobGasFeeCap() - if bgfc != nil { - blobGasFeeCap.SetBytes(bgfc.GetValue()) + if msg.GetBlobGasFeeCap() != nil { + bgfc := new(big.Int).SetBytes(msg.GetBlobGasFeeCap().GetValue()) + blobGasFeeCap = &bgfc } blobHashes := make([]types.Hash, len(msg.GetBlobHashes())) From 334f437138e34b6e2d1aa8243203113273d58a86 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 18:58:23 +0200 Subject: [PATCH 036/157] turns out single line worked --- protobuf/decode.go | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 87e5dc4..8efbb1c 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -101,20 +101,17 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { var diff *big.Int = nil if env.GetDifficulty() != nil { - d := new(big.Int).SetBytes(env.GetDifficulty()) - diff = &d + diff := new(big.Int).SetBytes(env.GetDifficulty()) } var baseFee *big.Int = nil if env.GetBaseFee() != nil { - bf := new(big.Int).SetBytes(env.GetBaseFee().GetValue()) - baseFee = &bf + baseFee := new(big.Int).SetBytes(env.GetBaseFee().GetValue()) } var blobBaseFee *big.Int = nil if env.GetBlobBaseFee() != nil { - bfb := new(big.Int).SetBytes(env.GetBlobBaseFee().GetValue()) - blobBaseFee = &bfb + blobBaseFee := new(big.Int).SetBytes(env.GetBlobBaseFee().GetValue()) } return &substate.Env{ @@ -177,22 +174,18 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { // London hard fork, EIP-1559: Fee market var gasFeeCap *big.Int = nil if msg.GetGasFeeCap() != nil { - gfc := new(big.Int).SetBytes(msg.GetGasFeeCap().GetValue()) - gasFeeCap = &gfc + gasFeeCap := new(big.Int).SetBytes(msg.GetGasFeeCap().GetValue()) } var gasTipCap *big.Int = nil if msg.GetGasTipCap() != nil { - gtc := new(big.Int).SetBytes(msg.GetGasTipCap().GetValue()) - gasTipCap = >c + gasTipCap := new(big.Int).SetBytes(msg.GetGasTipCap().GetValue()) } // Cancun hard fork, EIP-4844 var blobGasFeeCap *big.Int = nil - bgfc := msg.GetBlobGasFeeCap() if msg.GetBlobGasFeeCap() != nil { - bgfc := new(big.Int).SetBytes(msg.GetBlobGasFeeCap().GetValue()) - blobGasFeeCap = &bgfc + blobGasFeeCap := new(big.Int).SetBytes(msg.GetBlobGasFeeCap().GetValue()) } blobHashes := make([]types.Hash, len(msg.GetBlobHashes())) From d46d6e05d720a83af96755e0e8029f7fd43c501a Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 19:01:09 +0200 Subject: [PATCH 037/157] what --- protobuf/decode.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 8efbb1c..36aa006 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -101,17 +101,17 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { var diff *big.Int = nil if env.GetDifficulty() != nil { - diff := new(big.Int).SetBytes(env.GetDifficulty()) + diff = new(big.Int).SetBytes(env.GetDifficulty()) } var baseFee *big.Int = nil if env.GetBaseFee() != nil { - baseFee := new(big.Int).SetBytes(env.GetBaseFee().GetValue()) + baseFee = new(big.Int).SetBytes(env.GetBaseFee().GetValue()) } var blobBaseFee *big.Int = nil if env.GetBlobBaseFee() != nil { - blobBaseFee := new(big.Int).SetBytes(env.GetBlobBaseFee().GetValue()) + blobBaseFee = new(big.Int).SetBytes(env.GetBlobBaseFee().GetValue()) } return &substate.Env{ @@ -174,18 +174,18 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { // London hard fork, EIP-1559: Fee market var gasFeeCap *big.Int = nil if msg.GetGasFeeCap() != nil { - gasFeeCap := new(big.Int).SetBytes(msg.GetGasFeeCap().GetValue()) + gasFeeCap = new(big.Int).SetBytes(msg.GetGasFeeCap().GetValue()) } var gasTipCap *big.Int = nil if msg.GetGasTipCap() != nil { - gasTipCap := new(big.Int).SetBytes(msg.GetGasTipCap().GetValue()) + gasTipCap = new(big.Int).SetBytes(msg.GetGasTipCap().GetValue()) } // Cancun hard fork, EIP-4844 var blobGasFeeCap *big.Int = nil if msg.GetBlobGasFeeCap() != nil { - blobGasFeeCap := new(big.Int).SetBytes(msg.GetBlobGasFeeCap().GetValue()) + blobGasFeeCap = new(big.Int).SetBytes(msg.GetBlobGasFeeCap().GetValue()) } blobHashes := make([]types.Hash, len(msg.GetBlobHashes())) From ff41839c40d555160c7d0a021ece09c665976653 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 4 Sep 2024 19:09:21 +0200 Subject: [PATCH 038/157] empty codehash --- protobuf/decode.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 36aa006..8db3bf9 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -166,10 +166,10 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { // dataHash defaults to nil var dataHash types.Hash - dh := msg.GetInitCodeHash() - if dh != nil { - dataHash = types.BytesToHash(dh) - } + //dh := msg.GetInitCodeHash() + //if dh != nil { + // dataHash = types.BytesToHash(dh) + //} // London hard fork, EIP-1559: Fee market var gasFeeCap *big.Int = nil From 6ace404d82da7d63776ec58c7837e65d3c25db9a Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 5 Sep 2024 08:04:45 +0200 Subject: [PATCH 039/157] added more debug --- db/substate_iterator.go | 4 ++-- protobuf/decode.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/db/substate_iterator.go b/db/substate_iterator.go index 1d6bc73..254af1a 100644 --- a/db/substate_iterator.go +++ b/db/substate_iterator.go @@ -41,8 +41,8 @@ func (i *substateIterator) decode(data rawEntry) (*substate.Substate, error) { if err := proto.Unmarshal(value, pbSubstate); err != nil { return nil, err } - - fmt.Printf("iterator: %+v\n", pbSubstate) + + fmt.Printf("=================== iterator [%d/%d]: %+v\n", block, tx, pbSubstate) return pbSubstate.Decode(block, tx) } diff --git a/protobuf/decode.go b/protobuf/decode.go index 8db3bf9..16ec7d9 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -22,21 +22,21 @@ func (s *Substate) Decode(block uint64, tx int) (*substate.Substate, error) { return nil, err } - fmt.Printf("decode blockenv: %+v\n", s.GetBlockEnv()) + fmt.Printf("decode blockenv [%d/%d]: %+v\n", block, tx, s.GetBlockEnv()) environment, err := s.GetBlockEnv().decode() if err != nil { return nil, err } - fmt.Printf("decode txmsg: %+v\n", s.GetTxMessage()) + fmt.Printf("decode txmsg [%d/%d]: %+v\n", block, tx, s.GetTxMessage()) message, err := s.GetTxMessage().decode() if err != nil { return nil, err } - fmt.Printf("decode result: %+v\n", s.GetResult()) + fmt.Printf("decode result [%d/%d]: %+v\n", block, tx, s.GetResult()) contractAddress := s.GetTxMessage().getContractAddress() result, err := s.GetResult().decode(contractAddress) From 5731302022db3fc4ace02556cb5748e9475bf541 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 5 Sep 2024 09:29:39 +0200 Subject: [PATCH 040/157] remove debug --- db/substate_db.go | 2 -- db/substate_iterator.go | 2 -- protobuf/decode.go | 6 ------ 3 files changed, 10 deletions(-) diff --git a/db/substate_db.go b/db/substate_db.go index 723ae84..ff299ba 100644 --- a/db/substate_db.go +++ b/db/substate_db.go @@ -122,8 +122,6 @@ func (db *substateDB) GetSubstate(block uint64, tx int) (*substate.Substate, err return nil, err } - fmt.Printf("GetSubstate: %+v\n", pbSubstate) - return pbSubstate.Decode(block, tx) } diff --git a/db/substate_iterator.go b/db/substate_iterator.go index 254af1a..1469f65 100644 --- a/db/substate_iterator.go +++ b/db/substate_iterator.go @@ -42,8 +42,6 @@ func (i *substateIterator) decode(data rawEntry) (*substate.Substate, error) { return nil, err } - fmt.Printf("=================== iterator [%d/%d]: %+v\n", block, tx, pbSubstate) - return pbSubstate.Decode(block, tx) } diff --git a/protobuf/decode.go b/protobuf/decode.go index 16ec7d9..5062069 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -22,22 +22,16 @@ func (s *Substate) Decode(block uint64, tx int) (*substate.Substate, error) { return nil, err } - fmt.Printf("decode blockenv [%d/%d]: %+v\n", block, tx, s.GetBlockEnv()) - environment, err := s.GetBlockEnv().decode() if err != nil { return nil, err } - fmt.Printf("decode txmsg [%d/%d]: %+v\n", block, tx, s.GetTxMessage()) - message, err := s.GetTxMessage().decode() if err != nil { return nil, err } - fmt.Printf("decode result [%d/%d]: %+v\n", block, tx, s.GetResult()) - contractAddress := s.GetTxMessage().getContractAddress() result, err := s.GetResult().decode(contractAddress) if err != nil { From 7b99eb43bb4f2d4017307340b6f6866c6bb65a27 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 5 Sep 2024 10:57:41 +0200 Subject: [PATCH 041/157] now uses code instead of codehash when creating world state --- protobuf/decode.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 5062069..50d5005 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -60,12 +60,12 @@ func (alloc *Substate_Alloc) decode() (*substate.WorldState, error) { } address := types.BytesToAddress(addr) - nonce, balance, codehash, err := acct.decode() + nonce, balance, code, _, err := acct.decode() if err != nil { return nil, fmt.Errorf("Error decoding entry account; %w", err) } - world = world.Add(address, nonce, balance, codehash) + world = world.Add(address, nonce, balance, code) } return &world, nil @@ -75,9 +75,10 @@ func (entry *Substate_AllocEntry) decode() ([]byte, *Substate_Account, error) { return entry.GetAddress(), entry.GetAccount(), nil } -func (acct *Substate_Account) decode() (uint64, *big.Int, []byte, error) { - return acct.GetNonce(), +func (acct *Substate_Account) decode() (uint64, *big.Int, []byte, []byte, error) { + return acct.GetNonce(), new(big.Int).SetBytes(acct.GetBalance()), + acct.GetCode(), acct.GetCodeHash(), nil } From f86f14a826db9b461ec7a1f9c7c4d4bae7c998e2 Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 08:24:18 +0200 Subject: [PATCH 042/157] gasfeecap, gastipcap now defaults to gasprice --- protobuf/decode.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 50d5005..9fb2872 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -167,12 +167,12 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { //} // London hard fork, EIP-1559: Fee market - var gasFeeCap *big.Int = nil + var gasFeeCap *big.Int = new(big.Int).SetBytes(msg.GetGasPrice()) if msg.GetGasFeeCap() != nil { gasFeeCap = new(big.Int).SetBytes(msg.GetGasFeeCap().GetValue()) } - var gasTipCap *big.Int = nil + var gasTipCap *big.Int = new(big.Int).SetBytes(msg.GetGasPrice()) if msg.GetGasTipCap() != nil { gasTipCap = new(big.Int).SetBytes(msg.GetGasTipCap().GetValue()) } From 72b56a0857a36f7b147090f751ed2d510a0a0cf0 Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 15:40:10 +0200 Subject: [PATCH 043/157] message is now a new constructor --- protobuf/decode.go | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 9fb2872..61ae637 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -76,7 +76,7 @@ func (entry *Substate_AllocEntry) decode() ([]byte, *Substate_Account, error) { } func (acct *Substate_Account) decode() (uint64, *big.Int, []byte, []byte, error) { - return acct.GetNonce(), + return acct.GetNonce(), new(big.Int).SetBytes(acct.GetBalance()), acct.GetCode(), acct.GetCodeHash(), @@ -188,23 +188,21 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { blobHashes[i] = types.BytesToHash(hash) } - // dataHash is not exposed, so we must create Message using constructor - return substate.NewMessage( - msg.GetNonce(), // nonce - true, // CheckNonce - new(big.Int).SetBytes(msg.GetGasPrice()), // GasPrice - msg.GetGas(), // Gas - types.BytesToAddress(msg.GetFrom()), // From - pTo, // To - new(big.Int).SetBytes(msg.GetValue()), // Value - msg.GetData(), // Data - &dataHash, // dataHash - accessList, // AccessList - gasFeeCap, // GasFeeCap - gasTipCap, // GasTipCap - blobGasFeeCap, // BlobGasFeeCap - blobHashes, // BlobHashes - ), nil + return &substate.Message{ + Nonce: msg.GetNonce(), + CheckNonce: true, + GasPrice: new(big.Int).SetBytes(msg.GetGasPrice()), + Gas: msg.GetGas(), + From: types.BytesToAddress(msg.GetFrom()), + To: pTo, + Value: new(big.Int).SetBytes(msg.GetValue()), + Data: msg.GetData(), + AccessList: accessList, + GasFeeCap: gasFeeCap, + GasTipCap: gasTipCap, + BlobGasFeeCap: blobGasFeeCap, + BlobHashes: blobHashes, + }, nil } func (entry *Substate_TxMessage_AccessListEntry) decode() ([]byte, [][]byte, error) { From 0e6adfc2b4f5e96f4ea69fe178023fbe97f0a8bf Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 15:42:44 +0200 Subject: [PATCH 044/157] removed dataHash temporarily --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 61ae637..bfe903a 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -160,7 +160,7 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { } // dataHash defaults to nil - var dataHash types.Hash + //var dataHash types.Hash //dh := msg.GetInitCodeHash() //if dh != nil { // dataHash = types.BytesToHash(dh) From 0dc0bf459b369079a0457cbcd211896d6e25a84b Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 16:02:05 +0200 Subject: [PATCH 045/157] data now switches between data/initcodehash --- protobuf/decode.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index bfe903a..7703c26 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -136,6 +136,14 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { pTo = &address } + var data []byte + switch in := msg.Input.(type) { + case *Substate_TxMessage_Data: + data = msg.GetData() + case *Substate_TxMessage_InitCodeHash: + data = msg.GetInitCodeHash() + } + // Berlin hard fork, EIP-2930: Optional access lists var accessList types.AccessList = nil // nil if EIP-2930 is not activated if msg.GetAccessList() != nil { @@ -159,13 +167,6 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { } } - // dataHash defaults to nil - //var dataHash types.Hash - //dh := msg.GetInitCodeHash() - //if dh != nil { - // dataHash = types.BytesToHash(dh) - //} - // London hard fork, EIP-1559: Fee market var gasFeeCap *big.Int = new(big.Int).SetBytes(msg.GetGasPrice()) if msg.GetGasFeeCap() != nil { @@ -196,7 +197,7 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { From: types.BytesToAddress(msg.GetFrom()), To: pTo, Value: new(big.Int).SetBytes(msg.GetValue()), - Data: msg.GetData(), + Data: data, AccessList: accessList, GasFeeCap: gasFeeCap, GasTipCap: gasTipCap, From 5656431d7481637d07937c132e31a91438ba256a Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 16:05:02 +0200 Subject: [PATCH 046/157] correct switch syntax --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 7703c26..b10995c 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -137,7 +137,7 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { } var data []byte - switch in := msg.Input.(type) { + switch msg.Input.(type) { case *Substate_TxMessage_Data: data = msg.GetData() case *Substate_TxMessage_InitCodeHash: From 1c79c4a6e52b6dd86b750cbfa32c198366c7b488 Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 17:06:58 +0200 Subject: [PATCH 047/157] data now defaults to blank bytes[] --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index b10995c..a83eab8 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -136,7 +136,7 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { pTo = &address } - var data []byte + var data []byte = []byte{} switch msg.Input.(type) { case *Substate_TxMessage_Data: data = msg.GetData() From ddb2d262a027838662290c4b7db5b5822eb4a6bb Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 17:11:43 +0200 Subject: [PATCH 048/157] add debug for data --- protobuf/decode.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/protobuf/decode.go b/protobuf/decode.go index a83eab8..813da7d 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -144,6 +144,8 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { data = msg.GetInitCodeHash() } + fmt.Println("data: ", data) + // Berlin hard fork, EIP-2930: Optional access lists var accessList types.AccessList = nil // nil if EIP-2930 is not activated if msg.GetAccessList() != nil { From 1b441a8a0496a66f2e520e3bd8060d5706b63ccf Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 17:18:21 +0200 Subject: [PATCH 049/157] msg.Input now msg.GetInput() --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 813da7d..49ef961 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -137,7 +137,7 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { } var data []byte = []byte{} - switch msg.Input.(type) { + switch msg.GetInput().(type) { case *Substate_TxMessage_Data: data = msg.GetData() case *Substate_TxMessage_InitCodeHash: From cf941b0cc4b058800784c519ca963e66553e8123 Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 17:21:13 +0200 Subject: [PATCH 050/157] revert to rr04convert method to populate data --- protobuf/decode.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 49ef961..38029a2 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -137,12 +137,16 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { } var data []byte = []byte{} + if msg.GetData() != nil { + data = msg.GetData() + } + /* switch msg.GetInput().(type) { case *Substate_TxMessage_Data: data = msg.GetData() case *Substate_TxMessage_InitCodeHash: data = msg.GetInitCodeHash() - } + }*/ fmt.Println("data: ", data) From aab3b289fdd34e97cd91af898a890ffc37de02ba Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 17:25:49 +0200 Subject: [PATCH 051/157] add debug for tx message decode --- protobuf/decode.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/protobuf/decode.go b/protobuf/decode.go index 38029a2..179587e 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -128,6 +128,10 @@ func (entry *Substate_BlockEnv_BlockHashEntry) decode() (uint64, []byte, error) // decode converts protobuf-encoded Substate_TxMessage into aida-comprehensible Message func (msg *Substate_TxMessage) decode() (*substate.Message, error) { + var jbytes []byte + jbytes, _ = json.MarshalIndent(msg, "", " ") + fmt.Printf("msg:\n%s\n", jbytes) + // to=nil means contract creation var pTo *types.Address = nil to := msg.GetTo() From 969ec536449162649c677d259a6459b358752b30 Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 17:27:46 +0200 Subject: [PATCH 052/157] add debug for tx message decode --- protobuf/decode.go | 1 + 1 file changed, 1 insertion(+) diff --git a/protobuf/decode.go b/protobuf/decode.go index 179587e..01e6ee9 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -3,6 +3,7 @@ package protobuf import ( "fmt" "math/big" + "encoding/json" "github.com/Fantom-foundation/Substate/substate" "github.com/Fantom-foundation/Substate/types" From f2cbc2597f5f6ee321d0f162088e06424c7da49c Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 17:40:36 +0200 Subject: [PATCH 053/157] try something plain --- protobuf/decode.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 01e6ee9..c21aad7 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -141,10 +141,13 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { pTo = &address } - var data []byte = []byte{} + var data []byte = msg.GetData() if msg.GetData() != nil { data = msg.GetData() } + if msg.GetInitCodeHash() != nil { + data = msg.GetInitCodeHash() + } /* switch msg.GetInput().(type) { case *Substate_TxMessage_Data: From c7745b4ad5c24d800afa065a5c9b526d05e26dee Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 17:52:30 +0200 Subject: [PATCH 054/157] one more --- protobuf/decode.go | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index c21aad7..7c4684d 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -129,9 +129,6 @@ func (entry *Substate_BlockEnv_BlockHashEntry) decode() (uint64, []byte, error) // decode converts protobuf-encoded Substate_TxMessage into aida-comprehensible Message func (msg *Substate_TxMessage) decode() (*substate.Message, error) { - var jbytes []byte - jbytes, _ = json.MarshalIndent(msg, "", " ") - fmt.Printf("msg:\n%s\n", jbytes) // to=nil means contract creation var pTo *types.Address = nil @@ -141,22 +138,18 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { pTo = &address } - var data []byte = msg.GetData() - if msg.GetData() != nil { - data = msg.GetData() - } - if msg.GetInitCodeHash() != nil { - data = msg.GetInitCodeHash() - } - /* + var data []byte switch msg.GetInput().(type) { case *Substate_TxMessage_Data: data = msg.GetData() case *Substate_TxMessage_InitCodeHash: data = msg.GetInitCodeHash() - }*/ + } - fmt.Println("data: ", data) + fmt.Println("extracted: ", data) + fmt.Println("input ", msg.GetInput()) + fmt.Println("data: ", msg.GetData()) + fmt.Println("init: ", msg.GetInitCodeHash()) // Berlin hard fork, EIP-2930: Optional access lists var accessList types.AccessList = nil // nil if EIP-2930 is not activated From cea29620ef0e6ed6726b898b7574f42cf5e346e4 Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 17:54:43 +0200 Subject: [PATCH 055/157] one more --- protobuf/decode.go | 1 - 1 file changed, 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 7c4684d..3fb59d9 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -3,7 +3,6 @@ package protobuf import ( "fmt" "math/big" - "encoding/json" "github.com/Fantom-foundation/Substate/substate" "github.com/Fantom-foundation/Substate/types" From 0b8f1e76985424b7b4b2227815e6c10493396ab7 Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 17:58:42 +0200 Subject: [PATCH 056/157] add also nonce --- protobuf/decode.go | 1 + 1 file changed, 1 insertion(+) diff --git a/protobuf/decode.go b/protobuf/decode.go index 3fb59d9..8b37f4c 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -145,6 +145,7 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { data = msg.GetInitCodeHash() } + fmt.Println("==nonce: ", msg.GetNonce()) fmt.Println("extracted: ", data) fmt.Println("input ", msg.GetInput()) fmt.Println("data: ", msg.GetData()) From 13f3bbb04b804001acb9e41883abb6102b7f6ed7 Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 20:34:02 +0200 Subject: [PATCH 057/157] add also block, tx --- protobuf/decode.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/protobuf/decode.go b/protobuf/decode.go index 8b37f4c..aa05a31 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -12,6 +12,8 @@ import ( // Decode converts protobuf-encoded Substate into aida-comprehensible substate func (s *Substate) Decode(block uint64, tx int) (*substate.Substate, error) { + fmt.Println("decoding substate:", block, tx) + input, err := s.GetInputAlloc().decode() if err != nil { return nil, err From a2b50054e51f4f3139caf128a3993a6d3f06607c Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 20:53:29 +0200 Subject: [PATCH 058/157] do a proper dump --- protobuf/decode.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index aa05a31..584a00b 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -3,6 +3,7 @@ package protobuf import ( "fmt" "math/big" + "encoding/json" "github.com/Fantom-foundation/Substate/substate" "github.com/Fantom-foundation/Substate/types" @@ -10,9 +11,21 @@ import ( "github.com/ethereum/go-ethereum/crypto" ) +func (s *Substate) Dump(block uint64, tx int) error { + out := fmt.Sprintf("decoding block: %v Transaction: %v\n", block, tx) + + var jbytes []byte + jbytes, _ = json.MarshalIndent(s, "", " ") + out += fmt.Sprintf("substate:\n%s\n", jbytes) + + log.Println(out) + + return nil +} + // Decode converts protobuf-encoded Substate into aida-comprehensible substate func (s *Substate) Decode(block uint64, tx int) (*substate.Substate, error) { - fmt.Println("decoding substate:", block, tx) + s.Dump(block, tx) input, err := s.GetInputAlloc().decode() if err != nil { From 48a20faa308128478fb0011777d4a4fb55a7b3e7 Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 20:55:42 +0200 Subject: [PATCH 059/157] do a proper dump +log --- protobuf/decode.go | 1 + 1 file changed, 1 insertion(+) diff --git a/protobuf/decode.go b/protobuf/decode.go index 584a00b..fe2361c 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -4,6 +4,7 @@ import ( "fmt" "math/big" "encoding/json" + "log" "github.com/Fantom-foundation/Substate/substate" "github.com/Fantom-foundation/Substate/types" From d1d7f6160d53cb6f7e8e1b7a3229b4258908538a Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 21:21:13 +0200 Subject: [PATCH 060/157] test lookup --- db/substate_iterator.go | 2 +- protobuf/decode.go | 29 ++++++++++++++++++++++------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/db/substate_iterator.go b/db/substate_iterator.go index 1469f65..9512801 100644 --- a/db/substate_iterator.go +++ b/db/substate_iterator.go @@ -42,7 +42,7 @@ func (i *substateIterator) decode(data rawEntry) (*substate.Substate, error) { return nil, err } - return pbSubstate.Decode(block, tx) + return pbSubstate.Decode(i.db.GetCode, block, tx) } func (i *substateIterator) start(numWorkers int) { diff --git a/protobuf/decode.go b/protobuf/decode.go index fe2361c..45f2e27 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -24,16 +24,18 @@ func (s *Substate) Dump(block uint64, tx int) error { return nil } +type CodeLookUp = func (types.Hash) ([]byte, error) + // Decode converts protobuf-encoded Substate into aida-comprehensible substate -func (s *Substate) Decode(block uint64, tx int) (*substate.Substate, error) { +func (s *Substate) Decode(lookup CodeLookUp, block uint64, tx int) (*substate.Substate, error) { s.Dump(block, tx) - input, err := s.GetInputAlloc().decode() + input, err := s.GetInputAlloc().decode(lookup) if err != nil { return nil, err } - output, err := s.GetOutputAlloc().decode() + output, err := s.GetOutputAlloc().decode(lookup) if err != nil { return nil, err } @@ -43,7 +45,7 @@ func (s *Substate) Decode(block uint64, tx int) (*substate.Substate, error) { return nil, err } - message, err := s.GetTxMessage().decode() + message, err := s.GetTxMessage().decode(lookup) if err != nil { return nil, err } @@ -66,7 +68,7 @@ func (s *Substate) Decode(block uint64, tx int) (*substate.Substate, error) { } // decode converts protobuf-encoded Substate_Alloc into aida-comprehensible WorldState -func (alloc *Substate_Alloc) decode() (*substate.WorldState, error) { +func (alloc *Substate_Alloc) decode(lookup CodeLookUp) (*substate.WorldState, error) { world := make(substate.WorldState, len(alloc.GetAlloc())) for _, entry := range alloc.GetAlloc() { @@ -76,11 +78,16 @@ func (alloc *Substate_Alloc) decode() (*substate.WorldState, error) { } address := types.BytesToAddress(addr) - nonce, balance, code, _, err := acct.decode() + nonce, balance, code, codehash, err := acct.decode() if err != nil { return nil, fmt.Errorf("Error decoding entry account; %w", err) } + c := lookup(codehash) + if c != code { + return nil, fmt.Errorf("code lookup doesn't match code") + } + world = world.Add(address, nonce, balance, code) } @@ -143,7 +150,7 @@ func (entry *Substate_BlockEnv_BlockHashEntry) decode() (uint64, []byte, error) } // decode converts protobuf-encoded Substate_TxMessage into aida-comprehensible Message -func (msg *Substate_TxMessage) decode() (*substate.Message, error) { +func (msg *Substate_TxMessage) decode(lookup CodeLookUp) (*substate.Message, error) { // to=nil means contract creation var pTo *types.Address = nil @@ -161,6 +168,14 @@ func (msg *Substate_TxMessage) decode() (*substate.Message, error) { data = msg.GetInitCodeHash() } + if pTo == nil { + code, err := lookup(msg.GetInitCodeHash()) + if err != nil { + return nil, fmt.Errorf("failed to decode tx message; %w", err) + } + data = code + } + fmt.Println("==nonce: ", msg.GetNonce()) fmt.Println("extracted: ", data) fmt.Println("input ", msg.GetInput()) From 3b5d1418455797230e3b49b71ad62076afcca48e Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 21:25:49 +0200 Subject: [PATCH 061/157] fix bug --- protobuf/decode.go | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 45f2e27..7b847e5 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -83,9 +83,12 @@ func (alloc *Substate_Alloc) decode(lookup CodeLookUp) (*substate.WorldState, er return nil, fmt.Errorf("Error decoding entry account; %w", err) } - c := lookup(codehash) + c, err := lookup(codehash) + if err != nil { + return nil, fmt.Errorf("Error looking up %s; %w", codehash, err) + } if c != code { - return nil, fmt.Errorf("code lookup doesn't match code") + return nil, fmt.Errorf("code lookup return %s, doesn't match found code %s", c, code) } world = world.Add(address, nonce, balance, code) @@ -102,7 +105,7 @@ func (acct *Substate_Account) decode() (uint64, *big.Int, []byte, []byte, error) return acct.GetNonce(), new(big.Int).SetBytes(acct.GetBalance()), acct.GetCode(), - acct.GetCodeHash(), + types.BytesToHash(acct.GetCodeHash())/, nil } @@ -160,16 +163,9 @@ func (msg *Substate_TxMessage) decode(lookup CodeLookUp) (*substate.Message, err pTo = &address } - var data []byte - switch msg.GetInput().(type) { - case *Substate_TxMessage_Data: - data = msg.GetData() - case *Substate_TxMessage_InitCodeHash: - data = msg.GetInitCodeHash() - } - + var data []byte = msg.GetData() if pTo == nil { - code, err := lookup(msg.GetInitCodeHash()) + code, err := lookup(types.BytesToHash(msg.GetInitCodeHash())) if err != nil { return nil, fmt.Errorf("failed to decode tx message; %w", err) } From 36b7dea740614a3ff53e96f1f91872fa909f0b96 Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 21:27:46 +0200 Subject: [PATCH 062/157] fix bug2 --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 7b847e5..cbc492e 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -105,7 +105,7 @@ func (acct *Substate_Account) decode() (uint64, *big.Int, []byte, []byte, error) return acct.GetNonce(), new(big.Int).SetBytes(acct.GetBalance()), acct.GetCode(), - types.BytesToHash(acct.GetCodeHash())/, + types.BytesToHash(acct.GetCodeHash()), nil } From 7c5d214f3550bb4ad4e21feb761743d12ab6fb4d Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 21:31:44 +0200 Subject: [PATCH 063/157] fix bug2 --- protobuf/decode.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index cbc492e..074f508 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -87,11 +87,8 @@ func (alloc *Substate_Alloc) decode(lookup CodeLookUp) (*substate.WorldState, er if err != nil { return nil, fmt.Errorf("Error looking up %s; %w", codehash, err) } - if c != code { - return nil, fmt.Errorf("code lookup return %s, doesn't match found code %s", c, code) - } - world = world.Add(address, nonce, balance, code) + world = world.Add(address, nonce, balance, c) } return &world, nil @@ -101,7 +98,7 @@ func (entry *Substate_AllocEntry) decode() ([]byte, *Substate_Account, error) { return entry.GetAddress(), entry.GetAccount(), nil } -func (acct *Substate_Account) decode() (uint64, *big.Int, []byte, []byte, error) { +func (acct *Substate_Account) decode() (uint64, *big.Int, []byte, types.Hash, error) { return acct.GetNonce(), new(big.Int).SetBytes(acct.GetBalance()), acct.GetCode(), From 73b48722e0e67f05c7655cc58f6272b375937e58 Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 21:33:26 +0200 Subject: [PATCH 064/157] fix bug3 --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 074f508..6e296a5 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -78,7 +78,7 @@ func (alloc *Substate_Alloc) decode(lookup CodeLookUp) (*substate.WorldState, er } address := types.BytesToAddress(addr) - nonce, balance, code, codehash, err := acct.decode() + nonce, balance, _, codehash, err := acct.decode() if err != nil { return nil, fmt.Errorf("Error decoding entry account; %w", err) } From 21f4055f7d52a5f203a8b903718979f370be9e79 Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 21:35:45 +0200 Subject: [PATCH 065/157] fix bug4 --- db/substate_db.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/substate_db.go b/db/substate_db.go index ff299ba..59bc53b 100644 --- a/db/substate_db.go +++ b/db/substate_db.go @@ -122,7 +122,7 @@ func (db *substateDB) GetSubstate(block uint64, tx int) (*substate.Substate, err return nil, err } - return pbSubstate.Decode(block, tx) + return pbSubstate.Decode(db.GetCode, block, tx) } // GetBlockSubstates returns substates for given block if exists within DB. From 28e3ca279dfd43a50cffe28d1464f356d7fdd239 Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 21:40:18 +0200 Subject: [PATCH 066/157] remove codehash from worldstate --- protobuf/decode.go | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 6e296a5..41e2593 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -16,8 +16,16 @@ func (s *Substate) Dump(block uint64, tx int) error { out := fmt.Sprintf("decoding block: %v Transaction: %v\n", block, tx) var jbytes []byte - jbytes, _ = json.MarshalIndent(s, "", " ") - out += fmt.Sprintf("substate:\n%s\n", jbytes) + jbytes, _ = json.MarshalIndent(s.GetInputAlloc(), "", " ") + out += fmt.Sprintf("i:\n%s\n", jbytes) + jbytes, _ = json.MarshalIndent(s.GetBlockEnv(), "", " ") + out += fmt.Sprintf("i:\n%s\n", jbytes) + jbytes, _ = json.MarshalIndent(s.GetTxMessage(), "", " ") + out += fmt.Sprintf("i:\n%s\n", jbytes) + jbytes, _ = json.MarshalIndent(s.GetOutputAlloc(), "", " ") + out += fmt.Sprintf("i:\n%s\n", jbytes) + jbytes, _ = json.MarshalIndent(s.GetResult(), "", " ") + out += fmt.Sprintf("i:\n%s\n", jbytes) log.Println(out) @@ -30,12 +38,12 @@ type CodeLookUp = func (types.Hash) ([]byte, error) func (s *Substate) Decode(lookup CodeLookUp, block uint64, tx int) (*substate.Substate, error) { s.Dump(block, tx) - input, err := s.GetInputAlloc().decode(lookup) + input, err := s.GetInputAlloc().decode() if err != nil { return nil, err } - output, err := s.GetOutputAlloc().decode(lookup) + output, err := s.GetOutputAlloc().decode() if err != nil { return nil, err } @@ -78,17 +86,12 @@ func (alloc *Substate_Alloc) decode(lookup CodeLookUp) (*substate.WorldState, er } address := types.BytesToAddress(addr) - nonce, balance, _, codehash, err := acct.decode() + nonce, balance, code, _, err := acct.decode() if err != nil { return nil, fmt.Errorf("Error decoding entry account; %w", err) } - c, err := lookup(codehash) - if err != nil { - return nil, fmt.Errorf("Error looking up %s; %w", codehash, err) - } - - world = world.Add(address, nonce, balance, c) + world = world.Add(address, nonce, balance, code) } return &world, nil From ec327addb33828fde53446e320daddfb8cb68042 Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 21:43:22 +0200 Subject: [PATCH 067/157] remove codehash from worldstate2 --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 41e2593..64b85b8 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -76,7 +76,7 @@ func (s *Substate) Decode(lookup CodeLookUp, block uint64, tx int) (*substate.Su } // decode converts protobuf-encoded Substate_Alloc into aida-comprehensible WorldState -func (alloc *Substate_Alloc) decode(lookup CodeLookUp) (*substate.WorldState, error) { +func (alloc *Substate_Alloc) decode() (*substate.WorldState, error) { world := make(substate.WorldState, len(alloc.GetAlloc())) for _, entry := range alloc.GetAlloc() { From 1dc09e21d438ecb28d9f3c645a18c98c7ac2dc49 Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 21:47:03 +0200 Subject: [PATCH 068/157] remove codehash from worldstate3 --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 64b85b8..ccc782b 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -166,7 +166,7 @@ func (msg *Substate_TxMessage) decode(lookup CodeLookUp) (*substate.Message, err var data []byte = msg.GetData() if pTo == nil { code, err := lookup(types.BytesToHash(msg.GetInitCodeHash())) - if err != nil { + if err != nil && !errors.Is(err, leveldb.ErrNotFound) { return nil, fmt.Errorf("failed to decode tx message; %w", err) } data = code From 645cc253761e86c54659bf9168fe334fc86f50c3 Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 21:50:56 +0200 Subject: [PATCH 069/157] remove codehash from worldstate4 --- protobuf/decode.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/protobuf/decode.go b/protobuf/decode.go index ccc782b..a895bef 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -5,11 +5,13 @@ import ( "math/big" "encoding/json" "log" + "errors" "github.com/Fantom-foundation/Substate/substate" "github.com/Fantom-foundation/Substate/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" + "github.com/syndtr/goleveldb/leveldb" ) func (s *Substate) Dump(block uint64, tx int) error { From aef723b0aebf757105f1f9a1f29ed349f3186c64 Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 21:54:02 +0200 Subject: [PATCH 070/157] remove debug --- protobuf/decode.go | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index a895bef..3e72184 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -1,11 +1,11 @@ package protobuf import ( - "fmt" - "math/big" "encoding/json" - "log" "errors" + "fmt" + "log" + "math/big" "github.com/Fantom-foundation/Substate/substate" "github.com/Fantom-foundation/Substate/types" @@ -34,12 +34,10 @@ func (s *Substate) Dump(block uint64, tx int) error { return nil } -type CodeLookUp = func (types.Hash) ([]byte, error) +type CodeLookUp = func(types.Hash) ([]byte, error) // Decode converts protobuf-encoded Substate into aida-comprehensible substate func (s *Substate) Decode(lookup CodeLookUp, block uint64, tx int) (*substate.Substate, error) { - s.Dump(block, tx) - input, err := s.GetInputAlloc().decode() if err != nil { return nil, err @@ -174,12 +172,6 @@ func (msg *Substate_TxMessage) decode(lookup CodeLookUp) (*substate.Message, err data = code } - fmt.Println("==nonce: ", msg.GetNonce()) - fmt.Println("extracted: ", data) - fmt.Println("input ", msg.GetInput()) - fmt.Println("data: ", msg.GetData()) - fmt.Println("init: ", msg.GetInitCodeHash()) - // Berlin hard fork, EIP-2930: Optional access lists var accessList types.AccessList = nil // nil if EIP-2930 is not activated if msg.GetAccessList() != nil { From 1fedcdd669abbad9c835ec16642bdecdb7d5d411 Mon Sep 17 00:00:00 2001 From: Rapol Date: Mon, 9 Sep 2024 22:21:57 +0200 Subject: [PATCH 071/157] getblocksubstates now protobuf --- db/substate_db.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/db/substate_db.go b/db/substate_db.go index 59bc53b..6b957e8 100644 --- a/db/substate_db.go +++ b/db/substate_db.go @@ -147,14 +147,24 @@ func (db *substateDB) GetBlockSubstates(block uint64) (map[int]*substate.Substat return nil, fmt.Errorf("record-replay: GetBlockSubstates(%v) iterated substates from block %v", block, b) } - rlpSubstate, err := rlp.Decode(value) - if err != nil { - return nil, fmt.Errorf("cannot decode data into rlp block: %v, tx %v; %w", block, tx, err) + //rlpSubstate, err := rlp.Decode(value) + //if err != nil { + // return nil, fmt.Errorf("cannot decode data into rlp block: %v, tx %v; %w", block, tx, err) + //} + + //sbstt, err := rlpSubstate.ToSubstate(db.GetCode, block, tx) + //if err != nil { + // return nil, fmt.Errorf("cannot decode data into substate: %w", err) + //} + + pbSubstate := &pb.Substate{} + if err := proto.Unmarshal(value, pbSubstate); err != nil { + return nil, err } - sbstt, err := rlpSubstate.ToSubstate(db.GetCode, block, tx) + sbstt, err := pbSubstate.Decode(db.GetCode, block, tx) if err != nil { - return nil, fmt.Errorf("cannot decode data into substate: %w", err) + return nil, err } txSubstate[tx] = sbstt From dbfc19d3037a05ac5149161fa85d54141205d3ec Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 06:48:17 +0200 Subject: [PATCH 072/157] debug code db --- db/code_db.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/db/code_db.go b/db/code_db.go index 49755c3..6d92bd1 100644 --- a/db/code_db.go +++ b/db/code_db.go @@ -79,6 +79,7 @@ func (db *codeDB) HasCode(codeHash types.Hash) (bool, error) { // GetCode gets the code for the given hash. func (db *codeDB) GetCode(codeHash types.Hash) ([]byte, error) { + fmt.Println("GetCode", codeHash) if codeHash.IsEmpty() { return nil, ErrorEmptyHash } @@ -88,11 +89,16 @@ func (db *codeDB) GetCode(codeHash types.Hash) ([]byte, error) { if err != nil { return nil, fmt.Errorf("cannot get code %s: %w", codeHash, err) } + + fmt.Println("GetCode", codeHash, code) + return code, nil } // PutCode creates hash for given code and inserts it into the baseDB. func (db *codeDB) PutCode(code []byte) error { + fmt.Println("PutCode", code) + codeHash := hash.Keccak256Hash(code) key := CodeDBKey(codeHash) err := db.Put(key, code) @@ -100,6 +106,8 @@ func (db *codeDB) PutCode(code []byte) error { return fmt.Errorf("cannot put code %s: %w", codeHash, err) } + fmt.Println("PutCode", code, codeHash) + return nil } From 6861765ebfa6a41623052f85828a36cc8be3b081 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 16:03:28 +0200 Subject: [PATCH 073/157] added lookup for worldstate --- protobuf/decode.go | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 3e72184..a3d5b9e 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -2,7 +2,7 @@ package protobuf import ( "encoding/json" - "errors" + //"errors" "fmt" "log" "math/big" @@ -11,7 +11,7 @@ import ( "github.com/Fantom-foundation/Substate/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" - "github.com/syndtr/goleveldb/leveldb" + //"github.com/syndtr/goleveldb/leveldb" ) func (s *Substate) Dump(block uint64, tx int) error { @@ -34,16 +34,20 @@ func (s *Substate) Dump(block uint64, tx int) error { return nil } -type CodeLookUp = func(types.Hash) ([]byte, error) +type CodeHash = types.Hash +type Code = []byte +type DbGetCode = func(CodeHash) (Code, error) // Decode converts protobuf-encoded Substate into aida-comprehensible substate -func (s *Substate) Decode(lookup CodeLookUp, block uint64, tx int) (*substate.Substate, error) { - input, err := s.GetInputAlloc().decode() +func (s *Substate) Decode(lookup DbGetCode, block uint64, tx int) (*substate.Substate, error) { + codeMap := s.generateCodeMap(lookup, make(CodeMap)) + + input, err := s.GetInputAlloc().decode(lookup) if err != nil { return nil, err } - output, err := s.GetOutputAlloc().decode() + output, err := s.GetOutputAlloc().decode(lookup) if err != nil { return nil, err } @@ -75,8 +79,9 @@ func (s *Substate) Decode(lookup CodeLookUp, block uint64, tx int) (*substate.Su }, nil } + // decode converts protobuf-encoded Substate_Alloc into aida-comprehensible WorldState -func (alloc *Substate_Alloc) decode() (*substate.WorldState, error) { +func (alloc *Substate_Alloc) decode(lookup DbGetCode) (*substate.WorldState, error) { world := make(substate.WorldState, len(alloc.GetAlloc())) for _, entry := range alloc.GetAlloc() { @@ -86,12 +91,17 @@ func (alloc *Substate_Alloc) decode() (*substate.WorldState, error) { } address := types.BytesToAddress(addr) - nonce, balance, code, _, err := acct.decode() + nonce, balance, _, codehash, err := acct.decode() if err != nil { return nil, fmt.Errorf("Error decoding entry account; %w", err) } - world = world.Add(address, nonce, balance, code) + code, err := lookup(codehash) + if err != nil && !errors.Is(err, leveldb.ErrNotFound) { + return nil, fmt.Errorf("Error looking up codehash; %w", err) + } + + world[address] = substate.NewAccount(nonce, balance, code) } return &world, nil @@ -101,7 +111,7 @@ func (entry *Substate_AllocEntry) decode() ([]byte, *Substate_Account, error) { return entry.GetAddress(), entry.GetAccount(), nil } -func (acct *Substate_Account) decode() (uint64, *big.Int, []byte, types.Hash, error) { +func (acct *Substate_Account) decode() (uint64, *big.Int, Code, CodeHash, error) { return acct.GetNonce(), new(big.Int).SetBytes(acct.GetBalance()), acct.GetCode(), From 5a24f190235387f37dcae19e47d767cdd54becb0 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 16:07:44 +0200 Subject: [PATCH 074/157] fix bug --- protobuf/decode.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index a3d5b9e..db4d088 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -2,7 +2,7 @@ package protobuf import ( "encoding/json" - //"errors" + "errors" "fmt" "log" "math/big" @@ -11,7 +11,7 @@ import ( "github.com/Fantom-foundation/Substate/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" - //"github.com/syndtr/goleveldb/leveldb" + "github.com/syndtr/goleveldb/leveldb" ) func (s *Substate) Dump(block uint64, tx int) error { @@ -40,8 +40,6 @@ type DbGetCode = func(CodeHash) (Code, error) // Decode converts protobuf-encoded Substate into aida-comprehensible substate func (s *Substate) Decode(lookup DbGetCode, block uint64, tx int) (*substate.Substate, error) { - codeMap := s.generateCodeMap(lookup, make(CodeMap)) - input, err := s.GetInputAlloc().decode(lookup) if err != nil { return nil, err @@ -163,7 +161,7 @@ func (entry *Substate_BlockEnv_BlockHashEntry) decode() (uint64, []byte, error) } // decode converts protobuf-encoded Substate_TxMessage into aida-comprehensible Message -func (msg *Substate_TxMessage) decode(lookup CodeLookUp) (*substate.Message, error) { +func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, error) { // to=nil means contract creation var pTo *types.Address = nil From d539e8f0dc8fe2d7705c2392061f605a6a251a2b Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 16:43:07 +0200 Subject: [PATCH 075/157] added storage --- protobuf/decode.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index db4d088..56220be 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -77,7 +77,6 @@ func (s *Substate) Decode(lookup DbGetCode, block uint64, tx int) (*substate.Sub }, nil } - // decode converts protobuf-encoded Substate_Alloc into aida-comprehensible WorldState func (alloc *Substate_Alloc) decode(lookup DbGetCode) (*substate.WorldState, error) { world := make(substate.WorldState, len(alloc.GetAlloc())) @@ -100,6 +99,15 @@ func (alloc *Substate_Alloc) decode(lookup DbGetCode) (*substate.WorldState, err } world[address] = substate.NewAccount(nonce, balance, code) + + for ix, entry := range alloc.GetStorage() { + key, value, err := entry.decode() + if err != nil { + return nil, fmt.Errorf("Error decoding account storage entry; %w", err) + } + + world[address].Storage[key] = value + } } return &world, nil @@ -117,6 +125,10 @@ func (acct *Substate_Account) decode() (uint64, *big.Int, Code, CodeHash, error) nil } +func (entry *Substate_Account_StorageEntry) decode() ([]byte, []byte, error) { + return entry.GetKey(), entry.GetValue(), nil +} + // decode converts protobuf-encoded Substate_BlockEnv into aida-comprehensible Env func (env *Substate_BlockEnv) decode() (*substate.Env, error) { blockHashes := make(map[uint64]types.Hash, len(env.GetBlockHashes())) From d74e2d9cc145ea0ee1f5af675d4db075e055fe67 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 16:46:33 +0200 Subject: [PATCH 076/157] bugfix1 --- protobuf/decode.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 56220be..60486e8 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -77,6 +77,7 @@ func (s *Substate) Decode(lookup DbGetCode, block uint64, tx int) (*substate.Sub }, nil } + // decode converts protobuf-encoded Substate_Alloc into aida-comprehensible WorldState func (alloc *Substate_Alloc) decode(lookup DbGetCode) (*substate.WorldState, error) { world := make(substate.WorldState, len(alloc.GetAlloc())) @@ -100,7 +101,7 @@ func (alloc *Substate_Alloc) decode(lookup DbGetCode) (*substate.WorldState, err world[address] = substate.NewAccount(nonce, balance, code) - for ix, entry := range alloc.GetStorage() { + for ix, entry := range acct.GetStorage() { key, value, err := entry.decode() if err != nil { return nil, fmt.Errorf("Error decoding account storage entry; %w", err) @@ -129,6 +130,7 @@ func (entry *Substate_Account_StorageEntry) decode() ([]byte, []byte, error) { return entry.GetKey(), entry.GetValue(), nil } + // decode converts protobuf-encoded Substate_BlockEnv into aida-comprehensible Env func (env *Substate_BlockEnv) decode() (*substate.Env, error) { blockHashes := make(map[uint64]types.Hash, len(env.GetBlockHashes())) From 9ee83c782081a05aee4b890c02c5cb1127042b99 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 16:50:08 +0200 Subject: [PATCH 077/157] bugfix2 --- protobuf/decode.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 60486e8..3f8c645 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -77,7 +77,6 @@ func (s *Substate) Decode(lookup DbGetCode, block uint64, tx int) (*substate.Sub }, nil } - // decode converts protobuf-encoded Substate_Alloc into aida-comprehensible WorldState func (alloc *Substate_Alloc) decode(lookup DbGetCode) (*substate.WorldState, error) { world := make(substate.WorldState, len(alloc.GetAlloc())) @@ -101,8 +100,8 @@ func (alloc *Substate_Alloc) decode(lookup DbGetCode) (*substate.WorldState, err world[address] = substate.NewAccount(nonce, balance, code) - for ix, entry := range acct.GetStorage() { - key, value, err := entry.decode() + for _, storage := range acct.GetStorage() { + key, value, err := storage.decode() if err != nil { return nil, fmt.Errorf("Error decoding account storage entry; %w", err) } @@ -126,11 +125,12 @@ func (acct *Substate_Account) decode() (uint64, *big.Int, Code, CodeHash, error) nil } -func (entry *Substate_Account_StorageEntry) decode() ([]byte, []byte, error) { - return entry.GetKey(), entry.GetValue(), nil +func (entry *Substate_Account_StorageEntry) decode() (types.Hash, types.Hash, error) { + return types.BytesToHash(entry.GetKey()), + types.BytesToHash(entry.GetValue()), + nil } - // decode converts protobuf-encoded Substate_BlockEnv into aida-comprehensible Env func (env *Substate_BlockEnv) decode() (*substate.Env, error) { blockHashes := make(map[uint64]types.Hash, len(env.GetBlockHashes())) From 7ad034befe883d80ab5e96de9093d78178b41ddb Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 17:00:29 +0200 Subject: [PATCH 078/157] remove code_db debug --- db/code_db.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/db/code_db.go b/db/code_db.go index 6d92bd1..0fed13b 100644 --- a/db/code_db.go +++ b/db/code_db.go @@ -79,7 +79,6 @@ func (db *codeDB) HasCode(codeHash types.Hash) (bool, error) { // GetCode gets the code for the given hash. func (db *codeDB) GetCode(codeHash types.Hash) ([]byte, error) { - fmt.Println("GetCode", codeHash) if codeHash.IsEmpty() { return nil, ErrorEmptyHash } @@ -90,15 +89,11 @@ func (db *codeDB) GetCode(codeHash types.Hash) ([]byte, error) { return nil, fmt.Errorf("cannot get code %s: %w", codeHash, err) } - fmt.Println("GetCode", codeHash, code) - return code, nil } // PutCode creates hash for given code and inserts it into the baseDB. func (db *codeDB) PutCode(code []byte) error { - fmt.Println("PutCode", code) - codeHash := hash.Keccak256Hash(code) key := CodeDBKey(codeHash) err := db.Put(key, code) @@ -106,8 +101,6 @@ func (db *codeDB) PutCode(code []byte) error { return fmt.Errorf("cannot put code %s: %w", codeHash, err) } - fmt.Println("PutCode", code, codeHash) - return nil } From 15186b1776f40afeda28775b4355faade9e52baa Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 17:48:45 +0200 Subject: [PATCH 079/157] starting on random --- protobuf/decode.go | 7 ++++++- substate/env.go | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 3f8c645..31be899 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -152,6 +152,11 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { baseFee = new(big.Int).SetBytes(env.GetBaseFee().GetValue()) } + var random *big.Int = nil + if env.GetRandom() != nil { + random = new(big.Int).SetBytes(env.GetRandom().GetValue()) + } + var blobBaseFee *big.Int = nil if env.GetBlobBaseFee() != nil { blobBaseFee = new(big.Int).SetBytes(env.GetBlobBaseFee().GetValue()) @@ -165,7 +170,7 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { Timestamp: env.GetTimestamp(), BlockHashes: blockHashes, BaseFee: baseFee, - //Random: env.GetRandom(), // does not exist in substate.Env + Random: random, // does not exist in substate.Env BlobBaseFee: blobBaseFee, }, nil } diff --git a/substate/env.go b/substate/env.go index 2b5ae6c..7cd751d 100644 --- a/substate/env.go +++ b/substate/env.go @@ -20,6 +20,8 @@ type Env struct { BaseFee *big.Int // nil if EIP-1559 is not activated // Cancun hard fork EIP-4844 BlobBaseFee *big.Int // nil if EIP-4844 is not activated + + Random *big.Int } func NewEnv( From b56909c8007e2e03383ceedb8a938e9f0d3c18a6 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 17:53:39 +0200 Subject: [PATCH 080/157] random is now hash --- protobuf/decode.go | 2 +- substate/env.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 31be899..5630452 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -154,7 +154,7 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { var random *big.Int = nil if env.GetRandom() != nil { - random = new(big.Int).SetBytes(env.GetRandom().GetValue()) + random = types.BytesToHash(env.GetRandom().GetValue()) } var blobBaseFee *big.Int = nil diff --git a/substate/env.go b/substate/env.go index 7cd751d..6c50792 100644 --- a/substate/env.go +++ b/substate/env.go @@ -21,7 +21,7 @@ type Env struct { // Cancun hard fork EIP-4844 BlobBaseFee *big.Int // nil if EIP-4844 is not activated - Random *big.Int + Random types.Hash } func NewEnv( From f92dd00f94909b4dd8838718092dc931d39b1185 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 18:04:13 +0200 Subject: [PATCH 081/157] introduced uint256, changed difficulty, balance -> uint256 --- protobuf/decode.go | 8 ++++---- types/uint256.go | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 types/uint256.go diff --git a/protobuf/decode.go b/protobuf/decode.go index 5630452..0f72ad6 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -117,9 +117,9 @@ func (entry *Substate_AllocEntry) decode() ([]byte, *Substate_Account, error) { return entry.GetAddress(), entry.GetAccount(), nil } -func (acct *Substate_Account) decode() (uint64, *big.Int, Code, CodeHash, error) { +func (acct *Substate_Account) decode() (uint64, *uint256, Code, CodeHash, error) { return acct.GetNonce(), - new(big.Int).SetBytes(acct.GetBalance()), + types.BytesToUint256(acct.GetBalance()), acct.GetCode(), types.BytesToHash(acct.GetCodeHash()), nil @@ -144,7 +144,7 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { var diff *big.Int = nil if env.GetDifficulty() != nil { - diff = new(big.Int).SetBytes(env.GetDifficulty()) + diff = types.BytesToBigInt(env.GetDifficulty()) } var baseFee *big.Int = nil @@ -164,7 +164,7 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { return &substate.Env{ Coinbase: types.BytesToAddress(env.GetCoinbase()), - Difficulty: diff, + Difficulty: types.BytesToBigInt(env.GetDifficulty()), GasLimit: env.GetGasLimit(), Number: env.GetNumber(), Timestamp: env.GetTimestamp(), diff --git a/types/uint256.go b/types/uint256.go new file mode 100644 index 0000000..e99e8ea --- /dev/null +++ b/types/uint256.go @@ -0,0 +1,21 @@ +package types + +import ( + "github.com/holiman/uint256" +) + +// BytesToUint256 in research package strictly returns nil if b is nil +func BytesToUint256(b []byte) *uint256.Int { + if b == nil { + return nil + } + return uint256.MustFromBig(BytesToBigInt(b)) +} + +// BytesToBigInt in research package strictly returns nil if b is nil +func BytesToBigInt(b []byte) *big.Int { + if b == nil { + return nil + } + return new(big.Int).SetBytes(b) +} From 457d2fee7c059a8a179a87e8c8afb716218845b9 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 18:06:39 +0200 Subject: [PATCH 082/157] bugfix --- types/uint256.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/types/uint256.go b/types/uint256.go index e99e8ea..c1bb87e 100644 --- a/types/uint256.go +++ b/types/uint256.go @@ -1,6 +1,8 @@ package types import ( + "math/big" + "github.com/holiman/uint256" ) From 70dbe18ff4de1c5a61242d9c1698f8e062d4d69e Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 18:09:34 +0200 Subject: [PATCH 083/157] bugfix2 --- protobuf/decode.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 0f72ad6..c1460c5 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -12,6 +12,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/syndtr/goleveldb/leveldb" + "github.com/holiman/uint256" ) func (s *Substate) Dump(block uint64, tx int) error { @@ -142,17 +143,12 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { blockHashes[key] = types.BytesToHash(value) } - var diff *big.Int = nil - if env.GetDifficulty() != nil { - diff = types.BytesToBigInt(env.GetDifficulty()) - } - var baseFee *big.Int = nil if env.GetBaseFee() != nil { baseFee = new(big.Int).SetBytes(env.GetBaseFee().GetValue()) } - var random *big.Int = nil + var random *types.Hash = nil if env.GetRandom() != nil { random = types.BytesToHash(env.GetRandom().GetValue()) } From fd2e80f88187a85e7352cc7f7ff01915c420c24a Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 18:13:37 +0200 Subject: [PATCH 084/157] bugfix3 --- protobuf/decode.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index c1460c5..9291874 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -118,7 +118,7 @@ func (entry *Substate_AllocEntry) decode() ([]byte, *Substate_Account, error) { return entry.GetAddress(), entry.GetAccount(), nil } -func (acct *Substate_Account) decode() (uint64, *uint256, Code, CodeHash, error) { +func (acct *Substate_Account) decode() (uint64, *uint256.Int, Code, CodeHash, error) { return acct.GetNonce(), types.BytesToUint256(acct.GetBalance()), acct.GetCode(), @@ -148,10 +148,6 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { baseFee = new(big.Int).SetBytes(env.GetBaseFee().GetValue()) } - var random *types.Hash = nil - if env.GetRandom() != nil { - random = types.BytesToHash(env.GetRandom().GetValue()) - } var blobBaseFee *big.Int = nil if env.GetBlobBaseFee() != nil { @@ -166,7 +162,7 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { Timestamp: env.GetTimestamp(), BlockHashes: blockHashes, BaseFee: baseFee, - Random: random, // does not exist in substate.Env + Random: types.BytesToHash(env.GetRandom().GetValue()), BlobBaseFee: blobBaseFee, }, nil } From a40d9950205ca5aebf9987bfeaf5d371c617397f Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 18:18:00 +0200 Subject: [PATCH 085/157] account now accepts uint256 --- substate/account.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/substate/account.go b/substate/account.go index 594c09f..74c1b69 100644 --- a/substate/account.go +++ b/substate/account.go @@ -8,17 +8,18 @@ import ( "github.com/Fantom-foundation/Substate/types" "github.com/Fantom-foundation/Substate/types/hash" + "github.com/holiman/uint256" ) // Account holds any information about account used in a transaction. type Account struct { Nonce uint64 - Balance *big.Int + Balance *uint256.Int Storage map[types.Hash]types.Hash Code []byte } -func NewAccount(nonce uint64, balance *big.Int, code []byte) *Account { +func NewAccount(nonce uint64, balance *uint256.Int, code []byte) *Account { return &Account{ Nonce: nonce, Balance: balance, From a0a7cc13eac107f3c499d44043d57b577d9e7b4f Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 20:23:49 +0200 Subject: [PATCH 086/157] bugfix --- substate/account.go | 1 - substate/world_state.go | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/substate/account.go b/substate/account.go index 74c1b69..0d8cbe1 100644 --- a/substate/account.go +++ b/substate/account.go @@ -3,7 +3,6 @@ package substate import ( "bytes" "fmt" - "math/big" "strings" "github.com/Fantom-foundation/Substate/types" diff --git a/substate/world_state.go b/substate/world_state.go index beee654..7716674 100644 --- a/substate/world_state.go +++ b/substate/world_state.go @@ -6,6 +6,7 @@ import ( "math/big" "strings" + "github.com/holiman/uint256" "github.com/Fantom-foundation/Substate/types" ) @@ -22,7 +23,7 @@ func NewWorldState() WorldState { type WorldState map[types.Address]*Account // Add assigns new Account to an Address -func (ws WorldState) Add(addr types.Address, nonce uint64, balance *big.Int, code []byte) WorldState { +func (ws WorldState) Add(addr types.Address, nonce uint64, balance *uint256.Int, code []byte) WorldState { ws[addr] = NewAccount(nonce, balance, code) return ws } @@ -37,7 +38,7 @@ func (ws WorldState) Merge(y WorldState) { // overwrite yAcc details in ws by y ws[yAddr].Nonce = yAcc.Nonce - ws[yAddr].Balance = new(big.Int).Set(yAcc.Balance) + ws[yAddr].Balance = types.BytesToUint256(yAcc.Balance) ws[yAddr].Code = make([]byte, len(yAcc.Code)) copy(ws[yAddr].Code, yAcc.Code) } else { From 2f34ea61a20b1757f237cbaffb7a6e7f52a23533 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 20:26:09 +0200 Subject: [PATCH 087/157] bugfix2 --- substate/world_state.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/substate/world_state.go b/substate/world_state.go index 7716674..469f89b 100644 --- a/substate/world_state.go +++ b/substate/world_state.go @@ -3,7 +3,6 @@ package substate import ( "bytes" "fmt" - "math/big" "strings" "github.com/holiman/uint256" @@ -38,7 +37,7 @@ func (ws WorldState) Merge(y WorldState) { // overwrite yAcc details in ws by y ws[yAddr].Nonce = yAcc.Nonce - ws[yAddr].Balance = types.BytesToUint256(yAcc.Balance) + ws[yAddr].Balance = yAcc.Balance ws[yAddr].Code = make([]byte, len(yAcc.Code)) copy(ws[yAddr].Code, yAcc.Code) } else { From ae05f3977c9d8af9e682347a1af4133e028d2557 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 20:31:20 +0200 Subject: [PATCH 088/157] bugfix3 --- protobuf/decode.go | 1 - rlp/rlp_account.go | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 9291874..583dde4 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -148,7 +148,6 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { baseFee = new(big.Int).SetBytes(env.GetBaseFee().GetValue()) } - var blobBaseFee *big.Int = nil if env.GetBlobBaseFee() != nil { blobBaseFee = new(big.Int).SetBytes(env.GetBlobBaseFee().GetValue()) diff --git a/rlp/rlp_account.go b/rlp/rlp_account.go index 2ff254d..843fc3c 100644 --- a/rlp/rlp_account.go +++ b/rlp/rlp_account.go @@ -11,7 +11,7 @@ import ( func NewRLPAccount(acc *substate.Account) *SubstateAccountRLP { a := &SubstateAccountRLP{ Nonce: acc.Nonce, - Balance: new(big.Int).Set(acc.Balance), + Balance: types.BytesToUint256(acc.Balance), CodeHash: acc.CodeHash(), Storage: [][2]types.Hash{}, } From 30153aa620ef48e76f4059e2ef7b7d10390630a1 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 20:45:23 +0200 Subject: [PATCH 089/157] bugfix4 --- protobuf/decode.go | 2 +- rlp/rlp_account.go | 2 +- rlp/rlp_world_state.go | 6 +++++- substate/world_state.go | 2 +- types/uint256.go | 12 ++++++++++-- 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 583dde4..b86607b 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -11,8 +11,8 @@ import ( "github.com/Fantom-foundation/Substate/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" - "github.com/syndtr/goleveldb/leveldb" "github.com/holiman/uint256" + "github.com/syndtr/goleveldb/leveldb" ) func (s *Substate) Dump(block uint64, tx int) error { diff --git a/rlp/rlp_account.go b/rlp/rlp_account.go index 843fc3c..b65436f 100644 --- a/rlp/rlp_account.go +++ b/rlp/rlp_account.go @@ -11,7 +11,7 @@ import ( func NewRLPAccount(acc *substate.Account) *SubstateAccountRLP { a := &SubstateAccountRLP{ Nonce: acc.Nonce, - Balance: types.BytesToUint256(acc.Balance), + Balance: acc.Balance.ToBig(), CodeHash: acc.CodeHash(), Storage: [][2]types.Hash{}, } diff --git a/rlp/rlp_world_state.go b/rlp/rlp_world_state.go index a072010..f614ead 100644 --- a/rlp/rlp_world_state.go +++ b/rlp/rlp_world_state.go @@ -41,7 +41,11 @@ func (ws WorldState) ToSubstate(getHashFunc func(codeHash types.Hash) ([]byte, e if err != nil && !errors.Is(err, leveldb.ErrNotFound) { return nil, err } - sws[addr] = substate.NewAccount(acc.Nonce, acc.Balance, code) + sws[addr] = substate.NewAccount( + acc.Nonce, + types.BigIntToUint256(acc.Balance), + code, + ) for pos := range acc.Storage { sws[addr].Storage[acc.Storage[pos][0]] = acc.Storage[pos][1] } diff --git a/substate/world_state.go b/substate/world_state.go index 469f89b..c019378 100644 --- a/substate/world_state.go +++ b/substate/world_state.go @@ -5,8 +5,8 @@ import ( "fmt" "strings" - "github.com/holiman/uint256" "github.com/Fantom-foundation/Substate/types" + "github.com/holiman/uint256" ) const ( diff --git a/types/uint256.go b/types/uint256.go index c1bb87e..3538fb6 100644 --- a/types/uint256.go +++ b/types/uint256.go @@ -6,7 +6,7 @@ import ( "github.com/holiman/uint256" ) -// BytesToUint256 in research package strictly returns nil if b is nil +// BytesToUint256 strictly returns nil if b is nil func BytesToUint256(b []byte) *uint256.Int { if b == nil { return nil @@ -14,10 +14,18 @@ func BytesToUint256(b []byte) *uint256.Int { return uint256.MustFromBig(BytesToBigInt(b)) } -// BytesToBigInt in research package strictly returns nil if b is nil +// BytesToBigInt strictly returns nil if b is nil func BytesToBigInt(b []byte) *big.Int { if b == nil { return nil } return new(big.Int).SetBytes(b) } + +// BigIntToUint256 strictly returns nil if big is nil +func BigIntToUint256(i *big.Int) { + if i == nil { + return nil + } + return uint256.MustFromBig(i) +} From aed0349bb3a4a5f0f07bc8fb078f4b7d6df40624 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 20:47:51 +0200 Subject: [PATCH 090/157] bugfix5 --- types/uint256.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/uint256.go b/types/uint256.go index 3538fb6..85454c8 100644 --- a/types/uint256.go +++ b/types/uint256.go @@ -23,7 +23,7 @@ func BytesToBigInt(b []byte) *big.Int { } // BigIntToUint256 strictly returns nil if big is nil -func BigIntToUint256(i *big.Int) { +func BigIntToUint256(i *big.Int) *uint256.Int { if i == nil { return nil } From df500b754195e6baac462525f6f139fbd3128280 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 20:51:06 +0200 Subject: [PATCH 091/157] updateset now uses uint256 --- updateset/update_set.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/updateset/update_set.go b/updateset/update_set.go index 36f0c3f..e882d67 100644 --- a/updateset/update_set.go +++ b/updateset/update_set.go @@ -63,7 +63,7 @@ func (up UpdateSetRLP) ToWorldState(getCodeFunc func(codeHash types.Hash) ([]byt acc := substate.Account{ Nonce: worldStateAcc.Nonce, - Balance: worldStateAcc.Balance, + Balance: types.BigIntToUint256(worldStateAcc.Balance), Storage: make(map[types.Hash]types.Hash), Code: code, } From 63bbde1bb52e6252ff6491eca0e7f2d6606387fd Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 21:39:44 +0200 Subject: [PATCH 092/157] debug --- protobuf/decode.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/protobuf/decode.go b/protobuf/decode.go index b86607b..5f29611 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -41,6 +41,10 @@ type DbGetCode = func(CodeHash) (Code, error) // Decode converts protobuf-encoded Substate into aida-comprehensible substate func (s *Substate) Decode(lookup DbGetCode, block uint64, tx int) (*substate.Substate, error) { + if tx == 5 { + s.Dump(block, tx) + } + input, err := s.GetInputAlloc().decode(lookup) if err != nil { return nil, err @@ -181,6 +185,10 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro pTo = &address } + // if InitCodeHash exists: + // 1. code = lookup the code using InitCodeHash + // 2. set data -> code from (1) + // 3. clear InitCodeHash var data []byte = msg.GetData() if pTo == nil { code, err := lookup(types.BytesToHash(msg.GetInitCodeHash())) From a29f69b99bceb1325f3dd593c0205a8f6fb774c7 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 21:43:33 +0200 Subject: [PATCH 093/157] debug2 --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 5f29611..1a8c534 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -30,7 +30,7 @@ func (s *Substate) Dump(block uint64, tx int) error { jbytes, _ = json.MarshalIndent(s.GetResult(), "", " ") out += fmt.Sprintf("i:\n%s\n", jbytes) - log.Println(out) + fmt.Println(out) return nil } From 4e5fdb868bce18e825aee204b78918e80645c079 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 21:45:14 +0200 Subject: [PATCH 094/157] debug3 --- protobuf/decode.go | 1 - 1 file changed, 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 1a8c534..83da24f 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "log" "math/big" "github.com/Fantom-foundation/Substate/substate" From 32e026a158b8bb714f07b6f97b8aafdc2b9c6c2d Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 21:54:32 +0200 Subject: [PATCH 095/157] also dump substate.Substate --- protobuf/decode.go | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 83da24f..b915692 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -19,21 +19,42 @@ func (s *Substate) Dump(block uint64, tx int) error { var jbytes []byte jbytes, _ = json.MarshalIndent(s.GetInputAlloc(), "", " ") - out += fmt.Sprintf("i:\n%s\n", jbytes) + out += fmt.Sprintf("input:\n%s\n", jbytes) jbytes, _ = json.MarshalIndent(s.GetBlockEnv(), "", " ") - out += fmt.Sprintf("i:\n%s\n", jbytes) + out += fmt.Sprintf("env:\n%s\n", jbytes) jbytes, _ = json.MarshalIndent(s.GetTxMessage(), "", " ") - out += fmt.Sprintf("i:\n%s\n", jbytes) + out += fmt.Sprintf("msg:\n%s\n", jbytes) jbytes, _ = json.MarshalIndent(s.GetOutputAlloc(), "", " ") - out += fmt.Sprintf("i:\n%s\n", jbytes) + out += fmt.Sprintf("output:\n%s\n", jbytes) jbytes, _ = json.MarshalIndent(s.GetResult(), "", " ") - out += fmt.Sprintf("i:\n%s\n", jbytes) + out += fmt.Sprintf("result:\n%s\n", jbytes) fmt.Println(out) return nil } +func (s *substate.Substate) Dump(block uint64, tx int) error { + out := fmt.Sprintf("decoded block: %v Transaction: %v\n", block, tx) + + var jbytes []byte + jbytes, _ = json.MarshalIndent(s.InputSubstate, "", " ") + out += fmt.Sprintf("input:\n%s\n", jbytes) + jbytes, _ = json.MarshalIndent(s.Env, "", " ") + out += fmt.Sprintf("env:\n%s\n", jbytes) + jbytes, _ = json.MarshalIndent(s.Message, "", " ") + out += fmt.Sprintf("msg:\n%s\n", jbytes) + jbytes, _ = json.MarshalIndent(s.OutputSubstate, "", " ") + out += fmt.Sprintf("output:\n%s\n", jbytes) + jbytes, _ = json.MarshalIndent(s.Result, "", " ") + out += fmt.Sprintf("result:\n%s\n", jbytes) + + fmt.Println(out) + + return nil +} + + type CodeHash = types.Hash type Code = []byte type DbGetCode = func(CodeHash) (Code, error) From f5481c6955a979a3e743ee74107e70ff822d98ff Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 21:56:48 +0200 Subject: [PATCH 096/157] also dump substate.Substate2 --- protobuf/decode.go | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index b915692..fa70a0e 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -34,27 +34,6 @@ func (s *Substate) Dump(block uint64, tx int) error { return nil } -func (s *substate.Substate) Dump(block uint64, tx int) error { - out := fmt.Sprintf("decoded block: %v Transaction: %v\n", block, tx) - - var jbytes []byte - jbytes, _ = json.MarshalIndent(s.InputSubstate, "", " ") - out += fmt.Sprintf("input:\n%s\n", jbytes) - jbytes, _ = json.MarshalIndent(s.Env, "", " ") - out += fmt.Sprintf("env:\n%s\n", jbytes) - jbytes, _ = json.MarshalIndent(s.Message, "", " ") - out += fmt.Sprintf("msg:\n%s\n", jbytes) - jbytes, _ = json.MarshalIndent(s.OutputSubstate, "", " ") - out += fmt.Sprintf("output:\n%s\n", jbytes) - jbytes, _ = json.MarshalIndent(s.Result, "", " ") - out += fmt.Sprintf("result:\n%s\n", jbytes) - - fmt.Println(out) - - return nil -} - - type CodeHash = types.Hash type Code = []byte type DbGetCode = func(CodeHash) (Code, error) @@ -91,7 +70,7 @@ func (s *Substate) Decode(lookup DbGetCode, block uint64, tx int) (*substate.Sub return nil, err } - return &substate.Substate{ + s := &substate.Substate{ InputSubstate: *input, OutputSubstate: *output, Env: environment, @@ -99,7 +78,10 @@ func (s *Substate) Decode(lookup DbGetCode, block uint64, tx int) (*substate.Sub Result: result, Block: block, Transaction: tx, - }, nil + } + + fmt.Println(s) + return s, nil } // decode converts protobuf-encoded Substate_Alloc into aida-comprehensible WorldState From 7ad5f56c9f075a66e6cfc7b1f982671f1dfb86e4 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 21:58:59 +0200 Subject: [PATCH 097/157] also dump substate.Substate3 --- protobuf/decode.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index fa70a0e..6082b9a 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -70,7 +70,7 @@ func (s *Substate) Decode(lookup DbGetCode, block uint64, tx int) (*substate.Sub return nil, err } - s := &substate.Substate{ + stest := &substate.Substate{ InputSubstate: *input, OutputSubstate: *output, Env: environment, @@ -80,8 +80,8 @@ func (s *Substate) Decode(lookup DbGetCode, block uint64, tx int) (*substate.Sub Transaction: tx, } - fmt.Println(s) - return s, nil + fmt.Println(stest) + return stest, nil } // decode converts protobuf-encoded Substate_Alloc into aida-comprehensible WorldState From 95af7111cde228cfc5c5cabb2920f7194c500bb1 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 22:03:12 +0200 Subject: [PATCH 098/157] dump using string did not work --- protobuf/decode.go | 5 ++++- substate/substate.go | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 6082b9a..122913e 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -80,7 +80,10 @@ func (s *Substate) Decode(lookup DbGetCode, block uint64, tx int) (*substate.Sub Transaction: tx, } - fmt.Println(stest) + if tx == 5 { + stest.Dump(block, tx) + } + return stest, nil } diff --git a/substate/substate.go b/substate/substate.go index 85244bd..3a650b1 100644 --- a/substate/substate.go +++ b/substate/substate.go @@ -79,3 +79,23 @@ func (s *Substate) String() string { return builder.String() } + +func (s *Substate) Dump(block uint64, tx int) error { + out := fmt.Sprintf("decoded block: %v Transaction: %v\n", block, tx) + + var jbytes []byte + jbytes, _ = json.MarshalIndent(s.InputSubstate, "", " ") + out += fmt.Sprintf("input:\n%s\n", jbytes) + jbytes, _ = json.MarshalIndent(s.Env, "", " ") + out += fmt.Sprintf("env:\n%s\n", jbytes) + jbytes, _ = json.MarshalIndent(s.Message, "", " ") + out += fmt.Sprintf("msg:\n%s\n", jbytes) + jbytes, _ = json.MarshalIndent(s.OutputSubstate, "", " ") + out += fmt.Sprintf("output:\n%s\n", jbytes) + jbytes, _ = json.MarshalIndent(s.GetResult(), "", " ") + out += fmt.Sprintf("result:\n%s\n", jbytes) + + fmt.Println(out) + + return nil +} From 03553c3b4eaaf622e173e0181a5fcf4c739c472a Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 10 Sep 2024 22:05:01 +0200 Subject: [PATCH 099/157] encoding/json --- substate/substate.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/substate/substate.go b/substate/substate.go index 3a650b1..6a1fa7c 100644 --- a/substate/substate.go +++ b/substate/substate.go @@ -1,6 +1,7 @@ package substate import ( + "encoding/json" "errors" "fmt" "strings" @@ -92,7 +93,7 @@ func (s *Substate) Dump(block uint64, tx int) error { out += fmt.Sprintf("msg:\n%s\n", jbytes) jbytes, _ = json.MarshalIndent(s.OutputSubstate, "", " ") out += fmt.Sprintf("output:\n%s\n", jbytes) - jbytes, _ = json.MarshalIndent(s.GetResult(), "", " ") + jbytes, _ = json.MarshalIndent(s.Result, "", " ") out += fmt.Sprintf("result:\n%s\n", jbytes) fmt.Println(out) From b46a819877c68400463f6342e1c667aa14d574e3 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 11 Sep 2024 12:14:15 +0200 Subject: [PATCH 100/157] uint 256 from byte now direct --- types/uint256.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/uint256.go b/types/uint256.go index 85454c8..4a2c5a9 100644 --- a/types/uint256.go +++ b/types/uint256.go @@ -11,7 +11,7 @@ func BytesToUint256(b []byte) *uint256.Int { if b == nil { return nil } - return uint256.MustFromBig(BytesToBigInt(b)) + return new(uint256.Int).SetBytes(b) } // BytesToBigInt strictly returns nil if b is nil From 922e1363a1bd1ce170e89127b1d33722aa5d88d9 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 11 Sep 2024 13:25:06 +0200 Subject: [PATCH 101/157] remove debug, blockhashes now nil if pb is nil --- protobuf/decode.go | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 122913e..b1fce23 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -40,10 +40,6 @@ type DbGetCode = func(CodeHash) (Code, error) // Decode converts protobuf-encoded Substate into aida-comprehensible substate func (s *Substate) Decode(lookup DbGetCode, block uint64, tx int) (*substate.Substate, error) { - if tx == 5 { - s.Dump(block, tx) - } - input, err := s.GetInputAlloc().decode(lookup) if err != nil { return nil, err @@ -70,7 +66,7 @@ func (s *Substate) Decode(lookup DbGetCode, block uint64, tx int) (*substate.Sub return nil, err } - stest := &substate.Substate{ + return &substate.Substate{ InputSubstate: *input, OutputSubstate: *output, Env: environment, @@ -78,13 +74,7 @@ func (s *Substate) Decode(lookup DbGetCode, block uint64, tx int) (*substate.Sub Result: result, Block: block, Transaction: tx, - } - - if tx == 5 { - stest.Dump(block, tx) - } - - return stest, nil + }, nil } // decode converts protobuf-encoded Substate_Alloc into aida-comprehensible WorldState @@ -143,13 +133,16 @@ func (entry *Substate_Account_StorageEntry) decode() (types.Hash, types.Hash, er // decode converts protobuf-encoded Substate_BlockEnv into aida-comprehensible Env func (env *Substate_BlockEnv) decode() (*substate.Env, error) { - blockHashes := make(map[uint64]types.Hash, len(env.GetBlockHashes())) - for _, entry := range env.GetBlockHashes() { - key, value, err := entry.decode() - if err != nil { - return nil, err + var blockHashes map[uint64]types.Hash = nil + if env.GetBlockHashes() != nil { + blockHashes := make(map[uint64]types.Hash, len(env.GetBlockHashes())) + for _, entry := range env.GetBlockHashes() { + key, value, err := entry.decode() + if err != nil { + return nil, err + } + blockHashes[key] = types.BytesToHash(value) } - blockHashes[key] = types.BytesToHash(value) } var baseFee *big.Int = nil From 9ba49e1b071903afb921fb60a5bf932d1b31b51c Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 11 Sep 2024 13:26:48 +0200 Subject: [PATCH 102/157] difficulty now nil if pb is nil --- protobuf/decode.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index b1fce23..23577ae 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -133,6 +133,11 @@ func (entry *Substate_Account_StorageEntry) decode() (types.Hash, types.Hash, er // decode converts protobuf-encoded Substate_BlockEnv into aida-comprehensible Env func (env *Substate_BlockEnv) decode() (*substate.Env, error) { + var difficulty big.Int = nil + if env.GetDifficulty() != nil { + difficulty = types.BytesToBigInt(env.GetDifficulty()) + } + var blockHashes map[uint64]types.Hash = nil if env.GetBlockHashes() != nil { blockHashes := make(map[uint64]types.Hash, len(env.GetBlockHashes())) @@ -157,7 +162,7 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { return &substate.Env{ Coinbase: types.BytesToAddress(env.GetCoinbase()), - Difficulty: types.BytesToBigInt(env.GetDifficulty()), + Difficulty: difficulty, GasLimit: env.GetGasLimit(), Number: env.GetNumber(), Timestamp: env.GetTimestamp(), From 050ea670aaf216d72f03c5b76cdbf0c20b154074 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 11 Sep 2024 15:27:41 +0200 Subject: [PATCH 103/157] difficulty now *big.Int --- protobuf/decode.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 23577ae..e0c565b 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -133,7 +133,7 @@ func (entry *Substate_Account_StorageEntry) decode() (types.Hash, types.Hash, er // decode converts protobuf-encoded Substate_BlockEnv into aida-comprehensible Env func (env *Substate_BlockEnv) decode() (*substate.Env, error) { - var difficulty big.Int = nil + var difficulty *big.Int = nil if env.GetDifficulty() != nil { difficulty = types.BytesToBigInt(env.GetDifficulty()) } @@ -152,12 +152,12 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { var baseFee *big.Int = nil if env.GetBaseFee() != nil { - baseFee = new(big.Int).SetBytes(env.GetBaseFee().GetValue()) + baseFee = types.BytesToBigInt(env.GetBaseFee().GetValue()) } var blobBaseFee *big.Int = nil if env.GetBlobBaseFee() != nil { - blobBaseFee = new(big.Int).SetBytes(env.GetBlobBaseFee().GetValue()) + blobBaseFee = types.BytesToBigInt(env.GetBlobBaseFee().GetValue()) } return &substate.Env{ From 258ec068e078640779ea08f9d2e48ac057f71062 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 08:38:00 +0200 Subject: [PATCH 104/157] gasfeecap, gastipcap now uses txMsg, utils --- protobuf/decode.go | 14 ++++++++++---- protobuf/utils.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 protobuf/utils.go diff --git a/protobuf/decode.go b/protobuf/decode.go index e0c565b..442bcd3 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -201,6 +201,8 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro data = code } + txType := msg.GetTxType() + // Berlin hard fork, EIP-2930: Optional access lists var accessList types.AccessList = nil // nil if EIP-2930 is not activated if msg.GetAccessList() != nil { @@ -226,14 +228,18 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro // London hard fork, EIP-1559: Fee market var gasFeeCap *big.Int = new(big.Int).SetBytes(msg.GetGasPrice()) - if msg.GetGasFeeCap() != nil { - gasFeeCap = new(big.Int).SetBytes(msg.GetGasFeeCap().GetValue()) + switch txType { + case Substate_TxMessage_TXTYPE_DYNAMICFEE: + case Substate_TxMessage_TXTYPE_BLOB: + gasFeeCap = BytesValueToBigInt(msg.GetGasFeeCap()) } var gasTipCap *big.Int = new(big.Int).SetBytes(msg.GetGasPrice()) - if msg.GetGasTipCap() != nil { - gasTipCap = new(big.Int).SetBytes(msg.GetGasTipCap().GetValue()) + case Substate_TxMessage_TXTYPE_DYNAMICFEE: + case Substate_TxMessage_TXTYPE_BLOB: + gasTipCap = BytesValueToBigInt(msg.GetGasTipCap()) } + // Cancun hard fork, EIP-4844 var blobGasFeeCap *big.Int = nil diff --git a/protobuf/utils.go b/protobuf/utils.go new file mode 100644 index 0000000..00cad59 --- /dev/null +++ b/protobuf/utils.go @@ -0,0 +1,31 @@ +package protobuf + +import ( + "math/big" + + wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" + "github.com/Fantom-foundation/Substate/types" +) + +func BytesValueToHash(bv *wrapperspb.BytesValue) *types.Hash { + if bv == nil { + return nil + } + hash := types.BytesToHash(bv.GetValue()) + return &hash +} + +func BytesValueToBigInt(bv *wrapperspb.BytesValue) *big.Int { + if bv == nil { + return nil + } + return new(big.Int).SetBytes(bv.GetValue()) +} + +func BytesValueToAddress(bv *wrapperspb.BytesValue) *types.Address { + if bv == nil { + return nil + } + addr := types.BytesToAddress(bv.GetValue()) + return &addr +} From 750da030f93eb97449bdec6b37502c6fa44c0530 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 08:44:57 +0200 Subject: [PATCH 105/157] switch txType --- protobuf/decode.go | 1 + 1 file changed, 1 insertion(+) diff --git a/protobuf/decode.go b/protobuf/decode.go index 442bcd3..c1523e4 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -235,6 +235,7 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro } var gasTipCap *big.Int = new(big.Int).SetBytes(msg.GetGasPrice()) + switch txType { case Substate_TxMessage_TXTYPE_DYNAMICFEE: case Substate_TxMessage_TXTYPE_BLOB: gasTipCap = BytesValueToBigInt(msg.GetGasTipCap()) From 173385c735d57c8927856a4517dda288285676cd Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 08:49:20 +0200 Subject: [PATCH 106/157] blobgasfeecap now uses bytesvalue --- protobuf/decode.go | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index c1523e4..b55689d 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -227,14 +227,14 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro } // London hard fork, EIP-1559: Fee market - var gasFeeCap *big.Int = new(big.Int).SetBytes(msg.GetGasPrice()) + var gasFeeCap *big.Int = types.BytesToBigInt(msg.GetGasPrice()) switch txType { case Substate_TxMessage_TXTYPE_DYNAMICFEE: case Substate_TxMessage_TXTYPE_BLOB: gasFeeCap = BytesValueToBigInt(msg.GetGasFeeCap()) } - var gasTipCap *big.Int = new(big.Int).SetBytes(msg.GetGasPrice()) + var gasTipCap *big.Int = types.BytesToBigInt(msg.GetGasPrice()) switch txType { case Substate_TxMessage_TXTYPE_DYNAMICFEE: case Substate_TxMessage_TXTYPE_BLOB: @@ -243,11 +243,6 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro // Cancun hard fork, EIP-4844 - var blobGasFeeCap *big.Int = nil - if msg.GetBlobGasFeeCap() != nil { - blobGasFeeCap = new(big.Int).SetBytes(msg.GetBlobGasFeeCap().GetValue()) - } - blobHashes := make([]types.Hash, len(msg.GetBlobHashes())) for i, hash := range msg.GetBlobHashes() { blobHashes[i] = types.BytesToHash(hash) @@ -256,16 +251,16 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro return &substate.Message{ Nonce: msg.GetNonce(), CheckNonce: true, - GasPrice: new(big.Int).SetBytes(msg.GetGasPrice()), + GasPrice: types.BytesToBigInt(msg.GetGasPrice()), Gas: msg.GetGas(), From: types.BytesToAddress(msg.GetFrom()), To: pTo, - Value: new(big.Int).SetBytes(msg.GetValue()), + Value: types.BytesToBigInt(msg.GetValue()), Data: data, AccessList: accessList, GasFeeCap: gasFeeCap, GasTipCap: gasTipCap, - BlobGasFeeCap: blobGasFeeCap, + BlobGasFeeCap: BytesValueToBigInt(msg.GetBlobGasFeeCap()), BlobHashes: blobHashes, }, nil } From 00967de0c12b98af8df10e318e2699700c56e2c9 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 09:01:43 +0200 Subject: [PATCH 107/157] blobhashes now uses txType --- protobuf/decode.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index b55689d..932ddd5 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -241,11 +241,16 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro gasTipCap = BytesValueToBigInt(msg.GetGasTipCap()) } - // Cancun hard fork, EIP-4844 - blobHashes := make([]types.Hash, len(msg.GetBlobHashes())) - for i, hash := range msg.GetBlobHashes() { - blobHashes[i] = types.BytesToHash(hash) + var blobHashes = []types.Hash = nil + switch txType { + case Substate_TxMessage_TXTYPE_BLOB: + if msg.GetBlobHashes() != nil { + blobHashes := make([]types.Hash, len(msg.GetBlobHashes())) + for i, hash := range msg.GetBlobHashes() { + blobHashes[i] = types.BytesToHash(hash) + } + } } return &substate.Message{ From ffbfc83a35c5a4835ac1dd48a75c3ef85de63d40 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 09:03:50 +0200 Subject: [PATCH 108/157] remove extraneous = --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 932ddd5..d765b65 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -242,7 +242,7 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro } // Cancun hard fork, EIP-4844 - var blobHashes = []types.Hash = nil + var blobHashes []types.Hash = nil switch txType { case Substate_TxMessage_TXTYPE_BLOB: if msg.GetBlobHashes() != nil { From cbd5dfe66ae86bef642f3593dfefa034d679aa66 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 09:34:12 +0200 Subject: [PATCH 109/157] debug gfc, gtp --- protobuf/decode.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/protobuf/decode.go b/protobuf/decode.go index d765b65..c088b36 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -234,12 +234,22 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro gasFeeCap = BytesValueToBigInt(msg.GetGasFeeCap()) } + fmt.Println("========= gasfeecap: ", gasFeeCap) + fmt.Println("txType: ", txType) + fmt.Println("ggp: ", types.BytesToBigInt(msg.GetGasPrice())) + fmt.Println("gfp: ", BytesValueToBigInt(msg.GetGasFeeCap())) + + var gasTipCap *big.Int = types.BytesToBigInt(msg.GetGasPrice()) switch txType { case Substate_TxMessage_TXTYPE_DYNAMICFEE: case Substate_TxMessage_TXTYPE_BLOB: gasTipCap = BytesValueToBigInt(msg.GetGasTipCap()) } + fmt.Println("========= gastipcap: ", gasTipCap) + fmt.Println("txType: ", txType) + fmt.Println("ggp: ", types.BytesToBigInt(msg.GetGasPrice())) + fmt.Println("gtp: ", BytesValueToBigInt(msg.GetGasTipCap())) // Cancun hard fork, EIP-4844 var blobHashes []types.Hash = nil From 3f3afcb76e97c50a3264fd0a2f87b426fd76d19c Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 10:04:37 +0200 Subject: [PATCH 110/157] debug gfc, gtp2 --- protobuf/decode.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index c088b36..0e06687 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -236,9 +236,10 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro fmt.Println("========= gasfeecap: ", gasFeeCap) fmt.Println("txType: ", txType) + fmt.Println("pre-ggp: ", msg.GetGasPrice()) fmt.Println("ggp: ", types.BytesToBigInt(msg.GetGasPrice())) + fmt.Println("pre-gfp: ", msg.GetGasFeeCap()) fmt.Println("gfp: ", BytesValueToBigInt(msg.GetGasFeeCap())) - var gasTipCap *big.Int = types.BytesToBigInt(msg.GetGasPrice()) switch txType { @@ -248,7 +249,9 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro } fmt.Println("========= gastipcap: ", gasTipCap) fmt.Println("txType: ", txType) + fmt.Println("pre-ggp: ", msg.GetGasPrice()) fmt.Println("ggp: ", types.BytesToBigInt(msg.GetGasPrice())) + fmt.Println("pre-ggp: ", msg.GetGasTipCap()) fmt.Println("gtp: ", BytesValueToBigInt(msg.GetGasTipCap())) // Cancun hard fork, EIP-4844 From 9cf590f8058955f7865d1adbdd07bec14e794fc7 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 10:34:17 +0200 Subject: [PATCH 111/157] debug gfc, gtp3 --- protobuf/decode.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 0e06687..996a630 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -236,9 +236,9 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro fmt.Println("========= gasfeecap: ", gasFeeCap) fmt.Println("txType: ", txType) - fmt.Println("pre-ggp: ", msg.GetGasPrice()) + fmt.Println("txType d: ", Substate_TxMessage_TXTYPE_DYNAMICFEE) + fmt.Println("txType b: ", Substate_TxMessage_TXTYPE_BLOB) fmt.Println("ggp: ", types.BytesToBigInt(msg.GetGasPrice())) - fmt.Println("pre-gfp: ", msg.GetGasFeeCap()) fmt.Println("gfp: ", BytesValueToBigInt(msg.GetGasFeeCap())) var gasTipCap *big.Int = types.BytesToBigInt(msg.GetGasPrice()) From e5568f76c756522f62c3fd53718489d5f0cccf4c Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 10:37:55 +0200 Subject: [PATCH 112/157] debug gfc, gtp4 --- protobuf/decode.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 996a630..5ba1b1b 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -231,7 +231,9 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro switch txType { case Substate_TxMessage_TXTYPE_DYNAMICFEE: case Substate_TxMessage_TXTYPE_BLOB: + fmt.Println("I'm doing this!") gasFeeCap = BytesValueToBigInt(msg.GetGasFeeCap()) + fmt.Println("gfp: " gasFeeCap), } fmt.Println("========= gasfeecap: ", gasFeeCap) @@ -251,7 +253,7 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro fmt.Println("txType: ", txType) fmt.Println("pre-ggp: ", msg.GetGasPrice()) fmt.Println("ggp: ", types.BytesToBigInt(msg.GetGasPrice())) - fmt.Println("pre-ggp: ", msg.GetGasTipCap()) + fmt.Println("pre-gtp: ", msg.GetGasTipCap()) fmt.Println("gtp: ", BytesValueToBigInt(msg.GetGasTipCap())) // Cancun hard fork, EIP-4844 From b18308b6dfd08b2857381279226fe1a0ae907989 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 10:39:41 +0200 Subject: [PATCH 113/157] debug gfc, gtp5 --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 5ba1b1b..ac4a7d5 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -233,7 +233,7 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro case Substate_TxMessage_TXTYPE_BLOB: fmt.Println("I'm doing this!") gasFeeCap = BytesValueToBigInt(msg.GetGasFeeCap()) - fmt.Println("gfp: " gasFeeCap), + fmt.Println("gfp: ", gasFeeCap) } fmt.Println("========= gasfeecap: ", gasFeeCap) From e8a3ec02e239183c074132c9f1ee78d5be6660e8 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 10:49:52 +0200 Subject: [PATCH 114/157] debug gfc, gtp6 --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index ac4a7d5..f373a9d 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -228,7 +228,7 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro // London hard fork, EIP-1559: Fee market var gasFeeCap *big.Int = types.BytesToBigInt(msg.GetGasPrice()) - switch txType { + switch t := *t.TxType; t { case Substate_TxMessage_TXTYPE_DYNAMICFEE: case Substate_TxMessage_TXTYPE_BLOB: fmt.Println("I'm doing this!") From 43b6cad929179f7624dde83a0ab53451889c2aa4 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 10:52:08 +0200 Subject: [PATCH 115/157] debug gfc, gtp7 --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index f373a9d..3da778c 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -228,7 +228,7 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro // London hard fork, EIP-1559: Fee market var gasFeeCap *big.Int = types.BytesToBigInt(msg.GetGasPrice()) - switch t := *t.TxType; t { + switch txTyp := *t.TxType; txType { case Substate_TxMessage_TXTYPE_DYNAMICFEE: case Substate_TxMessage_TXTYPE_BLOB: fmt.Println("I'm doing this!") From 238c27e3c5a55f638c44629229d360c531a8309f Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 10:52:20 +0200 Subject: [PATCH 116/157] debug gfc, gtp7 --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 3da778c..a1bf894 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -228,7 +228,7 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro // London hard fork, EIP-1559: Fee market var gasFeeCap *big.Int = types.BytesToBigInt(msg.GetGasPrice()) - switch txTyp := *t.TxType; txType { + switch txTyp := *t.TxType; txTyp { case Substate_TxMessage_TXTYPE_DYNAMICFEE: case Substate_TxMessage_TXTYPE_BLOB: fmt.Println("I'm doing this!") From cf057b37e66df56fdadf2a84310041eecd84bbd5 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 10:54:11 +0200 Subject: [PATCH 117/157] debug gfc, gtp8 --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index a1bf894..e44e61c 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -228,7 +228,7 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro // London hard fork, EIP-1559: Fee market var gasFeeCap *big.Int = types.BytesToBigInt(msg.GetGasPrice()) - switch txTyp := *t.TxType; txTyp { + switch txTyp := *msg.TxType; txTyp { case Substate_TxMessage_TXTYPE_DYNAMICFEE: case Substate_TxMessage_TXTYPE_BLOB: fmt.Println("I'm doing this!") From 655e83992f60f38aab34519392c8fe587b8da801 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 11:08:30 +0200 Subject: [PATCH 118/157] debug txtype --- protobuf/decode.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index e44e61c..41045c0 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -201,7 +201,18 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro data = code } - txType := msg.GetTxType() + var txType uint8 = 0 // txType defaults to TXTYPE_LEGACY + switch x := *msg.TxType; x { + case Substate_TxMessage_TXTYPE_ACCESSLIST: + txType = 1 + case Substate_TxMessage_TXTYPE_DYNAMICFEE: + txType = 2 + case Substate_TxMessage_TXTYPE_BLOB: + txType = 3 + } + + fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>", txType) + // Berlin hard fork, EIP-2930: Optional access lists var accessList types.AccessList = nil // nil if EIP-2930 is not activated @@ -228,7 +239,7 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro // London hard fork, EIP-1559: Fee market var gasFeeCap *big.Int = types.BytesToBigInt(msg.GetGasPrice()) - switch txTyp := *msg.TxType; txTyp { + switch txTyp := msg.GetTxType(); txTyp { case Substate_TxMessage_TXTYPE_DYNAMICFEE: case Substate_TxMessage_TXTYPE_BLOB: fmt.Println("I'm doing this!") From fdd8ee411375a2392b5bceadcd19b071542895bd Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 11:11:13 +0200 Subject: [PATCH 119/157] debug txtype2 --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 41045c0..0d6f259 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -255,7 +255,7 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro fmt.Println("gfp: ", BytesValueToBigInt(msg.GetGasFeeCap())) var gasTipCap *big.Int = types.BytesToBigInt(msg.GetGasPrice()) - switch txType { + switch txTyp := msg.GetTxType(); txTyp { case Substate_TxMessage_TXTYPE_DYNAMICFEE: case Substate_TxMessage_TXTYPE_BLOB: gasTipCap = BytesValueToBigInt(msg.GetGasTipCap()) From b83802b5e26c4c4000c45193704f0c9a4a8ee85c Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 11:12:59 +0200 Subject: [PATCH 120/157] debug txtype3 --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 0d6f259..8096b65 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -269,7 +269,7 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro // Cancun hard fork, EIP-4844 var blobHashes []types.Hash = nil - switch txType { + switch txTyp := msg.GetTxType(); txTyp { case Substate_TxMessage_TXTYPE_BLOB: if msg.GetBlobHashes() != nil { blobHashes := make([]types.Hash, len(msg.GetBlobHashes())) From f0248b2a81448fde7b829aafb8036779e65ed88a Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 11:25:09 +0200 Subject: [PATCH 121/157] txtype now uint8 --- protobuf/decode.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 8096b65..ee77d6c 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -239,9 +239,8 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro // London hard fork, EIP-1559: Fee market var gasFeeCap *big.Int = types.BytesToBigInt(msg.GetGasPrice()) - switch txTyp := msg.GetTxType(); txTyp { - case Substate_TxMessage_TXTYPE_DYNAMICFEE: - case Substate_TxMessage_TXTYPE_BLOB: + switch txType { + case 2, 3: fmt.Println("I'm doing this!") gasFeeCap = BytesValueToBigInt(msg.GetGasFeeCap()) fmt.Println("gfp: ", gasFeeCap) From e25f400e3bad3b393794ed28f2c56bed20135a16 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 11:35:06 +0200 Subject: [PATCH 122/157] gtp, blobhashes now uses uint8 txtype --- protobuf/decode.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index ee77d6c..05f9e5a 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -254,9 +254,8 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro fmt.Println("gfp: ", BytesValueToBigInt(msg.GetGasFeeCap())) var gasTipCap *big.Int = types.BytesToBigInt(msg.GetGasPrice()) - switch txTyp := msg.GetTxType(); txTyp { - case Substate_TxMessage_TXTYPE_DYNAMICFEE: - case Substate_TxMessage_TXTYPE_BLOB: + switch txType { + case 2, 3: gasTipCap = BytesValueToBigInt(msg.GetGasTipCap()) } fmt.Println("========= gastipcap: ", gasTipCap) @@ -268,8 +267,8 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro // Cancun hard fork, EIP-4844 var blobHashes []types.Hash = nil - switch txTyp := msg.GetTxType(); txTyp { - case Substate_TxMessage_TXTYPE_BLOB: + switch txType { + case 3: if msg.GetBlobHashes() != nil { blobHashes := make([]types.Hash, len(msg.GetBlobHashes())) for i, hash := range msg.GetBlobHashes() { From f13cc9e3b09533febb0acdc4d4b5d59fc2f9cf80 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 11:47:14 +0200 Subject: [PATCH 123/157] accessList now also uses txType --- protobuf/decode.go | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 05f9e5a..aa9083a 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -212,28 +212,28 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro } fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>", txType) - // Berlin hard fork, EIP-2930: Optional access lists var accessList types.AccessList = nil // nil if EIP-2930 is not activated - if msg.GetAccessList() != nil { - accessList = make([]types.AccessTuple, len(msg.GetAccessList())) + switch txType { + case 2, 3: + accessList = make([]types.AccessTuple, len(msg.GetAccessList()) for i, entry := range msg.GetAccessList() { addr, keys, err := entry.decode() if err != nil { return nil, err } + } - address := types.BytesToAddress(addr) - storageKeys := make([]types.Hash, len(keys)) - for j, key := range keys { - storageKeys[j] = types.BytesToHash(key) - } + address := types.BytesToAddress(addr) + storageKeys := make([]types.Hash, len(keys)) + for j, key := range keys { + storageKeys[j] = types.BytesToHash(key) + } - accessList[i] = types.AccessTuple{ - Address: address, - StorageKeys: storageKeys, - } + accessList[i] = types.AccessTuple { + Address: address, + StorageKeys: storageKeys, } } @@ -246,24 +246,11 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro fmt.Println("gfp: ", gasFeeCap) } - fmt.Println("========= gasfeecap: ", gasFeeCap) - fmt.Println("txType: ", txType) - fmt.Println("txType d: ", Substate_TxMessage_TXTYPE_DYNAMICFEE) - fmt.Println("txType b: ", Substate_TxMessage_TXTYPE_BLOB) - fmt.Println("ggp: ", types.BytesToBigInt(msg.GetGasPrice())) - fmt.Println("gfp: ", BytesValueToBigInt(msg.GetGasFeeCap())) - var gasTipCap *big.Int = types.BytesToBigInt(msg.GetGasPrice()) switch txType { case 2, 3: gasTipCap = BytesValueToBigInt(msg.GetGasTipCap()) } - fmt.Println("========= gastipcap: ", gasTipCap) - fmt.Println("txType: ", txType) - fmt.Println("pre-ggp: ", msg.GetGasPrice()) - fmt.Println("ggp: ", types.BytesToBigInt(msg.GetGasPrice())) - fmt.Println("pre-gtp: ", msg.GetGasTipCap()) - fmt.Println("gtp: ", BytesValueToBigInt(msg.GetGasTipCap())) // Cancun hard fork, EIP-4844 var blobHashes []types.Hash = nil From c447c59da6d204d7ab455841f1f1420724bff3f4 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 11:50:54 +0200 Subject: [PATCH 124/157] accessList debug --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index aa9083a..115c086 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -217,7 +217,7 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro var accessList types.AccessList = nil // nil if EIP-2930 is not activated switch txType { case 2, 3: - accessList = make([]types.AccessTuple, len(msg.GetAccessList()) + accessList = make([]types.AccessTuple, len(msg.GetAccessList())) for i, entry := range msg.GetAccessList() { addr, keys, err := entry.decode() if err != nil { From bbc8fa1c10b06458aebda43a3020ff32b1bf2a8c Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 11:53:02 +0200 Subject: [PATCH 125/157] accessList debug2 --- protobuf/decode.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 115c086..5e14411 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -223,17 +223,17 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro if err != nil { return nil, err } - } - address := types.BytesToAddress(addr) - storageKeys := make([]types.Hash, len(keys)) - for j, key := range keys { - storageKeys[j] = types.BytesToHash(key) - } + address := types.BytesToAddress(addr) + storageKeys := make([]types.Hash, len(keys)) + for j, key := range keys { + storageKeys[j] = types.BytesToHash(key) + } - accessList[i] = types.AccessTuple { - Address: address, - StorageKeys: storageKeys, + accessList[i] = types.AccessTuple { + Address: address, + StorageKeys: storageKeys, + } } } From f749dd26f4d990d7683d92a7d10c8e57227e132f Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 11:58:38 +0200 Subject: [PATCH 126/157] accessList debug3 --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 5e14411..94fb5f6 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -216,7 +216,7 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro // Berlin hard fork, EIP-2930: Optional access lists var accessList types.AccessList = nil // nil if EIP-2930 is not activated switch txType { - case 2, 3: + case 1, 2, 3: accessList = make([]types.AccessTuple, len(msg.GetAccessList())) for i, entry := range msg.GetAccessList() { addr, keys, err := entry.decode() From 7d05def95b97929a0c60874fc7398bbefbb247ce Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 12:14:50 +0200 Subject: [PATCH 127/157] message confirmed exactly the same --- protobuf/decode.go | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 94fb5f6..c5f23bb 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -201,6 +201,9 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro data = code } + // TODO: using this switch directly does not produce expected result + // For some reason, an intermediate enum works + // To be figured out and removed var txType uint8 = 0 // txType defaults to TXTYPE_LEGACY switch x := *msg.TxType; x { case Substate_TxMessage_TXTYPE_ACCESSLIST: @@ -211,8 +214,6 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro txType = 3 } - fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>", txType) - // Berlin hard fork, EIP-2930: Optional access lists var accessList types.AccessList = nil // nil if EIP-2930 is not activated switch txType { @@ -239,16 +240,10 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro // London hard fork, EIP-1559: Fee market var gasFeeCap *big.Int = types.BytesToBigInt(msg.GetGasPrice()) - switch txType { - case 2, 3: - fmt.Println("I'm doing this!") - gasFeeCap = BytesValueToBigInt(msg.GetGasFeeCap()) - fmt.Println("gfp: ", gasFeeCap) - } - var gasTipCap *big.Int = types.BytesToBigInt(msg.GetGasPrice()) switch txType { case 2, 3: + gasFeeCap = BytesValueToBigInt(msg.GetGasFeeCap()) gasTipCap = BytesValueToBigInt(msg.GetGasTipCap()) } From e0954ab1833517703f90536bbbff927b74926cb8 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 12:36:43 +0200 Subject: [PATCH 128/157] random now using bytesvaluetohash --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index c5f23bb..4b3302c 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -168,7 +168,7 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { Timestamp: env.GetTimestamp(), BlockHashes: blockHashes, BaseFee: baseFee, - Random: types.BytesToHash(env.GetRandom().GetValue()), + Random: BytesValueToHash(env.GetRandom()), BlobBaseFee: blobBaseFee, }, nil } From 027a877fb6f627b0e7ba0481fce4abe4ec0f2cb1 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 12:54:58 +0200 Subject: [PATCH 129/157] Random is now pHash --- substate/env.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substate/env.go b/substate/env.go index 6c50792..c705094 100644 --- a/substate/env.go +++ b/substate/env.go @@ -21,7 +21,7 @@ type Env struct { // Cancun hard fork EIP-4844 BlobBaseFee *big.Int // nil if EIP-4844 is not activated - Random types.Hash + Random *types.Hash } func NewEnv( From 504e2e5b32d1533c5cd77631d84b8e720f53dbd0 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 12 Sep 2024 13:18:37 +0200 Subject: [PATCH 130/157] difficulty now nil if random exists --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 4b3302c..8bcd9f4 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -134,7 +134,7 @@ func (entry *Substate_Account_StorageEntry) decode() (types.Hash, types.Hash, er // decode converts protobuf-encoded Substate_BlockEnv into aida-comprehensible Env func (env *Substate_BlockEnv) decode() (*substate.Env, error) { var difficulty *big.Int = nil - if env.GetDifficulty() != nil { + if env.GetDifficulty() != nil && env.GetRandom() == nil { difficulty = types.BytesToBigInt(env.GetDifficulty()) } From ad8235f231cb605a262124a9688269370f91e920 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 17 Sep 2024 10:34:58 +0200 Subject: [PATCH 131/157] add debug for blobhashes --- protobuf/decode.go | 1 + 1 file changed, 1 insertion(+) diff --git a/protobuf/decode.go b/protobuf/decode.go index 8bcd9f4..884778b 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -254,6 +254,7 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro if msg.GetBlobHashes() != nil { blobHashes := make([]types.Hash, len(msg.GetBlobHashes())) for i, hash := range msg.GetBlobHashes() { + fmt.Println(">>>>>", i, types.BytesToHash(hash)) blobHashes[i] = types.BytesToHash(hash) } } From 8f3f4cd5a2bbc1c4df45822a11fb46e0d9766879 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 17 Sep 2024 10:39:06 +0200 Subject: [PATCH 132/157] debug blobhashes len --- protobuf/decode.go | 1 + 1 file changed, 1 insertion(+) diff --git a/protobuf/decode.go b/protobuf/decode.go index 884778b..01ca15f 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -253,6 +253,7 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro case 3: if msg.GetBlobHashes() != nil { blobHashes := make([]types.Hash, len(msg.GetBlobHashes())) + fmt.Println("!!!!!", len(msg.GetBlobHashes())) for i, hash := range msg.GetBlobHashes() { fmt.Println(">>>>>", i, types.BytesToHash(hash)) blobHashes[i] = types.BytesToHash(hash) From 9f81e0833cc3c8f3b63da20917d53002b57b7751 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 17 Sep 2024 10:42:38 +0200 Subject: [PATCH 133/157] debug blobhashes len2 --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 01ca15f..d8fca51 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -253,7 +253,7 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro case 3: if msg.GetBlobHashes() != nil { blobHashes := make([]types.Hash, len(msg.GetBlobHashes())) - fmt.Println("!!!!!", len(msg.GetBlobHashes())) + fmt.Println("#####", len(msg.GetBlobHashes())) for i, hash := range msg.GetBlobHashes() { fmt.Println(">>>>>", i, types.BytesToHash(hash)) blobHashes[i] = types.BytesToHash(hash) From 0adce3304035498eacb449a2c9f209195c100ed2 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 17 Sep 2024 10:45:28 +0200 Subject: [PATCH 134/157] debug blobhashes len3 --- protobuf/decode.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index d8fca51..a2fb95a 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -252,9 +252,10 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro switch txType { case 3: if msg.GetBlobHashes() != nil { - blobHashes := make([]types.Hash, len(msg.GetBlobHashes())) - fmt.Println("#####", len(msg.GetBlobHashes())) - for i, hash := range msg.GetBlobHashes() { + hashes := msg.GetBlobHashes() + blobHashes := make([]types.Hash, len(hashes)) + fmt.Println("#####", len(hashes)) + for i, hash := range hashes { fmt.Println(">>>>>", i, types.BytesToHash(hash)) blobHashes[i] = types.BytesToHash(hash) } From a92ac8044d3969f4c586db551e8930daed44bd41 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 17 Sep 2024 10:53:21 +0200 Subject: [PATCH 135/157] debug blobhashes len4 --- protobuf/decode.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index a2fb95a..d602bc2 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -256,7 +256,8 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro blobHashes := make([]types.Hash, len(hashes)) fmt.Println("#####", len(hashes)) for i, hash := range hashes { - fmt.Println(">>>>>", i, types.BytesToHash(hash)) + fmt.Println("##########", i) + fmt.Println("##########", i, types.BytesToHash(hash)) blobHashes[i] = types.BytesToHash(hash) } } From 1b20bed7bf2381916da48572eaf4602206dfa4fb Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 17 Sep 2024 10:58:17 +0200 Subject: [PATCH 136/157] blobhashes --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index d602bc2..e068dde 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -253,7 +253,7 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro case 3: if msg.GetBlobHashes() != nil { hashes := msg.GetBlobHashes() - blobHashes := make([]types.Hash, len(hashes)) + blobHashes = make([]types.Hash, len(hashes)) fmt.Println("#####", len(hashes)) for i, hash := range hashes { fmt.Println("##########", i) From 63c1ff0d1ce716666eea1cbb1d7d3a367c941cb1 Mon Sep 17 00:00:00 2001 From: Rapol Date: Tue, 17 Sep 2024 11:05:51 +0200 Subject: [PATCH 137/157] blockhashes --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index e068dde..2da8f19 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -140,7 +140,7 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { var blockHashes map[uint64]types.Hash = nil if env.GetBlockHashes() != nil { - blockHashes := make(map[uint64]types.Hash, len(env.GetBlockHashes())) + blockHashes = make(map[uint64]types.Hash, len(env.GetBlockHashes())) for _, entry := range env.GetBlockHashes() { key, value, err := entry.decode() if err != nil { From 3650b464f7cadc409541bbd676bf38986cce3d3c Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 18 Sep 2024 08:29:22 +0200 Subject: [PATCH 138/157] remove debug --- protobuf/decode.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 2da8f19..56f0685 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -99,7 +99,6 @@ func (alloc *Substate_Alloc) decode(lookup DbGetCode) (*substate.WorldState, err } world[address] = substate.NewAccount(nonce, balance, code) - for _, storage := range acct.GetStorage() { key, value, err := storage.decode() if err != nil { @@ -211,7 +210,6 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro case Substate_TxMessage_TXTYPE_DYNAMICFEE: txType = 2 case Substate_TxMessage_TXTYPE_BLOB: - txType = 3 } // Berlin hard fork, EIP-2930: Optional access lists @@ -254,10 +252,7 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro if msg.GetBlobHashes() != nil { hashes := msg.GetBlobHashes() blobHashes = make([]types.Hash, len(hashes)) - fmt.Println("#####", len(hashes)) for i, hash := range hashes { - fmt.Println("##########", i) - fmt.Println("##########", i, types.BytesToHash(hash)) blobHashes[i] = types.BytesToHash(hash) } } From fc089b8c1000f739cc86f6e7ef8a7a72cd053624 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 18 Sep 2024 09:05:26 +0200 Subject: [PATCH 139/157] blobhash refactoring --- protobuf/decode.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 56f0685..6a2e291 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -250,9 +250,8 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro switch txType { case 3: if msg.GetBlobHashes() != nil { - hashes := msg.GetBlobHashes() - blobHashes = make([]types.Hash, len(hashes)) - for i, hash := range hashes { + blobHashes = make([]types.Hash, msg.GetBlobHashes()) + for i, hash := range msg.GetBlobHashes() { blobHashes[i] = types.BytesToHash(hash) } } From 7cba457c57970f931bb10e026bd06d43230e5033 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 18 Sep 2024 09:09:17 +0200 Subject: [PATCH 140/157] remove intermediate swtich --- protobuf/decode.go | 27 ++++++++++----------------- protobuf/utils.go | 2 +- types/uint256.go | 2 +- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 6a2e291..a191a6b 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -200,22 +200,13 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro data = code } - // TODO: using this switch directly does not produce expected result - // For some reason, an intermediate enum works - // To be figured out and removed - var txType uint8 = 0 // txType defaults to TXTYPE_LEGACY - switch x := *msg.TxType; x { - case Substate_TxMessage_TXTYPE_ACCESSLIST: - txType = 1 - case Substate_TxMessage_TXTYPE_DYNAMICFEE: - txType = 2 - case Substate_TxMessage_TXTYPE_BLOB: - } - // Berlin hard fork, EIP-2930: Optional access lists var accessList types.AccessList = nil // nil if EIP-2930 is not activated switch txType { - case 1, 2, 3: + case Substate_TxMessage_TXTYPE_ACCESSLIST, + Substate_TxMessage_TXTYPE_DYNAMICFEE, + Substate_TxMessage_TXTYPE_BLOB: + accessList = make([]types.AccessTuple, len(msg.GetAccessList())) for i, entry := range msg.GetAccessList() { addr, keys, err := entry.decode() @@ -229,7 +220,7 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro storageKeys[j] = types.BytesToHash(key) } - accessList[i] = types.AccessTuple { + accessList[i] = types.AccessTuple{ Address: address, StorageKeys: storageKeys, } @@ -240,15 +231,17 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro var gasFeeCap *big.Int = types.BytesToBigInt(msg.GetGasPrice()) var gasTipCap *big.Int = types.BytesToBigInt(msg.GetGasPrice()) switch txType { - case 2, 3: + case Substate_TxMessage_TXTYPE_DYNAMICFEE, + Substate_TxMessage_TXTYPE_BLOB: + gasFeeCap = BytesValueToBigInt(msg.GetGasFeeCap()) gasTipCap = BytesValueToBigInt(msg.GetGasTipCap()) } - + // Cancun hard fork, EIP-4844 var blobHashes []types.Hash = nil switch txType { - case 3: + case Substate_TxMessage_TXTYPE_BLOB: if msg.GetBlobHashes() != nil { blobHashes = make([]types.Hash, msg.GetBlobHashes()) for i, hash := range msg.GetBlobHashes() { diff --git a/protobuf/utils.go b/protobuf/utils.go index 00cad59..f24a15b 100644 --- a/protobuf/utils.go +++ b/protobuf/utils.go @@ -3,8 +3,8 @@ package protobuf import ( "math/big" - wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" "github.com/Fantom-foundation/Substate/types" + wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" ) func BytesValueToHash(bv *wrapperspb.BytesValue) *types.Hash { diff --git a/types/uint256.go b/types/uint256.go index 4a2c5a9..638a413 100644 --- a/types/uint256.go +++ b/types/uint256.go @@ -23,7 +23,7 @@ func BytesToBigInt(b []byte) *big.Int { } // BigIntToUint256 strictly returns nil if big is nil -func BigIntToUint256(i *big.Int) *uint256.Int { +func BigIntToUint256(i *big.Int) *uint256.Int { if i == nil { return nil } From 7362264ec23a0f66c1b61ea8591e2e5fa2304e3b Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 18 Sep 2024 09:12:25 +0200 Subject: [PATCH 141/157] add missing len() --- protobuf/decode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index a191a6b..79578e6 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -243,7 +243,7 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro switch txType { case Substate_TxMessage_TXTYPE_BLOB: if msg.GetBlobHashes() != nil { - blobHashes = make([]types.Hash, msg.GetBlobHashes()) + blobHashes = make([]types.Hash, len(msg.GetBlobHashes())) for i, hash := range msg.GetBlobHashes() { blobHashes[i] = types.BytesToHash(hash) } From 92df18197b6d657eb90d984b8189bbcf2df73289 Mon Sep 17 00:00:00 2001 From: Rapol Date: Wed, 18 Sep 2024 09:13:34 +0200 Subject: [PATCH 142/157] add missing getTxType --- protobuf/decode.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/protobuf/decode.go b/protobuf/decode.go index 79578e6..d78044b 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -200,6 +200,8 @@ func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, erro data = code } + txType := msg.GetTxType() + // Berlin hard fork, EIP-2930: Optional access lists var accessList types.AccessList = nil // nil if EIP-2930 is not activated switch txType { From 23f71f47fcdd5e571974d78051cebed3a5a47fea Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 19 Sep 2024 06:58:54 +0200 Subject: [PATCH 143/157] difficulty now directly parsed --- protobuf/decode.go | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index d78044b..0f415c8 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -14,26 +14,6 @@ import ( "github.com/syndtr/goleveldb/leveldb" ) -func (s *Substate) Dump(block uint64, tx int) error { - out := fmt.Sprintf("decoding block: %v Transaction: %v\n", block, tx) - - var jbytes []byte - jbytes, _ = json.MarshalIndent(s.GetInputAlloc(), "", " ") - out += fmt.Sprintf("input:\n%s\n", jbytes) - jbytes, _ = json.MarshalIndent(s.GetBlockEnv(), "", " ") - out += fmt.Sprintf("env:\n%s\n", jbytes) - jbytes, _ = json.MarshalIndent(s.GetTxMessage(), "", " ") - out += fmt.Sprintf("msg:\n%s\n", jbytes) - jbytes, _ = json.MarshalIndent(s.GetOutputAlloc(), "", " ") - out += fmt.Sprintf("output:\n%s\n", jbytes) - jbytes, _ = json.MarshalIndent(s.GetResult(), "", " ") - out += fmt.Sprintf("result:\n%s\n", jbytes) - - fmt.Println(out) - - return nil -} - type CodeHash = types.Hash type Code = []byte type DbGetCode = func(CodeHash) (Code, error) @@ -132,10 +112,10 @@ func (entry *Substate_Account_StorageEntry) decode() (types.Hash, types.Hash, er // decode converts protobuf-encoded Substate_BlockEnv into aida-comprehensible Env func (env *Substate_BlockEnv) decode() (*substate.Env, error) { - var difficulty *big.Int = nil + /*var difficulty *big.Int = nil if env.GetDifficulty() != nil && env.GetRandom() == nil { difficulty = types.BytesToBigInt(env.GetDifficulty()) - } + }*/ var blockHashes map[uint64]types.Hash = nil if env.GetBlockHashes() != nil { @@ -161,7 +141,7 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { return &substate.Env{ Coinbase: types.BytesToAddress(env.GetCoinbase()), - Difficulty: difficulty, + Difficulty: types.BytesToBigInt(env.GetDifficulty()), GasLimit: env.GetGasLimit(), Number: env.GetNumber(), Timestamp: env.GetTimestamp(), From 5f79cfcdac254f0a5b30e926f22c5b5c8213ee66 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 19 Sep 2024 07:01:53 +0200 Subject: [PATCH 144/157] debug2 --- protobuf/decode.go | 1 - 1 file changed, 1 deletion(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 0f415c8..6709afc 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -1,7 +1,6 @@ package protobuf import ( - "encoding/json" "errors" "fmt" "math/big" From 78b534adb52af2f1d4920f553a725f68ee31e99e Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 19 Sep 2024 07:05:47 +0200 Subject: [PATCH 145/157] DbGetCode -> dBGetCode --- protobuf/decode.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 6709afc..d6c616c 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -13,12 +13,10 @@ import ( "github.com/syndtr/goleveldb/leveldb" ) -type CodeHash = types.Hash -type Code = []byte -type DbGetCode = func(CodeHash) (Code, error) +type dbGetCode = func(types.Hash) ([]byte, error) // Decode converts protobuf-encoded Substate into aida-comprehensible substate -func (s *Substate) Decode(lookup DbGetCode, block uint64, tx int) (*substate.Substate, error) { +func (s *Substate) Decode(lookup dbGetCode, block uint64, tx int) (*substate.Substate, error) { input, err := s.GetInputAlloc().decode(lookup) if err != nil { return nil, err @@ -57,7 +55,7 @@ func (s *Substate) Decode(lookup DbGetCode, block uint64, tx int) (*substate.Sub } // decode converts protobuf-encoded Substate_Alloc into aida-comprehensible WorldState -func (alloc *Substate_Alloc) decode(lookup DbGetCode) (*substate.WorldState, error) { +func (alloc *Substate_Alloc) decode(lookup dbGetCode) (*substate.WorldState, error) { world := make(substate.WorldState, len(alloc.GetAlloc())) for _, entry := range alloc.GetAlloc() { @@ -95,7 +93,7 @@ func (entry *Substate_AllocEntry) decode() ([]byte, *Substate_Account, error) { return entry.GetAddress(), entry.GetAccount(), nil } -func (acct *Substate_Account) decode() (uint64, *uint256.Int, Code, CodeHash, error) { +func (acct *Substate_Account) decode() (uint64, *uint256.Int, []byte, types.Hash, error) { return acct.GetNonce(), types.BytesToUint256(acct.GetBalance()), acct.GetCode(), From 7df05ceb79fb80af9747f9fd3c27ebf8ebf35a99 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 19 Sep 2024 07:07:16 +0200 Subject: [PATCH 146/157] difficulty now no longer depended on random --- protobuf/decode.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index d6c616c..89f964c 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -81,7 +81,6 @@ func (alloc *Substate_Alloc) decode(lookup dbGetCode) (*substate.WorldState, err if err != nil { return nil, fmt.Errorf("Error decoding account storage entry; %w", err) } - world[address].Storage[key] = value } } @@ -109,10 +108,11 @@ func (entry *Substate_Account_StorageEntry) decode() (types.Hash, types.Hash, er // decode converts protobuf-encoded Substate_BlockEnv into aida-comprehensible Env func (env *Substate_BlockEnv) decode() (*substate.Env, error) { - /*var difficulty *big.Int = nil - if env.GetDifficulty() != nil && env.GetRandom() == nil { + var difficulty *big.Int = nil + //if env.GetDifficulty() != nil && env.GetRandom() == nil { + if env.GetDifficulty() != nil { difficulty = types.BytesToBigInt(env.GetDifficulty()) - }*/ + } var blockHashes map[uint64]types.Hash = nil if env.GetBlockHashes() != nil { From 4845d6bc64db054c1c33621eeaa240b2ade80608 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 19 Sep 2024 07:11:12 +0200 Subject: [PATCH 147/157] debug2 --- protobuf/decode.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 89f964c..9cfdbf3 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -138,7 +138,7 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { return &substate.Env{ Coinbase: types.BytesToAddress(env.GetCoinbase()), - Difficulty: types.BytesToBigInt(env.GetDifficulty()), + Difficulty: difficulty, GasLimit: env.GetGasLimit(), Number: env.GetNumber(), Timestamp: env.GetTimestamp(), @@ -154,7 +154,7 @@ func (entry *Substate_BlockEnv_BlockHashEntry) decode() (uint64, []byte, error) } // decode converts protobuf-encoded Substate_TxMessage into aida-comprehensible Message -func (msg *Substate_TxMessage) decode(lookup DbGetCode) (*substate.Message, error) { +func (msg *Substate_TxMessage) decode(lookup dbGetCode) (*substate.Message, error) { // to=nil means contract creation var pTo *types.Address = nil From 1c869b592eb46ac91c5e0af7e839b1f2ec3ed8dc Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 19 Sep 2024 07:12:11 +0200 Subject: [PATCH 148/157] test original difficulty instead --- protobuf/decode.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 9cfdbf3..1d7f713 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -109,8 +109,7 @@ func (entry *Substate_Account_StorageEntry) decode() (types.Hash, types.Hash, er // decode converts protobuf-encoded Substate_BlockEnv into aida-comprehensible Env func (env *Substate_BlockEnv) decode() (*substate.Env, error) { var difficulty *big.Int = nil - //if env.GetDifficulty() != nil && env.GetRandom() == nil { - if env.GetDifficulty() != nil { + if env.GetDifficulty() != nil && env.GetRandom() == nil { difficulty = types.BytesToBigInt(env.GetDifficulty()) } From ce980a33fa7bc6f8ac9f46a2146740fadd0b551c Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 19 Sep 2024 07:15:04 +0200 Subject: [PATCH 149/157] difficulty now using direct expr again --- protobuf/decode.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 1d7f713..0bd063d 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -108,10 +108,13 @@ func (entry *Substate_Account_StorageEntry) decode() (types.Hash, types.Hash, er // decode converts protobuf-encoded Substate_BlockEnv into aida-comprehensible Env func (env *Substate_BlockEnv) decode() (*substate.Env, error) { + /* var difficulty *big.Int = nil - if env.GetDifficulty() != nil && env.GetRandom() == nil { + //if env.GetDifficulty() != nil && env.GetRandom() == nil { + if env.GetDifficulty() != nil { difficulty = types.BytesToBigInt(env.GetDifficulty()) } + */ var blockHashes map[uint64]types.Hash = nil if env.GetBlockHashes() != nil { @@ -137,7 +140,7 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { return &substate.Env{ Coinbase: types.BytesToAddress(env.GetCoinbase()), - Difficulty: difficulty, + Difficulty: types.BytesToBigInt(env.GetDifficulty()), GasLimit: env.GetGasLimit(), Number: env.GetNumber(), Timestamp: env.GetTimestamp(), From 8891cdbcb40b987dbde3a9769837773a28e6b61d Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 19 Sep 2024 07:21:41 +0200 Subject: [PATCH 150/157] difficulty now working, remove debug --- protobuf/decode.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 0bd063d..2a1d991 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -108,14 +108,6 @@ func (entry *Substate_Account_StorageEntry) decode() (types.Hash, types.Hash, er // decode converts protobuf-encoded Substate_BlockEnv into aida-comprehensible Env func (env *Substate_BlockEnv) decode() (*substate.Env, error) { - /* - var difficulty *big.Int = nil - //if env.GetDifficulty() != nil && env.GetRandom() == nil { - if env.GetDifficulty() != nil { - difficulty = types.BytesToBigInt(env.GetDifficulty()) - } - */ - var blockHashes map[uint64]types.Hash = nil if env.GetBlockHashes() != nil { blockHashes = make(map[uint64]types.Hash, len(env.GetBlockHashes())) From 8850e6cee2d5fe75c3d533bbc78d792744de470e Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 19 Sep 2024 07:32:58 +0200 Subject: [PATCH 151/157] basefee, blobbasefee now uses BytesValueToBigInt --- protobuf/decode.go | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/protobuf/decode.go b/protobuf/decode.go index 2a1d991..73116d7 100644 --- a/protobuf/decode.go +++ b/protobuf/decode.go @@ -120,16 +120,6 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { } } - var baseFee *big.Int = nil - if env.GetBaseFee() != nil { - baseFee = types.BytesToBigInt(env.GetBaseFee().GetValue()) - } - - var blobBaseFee *big.Int = nil - if env.GetBlobBaseFee() != nil { - blobBaseFee = types.BytesToBigInt(env.GetBlobBaseFee().GetValue()) - } - return &substate.Env{ Coinbase: types.BytesToAddress(env.GetCoinbase()), Difficulty: types.BytesToBigInt(env.GetDifficulty()), @@ -137,9 +127,9 @@ func (env *Substate_BlockEnv) decode() (*substate.Env, error) { Number: env.GetNumber(), Timestamp: env.GetTimestamp(), BlockHashes: blockHashes, - BaseFee: baseFee, + BaseFee: BytesValueToBigInt(env.GetBaseFee()), Random: BytesValueToHash(env.GetRandom()), - BlobBaseFee: blobBaseFee, + BlobBaseFee: BytesValueToBigInt(env.GetBlobBaseFee()), }, nil } From b63251f068cb031ea4cd590569ef3311d319a05e Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 19 Sep 2024 21:20:57 +0200 Subject: [PATCH 152/157] added decodeUsing --- db/substate_db.go | 35 +++++---------------- db/substate_decoder.go | 69 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 27 deletions(-) create mode 100644 db/substate_decoder.go diff --git a/db/substate_db.go b/db/substate_db.go index 6b957e8..3572865 100644 --- a/db/substate_db.go +++ b/db/substate_db.go @@ -85,6 +85,7 @@ func newSubstateDB(path string, o *opt.Options, wo *opt.WriteOptions, ro *opt.Re type substateDB struct { *codeDB + decodeSubstate decoderFunc } func (db *substateDB) GetFirstSubstate() *substate.Substate { @@ -110,19 +111,10 @@ func (db *substateDB) GetSubstate(block uint64, tx int) (*substate.Substate, err return nil, fmt.Errorf("cannot get substate block: %v, tx: %v from db; %w", block, tx, err) } - //rlpSubstate, err := rlp.Decode(val) - //if err != nil { - // return nil, fmt.Errorf("cannot decode data into rlp block: %v, tx %v; %w", block, tx, err) - //} - - //return rlpSubstate.ToSubstate(db.GetCode, block, tx) - - pbSubstate := &pb.Substate{} - if err := proto.Unmarshal(val, pbSubstate); err != nil { - return nil, err + if db.decodeSubstate == nil { + db.decodeUsing("default") } - - return pbSubstate.Decode(db.GetCode, block, tx) + return db.decodeSubstate(val, block, tx) } // GetBlockSubstates returns substates for given block if exists within DB. @@ -147,24 +139,13 @@ func (db *substateDB) GetBlockSubstates(block uint64) (map[int]*substate.Substat return nil, fmt.Errorf("record-replay: GetBlockSubstates(%v) iterated substates from block %v", block, b) } - //rlpSubstate, err := rlp.Decode(value) - //if err != nil { - // return nil, fmt.Errorf("cannot decode data into rlp block: %v, tx %v; %w", block, tx, err) - //} - - //sbstt, err := rlpSubstate.ToSubstate(db.GetCode, block, tx) - //if err != nil { - // return nil, fmt.Errorf("cannot decode data into substate: %w", err) - //} - - pbSubstate := &pb.Substate{} - if err := proto.Unmarshal(value, pbSubstate); err != nil { - return nil, err + if db.decodeSubstate == nil { + db.decodeUsing("default") } - sbstt, err := pbSubstate.Decode(db.GetCode, block, tx) + sbstt, err := db.decodeSubstate(val, block, tx) if err != nil { - return nil, err + return nil, fmt.Errorf("Error decoding block %d, tx %d; %w", block, tx, err) } txSubstate[tx] = sbstt diff --git a/db/substate_decoder.go b/db/substate_decoder.go new file mode 100644 index 0000000..7e17e42 --- /dev/null +++ b/db/substate_decoder.go @@ -0,0 +1,69 @@ +package db + +import ( + pb "github.com/Fantom-foundation/Substate/protobuf" + "github.com/Fantom-foundation/Substate/rlp" + "github.com/Fantom-foundation/Substate/substate" + "github.com/Fantom-foundation/Substate/types" + trlp "github.com/Fantom-foundation/Substate/types/rlp" + "github.com/golang/protobuf/proto" +) + +// decodeUsing sets the runtime parsing behavior of substateDB +// intended usage: +// +// db := &substateDB{..} // initializing db +// db.decodeUsing() // end of init, or right before decoding +func (db *substateDB) decodeUsing(encoding string) *substateDB { + db.decodeSubstate = getDecoderFunc(encoding, db.GetCode) + return db +} + +type substateDecoder interface { + Decode(bytes []byte, block uint64, tx int) (*substate.Substate, error) +} + +// decoderFunc aliases the common function used to decode substate +type decoderFunc func([]byte, uint64, int) (*substate.Substate, error) + +func (decode *decoderFunc) Decode(bytes []byte, block uint64, tx int) { + decode(bytes, block, tx) +} + +type codeLookup = func(types.Hash) ([]byte, error) + +func getDecoderFunc(encoding string, lookup codeLookup) decoderFunc { + switch encoding { + case "protobuf", "pb": + return func(bytes []byte, block uint64, tx int) (*substate.Substate, error) { + return decodeProtobuf(bytes, lookup, block, tx) + } + default: + fmt.Println("Defaulting getDecoderFunc") + fallthrough + case "rlp": + return func(bytes []byte, block uint64, tx int) (*substate.Substate, error) { + return decodeRlp(bytes, lookup, block, tx) + } + } +} + +// decodeRlp decodes into substate the provided rlp-encoded bytecode +func decodeRlp(bytes []byte, lookup codeLookup, block uint64, tx int) (*substate.Substate, error) { + rlpSubstate, err := rlp.Decode(bytes) + if err != nil { + return nil, fmt.Errorf("cannot decode data into rlp block: %v, tx %v; %w", block, tx, err) + } + + return rlpSubstate.ToSubstate(lookup, block, tx) +} + +// decodeProtobuf decodes into substate the provided protobuf-encoded bytecode +func decodeProtobuf(bytes []byte, lookup codeLookup, block uint64, tx int) (*substate.Substate, error) { + pbSubstate := &pb.Substate{} + if err := proto.Unmarshal(bytes, pbSubstate); err != nil { + return nil, fmt.Errorf("cannot decode data into protobuf block: %v, tx %v; %w", block, tx, err) + } + + return pbSubstate.Decode(lookup, block, tx) +} From fb268a4f3ef3f386927c3d0c8efbb4d9b195aca6 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 19 Sep 2024 21:39:00 +0200 Subject: [PATCH 153/157] Decode->DecodeSubstate, decodeSubstate now pointers --- db/substate_db.go | 13 +++---------- db/substate_decoder.go | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/db/substate_db.go b/db/substate_db.go index 3572865..548af90 100644 --- a/db/substate_db.go +++ b/db/substate_db.go @@ -85,7 +85,7 @@ func newSubstateDB(path string, o *opt.Options, wo *opt.WriteOptions, ro *opt.Re type substateDB struct { *codeDB - decodeSubstate decoderFunc + decodeSubstate *decoderFunc } func (db *substateDB) GetFirstSubstate() *substate.Substate { @@ -111,10 +111,7 @@ func (db *substateDB) GetSubstate(block uint64, tx int) (*substate.Substate, err return nil, fmt.Errorf("cannot get substate block: %v, tx: %v from db; %w", block, tx, err) } - if db.decodeSubstate == nil { - db.decodeUsing("default") - } - return db.decodeSubstate(val, block, tx) + return db.DecodeSubstate(val, block, tx) } // GetBlockSubstates returns substates for given block if exists within DB. @@ -139,11 +136,7 @@ func (db *substateDB) GetBlockSubstates(block uint64) (map[int]*substate.Substat return nil, fmt.Errorf("record-replay: GetBlockSubstates(%v) iterated substates from block %v", block, b) } - if db.decodeSubstate == nil { - db.decodeUsing("default") - } - - sbstt, err := db.decodeSubstate(val, block, tx) + sbstt, err := db.DecodeSubstate(value, block, tx) if err != nil { return nil, fmt.Errorf("Error decoding block %d, tx %d; %w", block, tx, err) } diff --git a/db/substate_decoder.go b/db/substate_decoder.go index 7e17e42..20e806a 100644 --- a/db/substate_decoder.go +++ b/db/substate_decoder.go @@ -1,6 +1,8 @@ package db import ( + "fmt" + pb "github.com/Fantom-foundation/Substate/protobuf" "github.com/Fantom-foundation/Substate/rlp" "github.com/Fantom-foundation/Substate/substate" @@ -15,18 +17,26 @@ import ( // db := &substateDB{..} // initializing db // db.decodeUsing() // end of init, or right before decoding func (db *substateDB) decodeUsing(encoding string) *substateDB { - db.decodeSubstate = getDecoderFunc(encoding, db.GetCode) + f := getDecoderFunc(encoding, db.GetCode) + db.decodeSubstate = &f return db } type substateDecoder interface { - Decode(bytes []byte, block uint64, tx int) (*substate.Substate, error) + DecodeSubstate(bytes []byte, block uint64, tx int) (*substate.Substate, error) +} + +func (db *substateDB) DecodeSubstate(bytes []byte, block uint64, tx int) (*substate.Substate, error) { + if db.decodeSubstate == nil { + db.decodeUsing("default") + } + return db.decodeSubstate(bytes, block, tx) } // decoderFunc aliases the common function used to decode substate type decoderFunc func([]byte, uint64, int) (*substate.Substate, error) -func (decode *decoderFunc) Decode(bytes []byte, block uint64, tx int) { +func (decode decoderFunc) DecodeSubstate(bytes []byte, block uint64, tx int) { decode(bytes, block, tx) } From 4af65f740ca9d6857b5c8212e2692063e46f30bd Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 19 Sep 2024 21:41:52 +0200 Subject: [PATCH 154/157] removed unused packages, decodeSubstate no longer pointer --- db/substate_db.go | 4 +--- db/substate_decoder.go | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/db/substate_db.go b/db/substate_db.go index 548af90..d02f61d 100644 --- a/db/substate_db.go +++ b/db/substate_db.go @@ -4,11 +4,9 @@ import ( "encoding/binary" "fmt" - pb "github.com/Fantom-foundation/Substate/protobuf" "github.com/Fantom-foundation/Substate/rlp" "github.com/Fantom-foundation/Substate/substate" trlp "github.com/Fantom-foundation/Substate/types/rlp" - "github.com/golang/protobuf/proto" "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/opt" "github.com/syndtr/goleveldb/leveldb/util" @@ -85,7 +83,7 @@ func newSubstateDB(path string, o *opt.Options, wo *opt.WriteOptions, ro *opt.Re type substateDB struct { *codeDB - decodeSubstate *decoderFunc + decodeSubstate decoderFunc } func (db *substateDB) GetFirstSubstate() *substate.Substate { diff --git a/db/substate_decoder.go b/db/substate_decoder.go index 20e806a..ecf102f 100644 --- a/db/substate_decoder.go +++ b/db/substate_decoder.go @@ -7,7 +7,6 @@ import ( "github.com/Fantom-foundation/Substate/rlp" "github.com/Fantom-foundation/Substate/substate" "github.com/Fantom-foundation/Substate/types" - trlp "github.com/Fantom-foundation/Substate/types/rlp" "github.com/golang/protobuf/proto" ) From ffde78d8cf079a15bda360f32e43a62228d46150 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 19 Sep 2024 21:43:18 +0200 Subject: [PATCH 155/157] decodeSubstate now directly func --- db/substate_decoder.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/db/substate_decoder.go b/db/substate_decoder.go index ecf102f..2ad235d 100644 --- a/db/substate_decoder.go +++ b/db/substate_decoder.go @@ -16,8 +16,7 @@ import ( // db := &substateDB{..} // initializing db // db.decodeUsing() // end of init, or right before decoding func (db *substateDB) decodeUsing(encoding string) *substateDB { - f := getDecoderFunc(encoding, db.GetCode) - db.decodeSubstate = &f + db.decodeSubstate = getDecoderFunc(encoding, db.GetCode) return db } From 00c8cfd3d63a5eecb06ede237bde674052d80f02 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 19 Sep 2024 21:46:50 +0200 Subject: [PATCH 156/157] add nil to constructor --- db/substate_db.go | 8 ++++---- db/substate_decoder.go | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/db/substate_db.go b/db/substate_db.go index d02f61d..79484ec 100644 --- a/db/substate_db.go +++ b/db/substate_db.go @@ -57,11 +57,11 @@ func NewSubstateDB(path string, o *opt.Options, wo *opt.WriteOptions, ro *opt.Re } func MakeDefaultSubstateDB(db *leveldb.DB) SubstateDB { - return &substateDB{&codeDB{&baseDB{backend: db}}} + return &substateDB{&codeDB{&baseDB{backend: db}}, nil} } func MakeDefaultSubstateDBFromBaseDB(db BaseDB) SubstateDB { - return &substateDB{&codeDB{&baseDB{backend: db.getBackend()}}} + return &substateDB{&codeDB{&baseDB{backend: db.getBackend()}}, nil} } // NewReadOnlySubstateDB creates a new instance of read-only SubstateDB. @@ -70,7 +70,7 @@ func NewReadOnlySubstateDB(path string) (SubstateDB, error) { } func MakeSubstateDB(db *leveldb.DB, wo *opt.WriteOptions, ro *opt.ReadOptions) SubstateDB { - return &substateDB{&codeDB{&baseDB{backend: db, wo: wo, ro: ro}}} + return &substateDB{&codeDB{&baseDB{backend: db, wo: wo, ro: ro}}, nil} } func newSubstateDB(path string, o *opt.Options, wo *opt.WriteOptions, ro *opt.ReadOptions) (*substateDB, error) { @@ -78,7 +78,7 @@ func newSubstateDB(path string, o *opt.Options, wo *opt.WriteOptions, ro *opt.Re if err != nil { return nil, err } - return &substateDB{base}, nil + return &substateDB{base, nil}, nil } type substateDB struct { diff --git a/db/substate_decoder.go b/db/substate_decoder.go index 2ad235d..e432976 100644 --- a/db/substate_decoder.go +++ b/db/substate_decoder.go @@ -14,8 +14,8 @@ import ( // intended usage: // // db := &substateDB{..} // initializing db -// db.decodeUsing() // end of init, or right before decoding -func (db *substateDB) decodeUsing(encoding string) *substateDB { +// db.DecodeSubstateUsing() // end of init, or right before decoding +func (db *substateDB) DecodeSubstateUsing(encoding string) *substateDB { db.decodeSubstate = getDecoderFunc(encoding, db.GetCode) return db } From 5251e447ea4015eef43593a5cfc1d6d4c7eb8348 Mon Sep 17 00:00:00 2001 From: Rapol Date: Thu, 19 Sep 2024 23:20:39 +0200 Subject: [PATCH 157/157] Squash Debug --- db/substate_db.go | 3 +++ db/substate_decoder.go | 11 +++++------ db/substate_iterator.go | 15 ++------------- 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/db/substate_db.go b/db/substate_db.go index 79484ec..94c9213 100644 --- a/db/substate_db.go +++ b/db/substate_db.go @@ -43,6 +43,9 @@ type SubstateDB interface { // GetLastSubstate returns last substate (block and transaction wise) inside given DB. GetLastSubstate() (*substate.Substate, error) + + // SetDecoder sets the decoder func to the provided encoding + SetDecoder(encoding string) *substateDB } // NewDefaultSubstateDB creates new instance of SubstateDB with default options. diff --git a/db/substate_decoder.go b/db/substate_decoder.go index e432976..707d5dd 100644 --- a/db/substate_decoder.go +++ b/db/substate_decoder.go @@ -10,12 +10,12 @@ import ( "github.com/golang/protobuf/proto" ) -// decodeUsing sets the runtime parsing behavior of substateDB +// SetDecoder sets the runtime parsing behavior of substateDB // intended usage: // -// db := &substateDB{..} // initializing db -// db.DecodeSubstateUsing() // end of init, or right before decoding -func (db *substateDB) DecodeSubstateUsing(encoding string) *substateDB { +// db := &substateDB{..} // initializing db +// .SetDecoder() // end of init, or right before decoding +func (db *substateDB) SetDecoder(encoding string) *substateDB { db.decodeSubstate = getDecoderFunc(encoding, db.GetCode) return db } @@ -26,7 +26,7 @@ type substateDecoder interface { func (db *substateDB) DecodeSubstate(bytes []byte, block uint64, tx int) (*substate.Substate, error) { if db.decodeSubstate == nil { - db.decodeUsing("default") + db.SetDecoder("default") } return db.decodeSubstate(bytes, block, tx) } @@ -47,7 +47,6 @@ func getDecoderFunc(encoding string, lookup codeLookup) decoderFunc { return decodeProtobuf(bytes, lookup, block, tx) } default: - fmt.Println("Defaulting getDecoderFunc") fallthrough case "rlp": return func(bytes []byte, block uint64, tx int) (*substate.Substate, error) { diff --git a/db/substate_iterator.go b/db/substate_iterator.go index 9512801..54f196f 100644 --- a/db/substate_iterator.go +++ b/db/substate_iterator.go @@ -3,11 +3,8 @@ package db import ( "fmt" - "github.com/golang/protobuf/proto" - "github.com/syndtr/goleveldb/leveldb/util" - - pb "github.com/Fantom-foundation/Substate/protobuf" "github.com/Fantom-foundation/Substate/substate" + "github.com/syndtr/goleveldb/leveldb/util" ) func newSubstateIterator(db *substateDB, start []byte) *substateIterator { @@ -34,15 +31,7 @@ func (i *substateIterator) decode(data rawEntry) (*substate.Substate, error) { return nil, fmt.Errorf("invalid substate key: %v; %w", key, err) } - //rlpSubstate, err := rlp.Decode(value) - //return rlpSubstate.ToSubstate(i.db.GetCode, block, tx) - - pbSubstate := &pb.Substate{} - if err := proto.Unmarshal(value, pbSubstate); err != nil { - return nil, err - } - - return pbSubstate.Decode(i.db.GetCode, block, tx) + return i.db.DecodeSubstate(value, block, tx) } func (i *substateIterator) start(numWorkers int) {