Skip to content

Commit 2e6fea7

Browse files
committed
root of transactions by stateless merkledb
1 parent bfe1fcd commit 2e6fea7

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

chain/block.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ import (
99
"fmt"
1010
"time"
1111

12+
"github.com/ava-labs/avalanchego/database"
13+
"github.com/ava-labs/avalanchego/database/memdb"
1214
"github.com/ava-labs/avalanchego/ids"
1315
"github.com/ava-labs/avalanchego/snow/choices"
1416
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
1517
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
1618
"github.com/ava-labs/avalanchego/snow/validators"
1719
"github.com/ava-labs/avalanchego/utils/set"
20+
"github.com/ava-labs/avalanchego/utils/units"
1821
"github.com/ava-labs/avalanchego/vms/platformvm/warp"
1922
"github.com/ava-labs/avalanchego/x/merkledb"
2023
"go.opentelemetry.io/otel/attribute"
@@ -40,7 +43,8 @@ type StatefulBlock struct {
4043
Tmstmp int64 `json:"timestamp"`
4144
Hght uint64 `json:"height"`
4245

43-
Txs []*Transaction `json:"txs"`
46+
Txs []*Transaction `json:"txs"`
47+
TxsRoot []byte `json:"txsRoot"`
4448

4549
// StateRoot is the root of the post-execution state
4650
// of [Prnt].
@@ -290,6 +294,46 @@ func (b *StatelessBlock) initializeBuilt(
290294
b.containsWarp = true
291295
}
292296
}
297+
298+
// transaction hash generation
299+
db, err := merkledb.New(ctx, memdb.New(), merkledb.Config{
300+
BranchFactor: merkledb.BranchFactor16,
301+
HistoryLength: 100,
302+
EvictionBatchSize: units.MiB,
303+
IntermediateNodeCacheSize: units.MiB,
304+
ValueNodeCacheSize: units.MiB,
305+
Tracer: b.vm.Tracer(),
306+
})
307+
if err != nil {
308+
return err
309+
}
310+
// collect keys, values from transactions/results
311+
var ops []database.BatchOp
312+
for _, tx := range b.Txs {
313+
key := utils.ToID(tx.Bytes())
314+
ops = append(ops, database.BatchOp{
315+
Key: key[:],
316+
Value: tx.Bytes(),
317+
})
318+
}
319+
for _, result := range b.results {
320+
key := utils.ToID(result.Output)
321+
ops = append(ops, database.BatchOp{
322+
Key: key[:],
323+
Value: result.Output,
324+
})
325+
}
326+
view, err = db.NewView(ctx, merkledb.ViewChanges{BatchOps: ops})
327+
if err != nil {
328+
return err
329+
}
330+
view.CommitToDB(ctx)
331+
txsRoot, err := db.GetMerkleRoot(ctx)
332+
if err != nil {
333+
return err
334+
}
335+
b.TxsRoot = txsRoot[:]
336+
293337
return nil
294338
}
295339

0 commit comments

Comments
 (0)