Skip to content

Commit

Permalink
Merge pull request #472 from iotaledger/feat/simplify-serialization
Browse files Browse the repository at this point in the history
Simplify serialization
  • Loading branch information
alexsporn authored Nov 8, 2023
2 parents 861754c + d8aaf2f commit 9094100
Show file tree
Hide file tree
Showing 64 changed files with 1,458 additions and 1,774 deletions.
8 changes: 7 additions & 1 deletion components/debugapi/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ func getSlotBlockIDs(index iotago.SlotIndex) (*BlockChangesResponse, error) {
}

includedBlocks := make([]string, 0)
tangleTree := ads.NewSet[iotago.Identifier](mapdb.NewMapDB(), iotago.BlockID.Bytes, iotago.BlockIDFromBytes)
tangleTree := ads.NewSet[iotago.Identifier](
mapdb.NewMapDB(),
iotago.Identifier.Bytes,
iotago.IdentifierFromBytes,
iotago.BlockID.Bytes,
iotago.BlockIDFromBytes,
)

_ = blocksForSlot.StreamKeys(func(blockID iotago.BlockID) error {
includedBlocks = append(includedBlocks, blockID.String())
Expand Down
8 changes: 7 additions & 1 deletion components/debugapi/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ func storeTransactionsPerSlot(scd *notarization.SlotCommittedDetails) error {
if err != nil {
return ierrors.Wrapf(err, "failed to retrieve state diff for slot %d", slot)
}
mutationsTree := ads.NewSet[iotago.Identifier](mapdb.NewMapDB(), iotago.TransactionID.Bytes, iotago.TransactionIDFromBytes)
mutationsTree := ads.NewSet[iotago.Identifier](
mapdb.NewMapDB(),
iotago.Identifier.Bytes,
iotago.IdentifierFromBytes,
iotago.TransactionID.Bytes,
iotago.TransactionIDFromBytes,
)
tcs := &TransactionsChangesResponse{
Index: slot,
IncludedTransactions: make([]string, 0),
Expand Down
31 changes: 15 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,29 @@ require (
github.com/google/uuid v1.4.0
github.com/gorilla/websocket v1.5.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/iotaledger/hive.go/ads v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/app v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/constraints v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20231027195901-620bd7470e42
github.com/iotaledger/hive.go/crypto v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/ds v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/ierrors v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/kvstore v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/lo v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/logger v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/runtime v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231027195901-620bd7470e42
github.com/iotaledger/hive.go/stringify v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/ads v0.0.0-20231108050255-98e0fa35e936
github.com/iotaledger/hive.go/app v0.0.0-20231108050255-98e0fa35e936
github.com/iotaledger/hive.go/constraints v0.0.0-20231108050255-98e0fa35e936
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20231108050255-98e0fa35e936
github.com/iotaledger/hive.go/crypto v0.0.0-20231108050255-98e0fa35e936
github.com/iotaledger/hive.go/ds v0.0.0-20231108050255-98e0fa35e936
github.com/iotaledger/hive.go/ierrors v0.0.0-20231108050255-98e0fa35e936
github.com/iotaledger/hive.go/kvstore v0.0.0-20231108050255-98e0fa35e936
github.com/iotaledger/hive.go/lo v0.0.0-20231108050255-98e0fa35e936
github.com/iotaledger/hive.go/logger v0.0.0-20231108050255-98e0fa35e936
github.com/iotaledger/hive.go/runtime v0.0.0-20231108050255-98e0fa35e936
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231108050255-98e0fa35e936
github.com/iotaledger/hive.go/stringify v0.0.0-20231108050255-98e0fa35e936
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231031135002-4c79ea5193f5
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231031134131-b6ad918dc1ac
github.com/iotaledger/iota.go/v4 v4.0.0-20231102113728-20b8d01e826e
github.com/iotaledger/iota.go/v4 v4.0.0-20231108050608-afce96cfe8a6
github.com/labstack/echo/v4 v4.11.2
github.com/labstack/gommon v0.4.0
github.com/libp2p/go-libp2p v0.32.0
github.com/libp2p/go-libp2p-kad-dht v0.25.1
github.com/mr-tron/base58 v1.2.0
github.com/multiformats/go-multiaddr v0.12.0
github.com/multiformats/go-varint v0.0.7
github.com/orcaman/writerseeker v0.0.0-20200621085525-1d3f536ff85e
github.com/otiai10/copy v1.14.0
github.com/prometheus/client_golang v1.17.0
github.com/spf13/pflag v1.0.5
Expand Down Expand Up @@ -89,7 +88,7 @@ require (
github.com/huin/goupnp v1.3.0 // indirect
github.com/iancoleman/orderedmap v0.3.0 // indirect
github.com/iotaledger/grocksdb v1.7.5-0.20230220105546-5162e18885c7 // indirect
github.com/iotaledger/hive.go/log v0.0.0-20231027195901-620bd7470e42 // indirect
github.com/iotaledger/hive.go/log v0.0.0-20231108050255-98e0fa35e936 // indirect
github.com/ipfs/boxo v0.13.1 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/ipfs/go-datastore v0.6.0 // indirect
Expand Down
62 changes: 30 additions & 32 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -275,40 +275,40 @@ github.com/iancoleman/orderedmap v0.3.0/go.mod h1:XuLcCUkdL5owUCQeF2Ue9uuw1EptkJ
github.com/ianlancetaylor/demangle v0.0.0-20210905161508-09a460cdf81d/go.mod h1:aYm2/VgdVmcIU8iMfdMvDMsRAQjcfZSKFby6HOFvi/w=
github.com/iotaledger/grocksdb v1.7.5-0.20230220105546-5162e18885c7 h1:dTrD7X2PTNgli6EbS4tV9qu3QAm/kBU3XaYZV2xdzys=
github.com/iotaledger/grocksdb v1.7.5-0.20230220105546-5162e18885c7/go.mod h1:ZRdPu684P0fQ1z8sXz4dj9H5LWHhz4a9oCtvjunkSrw=
github.com/iotaledger/hive.go/ads v0.0.0-20231027195901-620bd7470e42 h1:EOfxTuAiBmED1VHuVh7/UIeB27cCRe13gdSzyioNMBw=
github.com/iotaledger/hive.go/ads v0.0.0-20231027195901-620bd7470e42/go.mod h1:IFh0gDfeMgZtfCo+5afK59IDR4xXh+cTR9YtLnZPcbY=
github.com/iotaledger/hive.go/app v0.0.0-20231027195901-620bd7470e42 h1:xAER9M9Uoz2EOWT43E9wmXRe+RmAk8OBSUoboH4Su8M=
github.com/iotaledger/hive.go/app v0.0.0-20231027195901-620bd7470e42/go.mod h1:8ZbIKR84oQd/3iQ5eeT7xpudO9/ytzXP7veIYnk7Orc=
github.com/iotaledger/hive.go/constraints v0.0.0-20231027195901-620bd7470e42 h1:drmpgLlJy7kZ09Dt1qKSnbILU+27Qu2jp4VdPDNwbFk=
github.com/iotaledger/hive.go/constraints v0.0.0-20231027195901-620bd7470e42/go.mod h1:dOBOM2s4se3HcWefPe8sQLUalGXJ8yVXw58oK8jke3s=
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20231027195901-620bd7470e42 h1:BC5GkIHyXdoJGdw6Tu5ds2kjw9grFLtwQiuMaKfdLU8=
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20231027195901-620bd7470e42/go.mod h1:Mc+ACqBGPxrPMIPUBOm6/HL0J6m0iVMwjtIEKW3uow8=
github.com/iotaledger/hive.go/crypto v0.0.0-20231027195901-620bd7470e42 h1:r8TkdQJB7/bJd8cF8z5GQ+rX/7JpbPdPoN7wMoV1OCM=
github.com/iotaledger/hive.go/crypto v0.0.0-20231027195901-620bd7470e42/go.mod h1:h3o6okvMSEK3KOX6pOp3yq1h9ohTkTfo6X8MzEadeb0=
github.com/iotaledger/hive.go/ds v0.0.0-20231027195901-620bd7470e42 h1:ytzZZPtclAzLfjxv26frbinCGx3Z6ouUENbx5U7lFGg=
github.com/iotaledger/hive.go/ds v0.0.0-20231027195901-620bd7470e42/go.mod h1:3XkUSKfHaVxGbT0XAvjNlVYqPzhfLTGhDtdNA5UBPco=
github.com/iotaledger/hive.go/ierrors v0.0.0-20231027195901-620bd7470e42 h1:QMxd32Y/veVhTDPCiOFgetjUbG7sr9MryF29/rSPkMA=
github.com/iotaledger/hive.go/ierrors v0.0.0-20231027195901-620bd7470e42/go.mod h1:HcE8B5lP96enc/OALTb2/rIIi+yOLouRoHOKRclKmC8=
github.com/iotaledger/hive.go/kvstore v0.0.0-20231027195901-620bd7470e42 h1:/xPwStUckZ2V0XPoY496cXU+c5elpHyvYoT6JAmuvRY=
github.com/iotaledger/hive.go/kvstore v0.0.0-20231027195901-620bd7470e42/go.mod h1:O/U3jtiUDeqqM0MZQFu2UPqS9fUm0C5hNISxlmg/thE=
github.com/iotaledger/hive.go/lo v0.0.0-20231027195901-620bd7470e42 h1:AvNLzONVMspwx7nD/NyYUgb5Hi7/zgzIOegr1uRD/M8=
github.com/iotaledger/hive.go/lo v0.0.0-20231027195901-620bd7470e42/go.mod h1:s4kzx9QY1MVWHJralj+3q5kI0eARtrJhphYD/iBbPfo=
github.com/iotaledger/hive.go/log v0.0.0-20231027195901-620bd7470e42 h1:e1uJAlXE3zeXpa+c4uFOG+/AMFbUlLt2mcrSK5NMxVs=
github.com/iotaledger/hive.go/log v0.0.0-20231027195901-620bd7470e42/go.mod h1:JvokzmpmFZPDskMlUqqjgHtD8usVJU4nAY/TNMGge8M=
github.com/iotaledger/hive.go/logger v0.0.0-20231027195901-620bd7470e42 h1:7wjs4t1snBDJ8LOTl+tZhr2ORywSOTgJMppxiIAMA0A=
github.com/iotaledger/hive.go/logger v0.0.0-20231027195901-620bd7470e42/go.mod h1:aBfAfIB2GO/IblhYt5ipCbyeL9bXSNeAwtYVA3hZaHg=
github.com/iotaledger/hive.go/runtime v0.0.0-20231027195901-620bd7470e42 h1:1QMJ39qXIx/IZVzus3+97IV7Pa++e+d340TvbMjhiBU=
github.com/iotaledger/hive.go/runtime v0.0.0-20231027195901-620bd7470e42/go.mod h1:jRw8yFipiPaqmTPHh7hTcxAP9u6pjRGpByS3REJKkbY=
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231027195901-620bd7470e42 h1:hZli4E9kJUAEQ7gzZR1XbPcpgqvqMPYq8YBPMbrBuos=
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231027195901-620bd7470e42/go.mod h1:SdK26z8/VhWtxaqCuQrufm80SELgowQPmu9T/8eUQ8g=
github.com/iotaledger/hive.go/stringify v0.0.0-20231027195901-620bd7470e42 h1:OlDhgvJ48bZxcvTeebJ1b96xtNnJAddejd2Q4rlH1mU=
github.com/iotaledger/hive.go/stringify v0.0.0-20231027195901-620bd7470e42/go.mod h1:FTo/UWzNYgnQ082GI9QVM9HFDERqf9rw9RivNpqrnTs=
github.com/iotaledger/hive.go/ads v0.0.0-20231108050255-98e0fa35e936 h1:2r4FgIGdc2lHcIbXiUFCCVq4+B0oZk9t6Z0SSLjrzCE=
github.com/iotaledger/hive.go/ads v0.0.0-20231108050255-98e0fa35e936/go.mod h1:gbUvr01B5ha530GnNm8K2OsHXOd2BtzBYOMxyTX3iDg=
github.com/iotaledger/hive.go/app v0.0.0-20231108050255-98e0fa35e936 h1:SnmQt9GxrWIvpW7pgQS049x1b8T+lQutTQbo35FImug=
github.com/iotaledger/hive.go/app v0.0.0-20231108050255-98e0fa35e936/go.mod h1:+riYmeLApkLlj4+EpuJpEJAsj/KGfD7cqLGy7oTsPOM=
github.com/iotaledger/hive.go/constraints v0.0.0-20231108050255-98e0fa35e936 h1:qkq0Wz+Y3J8QYRLd0fwTgHuur/A3k7d82BxOKSfvk8c=
github.com/iotaledger/hive.go/constraints v0.0.0-20231108050255-98e0fa35e936/go.mod h1:dOBOM2s4se3HcWefPe8sQLUalGXJ8yVXw58oK8jke3s=
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20231108050255-98e0fa35e936 h1:GtsYwcCqRomhMo190TPxBrOzs6YnVmqkmQgT/lJrJRo=
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20231108050255-98e0fa35e936/go.mod h1:CdixkrB7VdQzEDlVuwsxPtsiJL/WXrQgz3PELIqlLko=
github.com/iotaledger/hive.go/crypto v0.0.0-20231108050255-98e0fa35e936 h1:Xeb4w0g0Kv2ZjdCZQqz8oiqAU5qAy8OXG8kGTXSPzuY=
github.com/iotaledger/hive.go/crypto v0.0.0-20231108050255-98e0fa35e936/go.mod h1:OQ9EVTTQT1mkO/16BgwSIyQlAhEg+Cptud/yutevWsI=
github.com/iotaledger/hive.go/ds v0.0.0-20231108050255-98e0fa35e936 h1:NtQLSS0Lq5qg/w5nbMpXrlQpmcK3KiOaQmgZWoRc4mM=
github.com/iotaledger/hive.go/ds v0.0.0-20231108050255-98e0fa35e936/go.mod h1:JE8cbZSvzbB5TrwXibg6M0B7ck35YxF30ItHBzQRlgc=
github.com/iotaledger/hive.go/ierrors v0.0.0-20231108050255-98e0fa35e936 h1:o5S4KUAwToOLXoYYRj9ZgqeDsFv1VRM4+Mni0Tdj2Ck=
github.com/iotaledger/hive.go/ierrors v0.0.0-20231108050255-98e0fa35e936/go.mod h1:HcE8B5lP96enc/OALTb2/rIIi+yOLouRoHOKRclKmC8=
github.com/iotaledger/hive.go/kvstore v0.0.0-20231108050255-98e0fa35e936 h1:kXKJQ8UvbA8kI0Jx0EnlXbwDeZFY8pEX0Q6KaOPsYlQ=
github.com/iotaledger/hive.go/kvstore v0.0.0-20231108050255-98e0fa35e936/go.mod h1:ytfKoHr/nF8u0y0G4mamfG0yjFtJiJVk0kgjnPOtsSY=
github.com/iotaledger/hive.go/lo v0.0.0-20231108050255-98e0fa35e936 h1:coXPklQ7JgqTXIUXh3b4OHml1VIvI8x7pQsjsES/u/s=
github.com/iotaledger/hive.go/lo v0.0.0-20231108050255-98e0fa35e936/go.mod h1:6Ee7i6b4tuTHuRYnPP8VUb0wr9XFI5qlqtnttBd9jRg=
github.com/iotaledger/hive.go/log v0.0.0-20231108050255-98e0fa35e936 h1:VBvGnsVwqhoT9zMyMIlK5fPmz6fsbiPZOwdU1E8WU7o=
github.com/iotaledger/hive.go/log v0.0.0-20231108050255-98e0fa35e936/go.mod h1:vzO4/wRkEJDEZb/9fD10oKU9k1bj4qLir2Uhl5U1FkM=
github.com/iotaledger/hive.go/logger v0.0.0-20231108050255-98e0fa35e936 h1:05EbTaladbyo7mD8yBaWYJh9P8u/TUTmrjVmcUjoW8A=
github.com/iotaledger/hive.go/logger v0.0.0-20231108050255-98e0fa35e936/go.mod h1:w1psHM2MuKsen1WdsPKrpqElYH7ZOQ+YdQIgJZg4HTo=
github.com/iotaledger/hive.go/runtime v0.0.0-20231108050255-98e0fa35e936 h1:XbC1fmY87UJ/yMs8U2YqlUdJsqb0Xqj/ZYQKlZ7AUG8=
github.com/iotaledger/hive.go/runtime v0.0.0-20231108050255-98e0fa35e936/go.mod h1:DrZPvUvLarK8C2qb+3H2vdypp/MuhpQmB3iMJbDCr/Q=
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231108050255-98e0fa35e936 h1:LXhLW2cN9bQYoHQsgmJRb/jiRBRU5s2rLoCNjZfgHdg=
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231108050255-98e0fa35e936/go.mod h1:FoH3T6yKlZJp8xm8K+zsQiibSynp32v21CpWx8xkek8=
github.com/iotaledger/hive.go/stringify v0.0.0-20231108050255-98e0fa35e936 h1:Y4HgL5gm9S27usg5M2t6wi1BSdCxVorM62lwnpKuMd4=
github.com/iotaledger/hive.go/stringify v0.0.0-20231108050255-98e0fa35e936/go.mod h1:FTo/UWzNYgnQ082GI9QVM9HFDERqf9rw9RivNpqrnTs=
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231031135002-4c79ea5193f5 h1:17JDzMKTMXKF3xys6gPURRddkZhg1LY+xwfhbr/sVqg=
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231031135002-4c79ea5193f5/go.mod h1:LsJvoBUVVnY7tkwwByCVtAwmp5bFXdyJNGU/+KVQJVM=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231031134131-b6ad918dc1ac h1:c7R33+TQGMYP6pvLUQQaqpdDFl+GZbhAcfGMI0285fo=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231031134131-b6ad918dc1ac/go.mod h1:qPuMUvCTaghsnYRDnRoRuztTyEKFlmi2S7gb44rH7WM=
github.com/iotaledger/iota.go/v4 v4.0.0-20231102113728-20b8d01e826e h1:ZYRC1MHn/ghsqtjIpYGTxLQrh5n5eUmC0/YWnJiTRhk=
github.com/iotaledger/iota.go/v4 v4.0.0-20231102113728-20b8d01e826e/go.mod h1:jqbLYq4a/FwuiPBqFfkAwwxU8vs3+kReRq2/tyX5qRA=
github.com/iotaledger/iota.go/v4 v4.0.0-20231108050608-afce96cfe8a6 h1:4kvG+BB4GOBsNYPY/enPo3xeC65A133L9cD73Kf1p9Q=
github.com/iotaledger/iota.go/v4 v4.0.0-20231108050608-afce96cfe8a6/go.mod h1:8iDORW4/e4NztyAGqjW07uSMjbhs7snbxw+81IWOczY=
github.com/ipfs/boxo v0.13.1 h1:nQ5oQzcMZR3oL41REJDcTbrvDvuZh3J9ckc9+ILeRQI=
github.com/ipfs/boxo v0.13.1/go.mod h1:btrtHy0lmO1ODMECbbEY1pxNtrLilvKSYLoGQt1yYCk=
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
Expand Down Expand Up @@ -507,8 +507,6 @@ github.com/opencontainers/runtime-spec v1.1.0/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=
github.com/orcaman/writerseeker v0.0.0-20200621085525-1d3f536ff85e h1:s2RNOM/IGdY0Y6qfTeUKhDawdHDpK9RGBdx80qN4Ttw=
github.com/orcaman/writerseeker v0.0.0-20200621085525-1d3f536ff85e/go.mod h1:nBdnFKj15wFbf94Rwfq4m30eAcyY9V/IyKAGQFtqkW0=
github.com/otiai10/copy v1.14.0 h1:dCI/t1iTdYGtkvCuBG2BgR6KZa83PTclw4U5n2wAllU=
github.com/otiai10/copy v1.14.0/go.mod h1:ECfuL02W+/FkTWZWgQqXPWZgW9oeKCSQ5qVfSc4qc4w=
github.com/otiai10/mint v1.5.1 h1:XaPLeE+9vGbuyEHem1JNk3bYc7KKqyI/na0/mLd/Kks=
Expand Down
123 changes: 53 additions & 70 deletions pkg/core/account/accounts.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package account

import (
"bytes"
"encoding/binary"
"io"
"sync/atomic"

"github.com/iotaledger/hive.go/core/safemath"
"github.com/iotaledger/hive.go/ds/shrinkingmap"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/runtime/syncutils"
"github.com/iotaledger/hive.go/serializer/v2/marshalutil"
"github.com/iotaledger/hive.go/serializer/v2"
"github.com/iotaledger/hive.go/serializer/v2/stream"
iotago "github.com/iotaledger/iota.go/v4"
)

Expand All @@ -27,14 +26,9 @@ type Accounts struct {

// NewAccounts creates a new Weights instance.
func NewAccounts() *Accounts {
a := new(Accounts)
a.initialize()

return a
}

func (a *Accounts) initialize() {
a.accountPools = shrinkingmap.New[iotago.AccountID, *Pool]()
return &Accounts{
accountPools: shrinkingmap.New[iotago.AccountID, *Pool](),
}
}

func (a *Accounts) Has(id iotago.AccountID) bool {
Expand Down Expand Up @@ -137,91 +131,80 @@ func (a *Accounts) SelectCommittee(members ...iotago.AccountID) *SeatedAccounts
}

func AccountsFromBytes(b []byte) (*Accounts, int, error) {
return AccountsFromReader(bytes.NewReader(b))
}

func AccountsFromReader(readSeeker io.ReadSeeker) (*Accounts, int, error) {
a := new(Accounts)
n, err := a.readFromReadSeeker(readSeeker)
reader := stream.NewByteReader(b)

return a, n, err
}

func (a *Accounts) readFromReadSeeker(reader io.ReadSeeker) (n int, err error) {
a.mutex.Lock()
defer a.mutex.Unlock()

a.initialize()

var accountCount uint32
if err = binary.Read(reader, binary.LittleEndian, &accountCount); err != nil {
return n, ierrors.Wrap(err, "unable to read accounts count")
a, err := AccountsFromReader(reader)
if err != nil {
return nil, 0, ierrors.Wrap(err, "unable to read accounts from bytes")
}
n += 4

for i := uint32(0); i < accountCount; i++ {
var accountID iotago.AccountID

if _, err = io.ReadFull(reader, accountID[:]); err != nil {
return 0, ierrors.Wrap(err, "unable to read accountID")
}
n += iotago.AccountIDLength
return a, reader.BytesRead(), nil
}

poolBytes := make([]byte, poolBytesLength)
if _, err = io.ReadFull(reader, poolBytes); err != nil {
return 0, ierrors.Wrap(err, "unable to read pool bytes")
}
n += poolBytesLength
func AccountsFromReader(reader io.Reader) (*Accounts, error) {
a := NewAccounts()

pool, c, err := PoolFromBytes(poolBytes)
if err := stream.ReadCollection(reader, serializer.SeriLengthPrefixTypeAsUint32, func(i int) error {
accountID, err := stream.Read[iotago.AccountID](reader)
if err != nil {
return 0, ierrors.Wrap(err, "failed to parse pool")
return ierrors.Wrapf(err, "unable to read accountID at index %d", i)
}

if c != poolBytesLength {
return 0, ierrors.Wrap(err, "invalid pool bytes length")
pool, err := stream.ReadObject(reader, poolBytesLength, PoolFromBytes)
if err != nil {
return ierrors.Wrapf(err, "unable to read pool at index %d", i)
}

if err := a.setWithoutLocking(accountID, pool); err != nil {
return 0, ierrors.Wrapf(err, "failed to set pool for account %s", accountID.String())
return ierrors.Wrapf(err, "failed to set pool for account %s", accountID.String())
}

return nil
}); err != nil {
return nil, ierrors.Wrap(err, "failed to read account data")
}

var reused bool
if err = binary.Read(reader, binary.LittleEndian, &reused); err != nil {
return n, ierrors.Wrap(err, "unable to read reused flag")
reused, err := stream.Read[bool](reader)
if err != nil {
return nil, ierrors.Wrap(err, "failed to read reused flag")
}

a.reused.Store(reused)
n++

return n, nil
return a, nil
}

func (a *Accounts) Bytes() (bytes []byte, err error) {
func (a *Accounts) Bytes() ([]byte, error) {
a.mutex.RLock()
defer a.mutex.RUnlock()

m := marshalutil.New()
byteBuffer := stream.NewByteBuffer()

m.WriteUint32(uint32(a.accountPools.Size()))
var innerErr error
a.ForEach(func(id iotago.AccountID, pool *Pool) bool {
m.WriteBytes(id[:])
poolBytes, err := pool.Bytes()
if err != nil {
innerErr = err
return false
}
m.WriteBytes(poolBytes)
if err := stream.WriteCollection(byteBuffer, serializer.SeriLengthPrefixTypeAsUint32, func() (elementsCount int, err error) {
var innerErr error
a.ForEach(func(id iotago.AccountID, pool *Pool) bool {
if innerErr = stream.Write(byteBuffer, id); innerErr != nil {
return false
}

return true
})
if innerErr = stream.WriteObject(byteBuffer, pool, (*Pool).Bytes); innerErr != nil {
return false
}

m.WriteBool(a.reused.Load())
return true
})
if innerErr != nil {
return 0, innerErr
}

return a.accountPools.Size(), nil
}); err != nil {
return nil, ierrors.Wrap(err, "failed to write accounts")
}

if innerErr != nil {
return nil, innerErr
if err := stream.Write(byteBuffer, a.reused.Load()); err != nil {
return nil, ierrors.Wrap(err, "failed to write reused flag")
}

return m.Bytes(), nil
return byteBuffer.Bytes()
}
2 changes: 1 addition & 1 deletion pkg/core/account/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestAccounts(t *testing.T) {
require.Equal(t, accounts, accounts2)

// check "AccountsFromReader"
accounts3, _, err := account.AccountsFromReader(bytes.NewReader(accountBytes))
accounts3, err := account.AccountsFromReader(bytes.NewReader(accountBytes))
require.NoError(t, err)

// check if the new account is the same
Expand Down
Loading

0 comments on commit 9094100

Please sign in to comment.